libmt.js 21 KB

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