test_pn_interface.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. import unittest
  2. from utils import run_file, get_constructor
  3. do_instantiate_simple = [
  4. '"new"', '"PetriNets"', '"abc"',
  5. '"instantiate"', '"Transition"', '"t1"',
  6. '"instantiate"', '"Place"', '"p1"', '"attr_add"', '"p1"', '"tokens"', '5',
  7. '"instantiate"', '"Place"', '"p2"', '"attr_add"', '"p2"', '"tokens"', '0',
  8. '"instantiate"', '"P2T"', '"p2t"', '"p1"', '"t1"', '"attr_add"', '"p2t"', '"weight"', '2',
  9. '"instantiate"', '"T2P"', '"t2p"', '"t1"', '"p2"', '"attr_add"', '"t2p"', '"weight"', '1']
  10. instantiate_node = ["Type to instantiate?",
  11. "Name of new element?",
  12. "Instantiation successful!"]
  13. instantiate_edge = ["Type to instantiate?",
  14. "Name of new element?",
  15. "Source name?",
  16. "Destination name?",
  17. "Instantiation successful!"]
  18. all_files = [ "pn_interface.alc",
  19. "primitives.alc",
  20. "object_operations.alc",
  21. "conformance_scd.alc",
  22. "library.alc",
  23. "metamodels.alc",
  24. "constructors.alc",
  25. "modelling.alc",
  26. ]
  27. greeting = ["Welcome to the Model Management Interface, running live on the Modelverse!",
  28. "Use 'help' command for a list of possible commands"]
  29. new = ["Metamodel to instantiate?",
  30. "Name of model?"]
  31. prompt = ["Please give your command."]
  32. loaded = ["Model loaded, ready for commands!",
  33. "Use 'help' command for a list of possible commands"] + prompt
  34. load = ["Model to load?"]
  35. instantiate = ["Type to instantiate?"]
  36. instantiate_name = ["Name of new element?"]
  37. instantiate_ok = ["Instantiation successful!"]
  38. instantiate_source= ["Source name?"]
  39. instantiate_destination = ["Destination name?"]
  40. def list_menu(defined):
  41. defined.append(("PetriNets", "SimpleClassDiagrams"))
  42. defined.append(("SimpleClassDiagrams", "SimpleClassDiagrams"))
  43. defined.append(("LTM_bottom", "LTM_bottom"))
  44. return ["Found models:",
  45. set([" %s : %s" % (m, mm) for m, mm in defined])]
  46. def list_model(defined):
  47. return ["List of all elements:",
  48. set([" %s : %s" % (m, mm) for m, mm in defined])]
  49. def read_node(name, t, defs, attrs):
  50. return ["Element to read?",
  51. "Name: %s" % name,
  52. "Type: %s" % t,
  53. "Defines attributes:"] + \
  54. ([set([" %s : %s" % (m, mm) for m, mm in defs])] if defs else []) + \
  55. ["Attributes:"] + \
  56. ([set([' "%s" : "%s" = %s' % (m, mm, v) for m, mm, v in attrs])] if attrs else [])
  57. def read_edge(name, t, src, dst, defs, attrs):
  58. return ["Element to read?",
  59. "Name: %s" % name,
  60. "Type: %s" % t,
  61. "Source: %s" % src,
  62. "Destination: %s" % dst,
  63. "Defines attributes:"] + \
  64. ([set([" %s : %s" % (m, mm) for m, mm in defs])] if defs else []) + \
  65. ["Attributes:"] + \
  66. ([set([' "%s" : "%s" = %s' % (m, mm, v) for m, mm, v in attrs])] if attrs else [])
  67. def enabled(enableds):
  68. return ["Enabled transitions:"] + \
  69. [set(enableds)]
  70. def fire(fired):
  71. return ["Transition to fire?"] + \
  72. [set([" %s: %s" % (p, v) for p, v in fired])] + \
  73. ["Transition fired!"]
  74. delete = ["Model to delete?", "Deleted!"]
  75. rename = ["Old name?", "New name?", "Rename complete!"]
  76. attr_add = ["Which model do you want to assign an attribute to?",
  77. "Which attribute do you wish to assign?",
  78. "Value of attribute?",
  79. "Added attribute!"]
  80. attr_del = ["Which model do you want to remove an attribute of?",
  81. "Which attribute do you want to delete?",
  82. "Attribute deleted!",
  83. ]
  84. help_root = ["Currently no model is loaded, so your operations are limited to:",
  85. " new -- Create a new model and save it for future use"
  86. " load -- Load a previously made model",
  87. " rename -- Rename a previously made model",
  88. " delete -- Delete a previously made model",
  89. " list -- Show a list of all stored models",
  90. " help -- Show a list of possible commands"]
  91. verify_fail_weight= ["Natural number not larger than or equal to zero"]
  92. verify_fail_tokens= ["Natural number not larger than or equal to zero"]
  93. verify_fail_structure = ["Source of model edge not typed by source of type: p2t"]
  94. init = greeting + prompt
  95. did_instantiate_simple = init + \
  96. new + \
  97. loaded +\
  98. instantiate_node + \
  99. prompt + \
  100. instantiate_node + \
  101. prompt + \
  102. attr_add + \
  103. prompt + \
  104. instantiate_node + \
  105. prompt + \
  106. attr_add + \
  107. prompt + \
  108. instantiate_edge + \
  109. prompt + \
  110. attr_add + \
  111. prompt + \
  112. instantiate_edge + \
  113. prompt + \
  114. attr_add + \
  115. prompt
  116. def list_types(t):
  117. return ["List of types:"] + \
  118. [set([" %s : %s" % (m, mm) for m, mm in t])]
  119. modify = ["Element to modify?",
  120. "Attribute to modify?",
  121. "New value?",
  122. "Modified!",]
  123. class TestPetrinetInterface(unittest.TestCase):
  124. def test_po_pn_interface_manage(self):
  125. self.pn_interface_manage("PO")
  126. def test_co_pn_interface_manage(self):
  127. self.pn_interface_manage("CO")
  128. def pn_interface_manage(self, mode):
  129. self.assertTrue(run_file(all_files,
  130. ['"list"',
  131. '"new"', '"PetriNets"', '"abc"', '"exit"',
  132. '"list"',
  133. '"new"', '"PetriNets"', '"def"', '"exit"',
  134. '"list"',
  135. '"delete"', '"def"',
  136. '"list"',
  137. '"rename"', '"abc"', '"a"',
  138. '"list"',
  139. '"delete"', '"a"',
  140. '"list"',
  141. ],
  142. init + \
  143. list_menu([]) + prompt + \
  144. new + loaded + prompt + list_menu([("abc", "PetriNets")]) + prompt + \
  145. new + loaded + prompt + list_menu([("abc", "PetriNets"), ("def", "PetriNets")]) + prompt + \
  146. delete + prompt + list_menu([("abc", "PetriNets")]) + prompt + \
  147. rename + prompt + list_menu([("a", "PetriNets")]) + prompt + \
  148. delete + prompt + list_menu([]) + prompt,
  149. mode))
  150. def test_po_pn_interface_new_reload(self):
  151. self.pn_interface_new_reload("PO")
  152. def test_co_pn_interface_new_reload(self):
  153. self.pn_interface_new_reload("CO")
  154. def pn_interface_new_reload(self, mode):
  155. self.assertTrue(run_file(all_files,
  156. ['"new"', '"PetriNets"', '"abc"', '"exit"', '"load"', '"abc"'],
  157. init + new + loaded + prompt + load + loaded,
  158. mode))
  159. def test_po_pn_interface_instantiate_place(self):
  160. self.pn_interface_instantiate_place("PO")
  161. def test_co_pn_interface_instantiate_place(self):
  162. self.pn_interface_instantiate_place("CO")
  163. def pn_interface_instantiate_place(self, mode):
  164. self.assertTrue(run_file(all_files,
  165. ['"new"', '"PetriNets"', '"abc"',
  166. '"instantiate"', '"Place"', '"p1"',
  167. '"attr_add"', '"p1"', '"tokens"', '5',
  168. '"list"',
  169. '"read"', '"p1"',
  170. '"instantiate"', '"Transition"', '"t1"',
  171. '"list"',
  172. '"read"', '"t1"'],
  173. init + new + loaded + \
  174. instantiate_node + prompt + \
  175. attr_add + prompt + \
  176. list_model([("p1", "Place")]) + prompt + \
  177. read_node("p1", "Place", [], [("tokens", "Natural", 5)]) + prompt + \
  178. instantiate_node + prompt + \
  179. list_model([("p1", "Place"), ("t1", "Transition")]) + prompt + \
  180. read_node("t1", "Transition", [], []) + prompt,
  181. mode))
  182. def test_po_pn_interface_instantiate_arcs(self):
  183. self.pn_interface_instantiate_arcs("PO")
  184. def test_co_pn_interface_instantiate_arcs(self):
  185. self.pn_interface_instantiate_arcs("CO")
  186. def pn_interface_instantiate_arcs(self, mode):
  187. self.assertTrue(run_file(all_files,
  188. do_instantiate_simple + [
  189. '"read"', '"p1"',
  190. '"read"', '"p2"',
  191. '"read"', '"t1"',
  192. '"read"', '"p2t"',
  193. '"read"', '"t2p"',
  194. ],
  195. did_instantiate_simple + \
  196. read_node("p1", "Place", [], [("tokens", "Natural", 5)]) + prompt + \
  197. read_node("p2", "Place", [], [("tokens", "Natural", 0)]) + prompt + \
  198. read_node("t1", "Transition", [], []) + prompt + \
  199. read_edge("p2t", "P2T", "p1", "t1", [], [("weight", "Natural", 2)]) + prompt + \
  200. read_edge("t2p", "T2P", "t1", "p2", [], [("weight", "Natural", 1)]) + prompt,
  201. mode))
  202. def test_po_pn_interface_enabled(self):
  203. self.pn_interface_enabled("PO")
  204. def test_co_pn_interface_enabled(self):
  205. self.pn_interface_enabled("CO")
  206. def pn_interface_enabled(self, mode):
  207. self.assertTrue(run_file(all_files,
  208. do_instantiate_simple + ['"enabled"'],
  209. did_instantiate_simple + enabled(["t1"]) + prompt,
  210. mode))
  211. def test_po_pn_interface_fire(self):
  212. self.pn_interface_fire("PO")
  213. def test_co_pn_interface_fire(self):
  214. self.pn_interface_fire("CO")
  215. def pn_interface_fire(self, mode):
  216. self.assertTrue(run_file(all_files,
  217. do_instantiate_simple + ['"fire"', '"t1"'],
  218. did_instantiate_simple + fire([("p1", 3), ("p2", 1)]) + prompt,
  219. mode))
  220. def test_po_pn_interface_verify_OK(self):
  221. self.pn_interface_verify_OK("PO")
  222. def test_co_pn_interface_verify_OK(self):
  223. self.pn_interface_verify_OK("CO")
  224. def pn_interface_verify_OK(self, mode):
  225. self.assertTrue(run_file(all_files,
  226. do_instantiate_simple + ['"verify"'],
  227. did_instantiate_simple + ["OK"], mode))
  228. def test_po_pn_interface_verify_fail_tokens(self):
  229. self.pn_interface_verify_fail_tokens("PO")
  230. def test_co_pn_interface_verify_fail_tokens(self):
  231. self.pn_interface_verify_fail_tokens("CO")
  232. def pn_interface_verify_fail_tokens(self, mode):
  233. self.assertTrue(run_file(all_files,
  234. do_instantiate_simple + ['"modify"', '"p1"', '"tokens"', '-5', '"verify"'],
  235. did_instantiate_simple + modify + prompt + verify_fail_tokens + prompt, mode))
  236. def test_po_pn_interface_verify_fail_weight(self):
  237. self.pn_interface_verify_fail_weight("PO")
  238. def test_co_pn_interface_verify_fail_weight(self):
  239. self.pn_interface_verify_fail_weight("CO")
  240. def pn_interface_verify_fail_weight(self, mode):
  241. self.assertTrue(run_file(all_files,
  242. do_instantiate_simple + ['"modify"', '"p2t"', '"weight"', '-2', '"verify"'],
  243. did_instantiate_simple + modify + prompt + verify_fail_weight + prompt, mode))
  244. def test_po_pn_interface_verify_fail_structure(self):
  245. self.pn_interface_verify_fail_structure("PO")
  246. def test_co_pn_interface_verify_fail_structure(self):
  247. self.pn_interface_verify_fail_structure("CO")
  248. def pn_interface_verify_fail_structure(self, mode):
  249. self.assertTrue(run_file(all_files,
  250. ['"new"', '"PetriNets"', '"abc"',
  251. '"instantiate"', '"Transition"', '"t1"',
  252. '"instantiate"', '"Place"', '"p1"', '"attr_add"', '"p1"', '"tokens"', '5',
  253. '"instantiate"', '"P2T"', '"p2t"', '"t1"', '"p1"', '"attr_add"', '"p2t"', '"weight"', '2', '"verify"'],
  254. init + new + loaded + \
  255. instantiate_node + prompt + \
  256. instantiate_node + prompt + attr_add + prompt + \
  257. instantiate_edge + prompt + attr_add + prompt + \
  258. verify_fail_structure,
  259. mode))
  260. def test_po_pn_interface_types(self):
  261. self.pn_interface_types("PO")
  262. def test_co_pn_interface_types(self):
  263. self.pn_interface_types("CO")
  264. def pn_interface_types(self, mode):
  265. self.assertTrue(run_file(all_files,
  266. ['"new"', '"PetriNets"', '"abc"', '"types"'],
  267. init + new + loaded + list_types([("Place", "Class"), ("Transition", "Class"), ("P2T", "Association"), ("T2P", "Association"), ("Natural", "Class")]),
  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 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 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 = reverseKeyLookup(model["model"], read_edge_dst(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 = list_read(back_associations, 0)
  353. \t\t\tdestination = reverseKeyLookup(model["model"], read_edge_dst(association))
  354. \t\t\tif (destination != name):
  355. \t\t\t\treturn "Left link does not have a right link back to the same node"
  356. """
  357. constructors = get_constructor(constraint_code)
  358. self.assertTrue(run_file(all_files,
  359. ['"new"', '"SimpleClassDiagrams"', '"RPGame"',
  360. '"instantiate"', '"Class"', '"Scene"',
  361. '"instantiate"', '"Class"', '"Tile"',
  362. '"instantiate"', '"Class"', '"Item"',
  363. '"instantiate"', '"Class"', '"Goal"',
  364. '"instantiate"', '"Class"', '"Character"',
  365. '"instantiate"', '"Class"', '"Hero"',
  366. '"instantiate"', '"Class"', '"String"',
  367. '"instantiate"', '"Association"', '"scene_name"', '"Scene"', '"String"',
  368. '"instantiate"', '"Association"', '"scene_has_tiles"', '"Scene"', '"Tile"',
  369. '"instantiate"', '"Association"', '"tile_left"', '"Tile"', '"Tile"',
  370. '"instantiate"', '"Association"', '"tile_right"', '"Tile"', '"Tile"',
  371. '"instantiate"', '"Association"', '"tile_top"', '"Tile"', '"Tile"',
  372. '"instantiate"', '"Association"', '"tile_bottom"', '"Tile"', '"Tile"',
  373. '"instantiate"', '"Association"', '"character_on"', '"Character"', '"Tile"',
  374. '"instantiate"', '"Association"', '"item_on"', '"Item"', '"Tile"',
  375. '"instantiate"', '"Inheritance"', '"hero_is_character"', '"Hero"', '"Character"',
  376. '"instantiate"', '"Inheritance"', '"goal_is_item"', '"Goal"', '"Item"',
  377. '"attr_add"', '"Scene"', '"lower_cardinality"', '1',
  378. '"attr_add"', '"Scene"', '"upper_cardinality"', '1',
  379. '"attr_add"', '"Goal"', '"lower_cardinality"', '1',
  380. '"attr_add"', '"scene_has_tiles"', '"source_lower_cardinality"', '1',
  381. '"attr_add"', '"scene_has_tiles"', '"source_upper_cardinality"', '1',
  382. '"attr_add"', '"scene_has_tiles"', '"target_lower_cardinality"', '1',
  383. '"attr_add"', '"scene_name"', '"target_lower_cardinality"', '1',
  384. '"attr_add"', '"scene_name"', '"target_upper_cardinality"', '1',
  385. '"attr_add"', '"item_on"', '"target_lower_cardinality"', '1',
  386. '"attr_add"', '"item_on"', '"target_upper_cardinality"', '1',
  387. '"attr_add"', '"item_on"', '"source_upper_cardinality"', '1',
  388. '"attr_add"', '"character_on"', '"target_lower_cardinality"', '1',
  389. '"attr_add"', '"character_on"', '"target_upper_cardinality"', '1',
  390. '"attr_add"', '"character_on"', '"source_upper_cardinality"', '1',
  391. '"attr_add"', '"tile_left"', '"source_upper_cardinality"', '1',
  392. '"attr_add"', '"tile_left"', '"target_upper_cardinality"', '1',
  393. '"attr_add"', '"tile_right"', '"source_upper_cardinality"', '1',
  394. '"attr_add"', '"tile_right"', '"target_upper_cardinality"', '1',
  395. '"attr_add"', '"tile_top"', '"source_upper_cardinality"', '1',
  396. '"attr_add"', '"tile_top"', '"target_upper_cardinality"', '1',
  397. '"attr_add"', '"tile_bottom"', '"source_upper_cardinality"', '1',
  398. '"attr_add"', '"tile_bottom"', '"target_upper_cardinality"', '1',
  399. ] + constructors + [
  400. '"verify"',
  401. ],
  402. init + new + loaded + (instantiate_node + prompt) * 7 + (instantiate_edge + prompt) * 10 + (attr_add + prompt) * 22 + ["OK"] + prompt,
  403. mode))