test_pn_interface.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. import unittest
  2. from utils import run_file, get_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. "ramify.alc",
  29. "metamodels.alc",
  30. "random.alc",
  31. "constructors.alc",
  32. "modelling.alc",
  33. "compilation_manager.alc",
  34. ]
  35. greeting = ["Welcome to the Model Management Interface, running live on the Modelverse!",
  36. "Use 'help' command for a list of possible commands"]
  37. new = ["Metamodel to instantiate?",
  38. "Name of model?"]
  39. prompt = ["Please give your command."]
  40. loaded = ["Model loaded, ready for commands!",
  41. "Use 'help' command for a list of possible commands"] + prompt
  42. load = ["Model to load?"]
  43. instantiate = ["Type to instantiate?"]
  44. instantiate_name = ["Name of new element?"]
  45. instantiate_ok = ["Instantiation successful!"]
  46. instantiate_source= ["Source name?"]
  47. instantiate_destination = ["Destination name?"]
  48. def list_menu(defined):
  49. defined.append(("PetriNets", "SimpleClassDiagrams"))
  50. defined.append(("SimpleClassDiagrams", "SimpleClassDiagrams"))
  51. defined.append(("LTM_bottom", "LTM_bottom"))
  52. return ["Found models:",
  53. set([" %s : %s" % (m, mm) for m, mm in defined])]
  54. def list_model(defined):
  55. return ["List of all elements:",
  56. set([" %s : %s" % (m, mm) for m, mm in defined])]
  57. def read_node(name, t, defs, attrs):
  58. return ["Element to read?",
  59. "Name: %s" % name,
  60. "Type: %s" % t,
  61. "Defines attributes:"] + \
  62. ([set([" %s : %s" % (m, mm) for m, mm in defs])] if defs else []) + \
  63. ["Attributes:"] + \
  64. ([set([' "%s" : "%s" = %s' % (m, mm, v) for m, mm, v in attrs])] if attrs else [])
  65. def read_edge(name, t, src, dst, defs, attrs):
  66. return ["Element to read?",
  67. "Name: %s" % name,
  68. "Type: %s" % t,
  69. "Source: %s" % src,
  70. "Destination: %s" % dst,
  71. "Defines attributes:"] + \
  72. ([set([" %s : %s" % (m, mm) for m, mm in defs])] if defs else []) + \
  73. ["Attributes:"] + \
  74. ([set([' "%s" : "%s" = %s' % (m, mm, v) for m, mm, v in attrs])] if attrs else [])
  75. delete = ["Model to delete?", "Deleted!"]
  76. rename = ["Old name?", "New name?", "Rename complete!"]
  77. attr_add = ["Which model do you want to assign an attribute to?",
  78. "Which attribute do you wish to assign?",
  79. "Value of attribute?",
  80. "Added attribute!"]
  81. attr_del = ["Which model do you want to remove an attribute of?",
  82. "Which attribute do you want to delete?",
  83. "Attribute deleted!",
  84. ]
  85. help_root = ["Currently no model is loaded, so your operations are limited to:",
  86. " new -- Create a new model and save it for future use"
  87. " load -- Load a previously made model",
  88. " rename -- Rename a previously made model",
  89. " delete -- Delete a previously made model",
  90. " list -- Show a list of all stored models",
  91. " help -- Show a list of possible commands"]
  92. verify_fail_weight= ["Natural number not larger than or equal to zero"]
  93. verify_fail_tokens= ["Natural number not larger than or equal to zero"]
  94. verify_fail_structure = ["Source of model edge not typed by source of type: p2t"]
  95. init = greeting + prompt
  96. did_instantiate_simple = init + \
  97. new + \
  98. loaded +\
  99. instantiate_node + \
  100. prompt + \
  101. instantiate_node + \
  102. prompt + \
  103. attr_add + \
  104. prompt + \
  105. instantiate_node + \
  106. prompt + \
  107. attr_add + \
  108. prompt + \
  109. instantiate_edge + \
  110. prompt + \
  111. attr_add + \
  112. prompt + \
  113. instantiate_edge + \
  114. prompt + \
  115. attr_add + \
  116. prompt
  117. def list_types(t):
  118. return ["List of types:"] + \
  119. [set([" %s : %s" % (m, mm) for m, mm in t])]
  120. modify = ["Element to modify?",
  121. "Attribute to modify?",
  122. "New value?",
  123. "Modified!",]
  124. class TestPetrinetInterface(unittest.TestCase):
  125. def test_po_pn_interface_manage(self):
  126. self.pn_interface_manage("PO")
  127. def test_co_pn_interface_manage(self):
  128. self.pn_interface_manage("CO")
  129. def pn_interface_manage(self, mode):
  130. self.assertTrue(run_file(all_files,
  131. ["list",
  132. "new", "PetriNets", "abc", "exit",
  133. "list",
  134. "new", "PetriNets", "def", "exit",
  135. "list",
  136. "delete", "def",
  137. "list",
  138. "rename", "abc", "a",
  139. "list",
  140. "delete", "a",
  141. "list",
  142. ],
  143. init + \
  144. list_menu([]) + prompt + \
  145. new + loaded + prompt + list_menu([("abc", "PetriNets")]) + prompt + \
  146. new + loaded + prompt + list_menu([("abc", "PetriNets"), ("def", "PetriNets")]) + prompt + \
  147. delete + prompt + list_menu([("abc", "PetriNets")]) + prompt + \
  148. rename + prompt + list_menu([("a", "PetriNets")]) + prompt + \
  149. delete + prompt + list_menu([]) + prompt,
  150. mode))
  151. def test_po_pn_interface_new_reload(self):
  152. self.pn_interface_new_reload("PO")
  153. def test_co_pn_interface_new_reload(self):
  154. self.pn_interface_new_reload("CO")
  155. def pn_interface_new_reload(self, mode):
  156. self.assertTrue(run_file(all_files,
  157. ["new", "PetriNets", "abc", "exit", "load", "abc"],
  158. init + new + loaded + prompt + load + loaded,
  159. mode))
  160. def test_po_pn_interface_instantiate_place(self):
  161. self.pn_interface_instantiate_place("PO")
  162. def test_co_pn_interface_instantiate_place(self):
  163. self.pn_interface_instantiate_place("CO")
  164. def pn_interface_instantiate_place(self, mode):
  165. self.assertTrue(run_file(all_files,
  166. ["new", "PetriNets", "abc",
  167. "instantiate", "Place", "p1",
  168. "attr_add", "p1", "tokens", 5,
  169. "list",
  170. "read", "p1",
  171. "instantiate", "Transition", "t1",
  172. "list",
  173. "read", "t1"],
  174. init + new + loaded + \
  175. instantiate_node + prompt + \
  176. attr_add + prompt + \
  177. list_model([("p1", "Place"), ("p1.tokens", "Natural")]) + prompt + \
  178. read_node("p1", "Place", [], [("tokens", "Natural", 5)]) + prompt + \
  179. instantiate_node + prompt + \
  180. list_model([("p1", "Place"), ("t1", "Transition"), ("p1.tokens", "Natural")]) + prompt + \
  181. read_node("t1", "Transition", [], []) + prompt,
  182. mode))
  183. def test_po_pn_interface_instantiate_arcs(self):
  184. self.pn_interface_instantiate_arcs("PO")
  185. def test_co_pn_interface_instantiate_arcs(self):
  186. self.pn_interface_instantiate_arcs("CO")
  187. def pn_interface_instantiate_arcs(self, mode):
  188. self.assertTrue(run_file(all_files,
  189. do_instantiate_simple + [
  190. "read", "p1",
  191. "read", "p2",
  192. "read", "t1",
  193. "read", "p2t",
  194. "read", "t2p",
  195. ],
  196. did_instantiate_simple + \
  197. read_node("p1", "Place", [], [("tokens", "Natural", 5)]) + prompt + \
  198. read_node("p2", "Place", [], [("tokens", "Natural", 0)]) + prompt + \
  199. read_node("t1", "Transition", [], []) + prompt + \
  200. read_edge("p2t", "P2T", "p1", "t1", [], [("weight", "Natural", 2)]) + prompt + \
  201. read_edge("t2p", "T2P", "t1", "p2", [], [("weight", "Natural", 1)]) + prompt,
  202. mode))
  203. def test_po_pn_interface_verify_OK(self):
  204. self.pn_interface_verify_OK("PO")
  205. def test_co_pn_interface_verify_OK(self):
  206. self.pn_interface_verify_OK("CO")
  207. def pn_interface_verify_OK(self, mode):
  208. self.assertTrue(run_file(all_files,
  209. do_instantiate_simple + ["verify"],
  210. did_instantiate_simple + ["OK"], mode))
  211. def test_po_pn_interface_verify_fail_tokens(self):
  212. self.pn_interface_verify_fail_tokens("PO")
  213. def test_co_pn_interface_verify_fail_tokens(self):
  214. self.pn_interface_verify_fail_tokens("CO")
  215. def pn_interface_verify_fail_tokens(self, mode):
  216. self.assertTrue(run_file(all_files,
  217. do_instantiate_simple + ["modify", "p1", "tokens", -5, "verify"],
  218. did_instantiate_simple + modify + prompt + verify_fail_tokens + prompt, mode))
  219. def test_po_pn_interface_verify_fail_weight(self):
  220. self.pn_interface_verify_fail_weight("PO")
  221. def test_co_pn_interface_verify_fail_weight(self):
  222. self.pn_interface_verify_fail_weight("CO")
  223. def pn_interface_verify_fail_weight(self, mode):
  224. self.assertTrue(run_file(all_files,
  225. do_instantiate_simple + ["modify", "p2t", "weight", -2, "verify"],
  226. did_instantiate_simple + modify + prompt + verify_fail_weight + prompt, mode))
  227. def test_po_pn_interface_verify_fail_structure(self):
  228. self.pn_interface_verify_fail_structure("PO")
  229. def test_co_pn_interface_verify_fail_structure(self):
  230. self.pn_interface_verify_fail_structure("CO")
  231. def pn_interface_verify_fail_structure(self, mode):
  232. self.assertTrue(run_file(all_files,
  233. ["new", "PetriNets", "abc",
  234. "instantiate", "Transition", "t1",
  235. "instantiate", "Place", "p1", "attr_add", "p1", "tokens", 5,
  236. "instantiate", "P2T", "p2t", "t1", "p1", "attr_add", "p2t", "weight", 2, "verify"],
  237. init + new + loaded + \
  238. instantiate_node + prompt + \
  239. instantiate_node + prompt + attr_add + prompt + \
  240. instantiate_edge + prompt + attr_add + prompt + \
  241. verify_fail_structure,
  242. mode))
  243. def test_po_pn_interface_types(self):
  244. self.pn_interface_types("PO")
  245. def test_co_pn_interface_types(self):
  246. self.pn_interface_types("CO")
  247. def pn_interface_types(self, mode):
  248. self.assertTrue(run_file(all_files,
  249. ["new", "PetriNets", "abc", "types"],
  250. init + new + loaded + list_types([("Place", "Class"),
  251. ("Transition", "Class"),
  252. ("P2T", "Association"),
  253. ("T2P", "Association"),
  254. ("Natural", "Class"),
  255. ("Place_tokens", "Association"),
  256. ("Place_tokens.name", "String"),
  257. ("Place_tokens.target_lower_cardinality", "Natural"),
  258. ("Place_tokens.target_upper_cardinality", "Natural"),
  259. ("P2T_weight", "Association"),
  260. ("P2T_weight.name", "String"),
  261. ("P2T_weight.target_lower_cardinality", "Natural"),
  262. ("P2T_weight.target_upper_cardinality", "Natural"),
  263. ("T2P_weight", "Association"),
  264. ("T2P_weight.name", "String"),
  265. ("T2P_weight.target_lower_cardinality", "Natural"),
  266. ("T2P_weight.target_upper_cardinality", "Natural"),
  267. ]) + prompt,
  268. mode))
  269. def test_po_pn_interface_modify_place(self):
  270. self.pn_interface_modify_place("PO")
  271. def test_co_pn_interface_modify_place(self):
  272. self.pn_interface_modify_place("CO")
  273. def pn_interface_modify_place(self, mode):
  274. self.assertTrue(run_file(all_files,
  275. ["new", "PetriNets", "abc",
  276. "instantiate", "Place", "p1", "attr_add", "p1", "tokens", 5,
  277. "read", "p1",
  278. "modify", "p1", "tokens", 1, "read", "p1"],
  279. init + new + loaded + \
  280. instantiate_node + prompt + attr_add + prompt + \
  281. read_node("p1", "Place", [], [("tokens", "Natural", 5)]) + prompt + \
  282. modify + prompt + \
  283. read_node("p1", "Place", [], [("tokens", "Natural", 1)]) + prompt,
  284. mode))
  285. def test_po_pn_interface_verify_fail_attr_lower_cardinality(self):
  286. self.pn_interface_verify_fail_attr_lower_cardinality("PO")
  287. def test_co_pn_interface_verify_fail_attr_lower_cardinality(self):
  288. self.pn_interface_verify_fail_attr_lower_cardinality("CO")
  289. def pn_interface_verify_fail_attr_lower_cardinality(self, mode):
  290. self.assertTrue(run_file(all_files,
  291. do_instantiate_simple + ["instantiate", "Place", "p999", "verify"],
  292. did_instantiate_simple + instantiate_node + prompt + ["Lower cardinality violation for outgoing edge of type Place_tokens at p999"] + prompt,
  293. mode))
  294. def test_po_pn_interface_verify_fail_attr_upper_cardinality(self):
  295. self.pn_interface_verify_fail_attr_upper_cardinality("PO")
  296. def test_co_pn_interface_verify_fail_attr_upper_cardinality(self):
  297. self.pn_interface_verify_fail_attr_upper_cardinality("CO")
  298. def pn_interface_verify_fail_attr_upper_cardinality(self, mode):
  299. self.assertTrue(run_file(all_files,
  300. do_instantiate_simple + ["attr_add", "p1", "tokens", 5, "verify"],
  301. did_instantiate_simple + attr_add + prompt + ["Upper cardinality violation for outgoing edge of type Place_tokens at p1"] + prompt,
  302. mode))
  303. def test_po_pn_interface_verify_natural(self):
  304. self.pn_interface_verify_natural("PO")
  305. def test_co_pn_interface_verify_natural(self):
  306. self.pn_interface_verify_natural("CO")
  307. def pn_interface_verify_natural(self, mode):
  308. self.assertTrue(run_file(all_files,
  309. ["new", "PetriNets", "abc",
  310. "instantiate", "Place", "p1",
  311. "attr_add", "p1", "tokens", -5,
  312. "attr_del", "p1", "tokens",
  313. "attr_add", "p1", "tokens", 4,
  314. "verify"],
  315. init + new + loaded + \
  316. instantiate_node + prompt + \
  317. attr_add + prompt + \
  318. attr_del + prompt + \
  319. attr_add + prompt + \
  320. ["OK"] + prompt,
  321. mode))
  322. def test_po_pn_interface_verify_PN_OK(self):
  323. self.pn_interface_verify_PN_OK("PO")
  324. def test_co_pn_interface_verify_PN_OK(self):
  325. self.pn_interface_verify_PN_OK("CO")
  326. def pn_interface_verify_PN_OK(self, mode):
  327. self.assertTrue(run_file(all_files,
  328. ["load", "PetriNets", "verify"],
  329. init + load + loaded + ["OK"], mode))
  330. def test_po_rpgame(self):
  331. self.rpgame("PO")
  332. def test_co_rpgame(self):
  333. self.rpgame("CO")
  334. def rpgame(self, mode):
  335. constraint_code = \
  336. """
  337. include "primitives.alh"
  338. include "object_operations.alh"
  339. Element function constraint(model : Element, name : String):
  340. \tElement associations
  341. \tElement back_associations
  342. \tElement association
  343. \tString destination
  344. \tassociations = allOutgoingAssociationInstances(model, name, "tile_left")
  345. \twhile (0 < list_len(associations)):
  346. \t\tassociation = set_pop(associations)
  347. \t\tdestination = readAssociationDestination(model, association)
  348. \t\tback_associations = allOutgoingAssociationInstances(model, destination, "tile_right")
  349. \t\tif (list_len(back_associations) < 1):
  350. \t\t\treturn "Left link does not have a right link back"!
  351. \t\telse:
  352. \t\t\tassociation = set_pop(back_associations)
  353. \t\t\tdestination = readAssociationDestination(model, association)
  354. \t\t\tif (destination != name):
  355. \t\t\t\treturn "Right link does not have a left link back to the same tile"!
  356. \tassociations = allOutgoingAssociationInstances(model, name, "tile_right")
  357. \twhile (0 < list_len(associations)):
  358. \t\tassociation = set_pop(associations)
  359. \t\tdestination = readAssociationDestination(model, association)
  360. \t\tback_associations = allOutgoingAssociationInstances(model, destination, "tile_left")
  361. \t\tif (list_len(back_associations) < 1):
  362. \t\t\treturn "Right link does not have a left link back"!
  363. \t\telse:
  364. \t\t\tassociation = set_pop(back_associations)
  365. \t\t\tdestination = readAssociationDestination(model, association)
  366. \t\t\tif (destination != name):
  367. \t\t\t\treturn "Right link does not have a left link back to the same tile"!
  368. \tassociations = allOutgoingAssociationInstances(model, name, "tile_top")
  369. \twhile (0 < list_len(associations)):
  370. \t\tassociation = set_pop(associations)
  371. \t\tdestination = readAssociationDestination(model, association)
  372. \t\tback_associations = allOutgoingAssociationInstances(model, destination, "tile_bottom")
  373. \t\tif (list_len(back_associations) < 1):
  374. \t\t\treturn "Top link does not have a bottom link back"!
  375. \t\telse:
  376. \t\t\tassociation = set_pop(back_associations)
  377. \t\t\tdestination = readAssociationDestination(model, association)
  378. \t\t\tif (destination != name):
  379. \t\t\t\treturn "Top link does not have a bottom link back to the same tile"!
  380. \tassociations = allOutgoingAssociationInstances(model, name, "tile_bottom")
  381. \twhile (0 < list_len(associations)):
  382. \t\tassociation = set_pop(associations)
  383. \t\tdestination = readAssociationDestination(model, association)
  384. \t\tback_associations = allOutgoingAssociationInstances(model, destination, "tile_top")
  385. \t\tif (list_len(back_associations) < 1):
  386. \t\t\treturn "Bottom link does not have a top link back"!
  387. \t\telse:
  388. \t\t\tassociation = set_pop(back_associations)
  389. \t\t\tdestination = readAssociationDestination(model, association)
  390. \t\t\tif (destination != name):
  391. \t\t\t\treturn "Bottom link does not have a top link back to the same tile"!
  392. \treturn "OK"!
  393. """
  394. constructors = get_constructor(constraint_code)
  395. self.assertTrue(run_file(all_files,
  396. ["new", "SimpleClassDiagrams", "RPGame",
  397. "set_inheritance", "Inheritance",
  398. "instantiate", "Class", "Scene",
  399. "instantiate", "Class", "Tile",
  400. "instantiate", "Class", "Item",
  401. "instantiate", "Class", "Goal",
  402. "instantiate", "Class", "Character",
  403. "instantiate", "Class", "Hero",
  404. "instantiate", "Association", "scene_has_tiles", "Scene", "Tile",
  405. "instantiate", "Association", "tile_left", "Tile", "Tile",
  406. "instantiate", "Association", "tile_right", "Tile", "Tile",
  407. "instantiate", "Association", "tile_top", "Tile", "Tile",
  408. "instantiate", "Association", "tile_bottom", "Tile", "Tile",
  409. "instantiate", "Association", "character_on", "Character", "Tile",
  410. "instantiate", "Association", "item_on", "Item", "Tile",
  411. "instantiate", "Inheritance", "hero_is_character", "Hero", "Character",
  412. "instantiate", "Inheritance", "goal_is_item", "Goal", "Item",
  413. "attr_add", "Scene", "lower_cardinality", 1,
  414. "attr_add", "Scene", "upper_cardinality", 1,
  415. "attr_add", "Goal", "lower_cardinality", 1,
  416. "attr_add", "scene_has_tiles", "source_lower_cardinality", 1,
  417. "attr_add", "scene_has_tiles", "source_upper_cardinality", 1,
  418. "attr_add", "scene_has_tiles", "target_lower_cardinality", 1,
  419. "attr_add", "item_on", "target_lower_cardinality", 1,
  420. "attr_add", "item_on", "target_upper_cardinality", 1,
  421. "attr_add", "item_on", "source_upper_cardinality", 1,
  422. "attr_add", "character_on", "target_lower_cardinality", 1,
  423. "attr_add", "character_on", "target_upper_cardinality", 1,
  424. "attr_add", "character_on", "source_upper_cardinality", 1,
  425. "attr_add", "tile_left", "source_upper_cardinality", 1,
  426. "attr_add", "tile_left", "target_upper_cardinality", 1,
  427. "attr_add", "tile_right", "source_upper_cardinality", 1,
  428. "attr_add", "tile_right", "target_upper_cardinality", 1,
  429. "attr_add", "tile_top", "source_upper_cardinality", 1,
  430. "attr_add", "tile_top", "target_upper_cardinality", 1,
  431. "attr_add", "tile_bottom", "source_upper_cardinality", 1,
  432. "attr_add", "tile_bottom", "target_upper_cardinality", 1,
  433. "attr_add_code", "Tile", "constraint",
  434. ] + constructors + ["verify"] + ["exit"] + [
  435. "new", "RPGame", "my_game",
  436. "instantiate", "Scene", "scene",
  437. "instantiate", "Hero", "Link",
  438. "instantiate", "Goal", "goal",
  439. "instantiate", "Tile", "tile_00",
  440. "instantiate", "Tile", "tile_01",
  441. "instantiate", "Tile", "tile_10",
  442. "instantiate", "Tile", "tile_11",
  443. "instantiate", "scene_has_tiles", "", "scene", "tile_00",
  444. "instantiate", "scene_has_tiles", "", "scene", "tile_01",
  445. "instantiate", "scene_has_tiles", "", "scene", "tile_10",
  446. "instantiate", "scene_has_tiles", "", "scene", "tile_11",
  447. "instantiate", "character_on", "", "Link", "tile_00",
  448. "instantiate", "item_on", "", "goal", "tile_11",
  449. "instantiate", "tile_left", "", "tile_01", "tile_00",
  450. "instantiate", "tile_left", "", "tile_11", "tile_10",
  451. "instantiate", "tile_right", "", "tile_00", "tile_01",
  452. "instantiate", "tile_right", "", "tile_10", "tile_11",
  453. "instantiate", "tile_top", "", "tile_10", "tile_00",
  454. "instantiate", "tile_top", "", "tile_11", "tile_01",
  455. "instantiate", "tile_bottom", "", "tile_00", "tile_10",
  456. "instantiate", "tile_bottom", "", "tile_01", "tile_11",
  457. "verify",
  458. ],
  459. init + new + loaded + \
  460. set_inheritance + prompt + \
  461. (instantiate_node + prompt) * 6 + \
  462. (instantiate_edge + prompt) * 9 + \
  463. (attr_add + prompt) * 20 + \
  464. ["Element to constrain (empty for global)?",
  465. "Give input to function constructors for LOCAL constraint!",
  466. "Added constraint to model!"] + \
  467. prompt + \
  468. ["OK"] + \
  469. prompt + prompt + new + loaded + \
  470. (instantiate_node + prompt) * 7 + \
  471. (instantiate_edge + prompt) * 14 + \
  472. ["OK"],
  473. mode))
  474. def test_po_pn_interface_transform_pn(self):
  475. code_mark_constraint = \
  476. """
  477. include "primitives.alh"
  478. include "modelling.alh"
  479. include "object_operations.alh"
  480. Boolean function constraint(host_model : Element, name : String):
  481. \t// Make sure that all places have enough tokens
  482. \tElement links
  483. \tString link
  484. \tString place
  485. \tlinks = allIncomingAssociationInstances(host_model, name, "P2T")
  486. \twhile (read_nr_out(links) > 0):
  487. \t\tlink = set_pop(links)
  488. \t\tplace = readAssociationSource(host_model, link)
  489. \t\tlog("Check link " + link)
  490. \t\tlog("Check place " + place)
  491. \t\tif (integer_lt(read_attribute(host_model, place, "tokens"), read_attribute(host_model, link, "weight"))):
  492. \t\t\t// Not enough tokens for this weight
  493. \t\t\treturn False!
  494. \treturn True!
  495. """
  496. code_mark_action = \
  497. """
  498. include "primitives.alh"
  499. include "modelling.alh"
  500. Void function action(host_model : Element, name : String, mapping : Element):
  501. \tunset_attribute(host_model, name, "executing")
  502. \tinstantiate_attribute(host_model, name, "executing", True)
  503. \treturn!
  504. """
  505. code_consume_transition_constraint = \
  506. """
  507. include "primitives.alh"
  508. include "modelling.alh"
  509. Boolean function constraint(host_model : Element, name : String):
  510. \t// Check if this node is executing currently
  511. \treturn value_eq(read_attribute(host_model, name, "executing"), True)!
  512. """
  513. code_consume_place_constraint = \
  514. """
  515. include "primitives.alh"
  516. include "modelling.alh"
  517. Boolean function constraint(host_model : Element, name : String):
  518. \t// Check if this node is executing currently
  519. \treturn value_eq(read_attribute(host_model, name, "executed"), False)!
  520. """
  521. code_consume_place_action = \
  522. """
  523. include "primitives.alh"
  524. include "modelling.alh"
  525. Void function action(host_model : Element, name : String, mapping : Element):
  526. \tInteger tokens
  527. \tInteger weight
  528. \ttokens = read_attribute(host_model, name, "tokens")
  529. \tweight = read_attribute(host_model, mapping["2"], "weight")
  530. \tunset_attribute(host_model, name, "tokens")
  531. \tinstantiate_attribute(host_model, name, "tokens", tokens - weight)
  532. \tunset_attribute(host_model, name, "executed")
  533. \tinstantiate_attribute(host_model, name, "executed", True)
  534. \treturn!
  535. """
  536. code_produce_place_action = \
  537. """
  538. include "primitives.alh"
  539. include "modelling.alh"
  540. Void function action(host_model : Element, name : String, mapping : Element):
  541. \tInteger tokens
  542. \tInteger weight
  543. \ttokens = read_attribute(host_model, name, "tokens")
  544. \tweight = read_attribute(host_model, mapping["2"], "weight")
  545. \tunset_attribute(host_model, name, "tokens")
  546. \tinstantiate_attribute(host_model, name, "tokens", tokens + weight)
  547. \tunset_attribute(host_model, name, "executed")
  548. \tinstantiate_attribute(host_model, name, "executed", True)
  549. \treturn!
  550. """
  551. code_unmark_transition_constraint = \
  552. """
  553. include "primitives.alh"
  554. include "modelling.alh"
  555. Boolean function constraint(host_model : Element, name : String):
  556. \t// Check if this node is executing currently
  557. \treturn value_eq(read_attribute(host_model, name, "executing"), True)!
  558. """
  559. code_unmark_place_constraint = \
  560. """
  561. include "primitives.alh"
  562. include "modelling.alh"
  563. Boolean function constraint(host_model : Element, name : String):
  564. \t// Check if this node is executing currently
  565. \treturn value_eq(read_attribute(host_model, name, "executed"), True)!
  566. """
  567. code_unmark_transition_action = \
  568. """
  569. include "primitives.alh"
  570. include "modelling.alh"
  571. Void function action(host_model : Element, name : String, mapping : Element):
  572. \tunset_attribute(host_model, name, "executing")
  573. \tinstantiate_attribute(host_model, name, "executing", False)
  574. \treturn!
  575. """
  576. code_unmark_place_action = \
  577. """
  578. include "primitives.alh"
  579. include "modelling.alh"
  580. Void function action(host_model : Element, name : String, mapping : Element):
  581. \tunset_attribute(host_model, name, "executed")
  582. \tinstantiate_attribute(host_model, name, "executed", False)
  583. \treturn!
  584. """
  585. constructor_mark_constraint = get_constructor(code_mark_constraint)
  586. constructor_mark_action = get_constructor(code_mark_action)
  587. constructor_consume_transition_constraint = get_constructor(code_consume_transition_constraint)
  588. constructor_consume_place_constraint = get_constructor(code_consume_place_constraint)
  589. constructor_consume_place_action = get_constructor(code_consume_place_action)
  590. constructor_produce_transition_constraint = constructor_consume_transition_constraint
  591. constructor_produce_place_constraint = constructor_consume_place_constraint
  592. constructor_produce_place_action = get_constructor(code_produce_place_action)
  593. constructor_unmark_transition_constraint = constructor_consume_transition_constraint
  594. constructor_unmark_transition_action = get_constructor(code_unmark_transition_action)
  595. constructor_unmark_place_constraint = get_constructor(code_unmark_place_constraint)
  596. constructor_unmark_place_action = get_constructor(code_unmark_place_action)
  597. self.assertTrue(run_file(all_files,
  598. ["new", "SimpleClassDiagrams", "PetriNets_runtime",
  599. "set_inheritance", "Inheritance",
  600. "instantiate", "Class", "Natural",
  601. "instantiate", "Class", "Boolean",
  602. "instantiate", "Class", "Place",
  603. "attr_def", "Place", "tokens", "Natural",
  604. "attr_def", "Place", "executed", "Boolean",
  605. "instantiate", "Class", "Transition",
  606. "attr_def", "Transition", "executing", "Boolean",
  607. "instantiate", "Association", "P2T", "Place", "Transition",
  608. "attr_def", "P2T", "weight", "Natural",
  609. "instantiate", "Association", "T2P", "Transition", "Place",
  610. "attr_def", "T2P", "weight", "Natural",
  611. "exit",
  612. "new", "PetriNets_runtime", "pn",
  613. "instantiate", "Place", "p1",
  614. "attr_add", "p1", "tokens", 1,
  615. "attr_add", "p1", "executed", False,
  616. "instantiate", "Place", "p2",
  617. "attr_add", "p2", "tokens", 2,
  618. "attr_add", "p2", "executed", False,
  619. "instantiate", "Transition", "t1",
  620. "attr_add", "t1", "executing", False,
  621. "instantiate", "P2T", "p2t", "p1", "t1",
  622. "attr_add", "p2t", "weight", 1,
  623. "instantiate", "P2T", "p2t2", "p2", "t1",
  624. "attr_add", "p2t2", "weight", 1,
  625. "exit",
  626. "ramify", "PetriNets_runtime",
  627. "new", "PetriNets_runtime_SCHEDULE", "pn_simulate",
  628. # Rule 1: mark transition to execute
  629. # LHS
  630. "instantiate", "LHS", "lhs_mark",
  631. "instantiate", "Pre_Transition", "pre_mark_t",
  632. "attr_add", "pre_mark_t", "label", "0",
  633. "attr_add_code", "pre_mark_t", "constraint",
  634. ] + constructor_mark_constraint + [
  635. "instantiate", "LHS_contains", "", "lhs_mark", "pre_mark_t",
  636. # RHS
  637. "instantiate", "RHS", "rhs_mark",
  638. "instantiate", "Post_Transition", "post_mark_t",
  639. "attr_add", "post_mark_t", "label", "0",
  640. "attr_add_code", "post_mark_t", "action",
  641. ] + constructor_mark_action + [
  642. "instantiate", "RHS_contains", "", "rhs_mark", "post_mark_t",
  643. # Atomic of rule 1 "mark"
  644. "instantiate", "Atomic", "mark",
  645. "instantiate", "AtomicLHS", "", "mark", "lhs_mark",
  646. "instantiate", "AtomicRHS", "", "mark", "rhs_mark",
  647. # Rule 2: consume tokens going into marked transition
  648. # LHS
  649. "instantiate", "LHS", "lhs_consume",
  650. "instantiate", "Pre_Transition", "pre_consume_t",
  651. "attr_add", "pre_consume_t", "label", "0",
  652. "attr_add_code", "pre_consume_t", "constraint",
  653. ] + constructor_consume_transition_constraint + [
  654. "instantiate", "Pre_Place", "pre_consume_p",
  655. "attr_add", "pre_consume_p", "label", "1",
  656. "attr_add_code", "pre_consume_p", "constraint",
  657. ] + constructor_consume_place_constraint + [
  658. "instantiate", "Pre_P2T", "pre_consume_p2t", "pre_consume_p", "pre_consume_t",
  659. "attr_add", "pre_consume_p2t", "label", "2",
  660. "instantiate", "LHS_contains", "", "lhs_consume", "pre_consume_t",
  661. "instantiate", "LHS_contains", "", "lhs_consume", "pre_consume_p",
  662. "instantiate", "LHS_contains", "", "lhs_consume", "pre_consume_p2t",
  663. # RHS
  664. "instantiate", "RHS", "rhs_consume",
  665. "instantiate", "Post_Transition", "post_consume_t",
  666. "attr_add", "post_consume_t", "label", "0",
  667. "instantiate", "Post_Place", "post_consume_p",
  668. "attr_add", "post_consume_p", "label", "1",
  669. "attr_add_code", "post_consume_p", "action",
  670. ] + constructor_consume_place_action + [
  671. "instantiate", "Post_P2T", "post_consume_p2t", "post_consume_p", "post_consume_t",
  672. "attr_add", "post_consume_p2t", "label", "2",
  673. "instantiate", "RHS_contains", "", "rhs_consume", "post_consume_t",
  674. "instantiate", "RHS_contains", "", "rhs_consume", "post_consume_p",
  675. "instantiate", "RHS_contains", "", "rhs_consume", "post_consume_p2t",
  676. # Atomic of rule 2 "consume"
  677. "instantiate", "Atomic", "consume",
  678. "instantiate", "AtomicLHS", "", "consume", "lhs_consume",
  679. "instantiate", "AtomicRHS", "", "consume", "rhs_consume",
  680. # Rule 3: produce tokens going out of marked transition
  681. # LHS
  682. "instantiate", "LHS", "lhs_produce",
  683. "instantiate", "Pre_Transition", "pre_produce_t",
  684. "attr_add", "pre_produce_t", "label", "0",
  685. "attr_add_code", "pre_produce_t", "constraint",
  686. ] + constructor_produce_transition_constraint + [
  687. "instantiate", "Pre_Place", "pre_produce_p",
  688. "attr_add", "pre_produce_p", "label", "1",
  689. "attr_add_code", "pre_produce_p", "constraint",
  690. ] + constructor_produce_place_constraint + [
  691. "instantiate", "Pre_T2P", "pre_produce_t2p", "pre_produce_t", "pre_produce_p",
  692. "attr_add", "pre_produce_t2p", "label", "2",
  693. "instantiate", "LHS_contains", "", "lhs_produce", "pre_produce_t",
  694. "instantiate", "LHS_contains", "", "lhs_produce", "pre_produce_p",
  695. "instantiate", "LHS_contains", "", "lhs_produce", "pre_produce_t2p",
  696. # RHS
  697. "instantiate", "RHS", "rhs_produce",
  698. "instantiate", "Post_Transition", "post_produce_t",
  699. "attr_add", "post_produce_t", "label", "0",
  700. "instantiate", "Post_Place", "post_produce_p",
  701. "attr_add", "post_produce_p", "label", "1",
  702. "attr_add_code", "post_produce_p", "action",
  703. ] + constructor_produce_place_action + [
  704. "instantiate", "Post_T2P", "post_produce_t2p", "post_produce_t", "post_produce_p",
  705. "attr_add", "post_produce_t2p", "label", "2",
  706. "instantiate", "RHS_contains", "", "rhs_produce", "post_produce_t",
  707. "instantiate", "RHS_contains", "", "rhs_produce", "post_produce_p",
  708. "instantiate", "RHS_contains", "", "rhs_produce", "post_produce_t2p",
  709. # Atomic of rule 3 "produce"
  710. "instantiate", "Atomic", "produce",
  711. "instantiate", "AtomicLHS", "", "produce", "lhs_produce",
  712. "instantiate", "AtomicRHS", "", "produce", "rhs_produce",
  713. # Rule 4: unmark marked transition
  714. # LHS
  715. "instantiate", "LHS", "lhs_unmark_transition",
  716. "instantiate", "Pre_Transition", "pre_unmark_t",
  717. "attr_add", "pre_unmark_t", "label", "0",
  718. "attr_add_code", "pre_unmark_t", "constraint",
  719. ] + constructor_unmark_transition_constraint + [
  720. "instantiate", "LHS_contains", "", "lhs_unmark_transition", "pre_unmark_t",
  721. # RHS
  722. "instantiate", "RHS", "rhs_unmark_transition",
  723. "instantiate", "Post_Transition", "post_unmark_t",
  724. "attr_add", "post_unmark_t", "label", "0",
  725. "attr_add_code", "post_unmark_t", "action",
  726. ] + constructor_unmark_transition_action + [
  727. "instantiate", "RHS_contains", "", "rhs_unmark_transition", "post_unmark_t",
  728. # Atomic of rule 4 "unmark transition"
  729. "instantiate", "Atomic", "unmark_transition",
  730. "instantiate", "AtomicLHS", "", "unmark_transition", "lhs_unmark_transition",
  731. "instantiate", "AtomicRHS", "", "unmark_transition", "rhs_unmark_transition",
  732. # Rule 5: unmark marked places
  733. # LHS
  734. "instantiate", "LHS", "lhs_unmark_place",
  735. "instantiate", "Pre_Place", "pre_unmark_p",
  736. "attr_add", "pre_unmark_p", "label", "0",
  737. "attr_add_code", "pre_unmark_p", "constraint",
  738. ] + constructor_unmark_place_constraint + [
  739. "instantiate", "LHS_contains", "", "lhs_unmark_place", "pre_unmark_p",
  740. # RHS
  741. "instantiate", "RHS", "rhs_unmark_place",
  742. "instantiate", "Post_Place", "post_unmark_p",
  743. "attr_add", "post_unmark_p", "label", "0",
  744. "attr_add_code", "post_unmark_p", "action",
  745. ] + constructor_unmark_place_action + [
  746. "instantiate", "RHS_contains", "", "rhs_unmark_place", "post_unmark_p",
  747. # Atomic of rule 5 "unmark place"
  748. "instantiate", "Atomic", "unmark_place",
  749. "instantiate", "AtomicLHS", "", "unmark_place", "lhs_unmark_place",
  750. "instantiate", "AtomicRHS", "", "unmark_place", "rhs_unmark_place",
  751. # Compose schedule
  752. "instantiate", "Composite", "main",
  753. "instantiate", "Success", "success",
  754. "instantiate", "Failure", "failure",
  755. # Add all rules
  756. "instantiate", "Contains", "", "main", "success",
  757. "instantiate", "Contains", "", "main", "failure",
  758. "instantiate", "Contains", "", "main", "mark",
  759. "instantiate", "Contains", "", "main", "consume",
  760. "instantiate", "Contains", "", "main", "produce",
  761. "instantiate", "Contains", "", "main", "unmark_transition",
  762. "instantiate", "Contains", "", "main", "unmark_place",
  763. # Scheduling order
  764. "instantiate", "OnSuccess", "", "mark", "consume",
  765. "instantiate", "OnFailure", "", "mark", "success",
  766. "instantiate", "OnSuccess", "", "consume", "consume",
  767. "instantiate", "OnFailure", "", "consume", "produce",
  768. "instantiate", "OnSuccess", "", "produce", "produce",
  769. "instantiate", "OnFailure", "", "produce", "unmark_transition",
  770. "instantiate", "OnSuccess", "", "unmark_transition", "unmark_place",
  771. "instantiate", "OnFailure", "", "unmark_transition", "failure",
  772. "instantiate", "OnSuccess", "", "unmark_place", "unmark_place",
  773. "instantiate", "OnFailure", "", "unmark_place", "success",
  774. # Composite initial
  775. "instantiate", "Initial", "", "main", "mark",
  776. "exit",
  777. "transform", "pn", "pn_simulate",
  778. "load", "pn",
  779. "list",
  780. "verify",
  781. "read", "t1",
  782. "read", "p1",
  783. "read", "p2",
  784. "exit",
  785. ],
  786. None, "PO"))