SimplePDLToPetriNet.t 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import org.eclipse.emf.common.util.*;
  2. import org.eclipse.emf.ecore.*;
  3. import org.eclipse.emf.ecore.util.ECrossReferenceAdapter;
  4. import org.eclipse.emf.ecore.xmi.*;
  5. import org.eclipse.emf.ecore.xmi.impl.*;
  6. import SimplePDLSemantics.DDMMSimplePDL.*;
  7. import petrinetsemantics.DDMMPetriNet.*;
  8. import SimplePDLSemantics.EDMMSimplePDL.*;
  9. import petrinetsemantics.EDMMPetriNet.*;
  10. import SimplePDLSemantics.SDMMSimplePDL.*;
  11. import petrinetsemantics.SDMMPetriNet.*;
  12. import SimplePDLSemantics.TM3SimplePDL.*;
  13. import petrinetsemantics.TM3PetriNet.*;
  14. import java.util.*;
  15. import java.util.concurrent.ConcurrentMap;
  16. import java.util.concurrent.ConcurrentHashMap;
  17. import java.io.File;
  18. import java.io.Writer;
  19. import java.io.BufferedWriter;
  20. import java.io.OutputStreamWriter;
  21. import java.io.FileOutputStream;
  22. import java.io.FileInputStream;
  23. import tom.library.utils.ReferenceClass;
  24. import tom.library.utils.LinkClass;
  25. import tom.library.sl.*;
  26. import tom.library.emf.*;
  27. public class SimplePDLToPetriNet {
  28. %include{ sl.tom }
  29. %include{ LinkClass.tom }
  30. %include{ emf/ecore.tom }
  31. %include{ mappings/DDMMPetriNetPackage.tom }
  32. %include{ mappings/DDMMSimplePDLPackage.tom }
  33. %include{ mappings/EDMMPetriNetPackage.tom }
  34. %include{ mappings/EDMMSimplePDLPackage.tom }
  35. %include{ mappings/SDMMPetriNetPackage.tom }
  36. %include{ mappings/SDMMSimplePDLPackage.tom }
  37. %include{ mappings/TM3PetriNetPackage.tom }
  38. %include{ mappings/TM3SimplePDLPackage.tom }
  39. %typeterm SimplePDLToPetriNet { implement { SimplePDLToPetriNet }}
  40. private static Writer writer;
  41. private static PetriNet pn = null;
  42. private static LinkClass tom__linkClass;
  43. public SimplePDLToPetriNet() {
  44. this.tom__linkClass = new LinkClass();
  45. }
  46. %transformation SimplePDLToPetriNet(tom__linkClass:LinkClass,pn:PetriNet) : "metamodels/SimplePDLSemantics_updated.ecore" -> "metamodels/PetriNetSemantics_updated.ecore" {
  47. definition P2PN traversal `TopDown(P2PN(tom__linkClass,pn)) {
  48. p@Process[name=name] -> {
  49. Place p_ready = `Place(name + "_ready", pn,ArcEList(), ArcEList(), 1);
  50. Place p_running = `Place(name + "_running", pn,ArcEList(), ArcEList(), 0);
  51. Place p_finished = `Place(name + "_finished", pn,ArcEList(), ArcEList(), 0);
  52. String n1 = `name+"_start";
  53. %tracelink(t_start:Transition, `Transition(n1, pn,ArcEList(), ArcEList(), 1, 1));
  54. n1 = `name+"_finish";
  55. %tracelink(t_finish:Transition, `Transition(n1, pn,ArcEList(), ArcEList(), 1, 1));
  56. `Arc(t_start, p_ready, pn,ArcKindnormal(), 1);
  57. `Arc(p_running, t_start, pn,ArcKindnormal(), 1);
  58. `Arc(t_finish, p_running, pn,ArcKindnormal(), 1);
  59. `Arc(p_finished, t_finish, pn,ArcKindnormal(), 1);
  60. WorkDefinition from = `p.getFrom();
  61. if (from!=null) {
  62. Transition source = %resolve(from:WorkDefinition,t_start:Transition);
  63. source.setNet(pn);
  64. Arc tmpZoomIn = `Arc(p_ready,source,pn,ArcKindnormal(), 1);
  65. Transition target = %resolve(from:WorkDefinition,t_finish:Transition);
  66. target.setNet(pn);
  67. Arc tmpZoomOut = `Arc(target,p_finished,pn,ArcKindread_arc(), 1);
  68. }
  69. }
  70. }
  71. definition WD2PN traversal `TopDown(WD2PN(tom__linkClass,pn)) {
  72. wd@WorkDefinition[name=name] -> {
  73. //System.out.println("Je suis un A");
  74. Place p_ready = `Place(name + "_ready", pn,ArcEList(), ArcEList(), 1);
  75. String n1 = `name+"_started";
  76. %tracelink(p_started:Place, `Place(n1, pn,ArcEList(), ArcEList(), 0));
  77. Place p_running = `Place(name+"_running", pn,ArcEList(), ArcEList(), 0);
  78. n1 = `name+"_finished";
  79. %tracelink(p_finished:Place, `Place(n1, pn,ArcEList(), ArcEList(), 0));
  80. n1 = `name+"_start";
  81. %tracelink(t_start:Transition, `Transition(n1, pn,ArcEList(), ArcEList(), 1, 1));
  82. n1 = `name+"_finish";
  83. %tracelink(t_finish:Transition, `Transition(n1, pn,ArcEList(), ArcEList(), 1, 1));
  84. `Arc(t_start, p_ready, pn,ArcKindnormal(), 1);
  85. `Arc(p_started, t_start, pn,ArcKindnormal(), 1);
  86. `Arc(p_running, t_start, pn,ArcKindnormal(), 1);
  87. `Arc(t_finish, p_running, pn,ArcKindnormal(), 1);
  88. `Arc(p_finished, t_finish, pn,ArcKindnormal(), 1);
  89. SimplePDLSemantics.DDMMSimplePDL.Process parent = `wd.getParent();
  90. Transition source = %resolve(parent:Process,t_start:Transition);
  91. source.setNet(pn);
  92. Arc tmpDistribute = `Arc(p_ready,source,pn,ArcKindnormal(), 1);
  93. Transition target = %resolve(parent:Process,t_finish:Transition);
  94. target.setNet(pn);
  95. Arc tmpRejoin = `Arc(target,p_finished,pn,ArcKindread_arc(), 1);
  96. }
  97. }
  98. definition WS2PN traversal `TopDown(WS2PN(tom__linkClass,pn)) {
  99. ws@WorkSequence[predecessor=p,successor=s,linkType=linkType] -> {
  100. Place source= null;
  101. Transition target= null;
  102. WorkDefinition pre = `p;
  103. WorkDefinition suc = `s;
  104. %match(linkType) {
  105. (WorkSequenceTypefinishToFinish|WorkSequenceTypefinishToStart)[] -> {
  106. source = %resolve(pre:WorkDefinition,p_finished:Place);
  107. }
  108. (WorkSequenceTypestartToStart|WorkSequenceTypestartToFinish)[] -> {
  109. source = %resolve(pre:WorkDefinition,p_started:Place);
  110. }
  111. (WorkSequenceTypefinishToStart|WorkSequenceTypestartToStart)[] -> {
  112. target = %resolve(suc:WorkDefinition,t_start:Transition);
  113. }
  114. (WorkSequenceTypestartToFinish|WorkSequenceTypefinishToFinish)[] -> {
  115. target = %resolve(suc:WorkDefinition,t_finish:Transition);
  116. }
  117. }
  118. source.setNet(pn);
  119. target.setNet(pn);
  120. Arc wsImage = `Arc(target,source, pn,ArcKindread_arc(), 1);
  121. }
  122. }
  123. }
  124. public static void main(String[] args) {
  125. System.out.println("\nStarting...\n");
  126. XMIResourceImpl resource = new XMIResourceImpl();
  127. SimplePDLSemantics.DDMMSimplePDL.Process p_root;
  128. Map opts = new HashMap();
  129. opts.put(XMIResource.OPTION_SCHEMA_LOCATION, java.lang.Boolean.TRUE);
  130. if (args.length>0) {
  131. DDMMSimplePDLPackage packageInstance = DDMMSimplePDLPackage.eINSTANCE;
  132. File input = new File(args[0]);
  133. try {
  134. resource.load(new FileInputStream(input),opts);
  135. } catch (Exception e) {
  136. e.printStackTrace();
  137. }
  138. p_root = (SimplePDLSemantics.DDMMSimplePDL.Process) resource.getContents().get(0);
  139. } else {
  140. System.out.println("No model instance given in argument. Using default hardcoded model.");
  141. WorkDefinition wd1 = `WorkDefinition(null,WorkSequenceEList(),WorkSequenceEList(),"A",null);
  142. WorkDefinition wd2 = `WorkDefinition(null,WorkSequenceEList(),WorkSequenceEList(),"B",null);
  143. WorkDefinition wd3 = `WorkDefinition(null,WorkSequenceEList(),WorkSequenceEList(),"C",null);
  144. WorkDefinition wd4 = `WorkDefinition(null,WorkSequenceEList(),WorkSequenceEList(),"D",null);
  145. WorkSequence ws1 = `WorkSequence(null,WorkSequenceTypestartToStart(),wd1,wd2);
  146. WorkSequence ws2 = `WorkSequence(null,WorkSequenceTypestartToFinish(),wd3,wd4);
  147. p_root = `Process("root",ProcessElementEList(wd1,wd2,ws1),null);
  148. SimplePDLSemantics.DDMMSimplePDL.Process p_child = `Process("child",ProcessElementEList(wd3,wd4,ws2), wd2);
  149. wd1.setParent(p_root);
  150. wd2.setParent(p_root);
  151. wd2.setProcess(p_child);
  152. wd3.setParent(p_child);
  153. wd4.setParent(p_child);
  154. ws1.setParent(p_root);
  155. ws2.setParent(p_child);
  156. }
  157. SimplePDLToPetriNet translator = new SimplePDLToPetriNet();
  158. try {
  159. translator.pn = `PetriNet(NodeEList(),ArcEList(),"main");
  160. //System.out.println("Initial Petri net");
  161. //`Sequence(TopDown(PrintTransition()),TopDown(PrintPlace())).visit(translator.pn, new EcoreContainmentIntrospector());
  162. /*//transformer is equivalent to:
  163. Strategy transformer = `Sequence(
  164. TopDown(Process2PetriNet(translator)),
  165. TopDown(WorkDefinition2PetriNet(translator)),
  166. TopDown(WorkSequence2PetriNet(translator))
  167. );
  168. */
  169. //NOTE: force the user to give the link as first parameter, and target
  170. //model as second one
  171. Strategy transformer = `SimplePDLToPetriNet(translator.tom__linkClass,translator.pn);
  172. transformer.visit(p_root, new EcoreContainmentIntrospector());
  173. `TopDown(tom__StratResolve_SimplePDLToPetriNet(translator.tom__linkClass,translator.pn)).visit(translator.pn, new EcoreContainmentIntrospector());
  174. //for generation of textual Petri nets usable as input for TINA
  175. String outputName = "resultingPetri.net";
  176. writer = new BufferedWriter(new OutputStreamWriter(new
  177. FileOutputStream(new File(outputName))));
  178. System.out.println("\nResult");
  179. `Sequence(TopDown(PrintTransition()),TopDown(PrintPlace())).visit(translator.pn,
  180. new EcoreContainmentIntrospector());
  181. System.out.println("\nFinish to generate "+outputName+" file, usable as input for TINA");
  182. writer.flush();
  183. writer.close();
  184. System.out.println("done.");
  185. } catch(VisitFailure e) {
  186. System.out.println("strategy fail!");
  187. } catch(java.io.FileNotFoundException e) {
  188. System.out.println("Cannot create Petri net output file.");
  189. } catch (java.io.IOException e) {
  190. System.out.println("Petri net save failed!");
  191. }
  192. }
  193. %strategy PrintArc() extends Identity() {
  194. visit Arc {
  195. Arc[source=node1, target=node2] -> {
  196. System.out.println(`node1.getName() + " -> " + `node2.getName());
  197. }
  198. }
  199. }
  200. %strategy PrintTransition() extends Identity() {
  201. visit Transition {
  202. tr@ResolveWorkDefinitionTransition[tom_resolve_element_attribute_name=name] -> {
  203. System.out.println("tr resolve " + `name);
  204. return `tr;
  205. }
  206. ptr@ResolveProcessTransition[tom_resolve_element_attribute_name=name] -> {
  207. System.out.println("tr process resolve " + `name);
  208. return `ptr;
  209. }
  210. Transition[name=name,incomings=sources,outgoings=targets] -> {
  211. String s = " ";
  212. String t = " ";
  213. %match {
  214. ArcEList(_*,Arc[kind=k,weight=w,source=node],_*) << sources && Place[name=placename]<< node -> {
  215. s += `placename + ((`k==`ArcKindread_arc())?"?":"*") + `w + " ";
  216. }
  217. ArcEList(_*,Arc[kind=k,weight=w,target=node],_*) << targets && Place[name=placename]<< node -> {
  218. t += `placename + ((`k==`ArcKindread_arc())?"?":"*") + `w + " ";
  219. }
  220. }
  221. multiPrint("tr " + `name + s + "->" + t);
  222. }
  223. }
  224. }
  225. %strategy PrintPlace() extends Identity() {
  226. visit Place {
  227. pl@ResolveWorkDefinitionPlace[tom_resolve_element_attribute_name=name] -> {
  228. System.out.println("pl resolve " + `name);
  229. return `pl;
  230. }
  231. Place[name=name,initialMarking=w] && w!=0 -> {
  232. multiPrint("pl " + `name + " " + "(" + `w + ")");
  233. }
  234. }
  235. }
  236. public static void multiPrint(String s) {
  237. System.out.println(s);
  238. try {
  239. writer.write(s+"\n");
  240. } catch (java.io.IOException e) {
  241. System.out.println("Petri net save failed!");
  242. }
  243. }
  244. }