exporttosccdxml_debug.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* SCCDXML exporter plugin*/
  2. {
  3. 'interfaces' : [{'method':'POST', 'url=':'/exporttosccdxml_debug'}],
  4. 'csworker' :
  5. function(resp,method,uri,reqData,wcontext)
  6. {
  7. var self = this,
  8. actions = [__wHttpReq('GET','/current.model?wid='+wcontext.__aswid)];
  9. _do.chain(actions)(
  10. function(asdata){
  11. var writeActions = [_fspp.mkdirs('./exported_to_sccdxml/classes/')];
  12. _do.chain(writeActions) (
  13. function() {
  14. var file_contents = '',
  15. as = _utils.jsonp(asdata['data']),
  16. type_map = {},
  17. type_map_keys = {},
  18. incoming = {},
  19. outgoing = {};
  20. for (var key in as.nodes) {
  21. var node = as.nodes[key];
  22. if (!(node['$type'] in type_map)) {
  23. type_map[node['$type']] = [];
  24. type_map_keys[node['$type']] = [];
  25. }
  26. type_map[node['$type']].push(node);
  27. type_map_keys[node['$type']].push(key);
  28. node['$key'] = key;
  29. }
  30. function xml_safe(str) {
  31. return str.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;");
  32. }
  33. function safe_add(l, el) {
  34. if (l.indexOf(el) == -1) {
  35. l.push(el);
  36. }
  37. }
  38. function find_initial(state){
  39. var substates = outgoing[state['$key']].filter(function(el) {return as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/contain' || as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/ocContain'}).map(function(tid) {return as.nodes[tid]});
  40. for (var sidx in substates){
  41. var substate = as.nodes[outgoing[substates[sidx]['$key']]];
  42. if(substate['isStart']['value']){
  43. return substate['name']['value'];
  44. }
  45. }
  46. }
  47. function get_full_path(state){
  48. var parent_link = incoming[state['$key']].filter(function(el) {return as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/contain' || as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/ocContain' || as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/containOC' || as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/includes'}).map(function(tid) {return as.nodes[tid]})[0];
  49. if (parent_link != undefined){
  50. var parent = as.nodes[incoming[parent_link['$key']][0]];
  51. return get_full_path(parent).concat([state['name']['value']]);
  52. }
  53. else{
  54. return [state['name']['value']];
  55. }
  56. }
  57. function find_path(source, destination){
  58. if(source['$key'] == destination['$key']){
  59. return ".";
  60. }
  61. // Resolve absolute path of source
  62. source_path = get_full_path(source);
  63. // Resolve absolute path of destination
  64. destination_path = get_full_path(destination);
  65. // Now make the destination path relative to the source path
  66. var common;
  67. for (var i in source_path){
  68. if(source_path[i] != destination_path[i]){
  69. common = i;
  70. break;
  71. }
  72. }
  73. source_path = source_path.slice(i, source_path.length);
  74. destination_path = destination_path.slice(i, destination_path.length);
  75. var result = ".";
  76. for (var i in source_path){
  77. result += "/..";
  78. }
  79. for (var i in destination_path){
  80. result += "/" + destination_path[i];
  81. }
  82. return result;
  83. }
  84. function write_raise(lst){
  85. contents = "";
  86. for(var ridx in lst){
  87. var r = lst[ridx];
  88. contents += '<raise event="' + r['event'] + '"';
  89. if(r['scope'].length == 0){
  90. contents += ' scope="broad">\n';
  91. }
  92. else if(r['scope'].lastIndexOf("output(", 0) === 0){
  93. contents += ' scope="output" port="' + r['scope'].slice(7, -1)+ '">\n';
  94. }
  95. else{
  96. contents += ' scope="' + r['scope'] + '">\n';
  97. }
  98. arguments = r['arguments'];
  99. for (var aidx in arguments){
  100. var arg = arguments[aidx];
  101. if(arg != ""){
  102. contents += '<parameter expr="' + xml_safe(arg) + '"/>\n';
  103. }
  104. }
  105. contents += '</raise>\n';
  106. }
  107. return contents;
  108. }
  109. function resolve_transitions(state){
  110. contents = "";
  111. if(outgoing[state['$key']] == undefined){
  112. return "";
  113. }
  114. var transitions = outgoing[state['$key']].filter(function(el) {return as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/transition'}).map(function(tid) {return as.nodes[tid]});
  115. for (var tidx in transitions){
  116. var transition = transitions[tidx];
  117. contents += "<transition ";
  118. if(transition['after']['value'].length > 0){
  119. contents += 'after="' + xml_safe(transition['after']['value']) + '" ';
  120. }
  121. if(transition['event']['value'].length > 0){
  122. contents += 'event="' + transition['event']['value'] + '" ';
  123. }
  124. if(transition['guard']['value'].length > 0){
  125. contents += 'cond="' + xml_safe(transition['guard']['value']) + '" ';
  126. }
  127. if(transition['port']['value'].length > 0){
  128. contents += 'port="' + transition['port']['value'] + '" ';
  129. }
  130. var target = as.nodes[outgoing[transition['$key']][0]];
  131. contents += 'target="' + find_path(state, target) + '" ';
  132. contents += '>\n';
  133. var parameters = transition['parameters']['value'];
  134. for(var pidx in parameters){
  135. var param = parameters[pidx];
  136. contents += '<parameter name="' + param + '"/>\n';
  137. }
  138. if(transition['action']['value'].length > 0){
  139. contents += "<script><![CDATA[";
  140. contents += transition['action']['value'].split("/*newline*/").join("\n");
  141. contents += "]]></script>\n";
  142. }
  143. var raise = transition['raise']['value'];
  144. contents += write_raise(raise);
  145. contents += '</transition>\n';
  146. }
  147. return contents;
  148. }
  149. function find_priority(state){
  150. state = as.nodes[state['$key']];
  151. if(state['option']['value'] == "OTF"){
  152. return "source_parent";
  153. }
  154. else if(state['option']['value'] == "ITF"){
  155. return "source_child";
  156. }
  157. else {
  158. console.log("Unknown conflict resolution selected: " + state['option']['value']);
  159. return "undefined";
  160. }
  161. }
  162. function add_actions(state){
  163. contents = "<onentry>\n";
  164. state = as.nodes[state['$key']];
  165. if(state['entryAction'] == undefined){
  166. return "";
  167. }
  168. contents += "<script><![CDATA[";
  169. if(state['$type'] == "/Formalisms/SCCD/SCCD/BasicState"){
  170. contents += "if True:\n";
  171. contents += " data = json.dumps({'text':'CLIENT_BDAPI :: {\"func\":\"_highlightState\",\"args\":{\"asid\":\"" + state['$key'] + "\",\"followCrossFormalismLinks\":\"*\"}}'})\n";
  172. contents += " headers = {'Content-Type': 'text/plain'}\n";
  173. contents += " conn = httplib.HTTPConnection('127.0.0.1:8124')\n";
  174. contents += " conn.request('PUT', '/GET/console?wid=" + wcontext.__aswid + "', data, headers)\n";
  175. contents += " conn.getresponse()\n";
  176. contents += " conn.close()\n";
  177. contents += "else:\n";
  178. contents += " pass\n";
  179. }
  180. else{
  181. // To prevent crashes of the compiler
  182. contents += "pass\n";
  183. }
  184. if(state['entryAction']['value'].length > 0){
  185. contents += state['entryAction']['value'].split("/*newline*/").join("\n");
  186. }
  187. contents += "]]></script>\n";
  188. var raise = state['raiseEntry']['value'];
  189. contents += write_raise(raise);
  190. contents += "</onentry>\n";
  191. contents += "<onexit>\n";
  192. contents += "<script><![CDATA[";
  193. if(state['$type'] == "/Formalisms/SCCD/SCCD/BasicState"){
  194. contents += "if True:\n";
  195. contents += " data = json.dumps({'text':'CLIENT_BDAPI :: {\"func\":\"_unhighlightState\",\"args\":{\"asid\":\"" + state['$key'] + "\"}}'})\n";
  196. contents += " headers = {'Content-Type': 'text/plain'}\n";
  197. contents += " conn = httplib.HTTPConnection('127.0.0.1:8124')\n";
  198. contents += " conn.request('PUT', '/GET/console?wid=" + wcontext.__aswid + "', data, headers)\n";
  199. contents += " conn.getresponse()\n";
  200. contents += " conn.close()\n";
  201. contents += "else:\n";
  202. contents += " pass\n";
  203. }
  204. else{
  205. // To prevent crashes of the compiler
  206. contents += "pass\n";
  207. }
  208. if(state['exitAction']['value'].length > 0){
  209. contents += state['exitAction']['value'].split("/*newline*/").join("\n");
  210. }
  211. contents += "]]></script>\n";
  212. var raise = state['raiseExit']['value'];
  213. contents += write_raise(raise);
  214. contents += "</onexit>\n";
  215. return contents;
  216. }
  217. function recursive(state, is_root){
  218. var contents = "";
  219. if(outgoing[state['$key']] == undefined){
  220. var containOC = [],
  221. contain = [],
  222. ocContain = [];
  223. }
  224. else{
  225. var containOC = outgoing[state['$key']].filter(function(el) {return as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/containOC'}).map(function(tid) {return as.nodes[tid]});
  226. var contain = outgoing[state['$key']].filter(function(el) {return as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/contain'}).map(function(tid) {return as.nodes[tid]});
  227. var ocContain = outgoing[state['$key']].filter(function(el) {return as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/ocContain'}).map(function(tid) {return as.nodes[tid]});
  228. var history = outgoing[state['$key']].filter(function(el) {return as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/includes'}).map(function(tid) {return as.nodes[tid]});
  229. }
  230. var s = as.nodes[state['$key']];
  231. if (is_root){
  232. contents += '<scxml initial="' + find_initial(s) + '" priority="' + find_priority(s) + '">\n';
  233. var children = containOC.concat(contain).concat(ocContain).concat(history);
  234. console.log("Children of root: " + children);
  235. for (var cidx in children){
  236. var child = as.nodes[outgoing[children[cidx]['$key']]];
  237. contents += recursive(child, false);
  238. }
  239. contents += resolve_transitions(s);
  240. contents += '</scxml>\n';
  241. }
  242. else if (containOC.length > 0){
  243. contents += '<parallel id="' + s['name']['value'] + '">\n';
  244. var children = containOC.concat(contain).concat(ocContain).concat(history);
  245. for (var cidx in children){
  246. var child = as.nodes[outgoing[children[cidx]['$key']]];
  247. contents += recursive(child, false);
  248. }
  249. contents += resolve_transitions(s);
  250. contents += add_actions(s);
  251. contents += '</parallel>\n';
  252. }
  253. else if(contain.length + ocContain.length > 0){
  254. contents += '<state id="' + s['name']['value'] + '" initial="' + find_initial(s) + '">\n';
  255. var children = containOC.concat(contain).concat(ocContain).concat(history);
  256. for (var cidx in children){
  257. var child = as.nodes[outgoing[children[cidx]['$key']]];
  258. contents += recursive(child, false);
  259. }
  260. contents += resolve_transitions(s);
  261. contents += add_actions(s);
  262. contents += '</state>\n';
  263. }
  264. else {
  265. var history = incoming[state['$key']].filter(function(el) {return as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/includes'}).map(function(tid) {return as.nodes[tid]});
  266. if (history.length > 0){
  267. contents += '<history id="' + s['name']['value'] + '" type="' + s['type']['value'].trim() + '"/>\n';
  268. }
  269. else{
  270. contents += '<state id="' + s['name']['value'] + '">\n';
  271. contents += resolve_transitions(s);
  272. contents += add_actions(s);
  273. contents += '</state>\n';
  274. }
  275. }
  276. return contents;
  277. }
  278. function calc_in_out_rel(rel_type) {
  279. for (var idx in type_map_keys[rel_type]) {
  280. key = type_map_keys[rel_type][idx];
  281. incoming[key] = [];
  282. outgoing[key] = [];
  283. for (var e_key in as.edges) {
  284. var e = as.edges[e_key];
  285. if (e['dest'] == key) {
  286. safe_add(incoming[key], e['src'])
  287. if (!(e['src'] in outgoing)) {
  288. outgoing[e['src']] = [];
  289. }
  290. safe_add(outgoing[e['src']], key);
  291. }
  292. if (e['src'] == key) {
  293. safe_add(outgoing[key], e['dest'])
  294. if (!(e['dest'] in incoming)) {
  295. incoming[e['dest']] = [];
  296. }
  297. safe_add(incoming[e['dest']], key);
  298. }
  299. }
  300. }
  301. }
  302. calc_in_out_rel('/Formalisms/SCCD/SCCD/transition');
  303. calc_in_out_rel('/Formalisms/SCCD/SCCD/Inheritance');
  304. calc_in_out_rel('/Formalisms/SCCD/SCCD/Association');
  305. calc_in_out_rel('/Formalisms/SCCD/SCCD/contain');
  306. calc_in_out_rel('/Formalisms/SCCD/SCCD/containOC');
  307. calc_in_out_rel('/Formalisms/SCCD/SCCD/ocContain');
  308. calc_in_out_rel('/Formalisms/SCCD/SCCD/behaviour');
  309. calc_in_out_rel('/Formalisms/SCCD/SCCD/includes');
  310. for (var key in type_map['/Formalisms/SCCD/SCCD/Class']){
  311. var node = type_map['/Formalisms/SCCD/SCCD/Class'][key],
  312. external = node['external']['value'],
  313. type = node['name']['value'];
  314. if(!external){
  315. file_contents += '<class name="' + node['name']['value'] + '" default="true">\n';
  316. file_contents += '\t<relationships>\n';
  317. inheritances = outgoing[node['$key']].filter(function(el) {return as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/Inheritance'}).map(function(tid) {return as.nodes[tid]});
  318. for (var iidx in inheritances){
  319. target = as.nodes[outgoing[inheritances[iidx]['$key']][0]];
  320. file_contents += '\t\t<inheritance class="' + target['name']['value'] + '"/>\n';
  321. }
  322. associations = outgoing[node['$key']].filter(function(el) {return as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/Association'}).map(function(tid) {return as.nodes[tid]});
  323. for (var iidx in associations){
  324. target = as.nodes[outgoing[associations[iidx]['$key']][0]];
  325. file_contents += '\t\t<association name="' + associations[iidx]['name']['value'] + '" class="' + target['name']['value'] + '"/>\n';
  326. }
  327. file_contents += '';
  328. file_contents += '\t</relationships>\n';
  329. for (var fidx in node['methods']['value']){
  330. var func = node['methods']['value'][fidx];
  331. file_contents += '\t<method name="' + func['name'] + '">\n';
  332. for (var aidx in func['args']){
  333. var arg = func['args'][aidx];
  334. if(arg['name'] == ""){
  335. continue;
  336. }
  337. file_contents += '\t\t<parameter name="' + arg['name'] + '"/>\n';
  338. }
  339. file_contents += '\t<body><![CDATA[';
  340. file_contents += func['body'].split("/*newline*/").join("\n");
  341. file_contents += "\n";
  342. if(func['name'] == node['name']['value']){
  343. file_contents += "if True:\n";
  344. file_contents += "\tdata = json.dumps({'text':'CLIENT_BDAPI :: {\"func\":\"_unhighlight\",\"args\":{}}'})\n";
  345. file_contents += "\theaders = {'Content-Type': 'text/plain'}\n";
  346. file_contents += "\tconn = httplib.HTTPConnection('127.0.0.1:8124')\n";
  347. file_contents += "\tconn.request('PUT', '/GET/console?wid=" + wcontext.__aswid + "', data, headers)\n";
  348. file_contents += "\tconn.getresponse()\n";
  349. file_contents += "\tconn.close()\n";
  350. file_contents += "else:\n";
  351. file_contents += "\tpass\n";
  352. }
  353. file_contents += '\t]]></body>\n';
  354. file_contents += '\t</method>\n';
  355. }
  356. var behaviour = outgoing[node['$key']].filter(function(el) {return as.nodes[el]['$type'] == '/Formalisms/SCCD/SCCD/behaviour'}).map(function(tid) {return as.nodes[tid]});
  357. var statechart = as.nodes[outgoing[behaviour[0]['$key']][0]];
  358. file_contents += recursive(statechart, true);
  359. file_contents += '</class>\n';
  360. }
  361. }
  362. _fs.writeFileSync('./exported_to_sccdxml/classes/model.xml', file_contents);
  363. __postMessage({'statusCode': 200,
  364. 'respIndex': resp});
  365. },
  366. function(writeErr) {__postInternalErrorMsg(resp, writeErr);}
  367. );
  368. },
  369. function(err) {__postInternalErrorMsg(resp, err);}
  370. )
  371. },
  372. 'asworker' :
  373. function(resp,method,uri,reqData,wcontext)
  374. {
  375. this.localcontext.counter++;
  376. __postMessage(
  377. {'statusCode':200,
  378. 'respIndex':resp});
  379. }
  380. }