libmt.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*******************************************************************************
  2. AToMPM - A Tool for Multi-Paradigm Modelling
  3. Copyright (c) 2011 Raphael Mannadiar (raphael.mannadiar@mail.mcgill.ca)
  4. This file is part of AToMPM.
  5. AToMPM is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU Lesser General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or (at your option) any later
  8. version.
  9. AToMPM is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11. PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with AToMPM. If not, see <http://www.gnu.org/licenses/>.
  14. *******************************************************************************/
  15. {
  16. /* apply transformation T to specified model
  17. TBI:: this method only supports transforming class diagrams to ER diagrams
  18. and does so programmatically (vs. locating an appropriate [chain of]
  19. model transformations and running them) */
  20. 'transform' :
  21. function(_model,T)
  22. {
  23. if (!Array.prototype.find) {
  24. Array.prototype.find = function(predicate) {
  25. if (this == null) {
  26. throw new TypeError('Array.prototype.find called on null or undefined');
  27. }
  28. if (typeof predicate !== 'function') {
  29. throw new TypeError('predicate must be a function');
  30. }
  31. var list = Object(this);
  32. var length = list.length >>> 0;
  33. var thisArg = arguments[1];
  34. var value;
  35. for (var i = 0; i < length; i++) {
  36. value = list[i];
  37. if (predicate.call(thisArg, value, i, list)) {
  38. return value;
  39. }
  40. }
  41. return undefined;
  42. };
  43. }
  44. var model = _utils.jsonp(_model),
  45. new_model = _utils.jsonp(_model),
  46. SCD = '/Formalisms/__LanguageSyntax__/SimpleClassDiagram/SimpleClassDiagram',
  47. ER = '/Formalisms/__LanguageSyntax__/EntityRelationship/EntityRelationship';
  48. if( T == 'SimpleClassDiagram-2-EntityRelationship' )
  49. {
  50. /* 1 map each type to all of its ancestors
  51. 2 flatten ancestor contents into their children + 'reroute' associations
  52. 3 make new_model conform to ER
  53. 4 append type-to-parentType mapping (needed for MT subtype matching) */
  54. function copyAttributes(parent_id, child_id) {
  55. new_model.nodes[child_id]['attributes']['value'] =
  56. new_model.nodes[child_id]['attributes']['value'].concat(
  57. new_model.nodes[parent_id]['attributes']['value'].filter(
  58. function(attr) {
  59. return !new_model.nodes[child_id]['attributes']['value'].find(
  60. function(el) {return el['name'] == attr['name']}
  61. )
  62. }
  63. )
  64. );
  65. new_model.nodes[child_id]['constraints']['value'] =
  66. new_model.nodes[child_id]['constraints']['value'].concat(
  67. new_model.nodes[parent_id]['constraints']['value'].filter(
  68. function(constr) {
  69. return !new_model.nodes[child_id]['constraints']['value'].find(
  70. function(el) {return el['name'] == constr['name']}
  71. )
  72. }
  73. )
  74. );
  75. new_model.nodes[child_id]['actions']['value'] =
  76. new_model.nodes[child_id]['actions']['value'].concat(
  77. new_model.nodes[parent_id]['actions']['value'].filter(
  78. function(act) {
  79. return !new_model.nodes[child_id]['actions']['value'].find(
  80. function(el) {return el['name'] == act['name']}
  81. )
  82. }
  83. )
  84. );
  85. new_model.nodes[child_id]['cardinalities']['value'] =
  86. new_model.nodes[child_id]['cardinalities']['value'].concat(
  87. new_model.nodes[parent_id]['cardinalities']['value'].filter(
  88. function(card) {
  89. return !new_model.nodes[child_id]['cardinalities']['value'].find(
  90. function(el) {return el['dir'] == card['dir'] && el['type'] == card['type']}
  91. )
  92. }
  93. )
  94. );
  95. }
  96. function findAncestors(ids)
  97. {
  98. var inheritances = [];
  99. model.edges.forEach(
  100. function(edge)
  101. {
  102. if( _utils.contains(ids,edge['src']) &&
  103. model.nodes[edge['dest']]['$type'] == SCD+'/Inheritance' )
  104. inheritances.push(edge['dest']);
  105. });
  106. if( inheritances.length == 0 ) {
  107. return ids;
  108. }
  109. var parents = [];
  110. model.edges.forEach(
  111. function(edge)
  112. {
  113. if( _utils.contains(inheritances,edge['src']) )
  114. parents.push(edge['dest']);
  115. });
  116. return ids.concat(findAncestors(parents));
  117. }
  118. var ids2ancestors = {};
  119. for( var id in model.nodes )
  120. {
  121. /* 1 */
  122. var ids = findAncestors([id]);
  123. ids.reverse(); // oldest first
  124. var idx = 1;
  125. while (idx < ids.length) {
  126. copyAttributes(ids[idx-1], ids[idx]);
  127. idx++;
  128. }
  129. ids2ancestors[id] = ids;
  130. ids2ancestors[id].splice(-1);
  131. /* 2 */
  132. /* a) apply attributes, constraints, actions and cardinalities inheritance to id
  133. b) update cardinalities of Associations who connect to parent + add edges between Associations
  134. and children... this effectively does association inheritance */
  135. ids2ancestors[id].forEach(
  136. function(a)
  137. {
  138. var ancestorType = model.nodes[a]['name']['value'],
  139. idType = model.nodes[id]['name']['value'];
  140. model.edges.forEach(
  141. function(edge)
  142. {
  143. if( edge['src'] == a || edge['dest'] == a )
  144. {
  145. var ancestorIsSrc = (edge['src'] == a),
  146. assoc = (ancestorIsSrc ? edge['dest'] : edge['src']);
  147. if( model.nodes[assoc]['$type'] != SCD+'/Association' )
  148. return;
  149. for( var i in model.nodes[assoc]['cardinalities']['value'] )
  150. {
  151. var card = model.nodes[assoc]['cardinalities']['value'][i];
  152. if( card['type'] == ancestorType && card['dir'] == (ancestorIsSrc ? 'out' : 'in') )
  153. {
  154. var new_card = _utils.clone(card);
  155. new_card['type'] = idType;
  156. new_model.nodes[assoc]['cardinalities']['value'].push(new_card);
  157. }
  158. }
  159. new_model.edges.push(
  160. (ancestorIsSrc ? {'src':id,'dest':assoc} : {'src':assoc,'dest':id}) );
  161. }
  162. });
  163. });
  164. }
  165. /* 3 */
  166. /* due to extreme similarity between SCD and ER, we only need to
  167. a) update $type attributes
  168. special case: make abstract classes uninstantiable
  169. special case: remove inheritances
  170. b) update new_model.metamodels */
  171. for( var id in new_model.nodes )
  172. {
  173. if( new_model.nodes[id]['$type'] == SCD+'/Class' )
  174. {
  175. new_model.nodes[id]['$type'] = ER+'/Entity';
  176. if( new_model.nodes[id]['abstract']['value'] )
  177. {
  178. new_model.nodes[id]['constraints']['value'].push(
  179. {'name':'noAbstractInstances',
  180. 'event':'pre-create',
  181. 'code':'false'})
  182. }
  183. }
  184. else if( new_model.nodes[id]['$type'] == SCD+'/Association' )
  185. new_model.nodes[id]['$type'] = ER+'/Relationship';
  186. else if( new_model.nodes[id]['$type'] == SCD+'/GlobalConstraint' )
  187. new_model.nodes[id]['$type'] = ER+'/GlobalConstraint';
  188. else if( new_model.nodes[id]['$type'] == SCD+'/GlobalAction' )
  189. new_model.nodes[id]['$type'] = ER+'/GlobalAction';
  190. else if( new_model.nodes[id]['$type'] == SCD+'/Inheritance' )
  191. {
  192. /* special case: inheritance
  193. a) remove all edges pertaining to it
  194. b) remove it */
  195. new_model.edges = new_model.edges.filter(
  196. function(edge) {return edge['src'] != id && edge['dest'] != id});
  197. delete new_model.nodes[id];
  198. }
  199. }
  200. new_model.metamodels = [ER];
  201. /* 4 */
  202. var types2parentTypes = {};
  203. for( var id in new_model.nodes )
  204. {
  205. var type = new_model.nodes[id]['name']['value'];
  206. if( types2parentTypes[type] == undefined )
  207. {
  208. types2parentTypes[type] = []
  209. ids2ancestors[id].forEach(
  210. function(a)
  211. {
  212. types2parentTypes[type].push(new_model.nodes[a]['name']['value'])
  213. });
  214. }
  215. }
  216. new_model['types2parentTypes'] = types2parentTypes;
  217. /* return transformed model... note the little hack here... the reason for this is that pushing
  218. the 'same' attribute/constraint/action/cardiniality *objects* during the inheritance process
  219. causes problems when these are later edited (e.g., extended with targetType in compileToMM)
  220. since they all point to each other and changing one changes them all... */
  221. return _utils.clone(new_model);
  222. }
  223. else
  224. return {'$err':'unknown transformation :: '+T};
  225. },
  226. /* RAMify an AS metamodel and associated CS metamodel(s)
  227. 1. RELAX
  228. a) alter all constraints such that they are always satisfied (includes
  229. constraints that prevent instantiation of abstract types) + remember
  230. abstract types (for step 2diii)
  231. b) alter all actions such that they have no effect
  232. c) reduce all minimum multiplicities to 0
  233. 2. AUGMENT & MODIFY
  234. a) setup pattern types, constraints, actions, cardinalities, etc.
  235. b) replace types, constraints, etc. by results of steps a)
  236. c) insert post-create action that sets newly created, non-copied nodes'
  237. __pLabels to non-taken values
  238. d) alter each CS metamodel (i.e., icon definitions)
  239. i. use pattern type names
  240. ii. remove parsing and mapping functions (because pattern attribute
  241. values are code, as opposed to their 'original' types)
  242. iii. create basic icons for abstract types
  243. iv. add a Text VisualObject to show/edit the __pLabel attribute to
  244. every icon */
  245. 'ramify' :
  246. function(asmm,csmms)
  247. {
  248. var abstractTypes = [];
  249. for( var i in asmm.constraints )
  250. {
  251. if( asmm.constraints[i]['name'] == 'noAbstractInstances' )
  252. abstractTypes.push(asmm.constraints[i]['targetType']);
  253. asmm.constraints[i]['code'] =
  254. '/* comment next line to enable this constraint */\n'+
  255. 'throw "IgnoredConstraint"\n'+asmm.constraints[i]['code'];
  256. }
  257. for( var i in asmm.actions )
  258. asmm.actions[i]['code'] =
  259. '/* comment next line to enable this action */\n'+
  260. 'throw "IgnoredConstraint"\n'+asmm.actions[i]['code'];
  261. for( var t in asmm.cardinalities )
  262. for( var i in asmm.cardinalities[t] )
  263. asmm.cardinalities[t][i]['min'] = 0;
  264. var patternTypes = {},
  265. patternActions = [],
  266. patternCards = {},
  267. patternLegalConns = {},
  268. patternConnTypes = {},
  269. patternT2PT = {};
  270. for( var t in asmm.types )
  271. {
  272. patternTypes['__p'+t] =
  273. [{'name':'__pLabel', 'type':'string', 'default':''},
  274. {'name':'__pPivotIn', 'type':'string', 'default':''}, /* hergin motif-integration */
  275. {'name':'__pPivotOut', 'type':'string', 'default':''}, /* hergin motif-integration */
  276. {'name':'__pMatchSubtypes', 'type':'boolean', 'default':true}];
  277. for( var i in asmm.types[t] )
  278. patternTypes['__p'+t].push(
  279. {'name':asmm.types[t][i]['name'],
  280. 'type':'code',
  281. 'default':
  282. '"[PYTHON]"\n"Example:\t result = True"\n"Example:\t result = getAttr()"\n\n'+
  283. '"[JAVASCRIPT]"\n"Example:\t true"\n"Example:\t getAttr()"'});
  284. }
  285. for( var i in asmm.actions )
  286. {
  287. var action = asmm.actions[i];
  288. patternActions.push(_utils.clone(action));
  289. patternActions[patternActions.length-1]['targetType'] = '__p'+action['targetType'];
  290. }
  291. for( var t in asmm.cardinalities )
  292. {
  293. patternCards['__p'+t] = [];
  294. for( var i in asmm.cardinalities[t] )
  295. {
  296. var card = asmm.cardinalities[t][i];
  297. patternCards['__p'+t].push(_utils.clone(card));
  298. patternCards['__p'+t][i]['type'] = '__p'+card['type'];
  299. }
  300. }
  301. for( var t1 in asmm.legalConnections )
  302. {
  303. patternLegalConns['__p'+t1] = {};
  304. for( var t2 in asmm.legalConnections[t1] )
  305. {
  306. patternLegalConns['__p'+t1]['__p'+t2] = [];
  307. for( var i in asmm.legalConnections[t1][t2] )
  308. patternLegalConns['__p'+t1]['__p'+t2].push(
  309. '__p'+asmm.legalConnections[t1][t2][i]);
  310. }
  311. }
  312. for( var t in asmm.connectorTypes )
  313. patternConnTypes['__p'+t] = asmm.connectorTypes[t];
  314. for( var t in asmm.types2parentTypes )
  315. {
  316. patternT2PT['__p'+t] = [];
  317. for( var i in asmm.types2parentTypes[t] )
  318. patternT2PT['__p'+t].push('__p'+asmm.types2parentTypes[t][i]);
  319. }
  320. asmm.types = patternTypes;
  321. asmm.actions = patternActions;
  322. asmm.cardinalities = patternCards;
  323. asmm.legalConnections = patternLegalConns;
  324. asmm.connectorTypes = patternConnTypes;
  325. asmm.types2parentTypes = patternT2PT;
  326. patternActions.push(
  327. {'name': 'distinctPLabels',
  328. 'event': 'post-create',
  329. 'code': 'if( getAttr("__pLabel") == "" )\n'+
  330. '{\n'+
  331. ' var pLabels = getAllNodes().\n'+
  332. ' filter( function(n) {return hasAttr("__pLabel",n);} ).\n'+
  333. ' map( function(n) {return getAttr("__pLabel",n);} ),\n'+
  334. ' i = "0";\n'+
  335. '\n'+
  336. ' while( _utils.contains(pLabels,i) )\n'+
  337. ' i = String(parseInt(i)+1);\n'+
  338. ' setAttr("__pLabel",i);\n'+
  339. '}',
  340. 'targetType': '*'});
  341. function addPLabelTextVisualObject(nodes)
  342. {
  343. nodes['__pLabelText'] = {"position": {"type": "list<double>", "value": [0,0]},
  344. "orientation": {"type": "double", "value": 0},
  345. "scale": {"type": "list<double>", "value": [1,1]},
  346. "textContent": {"type": "string", "value": "#"},
  347. "style": {"type": "map<string,string>", "value": {"stroke": "#6000ff",
  348. "fill": "#6000ff",
  349. "font-size": "15px",
  350. "opacity": "1"}},
  351. "mapper": {"type": "code", "value": "({'textContent':getAttr('__pLabel')})"},
  352. "parser": {"type": "code", "value": "({'__pLabel':getAttr('textContent')})"},
  353. "$type": "/Formalisms/__LanguageSyntax__/ConcreteSyntax/ConcreteSyntax/Text"};
  354. }
  355. var NO_MAPPERS_PARSERS = '/* mapping and parsing code is disabled by default because pattern attribute values are code */';
  356. for( var csmm in csmms )
  357. {
  358. var patternTypes = {},
  359. patternCards = {},
  360. patternT2PT = {};
  361. csmms[csmm] = _utils.jsonp(csmms[csmm]);
  362. for( type in csmms[csmm].types )
  363. {
  364. var contents = undefined;
  365. patternTypes['__p'+type] = _utils.clone(csmms[csmm].types[type]);
  366. patternCards['__p'+type] = [];
  367. patternT2PT['__p'+type] = [];
  368. patternTypes['__p'+type].forEach(
  369. function(attr)
  370. {
  371. if( attr['name'] == '$contents' )
  372. contents = attr;
  373. else if( attr['name'] == 'parser' || attr['name'] == 'mapper' )
  374. attr['default'] = NO_MAPPERS_PARSERS;
  375. });
  376. for( id in contents['default']['nodes'] )
  377. for( attr in contents['default']['nodes'][id] )
  378. if( attr == 'parser' || attr == 'mapper' )
  379. contents['default']['nodes'][id][attr]['value'] = NO_MAPPERS_PARSERS;
  380. addPLabelTextVisualObject(contents['default']['nodes']);
  381. }
  382. abstractTypes.forEach(
  383. function(type)
  384. {
  385. patternCards['__p'+type+'Icon'] = [];
  386. patternT2PT['__p'+type+'Icon'] = [];
  387. patternTypes['__p'+type+'Icon'] = [{"name": "typename", "type": "string", "default": '__p'+type+'Icon'},
  388. {"name": "position", "type": "list<double>", "default": [0,0]},
  389. {"name": "orientation", "type": "double", "default": 0},
  390. {"name": "scale", "type": "list<double>", "default": [1,1]},
  391. {"name": "mapper", "type": "code", "default": ''},
  392. {"name": "parser", "type": "code", "default": ''},
  393. {"name": "$contents", "type": "map<string,*>", "default": {
  394. "nodes": {
  395. "text": {
  396. "textContent": {"type": "string", "value": '__p'+type+'Icon'},
  397. "style": {"type": "map<string,string>", "value": {"stroke": "#000000",
  398. "stroke-dasharray": "",
  399. "fill": "#000000",
  400. "fill-opacity": "1",
  401. "font-size": "13px"}},
  402. "mapper": {"type": "code", "value": ""},
  403. "parser": {"type": "code", "value": ""},
  404. "$type": "/Formalisms/__LanguageSyntax__/ConcreteSyntax/ConcreteSyntax/Text",
  405. "position": {"type": "list<double>", "value": [10,76]},
  406. "orientation": {"type": "double", "value": 0},
  407. "scale": {"type": "list<double>", "value": [1,1]}},
  408. "rect": {
  409. "width": {"type": "double", "value": 75},
  410. "height": {"type": "double", "value": 75},
  411. "cornerRadius": {"type": "double", "value": 15},
  412. "style": {"type": "map<string,string>", "value": {"stroke": "#000000",
  413. "fill": "#ffffff",
  414. "fill-opacity": 0.75}},
  415. "mapper": {"type": "code", "value": ""},
  416. "parser": {"type": "code", "value": ""},
  417. "$type": "/Formalisms/__LanguageSyntax__/ConcreteSyntax/ConcreteSyntax/Rectangle",
  418. "position": {"type": "list<double>", "value": [0,0]},
  419. "orientation": {"type": "double", "value": 0},
  420. "scale": {"type": "list<double>", "value": [1,1]}},
  421. "textBelowRect": {
  422. "distance": {"type": "double", "value": 10},
  423. "alignment": {"type": "ENUM(\"right\",\"left\",\"center\")", "value": "center"},
  424. "$type": "/Formalisms/__LanguageSyntax__/ConcreteSyntax/ConcreteSyntax/Below",
  425. "position": {"type": "list<double>", "value": [5,38]},
  426. "orientation": {"type": "double", "value": 0},
  427. "scale": {"type": "list<double>", "value": [1,1]},
  428. "link-style": {"type": "map<string,string>", "value": {"stroke": "#00ff00",
  429. "stroke-dasharray": "",
  430. "stroke-opacity": 1,
  431. "arrow-start": "none",
  432. "arrow-end": "classic-wide-long"}}}},
  433. "edges": [{"src": "text", "dest": "textBelowRect"},
  434. {"src": "textBelowRect", "dest": "rect"}]}},
  435. {"name": "$asuri", "type": "string", "default": "-1"}];
  436. addPLabelTextVisualObject(patternTypes['__p'+type+'Icon'][6]['default']['nodes']);
  437. });
  438. csmms[csmm].types = patternTypes;
  439. csmms[csmm].cardinalities = patternCards;
  440. csmms[csmm].types2parentTypes = patternT2PT;
  441. }
  442. return {'asmm':asmm,'csmms':csmms};
  443. }
  444. }