exportpnml.js 5.3 KB

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