test_pn_interface.py 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. import unittest
  2. from utils import run_file, get_constructor, get_model_constructor
  3. set_inheritance = [
  4. "Which link in the metamodel is the inheritance link?",
  5. "Set inheritance link!",
  6. ]
  7. do_instantiate_simple = [
  8. "new", "PetriNets", "abc",
  9. "instantiate", "Transition", "t1",
  10. "instantiate", "Place", "p1", "attr_add", "p1", "tokens", 5,
  11. "instantiate", "Place", "p2", "attr_add", "p2", "tokens", 0,
  12. "instantiate", "P2T", "p2t", "p1", "t1", "attr_add", "p2t", "weight", 2,
  13. "instantiate", "T2P", "t2p", "t1", "p2", "attr_add", "t2p", "weight", 1]
  14. instantiate_node = ["Type to instantiate?",
  15. "Name of new element?",
  16. "Instantiation successful!"]
  17. instantiate_edge = ["Type to instantiate?",
  18. "Name of new element?",
  19. "Source name?",
  20. "Destination name?",
  21. "Instantiation successful!"]
  22. all_files = [ "pn_interface.alc",
  23. "primitives.alc",
  24. "object_operations.alc",
  25. "conformance_scd.alc",
  26. "library.alc",
  27. "transform.alc",
  28. "model_management.alc",
  29. "ramify.alc",
  30. "metamodels.alc",
  31. "random.alc",
  32. "constructors.alc",
  33. "modelling.alc",
  34. "compilation_manager.alc",
  35. ]
  36. greeting = ["Welcome to the Model Management Interface, running live on the Modelverse!",
  37. "Use 'help' command for a list of possible commands"]
  38. new = ["Metamodel to instantiate?",
  39. "Name of model?"]
  40. prompt = ["Please give your command."]
  41. loaded = ["Model loaded, ready for commands!",
  42. "Use 'help' command for a list of possible commands"] + prompt
  43. load = ["Model to load?"]
  44. instantiate = ["Type to instantiate?"]
  45. instantiate_name = ["Name of new element?"]
  46. instantiate_ok = ["Instantiation successful!"]
  47. instantiate_source= ["Source name?"]
  48. instantiate_destination = ["Destination name?"]
  49. def list_menu(defined):
  50. defined.append(("PetriNets", "SimpleClassDiagrams"))
  51. defined.append(("SimpleClassDiagrams", "SimpleClassDiagrams"))
  52. defined.append(("LTM_bottom", "LTM_bottom"))
  53. return ["Found models:",
  54. set([" %s : %s" % (m, mm) for m, mm in defined])]
  55. def list_model(defined):
  56. return ["List of all elements:",
  57. set([" %s : %s" % (m, mm) for m, mm in defined])]
  58. def read_node(name, t, defs, attrs):
  59. return ["Element to read?",
  60. "Name: %s" % name,
  61. "Type: %s" % t,
  62. "Defines attributes:"] + \
  63. ([set([" %s : %s" % (m, mm) for m, mm in defs])] if defs else []) + \
  64. ["Attributes:"] + \
  65. ([set([' "%s" : "%s" = %s' % (m, mm, v) for m, mm, v in attrs])] if attrs else [])
  66. def read_edge(name, t, src, dst, defs, attrs):
  67. return ["Element to read?",
  68. "Name: %s" % name,
  69. "Type: %s" % t,
  70. "Source: %s" % src,
  71. "Destination: %s" % dst,
  72. "Defines attributes:"] + \
  73. ([set([" %s : %s" % (m, mm) for m, mm in defs])] if defs else []) + \
  74. ["Attributes:"] + \
  75. ([set([' "%s" : "%s" = %s' % (m, mm, v) for m, mm, v in attrs])] if attrs else [])
  76. delete = ["Model to delete?", "Deleted!"]
  77. rename = ["Old name?", "New name?", "Rename complete!"]
  78. attr_add = ["Which model do you want to assign an attribute to?",
  79. "Which attribute do you wish to assign?",
  80. "Value of attribute?",
  81. "Added attribute!"]
  82. attr_del = ["Which model do you want to remove an attribute of?",
  83. "Which attribute do you want to delete?",
  84. "Attribute deleted!",
  85. ]
  86. help_root = ["Currently no model is loaded, so your operations are limited to:",
  87. " new -- Create a new model and save it for future use"
  88. " load -- Load a previously made model",
  89. " rename -- Rename a previously made model",
  90. " delete -- Delete a previously made model",
  91. " list -- Show a list of all stored models",
  92. " help -- Show a list of possible commands"]
  93. verify_fail_weight= ["Natural number not larger than or equal to zero"]
  94. verify_fail_tokens= ["Natural number not larger than or equal to zero"]
  95. verify_fail_structure = ["Source of model edge not typed by source of type: p2t"]
  96. init = greeting + prompt
  97. did_instantiate_simple = init + \
  98. new + \
  99. loaded +\
  100. instantiate_node + \
  101. prompt + \
  102. instantiate_node + \
  103. prompt + \
  104. attr_add + \
  105. prompt + \
  106. instantiate_node + \
  107. prompt + \
  108. attr_add + \
  109. prompt + \
  110. instantiate_edge + \
  111. prompt + \
  112. attr_add + \
  113. prompt + \
  114. instantiate_edge + \
  115. prompt + \
  116. attr_add + \
  117. prompt
  118. def list_types(t):
  119. return ["List of types:"] + \
  120. [set([" %s : %s" % (m, mm) for m, mm in t])]
  121. modify = ["Element to modify?",
  122. "Attribute to modify?",
  123. "New value?",
  124. "Modified!",]
  125. class TestPetrinetInterface(unittest.TestCase):
  126. def test_po_pn_interface_manage(self):
  127. self.pn_interface_manage("PO")
  128. def test_co_pn_interface_manage(self):
  129. self.pn_interface_manage("CO")
  130. def pn_interface_manage(self, mode):
  131. self.assertTrue(run_file(all_files,
  132. ["list",
  133. "new", "PetriNets", "abc", "exit",
  134. "list",
  135. "new", "PetriNets", "def", "exit",
  136. "list",
  137. "delete", "def",
  138. "list",
  139. "rename", "abc", "a",
  140. "list",
  141. "delete", "a",
  142. "list",
  143. ],
  144. init + \
  145. list_menu([]) + prompt + \
  146. new + loaded + prompt + list_menu([("abc", "PetriNets")]) + prompt + \
  147. new + loaded + prompt + list_menu([("abc", "PetriNets"), ("def", "PetriNets")]) + prompt + \
  148. delete + prompt + list_menu([("abc", "PetriNets")]) + prompt + \
  149. rename + prompt + list_menu([("a", "PetriNets")]) + prompt + \
  150. delete + prompt + list_menu([]) + prompt,
  151. mode))
  152. def test_po_pn_interface_new_reload(self):
  153. self.pn_interface_new_reload("PO")
  154. def test_co_pn_interface_new_reload(self):
  155. self.pn_interface_new_reload("CO")
  156. def pn_interface_new_reload(self, mode):
  157. self.assertTrue(run_file(all_files,
  158. ["new", "PetriNets", "abc", "exit", "load", "abc"],
  159. init + new + loaded + prompt + load + loaded,
  160. mode))
  161. def test_po_pn_interface_instantiate_place(self):
  162. self.pn_interface_instantiate_place("PO")
  163. def test_co_pn_interface_instantiate_place(self):
  164. self.pn_interface_instantiate_place("CO")
  165. def pn_interface_instantiate_place(self, mode):
  166. self.assertTrue(run_file(all_files,
  167. ["new", "PetriNets", "abc",
  168. "instantiate", "Place", "p1",
  169. "attr_add", "p1", "tokens", 5,
  170. "list",
  171. "read", "p1",
  172. "instantiate", "Transition", "t1",
  173. "list",
  174. "read", "t1"],
  175. init + new + loaded + \
  176. instantiate_node + prompt + \
  177. attr_add + prompt + \
  178. list_model([("p1", "Place"), ("p1.tokens", "Natural")]) + prompt + \
  179. read_node("p1", "Place", [], [("tokens", "Natural", 5)]) + prompt + \
  180. instantiate_node + prompt + \
  181. list_model([("p1", "Place"), ("t1", "Transition"), ("p1.tokens", "Natural")]) + prompt + \
  182. read_node("t1", "Transition", [], []) + prompt,
  183. mode))
  184. def test_po_pn_interface_instantiate_arcs(self):
  185. self.pn_interface_instantiate_arcs("PO")
  186. def test_co_pn_interface_instantiate_arcs(self):
  187. self.pn_interface_instantiate_arcs("CO")
  188. def pn_interface_instantiate_arcs(self, mode):
  189. self.assertTrue(run_file(all_files,
  190. do_instantiate_simple + [
  191. "read", "p1",
  192. "read", "p2",
  193. "read", "t1",
  194. "read", "p2t",
  195. "read", "t2p",
  196. ],
  197. did_instantiate_simple + \
  198. read_node("p1", "Place", [], [("tokens", "Natural", 5)]) + prompt + \
  199. read_node("p2", "Place", [], [("tokens", "Natural", 0)]) + prompt + \
  200. read_node("t1", "Transition", [], []) + prompt + \
  201. read_edge("p2t", "P2T", "p1", "t1", [], [("weight", "Natural", 2)]) + prompt + \
  202. read_edge("t2p", "T2P", "t1", "p2", [], [("weight", "Natural", 1)]) + prompt,
  203. mode))
  204. def test_po_pn_interface_verify_OK(self):
  205. self.pn_interface_verify_OK("PO")
  206. def test_co_pn_interface_verify_OK(self):
  207. self.pn_interface_verify_OK("CO")
  208. def pn_interface_verify_OK(self, mode):
  209. self.assertTrue(run_file(all_files,
  210. do_instantiate_simple + ["verify"],
  211. did_instantiate_simple + ["OK"], mode))
  212. def test_po_pn_interface_verify_fail_tokens(self):
  213. self.pn_interface_verify_fail_tokens("PO")
  214. def test_co_pn_interface_verify_fail_tokens(self):
  215. self.pn_interface_verify_fail_tokens("CO")
  216. def pn_interface_verify_fail_tokens(self, mode):
  217. self.assertTrue(run_file(all_files,
  218. do_instantiate_simple + ["modify", "p1", "tokens", -5, "verify"],
  219. did_instantiate_simple + modify + prompt + verify_fail_tokens + prompt, mode))
  220. def test_po_pn_interface_verify_fail_weight(self):
  221. self.pn_interface_verify_fail_weight("PO")
  222. def test_co_pn_interface_verify_fail_weight(self):
  223. self.pn_interface_verify_fail_weight("CO")
  224. def pn_interface_verify_fail_weight(self, mode):
  225. self.assertTrue(run_file(all_files,
  226. do_instantiate_simple + ["modify", "p2t", "weight", -2, "verify"],
  227. did_instantiate_simple + modify + prompt + verify_fail_weight + prompt, mode))
  228. def test_po_pn_interface_verify_fail_structure(self):
  229. self.pn_interface_verify_fail_structure("PO")
  230. def test_co_pn_interface_verify_fail_structure(self):
  231. self.pn_interface_verify_fail_structure("CO")
  232. def pn_interface_verify_fail_structure(self, mode):
  233. self.assertTrue(run_file(all_files,
  234. ["new", "PetriNets", "abc",
  235. "instantiate", "Transition", "t1",
  236. "instantiate", "Place", "p1", "attr_add", "p1", "tokens", 5,
  237. "instantiate", "P2T", "p2t", "t1", "p1", "attr_add", "p2t", "weight", 2, "verify"],
  238. init + new + loaded + \
  239. instantiate_node + prompt + \
  240. instantiate_node + prompt + attr_add + prompt + \
  241. instantiate_edge + prompt + attr_add + prompt + \
  242. verify_fail_structure,
  243. mode))
  244. def test_po_pn_interface_types(self):
  245. self.pn_interface_types("PO")
  246. def test_co_pn_interface_types(self):
  247. self.pn_interface_types("CO")
  248. def pn_interface_types(self, mode):
  249. self.assertTrue(run_file(all_files,
  250. ["new", "PetriNets", "abc", "types"],
  251. init + new + loaded + list_types([("Place", "Class"),
  252. ("Transition", "Class"),
  253. ("P2T", "Association"),
  254. ("T2P", "Association"),
  255. ("Natural", "Class"),
  256. ("Place_tokens", "Association"),
  257. ("Place_tokens.name", "String"),
  258. ("Place_tokens.target_lower_cardinality", "Natural"),
  259. ("Place_tokens.target_upper_cardinality", "Natural"),
  260. ("P2T_weight", "Association"),
  261. ("P2T_weight.name", "String"),
  262. ("P2T_weight.target_lower_cardinality", "Natural"),
  263. ("P2T_weight.target_upper_cardinality", "Natural"),
  264. ("T2P_weight", "Association"),
  265. ("T2P_weight.name", "String"),
  266. ("T2P_weight.target_lower_cardinality", "Natural"),
  267. ("T2P_weight.target_upper_cardinality", "Natural"),
  268. ]) + prompt,
  269. mode))
  270. def test_po_pn_interface_modify_place(self):
  271. self.pn_interface_modify_place("PO")
  272. def test_co_pn_interface_modify_place(self):
  273. self.pn_interface_modify_place("CO")
  274. def pn_interface_modify_place(self, mode):
  275. self.assertTrue(run_file(all_files,
  276. ["new", "PetriNets", "abc",
  277. "instantiate", "Place", "p1", "attr_add", "p1", "tokens", 5,
  278. "read", "p1",
  279. "modify", "p1", "tokens", 1, "read", "p1"],
  280. init + new + loaded + \
  281. instantiate_node + prompt + attr_add + prompt + \
  282. read_node("p1", "Place", [], [("tokens", "Natural", 5)]) + prompt + \
  283. modify + prompt + \
  284. read_node("p1", "Place", [], [("tokens", "Natural", 1)]) + prompt,
  285. mode))
  286. def test_po_pn_interface_verify_fail_attr_lower_cardinality(self):
  287. self.pn_interface_verify_fail_attr_lower_cardinality("PO")
  288. def test_co_pn_interface_verify_fail_attr_lower_cardinality(self):
  289. self.pn_interface_verify_fail_attr_lower_cardinality("CO")
  290. def pn_interface_verify_fail_attr_lower_cardinality(self, mode):
  291. self.assertTrue(run_file(all_files,
  292. do_instantiate_simple + ["instantiate", "Place", "p999", "verify"],
  293. did_instantiate_simple + instantiate_node + prompt + ["Lower cardinality violation for outgoing edge of type Place_tokens at p999"] + prompt,
  294. mode))
  295. def test_po_pn_interface_verify_fail_attr_upper_cardinality(self):
  296. self.pn_interface_verify_fail_attr_upper_cardinality("PO")
  297. def test_co_pn_interface_verify_fail_attr_upper_cardinality(self):
  298. self.pn_interface_verify_fail_attr_upper_cardinality("CO")
  299. def pn_interface_verify_fail_attr_upper_cardinality(self, mode):
  300. self.assertTrue(run_file(all_files,
  301. do_instantiate_simple + ["attr_add", "p1", "tokens", 5, "verify"],
  302. did_instantiate_simple + attr_add + prompt + ["Upper cardinality violation for outgoing edge of type Place_tokens at p1"] + prompt,
  303. mode))
  304. def test_po_pn_interface_verify_natural(self):
  305. self.pn_interface_verify_natural("PO")
  306. def test_co_pn_interface_verify_natural(self):
  307. self.pn_interface_verify_natural("CO")
  308. def pn_interface_verify_natural(self, mode):
  309. self.assertTrue(run_file(all_files,
  310. ["new", "PetriNets", "abc",
  311. "instantiate", "Place", "p1",
  312. "attr_add", "p1", "tokens", -5,
  313. "attr_del", "p1", "tokens",
  314. "attr_add", "p1", "tokens", 4,
  315. "verify"],
  316. init + new + loaded + \
  317. instantiate_node + prompt + \
  318. attr_add + prompt + \
  319. attr_del + prompt + \
  320. attr_add + prompt + \
  321. ["OK"] + prompt,
  322. mode))
  323. def test_po_pn_interface_verify_PN_OK(self):
  324. self.pn_interface_verify_PN_OK("PO")
  325. def test_co_pn_interface_verify_PN_OK(self):
  326. self.pn_interface_verify_PN_OK("CO")
  327. def pn_interface_verify_PN_OK(self, mode):
  328. self.assertTrue(run_file(all_files,
  329. ["load", "PetriNets", "verify"],
  330. init + load + loaded + ["OK"], mode))
  331. def test_po_rpgame(self):
  332. self.rpgame("PO")
  333. def test_co_rpgame(self):
  334. self.rpgame("CO")
  335. def rpgame(self, mode):
  336. constraint_code = \
  337. """
  338. include "primitives.alh"
  339. include "object_operations.alh"
  340. Element function constraint(model : Element, name : String):
  341. Element associations
  342. Element back_associations
  343. Element association
  344. String destination
  345. associations = allOutgoingAssociationInstances(model, name, "tile_left")
  346. while (0 < list_len(associations)):
  347. association = set_pop(associations)
  348. destination = readAssociationDestination(model, association)
  349. back_associations = allOutgoingAssociationInstances(model, destination, "tile_right")
  350. if (list_len(back_associations) < 1):
  351. return "Left link does not have a right link back"!
  352. else:
  353. association = set_pop(back_associations)
  354. destination = readAssociationDestination(model, association)
  355. if (destination != name):
  356. return "Right link does not have a left link back to the same tile"!
  357. associations = allOutgoingAssociationInstances(model, name, "tile_right")
  358. while (0 < list_len(associations)):
  359. association = set_pop(associations)
  360. destination = readAssociationDestination(model, association)
  361. back_associations = allOutgoingAssociationInstances(model, destination, "tile_left")
  362. if (list_len(back_associations) < 1):
  363. return "Right link does not have a left link back"!
  364. else:
  365. association = set_pop(back_associations)
  366. destination = readAssociationDestination(model, association)
  367. if (destination != name):
  368. return "Right link does not have a left link back to the same tile"!
  369. associations = allOutgoingAssociationInstances(model, name, "tile_top")
  370. while (0 < list_len(associations)):
  371. association = set_pop(associations)
  372. destination = readAssociationDestination(model, association)
  373. back_associations = allOutgoingAssociationInstances(model, destination, "tile_bottom")
  374. if (list_len(back_associations) < 1):
  375. return "Top link does not have a bottom link back"!
  376. else:
  377. association = set_pop(back_associations)
  378. destination = readAssociationDestination(model, association)
  379. if (destination != name):
  380. return "Top link does not have a bottom link back to the same tile"!
  381. associations = allOutgoingAssociationInstances(model, name, "tile_bottom")
  382. while (0 < list_len(associations)):
  383. association = set_pop(associations)
  384. destination = readAssociationDestination(model, association)
  385. back_associations = allOutgoingAssociationInstances(model, destination, "tile_top")
  386. if (list_len(back_associations) < 1):
  387. return "Bottom link does not have a top link back"!
  388. else:
  389. association = set_pop(back_associations)
  390. destination = readAssociationDestination(model, association)
  391. if (destination != name):
  392. return "Bottom link does not have a top link back to the same tile"!
  393. return "OK"!
  394. """
  395. constructors = get_constructor(constraint_code)
  396. self.assertTrue(run_file(all_files,
  397. ["new", "SimpleClassDiagrams", "RPGame",
  398. "set_inheritance", "Inheritance",
  399. "instantiate", "Class", "Scene",
  400. "instantiate", "Class", "Tile",
  401. "instantiate", "Class", "Item",
  402. "instantiate", "Class", "Goal",
  403. "instantiate", "Class", "Character",
  404. "instantiate", "Class", "Hero",
  405. "instantiate", "Association", "scene_has_tiles", "Scene", "Tile",
  406. "instantiate", "Association", "tile_left", "Tile", "Tile",
  407. "instantiate", "Association", "tile_right", "Tile", "Tile",
  408. "instantiate", "Association", "tile_top", "Tile", "Tile",
  409. "instantiate", "Association", "tile_bottom", "Tile", "Tile",
  410. "instantiate", "Association", "character_on", "Character", "Tile",
  411. "instantiate", "Association", "item_on", "Item", "Tile",
  412. "instantiate", "Inheritance", "hero_is_character", "Hero", "Character",
  413. "instantiate", "Inheritance", "goal_is_item", "Goal", "Item",
  414. "attr_add", "Scene", "lower_cardinality", 1,
  415. "attr_add", "Scene", "upper_cardinality", 1,
  416. "attr_add", "Goal", "lower_cardinality", 1,
  417. "attr_add", "scene_has_tiles", "source_lower_cardinality", 1,
  418. "attr_add", "scene_has_tiles", "source_upper_cardinality", 1,
  419. "attr_add", "scene_has_tiles", "target_lower_cardinality", 1,
  420. "attr_add", "item_on", "target_lower_cardinality", 1,
  421. "attr_add", "item_on", "target_upper_cardinality", 1,
  422. "attr_add", "item_on", "source_upper_cardinality", 1,
  423. "attr_add", "character_on", "target_lower_cardinality", 1,
  424. "attr_add", "character_on", "target_upper_cardinality", 1,
  425. "attr_add", "character_on", "source_upper_cardinality", 1,
  426. "attr_add", "tile_left", "source_upper_cardinality", 1,
  427. "attr_add", "tile_left", "target_upper_cardinality", 1,
  428. "attr_add", "tile_right", "source_upper_cardinality", 1,
  429. "attr_add", "tile_right", "target_upper_cardinality", 1,
  430. "attr_add", "tile_top", "source_upper_cardinality", 1,
  431. "attr_add", "tile_top", "target_upper_cardinality", 1,
  432. "attr_add", "tile_bottom", "source_upper_cardinality", 1,
  433. "attr_add", "tile_bottom", "target_upper_cardinality", 1,
  434. "attr_add_code", "Tile", "constraint",
  435. ] + constructors + ["verify"] + ["exit"] + [
  436. "new", "RPGame", "my_game",
  437. "instantiate", "Scene", "scene",
  438. "instantiate", "Hero", "Link",
  439. "instantiate", "Goal", "goal",
  440. "instantiate", "Tile", "tile_00",
  441. "instantiate", "Tile", "tile_01",
  442. "instantiate", "Tile", "tile_10",
  443. "instantiate", "Tile", "tile_11",
  444. "instantiate", "scene_has_tiles", "", "scene", "tile_00",
  445. "instantiate", "scene_has_tiles", "", "scene", "tile_01",
  446. "instantiate", "scene_has_tiles", "", "scene", "tile_10",
  447. "instantiate", "scene_has_tiles", "", "scene", "tile_11",
  448. "instantiate", "character_on", "", "Link", "tile_00",
  449. "instantiate", "item_on", "", "goal", "tile_11",
  450. "instantiate", "tile_left", "", "tile_01", "tile_00",
  451. "instantiate", "tile_left", "", "tile_11", "tile_10",
  452. "instantiate", "tile_right", "", "tile_00", "tile_01",
  453. "instantiate", "tile_right", "", "tile_10", "tile_11",
  454. "instantiate", "tile_top", "", "tile_10", "tile_00",
  455. "instantiate", "tile_top", "", "tile_11", "tile_01",
  456. "instantiate", "tile_bottom", "", "tile_00", "tile_10",
  457. "instantiate", "tile_bottom", "", "tile_01", "tile_11",
  458. "verify",
  459. ],
  460. init + new + loaded + \
  461. set_inheritance + prompt + \
  462. (instantiate_node + prompt) * 6 + \
  463. (instantiate_edge + prompt) * 9 + \
  464. (attr_add + prompt) * 20 + \
  465. ["Element to constrain (empty for global)?",
  466. "Give input to function constructors for LOCAL constraint!",
  467. "Added constraint to model!"] + \
  468. prompt + \
  469. ["OK"] + \
  470. prompt + prompt + new + loaded + \
  471. (instantiate_node + prompt) * 7 + \
  472. (instantiate_edge + prompt) * 14 + \
  473. ["OK"],
  474. mode))
  475. def test_po_pn_interface_transform_pn(self):
  476. code_mark_constraint = \
  477. """
  478. include "primitives.alh"
  479. include "modelling.alh"
  480. include "object_operations.alh"
  481. Boolean function constraint(host_model : Element, name : String):
  482. // Make sure that all places have enough tokens
  483. Element links
  484. String link
  485. String place
  486. links = allIncomingAssociationInstances(host_model, name, "P2T")
  487. while (read_nr_out(links) > 0):
  488. link = set_pop(links)
  489. place = readAssociationSource(host_model, link)
  490. log("Check link " + link)
  491. log("Check place " + place)
  492. if (integer_lt(read_attribute(host_model, place, "tokens"), read_attribute(host_model, link, "weight"))):
  493. // Not enough tokens for this weight
  494. return False!
  495. return True!
  496. """
  497. code_mark_action = \
  498. """
  499. include "primitives.alh"
  500. include "modelling.alh"
  501. Void function action(host_model : Element, name : String, mapping : Element):
  502. unset_attribute(host_model, name, "executing")
  503. instantiate_attribute(host_model, name, "executing", True)
  504. return!
  505. """
  506. code_consume_transition_constraint = \
  507. """
  508. include "primitives.alh"
  509. include "modelling.alh"
  510. Boolean function constraint(host_model : Element, name : String):
  511. // Check if this node is executing currently
  512. return value_eq(read_attribute(host_model, name, "executing"), True)!
  513. """
  514. code_consume_place_constraint = \
  515. """
  516. include "primitives.alh"
  517. include "modelling.alh"
  518. Boolean function constraint(host_model : Element, name : String):
  519. // Check if this node is executing currently
  520. return value_eq(read_attribute(host_model, name, "executed"), False)!
  521. """
  522. code_consume_place_action = \
  523. """
  524. include "primitives.alh"
  525. include "modelling.alh"
  526. Void function action(host_model : Element, name : String, mapping : Element):
  527. Integer tokens
  528. Integer weight
  529. tokens = read_attribute(host_model, name, "tokens")
  530. weight = read_attribute(host_model, mapping["2"], "weight")
  531. unset_attribute(host_model, name, "tokens")
  532. instantiate_attribute(host_model, name, "tokens", tokens - weight)
  533. unset_attribute(host_model, name, "executed")
  534. instantiate_attribute(host_model, name, "executed", True)
  535. return!
  536. """
  537. code_produce_place_action = \
  538. """
  539. include "primitives.alh"
  540. include "modelling.alh"
  541. Void function action(host_model : Element, name : String, mapping : Element):
  542. Integer tokens
  543. Integer weight
  544. tokens = read_attribute(host_model, name, "tokens")
  545. weight = read_attribute(host_model, mapping["2"], "weight")
  546. unset_attribute(host_model, name, "tokens")
  547. instantiate_attribute(host_model, name, "tokens", tokens + weight)
  548. unset_attribute(host_model, name, "executed")
  549. instantiate_attribute(host_model, name, "executed", True)
  550. return!
  551. """
  552. code_unmark_transition_constraint = \
  553. """
  554. include "primitives.alh"
  555. include "modelling.alh"
  556. Boolean function constraint(host_model : Element, name : String):
  557. // Check if this node is executing currently
  558. return value_eq(read_attribute(host_model, name, "executing"), True)!
  559. """
  560. code_unmark_place_constraint = \
  561. """
  562. include "primitives.alh"
  563. include "modelling.alh"
  564. Boolean function constraint(host_model : Element, name : String):
  565. // Check if this node is executing currently
  566. return value_eq(read_attribute(host_model, name, "executed"), True)!
  567. """
  568. code_unmark_transition_action = \
  569. """
  570. include "primitives.alh"
  571. include "modelling.alh"
  572. Void function action(host_model : Element, name : String, mapping : Element):
  573. unset_attribute(host_model, name, "executing")
  574. instantiate_attribute(host_model, name, "executing", False)
  575. return!
  576. """
  577. code_unmark_place_action = \
  578. """
  579. include "primitives.alh"
  580. include "modelling.alh"
  581. Void function action(host_model : Element, name : String, mapping : Element):
  582. unset_attribute(host_model, name, "executed")
  583. instantiate_attribute(host_model, name, "executed", False)
  584. return!
  585. """
  586. constructor_mark_constraint = get_constructor(code_mark_constraint)
  587. constructor_mark_action = get_constructor(code_mark_action)
  588. constructor_consume_transition_constraint = get_constructor(code_consume_transition_constraint)
  589. constructor_consume_place_constraint = get_constructor(code_consume_place_constraint)
  590. constructor_consume_place_action = get_constructor(code_consume_place_action)
  591. constructor_produce_transition_constraint = constructor_consume_transition_constraint
  592. constructor_produce_place_constraint = constructor_consume_place_constraint
  593. constructor_produce_place_action = get_constructor(code_produce_place_action)
  594. constructor_unmark_transition_constraint = constructor_consume_transition_constraint
  595. constructor_unmark_transition_action = get_constructor(code_unmark_transition_action)
  596. constructor_unmark_place_constraint = get_constructor(code_unmark_place_constraint)
  597. constructor_unmark_place_action = get_constructor(code_unmark_place_action)
  598. self.assertTrue(run_file(all_files,
  599. ["new", "SimpleClassDiagrams", "PetriNets_Runtime",
  600. "set_inheritance", "Inheritance",
  601. "instantiate", "Class", "Natural",
  602. "instantiate", "Class", "Boolean",
  603. "instantiate", "Class", "Place",
  604. "attr_def", "Place", "tokens", "Natural",
  605. "attr_def", "Place", "executed", "Boolean",
  606. "instantiate", "Class", "Transition",
  607. "attr_def", "Transition", "executing", "Boolean",
  608. "instantiate", "Association", "P2T", "Place", "Transition",
  609. "attr_def", "P2T", "weight", "Natural",
  610. "instantiate", "Association", "T2P", "Transition", "Place",
  611. "attr_def", "T2P", "weight", "Natural",
  612. "exit",
  613. "new", "PetriNets_Runtime", "pn",
  614. "instantiate", "Place", "p1",
  615. "attr_add", "p1", "tokens", 1,
  616. "attr_add", "p1", "executed", False,
  617. "instantiate", "Place", "p2",
  618. "attr_add", "p2", "tokens", 2,
  619. "attr_add", "p2", "executed", False,
  620. "instantiate", "Transition", "t1",
  621. "attr_add", "t1", "executing", False,
  622. "instantiate", "P2T", "p2t", "p1", "t1",
  623. "attr_add", "p2t", "weight", 1,
  624. "instantiate", "P2T", "p2t2", "p2", "t1",
  625. "attr_add", "p2t2", "weight", 1,
  626. "exit",
  627. "ramify", "PetriNets_Runtime_SCHEDULE", "in", "PetriNets_Runtime",
  628. "new", "PetriNets_Runtime_SCHEDULE", "pn_simulate",
  629. # Rule 1: mark transition to execute
  630. # LHS
  631. "instantiate", "LHS", "lhs_mark",
  632. "instantiate", "Pre_Transition", "pre_mark_t",
  633. "attr_add", "pre_mark_t", "label", "0",
  634. "attr_add_code", "pre_mark_t", "constraint",
  635. ] + constructor_mark_constraint + [
  636. "instantiate", "LHS_contains", "", "lhs_mark", "pre_mark_t",
  637. # RHS
  638. "instantiate", "RHS", "rhs_mark",
  639. "instantiate", "Post_Transition", "post_mark_t",
  640. "attr_add", "post_mark_t", "label", "0",
  641. "attr_add_code", "post_mark_t", "action",
  642. ] + constructor_mark_action + [
  643. "instantiate", "RHS_contains", "", "rhs_mark", "post_mark_t",
  644. # Atomic of rule 1 "mark"
  645. "instantiate", "Atomic", "mark",
  646. "instantiate", "AtomicLHS", "", "mark", "lhs_mark",
  647. "instantiate", "AtomicRHS", "", "mark", "rhs_mark",
  648. # Rule 2: consume tokens going into marked transition
  649. # LHS
  650. "instantiate", "LHS", "lhs_consume",
  651. "instantiate", "Pre_Transition", "pre_consume_t",
  652. "attr_add", "pre_consume_t", "label", "0",
  653. "attr_add_code", "pre_consume_t", "constraint",
  654. ] + constructor_consume_transition_constraint + [
  655. "instantiate", "Pre_Place", "pre_consume_p",
  656. "attr_add", "pre_consume_p", "label", "1",
  657. "attr_add_code", "pre_consume_p", "constraint",
  658. ] + constructor_consume_place_constraint + [
  659. "instantiate", "Pre_P2T", "pre_consume_p2t", "pre_consume_p", "pre_consume_t",
  660. "attr_add", "pre_consume_p2t", "label", "2",
  661. "instantiate", "LHS_contains", "", "lhs_consume", "pre_consume_t",
  662. "instantiate", "LHS_contains", "", "lhs_consume", "pre_consume_p",
  663. "instantiate", "LHS_contains", "", "lhs_consume", "pre_consume_p2t",
  664. # RHS
  665. "instantiate", "RHS", "rhs_consume",
  666. "instantiate", "Post_Transition", "post_consume_t",
  667. "attr_add", "post_consume_t", "label", "0",
  668. "instantiate", "Post_Place", "post_consume_p",
  669. "attr_add", "post_consume_p", "label", "1",
  670. "attr_add_code", "post_consume_p", "action",
  671. ] + constructor_consume_place_action + [
  672. "instantiate", "Post_P2T", "post_consume_p2t", "post_consume_p", "post_consume_t",
  673. "attr_add", "post_consume_p2t", "label", "2",
  674. "instantiate", "RHS_contains", "", "rhs_consume", "post_consume_t",
  675. "instantiate", "RHS_contains", "", "rhs_consume", "post_consume_p",
  676. "instantiate", "RHS_contains", "", "rhs_consume", "post_consume_p2t",
  677. # Atomic of rule 2 "consume"
  678. "instantiate", "Atomic", "consume",
  679. "instantiate", "AtomicLHS", "", "consume", "lhs_consume",
  680. "instantiate", "AtomicRHS", "", "consume", "rhs_consume",
  681. # Rule 3: produce tokens going out of marked transition
  682. # LHS
  683. "instantiate", "LHS", "lhs_produce",
  684. "instantiate", "Pre_Transition", "pre_produce_t",
  685. "attr_add", "pre_produce_t", "label", "0",
  686. "attr_add_code", "pre_produce_t", "constraint",
  687. ] + constructor_produce_transition_constraint + [
  688. "instantiate", "Pre_Place", "pre_produce_p",
  689. "attr_add", "pre_produce_p", "label", "1",
  690. "attr_add_code", "pre_produce_p", "constraint",
  691. ] + constructor_produce_place_constraint + [
  692. "instantiate", "Pre_T2P", "pre_produce_t2p", "pre_produce_t", "pre_produce_p",
  693. "attr_add", "pre_produce_t2p", "label", "2",
  694. "instantiate", "LHS_contains", "", "lhs_produce", "pre_produce_t",
  695. "instantiate", "LHS_contains", "", "lhs_produce", "pre_produce_p",
  696. "instantiate", "LHS_contains", "", "lhs_produce", "pre_produce_t2p",
  697. # RHS
  698. "instantiate", "RHS", "rhs_produce",
  699. "instantiate", "Post_Transition", "post_produce_t",
  700. "attr_add", "post_produce_t", "label", "0",
  701. "instantiate", "Post_Place", "post_produce_p",
  702. "attr_add", "post_produce_p", "label", "1",
  703. "attr_add_code", "post_produce_p", "action",
  704. ] + constructor_produce_place_action + [
  705. "instantiate", "Post_T2P", "post_produce_t2p", "post_produce_t", "post_produce_p",
  706. "attr_add", "post_produce_t2p", "label", "2",
  707. "instantiate", "RHS_contains", "", "rhs_produce", "post_produce_t",
  708. "instantiate", "RHS_contains", "", "rhs_produce", "post_produce_p",
  709. "instantiate", "RHS_contains", "", "rhs_produce", "post_produce_t2p",
  710. # Atomic of rule 3 "produce"
  711. "instantiate", "Atomic", "produce",
  712. "instantiate", "AtomicLHS", "", "produce", "lhs_produce",
  713. "instantiate", "AtomicRHS", "", "produce", "rhs_produce",
  714. # Rule 4: unmark marked transition
  715. # LHS
  716. "instantiate", "LHS", "lhs_unmark_transition",
  717. "instantiate", "Pre_Transition", "pre_unmark_t",
  718. "attr_add", "pre_unmark_t", "label", "0",
  719. "attr_add_code", "pre_unmark_t", "constraint",
  720. ] + constructor_unmark_transition_constraint + [
  721. "instantiate", "LHS_contains", "", "lhs_unmark_transition", "pre_unmark_t",
  722. # RHS
  723. "instantiate", "RHS", "rhs_unmark_transition",
  724. "instantiate", "Post_Transition", "post_unmark_t",
  725. "attr_add", "post_unmark_t", "label", "0",
  726. "attr_add_code", "post_unmark_t", "action",
  727. ] + constructor_unmark_transition_action + [
  728. "instantiate", "RHS_contains", "", "rhs_unmark_transition", "post_unmark_t",
  729. # Atomic of rule 4 "unmark transition"
  730. "instantiate", "Atomic", "unmark_transition",
  731. "instantiate", "AtomicLHS", "", "unmark_transition", "lhs_unmark_transition",
  732. "instantiate", "AtomicRHS", "", "unmark_transition", "rhs_unmark_transition",
  733. # Rule 5: unmark marked places
  734. # LHS
  735. "instantiate", "LHS", "lhs_unmark_place",
  736. "instantiate", "Pre_Place", "pre_unmark_p",
  737. "attr_add", "pre_unmark_p", "label", "0",
  738. "attr_add_code", "pre_unmark_p", "constraint",
  739. ] + constructor_unmark_place_constraint + [
  740. "instantiate", "LHS_contains", "", "lhs_unmark_place", "pre_unmark_p",
  741. # RHS
  742. "instantiate", "RHS", "rhs_unmark_place",
  743. "instantiate", "Post_Place", "post_unmark_p",
  744. "attr_add", "post_unmark_p", "label", "0",
  745. "attr_add_code", "post_unmark_p", "action",
  746. ] + constructor_unmark_place_action + [
  747. "instantiate", "RHS_contains", "", "rhs_unmark_place", "post_unmark_p",
  748. # Atomic of rule 5 "unmark place"
  749. "instantiate", "Atomic", "unmark_place",
  750. "instantiate", "AtomicLHS", "", "unmark_place", "lhs_unmark_place",
  751. "instantiate", "AtomicRHS", "", "unmark_place", "rhs_unmark_place",
  752. # Compose schedule
  753. "instantiate", "Composite", "main",
  754. "instantiate", "Success", "success",
  755. "instantiate", "Failure", "failure",
  756. # Add all rules
  757. "instantiate", "Contains", "", "main", "success",
  758. "instantiate", "Contains", "", "main", "failure",
  759. "instantiate", "Contains", "", "main", "mark",
  760. "instantiate", "Contains", "", "main", "consume",
  761. "instantiate", "Contains", "", "main", "produce",
  762. "instantiate", "Contains", "", "main", "unmark_transition",
  763. "instantiate", "Contains", "", "main", "unmark_place",
  764. # Scheduling order
  765. "instantiate", "OnSuccess", "", "mark", "consume",
  766. "instantiate", "OnFailure", "", "mark", "success",
  767. "instantiate", "OnSuccess", "", "consume", "consume",
  768. "instantiate", "OnFailure", "", "consume", "produce",
  769. "instantiate", "OnSuccess", "", "produce", "produce",
  770. "instantiate", "OnFailure", "", "produce", "unmark_transition",
  771. "instantiate", "OnSuccess", "", "unmark_transition", "unmark_place",
  772. "instantiate", "OnFailure", "", "unmark_transition", "failure",
  773. "instantiate", "OnSuccess", "", "unmark_place", "unmark_place",
  774. "instantiate", "OnFailure", "", "unmark_place", "success",
  775. # Composite initial
  776. "instantiate", "Initial", "", "main", "mark",
  777. "exit",
  778. "transform", "pn", "pn_simulate",
  779. "load", "pn",
  780. "list",
  781. "verify",
  782. "read", "t1",
  783. "read", "p1",
  784. "read", "p2",
  785. "exit",
  786. ],
  787. None, "PO"))
  788. def test_po_pn_interface_model_transform_pn(self):
  789. PN_runtime = \
  790. """
  791. import models/SimpleClassDiagrams as SimpleClassDiagrams
  792. SimpleClassDiagrams (Inheritance) PetriNets_Runtime{
  793. Class Natural {}
  794. Class Boolean {}
  795. Class Place {
  796. tokens : Natural
  797. executed : Boolean
  798. }
  799. Class Transition {
  800. executing : Boolean
  801. }
  802. Association P2T (Place, Transition) {
  803. weight : Natural
  804. }
  805. Association T2P (Transition, Place) {
  806. weight : Natural
  807. }
  808. }
  809. export PetriNets_Runtime to models/PetriNets_Runtime
  810. """
  811. PN_model = \
  812. """
  813. import models/PetriNets_Runtime as PetriNets_Runtime
  814. PetriNets_Runtime pn {
  815. Place p1 {
  816. tokens = 1
  817. executed = False
  818. }
  819. Place p2 {
  820. tokens = 2
  821. executed = False
  822. }
  823. Place p3 {
  824. tokens = 3
  825. executed = False
  826. }
  827. Transition t1 {
  828. executing = False
  829. }
  830. P2T (p1, t1) {
  831. weight = 1
  832. }
  833. P2T (p2, t1) {
  834. weight = 1
  835. }
  836. T2P (t1, p3) {
  837. weight = 2
  838. }
  839. }
  840. export pn to models/pn
  841. """
  842. schedule_model = \
  843. """
  844. import models/PetriNets_Runtime_SCHEDULE as PN_Transform
  845. PN_Transform s {
  846. Composite schedule {
  847. {Contains} Failure failure {}
  848. {Contains} Success success {}
  849. {Contains} Atomic mark {
  850. LHS {
  851. Pre_Transition {
  852. label = "1"
  853. constraint = $
  854. include "primitives.alh"
  855. include "modelling.alh"
  856. include "object_operations.alh"
  857. Boolean function constraint(host_model : Element, name : String):
  858. Element links
  859. String link
  860. String place
  861. links = allIncomingAssociationInstances(host_model, name, "P2T")
  862. while (read_nr_out(links) > 0):
  863. link = set_pop(links)
  864. place = readAssociationSource(host_model, link)
  865. if (integer_lt(read_attribute(host_model, place, "tokens"), read_attribute(host_model, link, "weight"))):
  866. return False!
  867. return True!
  868. $
  869. }
  870. }
  871. RHS {
  872. Post_Transition {
  873. label = "1"
  874. action = $
  875. include "primitives.alh"
  876. include "modelling.alh"
  877. Void function action(host_model : Element, name : String, mapping : Element):
  878. unset_attribute(host_model, name, "executing")
  879. instantiate_attribute(host_model, name, "executing", True)
  880. return!
  881. $
  882. }
  883. }
  884. }
  885. {Contains} ForAll consume {
  886. LHS {
  887. Pre_Transition lhs_consume_t{
  888. label = "0"
  889. constraint = $
  890. include "primitives.alh"
  891. include "modelling.alh"
  892. Boolean function constraint(host_model : Element, name : String):
  893. // Check if this node is executing currently
  894. return value_eq(read_attribute(host_model, name, "executing"), True)!
  895. $
  896. }
  897. Pre_Place lhs_consume_p{
  898. label = "1"
  899. }
  900. Pre_P2T lhs_consume_p2t(lhs_consume_p, lhs_consume_t){
  901. label = "2"
  902. }
  903. }
  904. RHS {
  905. Post_Transition rhs_consume_t {
  906. label = "0"
  907. }
  908. Post_Place rhs_consume_p {
  909. label = "1"
  910. action = $
  911. include "primitives.alh"
  912. include "modelling.alh"
  913. Void function action(host_model : Element, name : String, mapping : Element):
  914. Integer tokens
  915. Integer weight
  916. tokens = read_attribute(host_model, name, "tokens")
  917. weight = read_attribute(host_model, mapping["2"], "weight")
  918. unset_attribute(host_model, name, "tokens")
  919. instantiate_attribute(host_model, name, "tokens", tokens - weight)
  920. return!
  921. $
  922. }
  923. Post_P2T (rhs_consume_p, rhs_consume_t){
  924. label = "2"
  925. }
  926. }
  927. }
  928. {Contains} ForAll produce {
  929. LHS {
  930. Pre_Transition lhs_produce_t{
  931. label = "0"
  932. constraint = $
  933. include "primitives.alh"
  934. include "modelling.alh"
  935. Boolean function constraint(host_model : Element, name : String):
  936. // Check if this node is executing currently
  937. return value_eq(read_attribute(host_model, name, "executing"), True)!
  938. $
  939. }
  940. Pre_Place lhs_produce_p{
  941. label = "1"
  942. }
  943. Pre_T2P (lhs_produce_t, lhs_produce_p){
  944. label = "2"
  945. }
  946. }
  947. RHS {
  948. Post_Transition rhs_produce_t{
  949. label = "0"
  950. }
  951. Post_Place rhs_produce_p{
  952. label = "1"
  953. action = $
  954. include "primitives.alh"
  955. include "modelling.alh"
  956. Void function action(host_model : Element, name : String, mapping : Element):
  957. Integer tokens
  958. Integer weight
  959. tokens = read_attribute(host_model, name, "tokens")
  960. weight = read_attribute(host_model, mapping["2"], "weight")
  961. unset_attribute(host_model, name, "tokens")
  962. instantiate_attribute(host_model, name, "tokens", tokens + weight)
  963. return!
  964. $
  965. }
  966. Post_T2P (rhs_produce_t, rhs_produce_p){
  967. label = "2"
  968. }
  969. }
  970. }
  971. {Contains} Atomic unmark_transition {
  972. LHS {
  973. Pre_Transition {
  974. label = "0"
  975. constraint = $
  976. include "primitives.alh"
  977. include "modelling.alh"
  978. Boolean function constraint(host_model : Element, name : String):
  979. // Check if this node is executing currently
  980. return value_eq(read_attribute(host_model, name, "executing"), True)!
  981. $
  982. }
  983. }
  984. RHS {
  985. Post_Transition {
  986. label = "0"
  987. action = $
  988. include "primitives.alh"
  989. include "modelling.alh"
  990. Void function action(host_model : Element, name : String, mapping : Element):
  991. unset_attribute(host_model, name, "executing")
  992. instantiate_attribute(host_model, name, "executing", False)
  993. return!
  994. $
  995. }
  996. }
  997. }
  998. }
  999. OnSuccess (mark, consume) {}
  1000. OnFailure (mark, failure) {}
  1001. OnSuccess (consume, produce) {}
  1002. OnFailure (consume, produce) {}
  1003. OnSuccess (produce, unmark_transition) {}
  1004. OnFailure (produce, unmark_transition) {}
  1005. OnSuccess (unmark_transition, success) {}
  1006. OnFailure (unmark_transition, failure) {}
  1007. Initial (schedule, mark) {}
  1008. }
  1009. export s to models/pn_simulate
  1010. """
  1011. self.assertTrue(run_file(all_files,
  1012. get_model_constructor(PN_runtime) + \
  1013. get_model_constructor(PN_model) + [
  1014. "ramify", "PetriNets_Runtime_SCHEDULE", "PetriNets_Runtime",
  1015. ] + get_model_constructor(schedule_model) + [
  1016. "transform", "pn", "pn_simulate",
  1017. "load", "pn",
  1018. "list",
  1019. "verify",
  1020. "read", "t1",
  1021. "read", "p1",
  1022. "read", "p2",
  1023. "read", "p3",
  1024. "exit",
  1025. ],
  1026. None, "PO"))
  1027. def test_po_pn_interface_transform_pn_to_runtime(self):
  1028. PN_runtime = \
  1029. """
  1030. import models/SimpleClassDiagrams as SimpleClassDiagrams
  1031. SimpleClassDiagrams (Inheritance) PetriNets_Design{
  1032. Class Natural {}
  1033. Class Place {
  1034. tokens : Natural
  1035. }
  1036. Class Transition {}
  1037. Association P2T (Place, Transition) {
  1038. weight : Natural
  1039. }
  1040. Association T2P (Transition, Place) {
  1041. weight : Natural
  1042. }
  1043. }
  1044. SimpleClassDiagrams (Inheritance) PetriNets_Runtime{
  1045. Class Natural {}
  1046. Class Boolean {}
  1047. Class Place {
  1048. tokens : Natural
  1049. }
  1050. Class Transition {
  1051. executing : Boolean
  1052. }
  1053. Association P2T (Place, Transition) {
  1054. weight : Natural
  1055. }
  1056. Association T2P (Transition, Place) {
  1057. weight : Natural
  1058. }
  1059. }
  1060. export PetriNets_Design to models/PetriNets_Design
  1061. export PetriNets_Runtime to models/PetriNets_Runtime
  1062. """
  1063. PN_model = \
  1064. """
  1065. import models/PetriNets_Design as PetriNets
  1066. PetriNets pn {
  1067. Place p1 {
  1068. tokens = 1
  1069. }
  1070. Place p2 {
  1071. tokens = 2
  1072. }
  1073. Place p3 {
  1074. tokens = 3
  1075. }
  1076. Transition t1 {}
  1077. P2T (p1, t1) {
  1078. weight = 1
  1079. }
  1080. P2T (p2, t1) {
  1081. weight = 1
  1082. }
  1083. T2P (t1, p3) {
  1084. weight = 2
  1085. }
  1086. }
  1087. export pn to models/pn
  1088. """
  1089. schedule_model = \
  1090. """
  1091. import models/PetriNets_SCHEDULE as PN_Transform
  1092. PN_Transform annotate {
  1093. Composite schedule {
  1094. {Contains} Failure failure {}
  1095. {Contains} Success success {}
  1096. {Contains} ForAll copy_transitions {
  1097. LHS {
  1098. Pre_SOURCE_Transition {
  1099. label = "0"
  1100. }
  1101. }
  1102. RHS {
  1103. Post_SOURCE_Transition ct1 {
  1104. label = "0"
  1105. }
  1106. Post_TARGET_Transition ct2 {
  1107. label = "1"
  1108. action = $
  1109. include "primitives.alh"
  1110. include "modelling.alh"
  1111. Void function action(host_model : Element, name : String, mapping : Element):
  1112. instantiate_attribute(host_model, name, "executing", False)
  1113. return!
  1114. $
  1115. }
  1116. Post_TransitionLink (ct1, ct2){
  1117. label = "2"
  1118. }
  1119. }
  1120. }
  1121. {Contains} ForAll copy_places {
  1122. LHS {
  1123. Pre_SOURCE_Place {
  1124. label = "0"
  1125. }
  1126. }
  1127. RHS {
  1128. Post_SOURCE_Place cp1 {
  1129. label = "0"
  1130. }
  1131. Post_TARGET_Place cp2 {
  1132. label = "1"
  1133. action = $
  1134. include "primitives.alh"
  1135. include "modelling.alh"
  1136. Void function action(host_model : Element, name : String, mapping : Element):
  1137. instantiate_attribute(host_model, name, "tokens", read_attribute(host_model, mapping["0"], "tokens"))
  1138. return!
  1139. $
  1140. }
  1141. Post_PlaceLink (cp1, cp2){
  1142. label = "2"
  1143. }
  1144. }
  1145. }
  1146. {Contains} ForAll copy_P2T {
  1147. LHS {
  1148. Pre_SOURCE_Place cp2t_p{
  1149. label = "0"
  1150. }
  1151. Pre_SOURCE_Transition cp2t_t{
  1152. label = "1"
  1153. }
  1154. Pre_SOURCE_P2T (cp2t_p, cp2t_t){
  1155. label = "2"
  1156. }
  1157. Pre_TARGET_Place cp2t_p2{
  1158. label = "3"
  1159. }
  1160. Pre_TARGET_Transition cp2t_t2{
  1161. label = "4"
  1162. }
  1163. Pre_PlaceLink (cp2t_p, cp2t_p2){
  1164. label = "5"
  1165. }
  1166. Pre_TransitionLink (cp2t_t, cp2t_t2){
  1167. label = "6"
  1168. }
  1169. }
  1170. RHS {
  1171. Post_SOURCE_Place cp2t_p{
  1172. label = "0"
  1173. }
  1174. Post_SOURCE_Transition cp2t_t{
  1175. label = "1"
  1176. }
  1177. Post_SOURCE_P2T cp2t_p2t (cp2t_p, cp2t_t){
  1178. label = "2"
  1179. }
  1180. Post_TARGET_Place cp2t_p2 {
  1181. label = "3"
  1182. }
  1183. Post_TARGET_Transition cp2t_t2 {
  1184. label = "4"
  1185. }
  1186. Post_PlaceLink (cp2t_p, cp2t_p2){
  1187. label = "5"
  1188. }
  1189. Post_TransitionLink (cp2t_t, cp2t_t2){
  1190. label = "6"
  1191. }
  1192. Post_TARGET_P2T cp2t_p2t2(cp2t_p2, cp2t_t2) {
  1193. label = "7"
  1194. action = $
  1195. include "primitives.alh"
  1196. include "modelling.alh"
  1197. Void function action(host_model : Element, name : String, mapping : Element):
  1198. instantiate_attribute(host_model, name, "weight", read_attribute(host_model, mapping["2"], "weight"))
  1199. return!
  1200. $
  1201. }
  1202. }
  1203. }
  1204. {Contains} ForAll copy_T2P {
  1205. LHS {
  1206. Pre_SOURCE_Place ct2p_p{
  1207. label = "0"
  1208. }
  1209. Pre_SOURCE_Transition ct2p_t{
  1210. label = "1"
  1211. }
  1212. Pre_SOURCE_T2P (ct2p_p, ct2p_t){
  1213. label = "2"
  1214. }
  1215. Pre_TARGET_Place ct2p_p2{
  1216. label = "3"
  1217. }
  1218. Pre_TARGET_Transition ct2p_t2{
  1219. label = "4"
  1220. }
  1221. Pre_PlaceLink (ct2p_p, ct2p_p2){
  1222. label = "5"
  1223. }
  1224. Pre_TransitionLink (ct2p_t, ct2p_t2){
  1225. label = "6"
  1226. }
  1227. }
  1228. RHS {
  1229. Post_SOURCE_Place ct2p_p{
  1230. label = "0"
  1231. }
  1232. Post_SOURCE_Transition ct2p_t{
  1233. label = "1"
  1234. }
  1235. Post_SOURCE_T2P (ct2p_p, ct2p_t){
  1236. label = "2"
  1237. }
  1238. Post_TARGET_Place ct2p_p2 {
  1239. label = "3"
  1240. }
  1241. Post_TARGET_Transition ct2p_t2 {
  1242. label = "4"
  1243. }
  1244. Post_PlaceLink (ct2p_p, ct2p_p2){
  1245. label = "5"
  1246. }
  1247. Post_TransitionLink (ct2p_t, ct2p_t2){
  1248. label = "6"
  1249. }
  1250. Post_TARGET_T2P (ct2p_p2, ct2p_t2) {
  1251. label = "7"
  1252. action = $
  1253. include "primitives.alh"
  1254. include "modelling.alh"
  1255. Void function action(host_model : Element, name : String, mapping : Element):
  1256. instantiate_attribute(host_model, name, "weight", read_attribute(host_model, mapping["2"], "weight"))
  1257. return!
  1258. $
  1259. }
  1260. }
  1261. }
  1262. }
  1263. OnSuccess (copy_places, copy_transitions) {}
  1264. OnSuccess (copy_transitions, copy_P2T) {}
  1265. OnSuccess (copy_P2T, copy_T2P) {}
  1266. OnSuccess (copy_T2P, success) {}
  1267. OnFailure (copy_places, copy_transitions) {}
  1268. OnFailure (copy_transitions, copy_P2T) {}
  1269. OnFailure (copy_P2T, copy_T2P) {}
  1270. OnFailure (copy_T2P, success) {}
  1271. Initial (schedule, copy_places) {}
  1272. }
  1273. export annotate to models/pn_annotate
  1274. """
  1275. self.assertTrue(run_file(all_files,
  1276. get_model_constructor(PN_runtime) + \
  1277. get_model_constructor(PN_model) + [
  1278. "unify", "PetriNets_Design_to_Runtime", "PetriNets_Design", "SOURCE_", "PetriNets_Runtime", "TARGET_",
  1279. "join", "pn", "PetriNets_Design_to_Runtime", "SOURCE_",
  1280. "load", "PetriNets_Design_to_Runtime",
  1281. "instantiate", "Association", "PlaceLink", "SOURCE_Place", "TARGET_Place",
  1282. "instantiate", "Association", "TransitionLink", "SOURCE_Transition", "TARGET_Transition",
  1283. "exit",
  1284. "ramify", "PetriNets_SCHEDULE", "PetriNets_Design_to_Runtime",
  1285. ] + get_model_constructor(schedule_model) + [
  1286. "transform", "pn", "pn_annotate",
  1287. "split", "pn", "PetriNets_Runtime", "TARGET/",
  1288. "load", "pn",
  1289. "list",
  1290. "verify",
  1291. "read", "t1",
  1292. "read", "p1",
  1293. "read", "p2",
  1294. "read", "p3",
  1295. "exit",
  1296. ],
  1297. None, "PO"))