exportpnml.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* a simple hello world plugin... it listens for "POST /hello" requests... requests are received by "csworker" and forwarded to "asworker"... both respond with their __worker\'s id and with a counter of the number of handled requests */
  2. const {
  3. __errorContinuable,
  4. __httpReq,
  5. __wHttpReq,
  6. __postInternalErrorMsg, __postMessage,
  7. __sequenceNumber,
  8. __successContinuable,
  9. __uri_to_id
  10. } = require("../__worker");
  11. const _do = require("../___do");
  12. const _utils = require('../utils');
  13. const _mmmk = require("../mmmk");
  14. const _fs = _do.convert(require('fs'), ['readFile', 'writeFile', 'readdir']);
  15. const _fspp = _do.convert(require('../___fs++'), ['mkdirs']);
  16. module.exports = {
  17. 'interfaces' : [{'method':'POST', 'url=':'/pnml'}],
  18. 'csworker' :
  19. function(resp,method,uri,reqData,wcontext)
  20. {
  21. //console.log('Inside PNML');
  22. var self = this,
  23. actions = [__wHttpReq('GET','/current.model?wid='+wcontext.__aswid)];
  24. //console.log(reqData);
  25. _do.chain(actions)(
  26. function(asdata)
  27. {
  28. var sn = asdata['sequence#'];
  29. if( self.__nextASWSequenceNumber - 1 > sn )
  30. self['PUT *.model'](resp,urin);
  31. else if( self.__nextASWSequenceNumber - 1 < sn )
  32. setTimeout(self['PUT *.model'], 200, resp, urin);
  33. else
  34. {
  35. if( (res = _mmmk.read())['$err'] )
  36. __postInternalErrorMsg(resp,res['$err']);
  37. else
  38. {
  39. var ts = Math.round((new Date()).getTime() / 1000);
  40. head = '<pnml xmlns="http://www.pnml.org/version-2009/grammar/pnml">';
  41. head += '<net id="'+ts+'" type="http://www.pnml.org/version-2009/grammar/ptnet">';
  42. head +='<page>\n';
  43. var cs = _utils.jsonp(res);
  44. var as = _utils.jsonp(asdata['data']);
  45. let addPlace = function(name,id,x,y,initial) {
  46. var file='<place id="'+id+'">\n';
  47. file+='<name>\n';
  48. file +='<text>"'+name+'"</text>\n';
  49. file +='<graphics><offset x="22" y="-10"/> </graphics>\n';
  50. //graphics with offset later
  51. file+='</name>\n';
  52. file+='<graphics><position x="'+x+'" y="'+y+'"/> </graphics>\n';
  53. if (initial) {
  54. file+='<initialMarking>\n<text>'+initial+'</text>\n';
  55. file+=' <graphics> <offset x="22" y="20"/></graphics> \n</initialMarking>\n';
  56. }
  57. file+='</place>\n';
  58. return file;
  59. };
  60. let addTrans = function(name,id,x,y) {
  61. var file='<transition id="'+id+'">\n <name> <text>'+name+'</text>\n';
  62. file+='<graphics> <offset x="22" y="-14"/> </graphics>\n';
  63. file+='</name>\n<graphics> <position x="'+x+'" y="'+y+'"/></graphics>\n</transition>\n';
  64. return file;
  65. };
  66. let addArc = function(from,to) {
  67. var file='<arc id="'+from+to+'" source="'+from+'" target="'+to+'">\n';
  68. file+='<inscription> <text>1</text> </inscription> </arc>\n';
  69. return file;
  70. };
  71. var mData = {
  72. 'csm':_utils.jsonp(res),
  73. 'asm':_utils.jsonp(asdata['data'])},
  74. path = reqData['fname'];
  75. dir = _path.dirname(path).replace(/"/g,'\\"'),
  76. writeActions =
  77. [//_cp.exec('mkdir -p "'+dir+'"'),
  78. function()
  79. {
  80. var cs = mData['csm'];
  81. var as = mData['asm'];
  82. var tp = [];
  83. for (key in as.nodes) {
  84. var id = key;
  85. var x=0;
  86. var y=0;
  87. var initial = 0;
  88. var name = '';
  89. var tokens = 0;
  90. if (as.nodes[key]['$type'].indexOf('Transition') != -1) {
  91. x = cs.nodes[key]['position']['value'][0];
  92. y = cs.nodes[key]['position']['value'][1];
  93. name = as.nodes[key]['name']['value'];
  94. head+=addTrans(name,id,x,y);
  95. tp.push(key);
  96. } else if (as.nodes[key]['$type'].indexOf('Place') != -1) {
  97. x = cs.nodes[key]['position']['value'][0];
  98. y = cs.nodes[key]['position']['value'][1];
  99. name = as.nodes[key]['name']['value'];
  100. tokens = as.nodes[key]['nbTokens']['value'];
  101. head+=addPlace(name,id,x,y,tokens);
  102. tp.push(key);
  103. }
  104. }
  105. var to = new Array();
  106. var from = new Array();
  107. for (key in as.edges) {
  108. //console.log(as.edges[key]['src']);
  109. if (tp.indexOf(as.edges[key]['src'] ) !=-1 ) {
  110. from[as.edges[key]['dest']]=as.edges[key]['src'];
  111. } else {
  112. to[as.edges[key]['src']] = as.edges[key]['dest'];
  113. }
  114. }
  115. for (key in from) {
  116. head += addArc(from[key],to[key]);
  117. }
  118. head+=' </page>\n </net>\n </pnml>';
  119. console.log(path+".pnml");
  120. _fs.open(path+".pnml",'w',function (err, fd) {
  121. console.log(fd);
  122. _fs.write(fd,head);
  123. _fs.close(fd);
  124. });
  125. return true;
  126. }];
  127. _do.chain(writeActions)(
  128. function()
  129. {
  130. __postMessage(
  131. {'statusCode':204,
  132. 'respIndex':resp});
  133. },
  134. function(writeErr)
  135. {
  136. __postInternalErrorMsg(resp,writeErr);
  137. }
  138. );
  139. }
  140. }
  141. },
  142. function(err) {__postInternalErrorMsg(resp,err);}
  143. );
  144. },
  145. 'asworker' :
  146. function(resp,method,uri,reqData,wcontext)
  147. {
  148. this.localcontext.counter++;
  149. __postMessage(
  150. {'statusCode':200,
  151. 'changelog':
  152. [{'op':'SYSOUT',
  153. 'text':'Hello #'+this.localcontext.counter+' from asworker #'+__wid}],
  154. 'sequence#':__sequenceNumber(),
  155. 'respIndex':resp});
  156. }
  157. };