test_pn_interface.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. constraint_mark_code = \
  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. action_mark_code = \
  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. constraint_consume_code = \
  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. action_consume_code = \
  514. """
  515. include "primitives.alh"
  516. include "modelling.alh"
  517. Void function action(host_model : Element, name : String, mapping : Element):
  518. \tInteger tokens
  519. \tInteger weight
  520. \ttokens = read_attribute(host_model, name, "tokens")
  521. \tweight = read_attribute(host_model, mapping["2"], "weight")
  522. \tunset_attribute(host_model, name, "tokens")
  523. \tinstantiate_attribute(host_model, name, "tokens", tokens - weight)
  524. \treturn!
  525. """
  526. constructor_mark_action = get_constructor(action_mark_code)
  527. constructor_mark_constraint = get_constructor(constraint_mark_code)
  528. constructor_consume_constraint = get_constructor(constraint_consume_code)
  529. constructor_consume_action = get_constructor(action_consume_code)
  530. self.assertTrue(run_file(all_files,
  531. ["new", "SimpleClassDiagrams", "PetriNets_runtime",
  532. "set_inheritance", "Inheritance",
  533. "instantiate", "Class", "Natural",
  534. "instantiate", "Class", "Boolean",
  535. "instantiate", "Class", "Place",
  536. "attr_def", "Place", "tokens", "Natural",
  537. "attr_def", "Place", "executed", "Boolean",
  538. "instantiate", "Class", "Transition",
  539. "attr_def", "Transition", "executing", "Boolean",
  540. "instantiate", "Association", "P2T", "Place", "Transition",
  541. "attr_def", "P2T", "weight", "Natural",
  542. "instantiate", "Association", "T2P", "Transition", "Place",
  543. "attr_def", "T2P", "weight", "Natural",
  544. "exit",
  545. "new", "PetriNets_runtime", "pn",
  546. "instantiate", "Place", "p1",
  547. "attr_add", "p1", "tokens", 1,
  548. "attr_add", "p1", "executed", False,
  549. "instantiate", "Place", "p2",
  550. "attr_add", "p2", "tokens", 2,
  551. "attr_add", "p2", "executed", False,
  552. "instantiate", "Transition", "t1",
  553. "attr_add", "t1", "executing", False,
  554. "instantiate", "P2T", "p2t", "p1", "t1",
  555. "attr_add", "p2t", "weight", 1,
  556. "instantiate", "P2T", "p2t2", "p2", "t1",
  557. "attr_add", "p2t2", "weight", 1,
  558. "exit",
  559. "ramify", "PetriNets_runtime",
  560. "new", "PetriNets_runtime_SCHEDULE", "pn_simulate",
  561. # Rule 1: mark transition to execute
  562. "instantiate", "LHS", "lhs_mark",
  563. "instantiate", "Pre_Transition", "pre_mark_t",
  564. "attr_add", "pre_mark_t", "label", "0",
  565. "attr_add_code", "pre_mark_t", "constraint",
  566. ] + constructor_mark_constraint + [
  567. "instantiate", "LHS_contains", "", "lhs_mark", "pre_mark_t",
  568. "instantiate", "RHS", "rhs_mark",
  569. "instantiate", "Post_Transition", "post_mark_t",
  570. "attr_add", "post_mark_t", "label", "0",
  571. "attr_add_code", "post_mark_t", "action",
  572. ] + constructor_mark_action + [
  573. "instantiate", "RHS_contains", "", "rhs_mark", "post_mark_t",
  574. "instantiate", "Composite", "main",
  575. "instantiate", "Atomic", "mark",
  576. "instantiate", "AtomicLHS", "", "mark", "lhs_mark",
  577. "instantiate", "AtomicRHS", "", "mark", "rhs_mark",
  578. "instantiate", "Success", "success",
  579. "instantiate", "Failure", "failure",
  580. "instantiate", "Contains", "", "main", "mark",
  581. "instantiate", "Contains", "", "main", "success",
  582. "instantiate", "Contains", "", "main", "failure",
  583. "instantiate", "OnSuccess", "", "mark", "success",
  584. "instantiate", "OnFailure", "", "mark", "failure",
  585. "instantiate", "Initial", "", "main", "mark",
  586. # Rule 2: consume tokens going into marked transition
  587. "exit",
  588. "transform", "pn", "pn_simulate",
  589. "load", "pn",
  590. "list",
  591. "verify",
  592. "read",
  593. "t1",
  594. "exit",
  595. ],
  596. None, "PO"))