test_pn_interface.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. import unittest
  2. from utils import run_file
  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. "constructors.alc",
  24. "metamodels.alc",
  25. "--fast",
  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" for m, mm in defs]) if defs else []) + \
  55. ["Attributes:"] + \
  56. (set([' "%s" : "%s" = %s' 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" for m, mm in defs]) if defs else []) + \
  65. ["Attributes:"] + \
  66. (set([' "%s" : "%s" = %s' 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. help_root = ["Currently no model is loaded, so your operations are limited to:",
  77. " new -- Create a new model and save it for future use"
  78. " load -- Load a previously made model",
  79. " rename -- Rename a previously made model",
  80. " delete -- Delete a previously made model",
  81. " list -- Show a list of all stored models",
  82. " help -- Show a list of possible commands"]
  83. verify_ok = ["OK"]
  84. verify_fail_weight= ["Negative weight in arc p2t"]
  85. verify_fail_tokens= ["Negative number of tokens in Place p1"]
  86. verify_fail_structure = ["Source of model edge not typed by source of type: p2t"]
  87. init = greeting + prompt
  88. did_instantiate_simple = init + \
  89. new + \
  90. loaded +\
  91. instantiate_node +\
  92. prompt + \
  93. instantiate_node +\
  94. prompt + \
  95. instantiate_node +\
  96. prompt + \
  97. instantiate_edge +\
  98. prompt + \
  99. instantiate_edge +\
  100. prompt
  101. def list_types(t):
  102. return ["List of types:"] + \
  103. [set([" %s : %s" % (m, mm) for m, mm in t])]
  104. modify = ["Element to modify?",
  105. "Attribute to modify?",
  106. "New value?",
  107. "Modified",]
  108. class TestPetrinetInterface(unittest.TestCase):
  109. def test_po_pn_interface_manage(self):
  110. self.pn_interface_manage("PO")
  111. def test_co_pn_interface_manage(self):
  112. self.pn_interface_manage("CO")
  113. def pn_interface_manage(self, mode):
  114. self.assertTrue(run_file(all_files,
  115. ["list",
  116. "new", "PetriNets", "abc", "exit",
  117. "list",
  118. "new", "PetriNets", "def", "exit",
  119. "list",
  120. "delete", "def",
  121. "list",
  122. "rename", "abc", "a",
  123. "list",
  124. "delete", "a",
  125. "list",
  126. ],
  127. init + \
  128. list_menu([]) + prompt + \
  129. new + loaded + prompt + list_menu([("abc", "PetriNets")]) + prompt + \
  130. new + loaded + prompt + list_menu([("abc", "PetriNets"), ("def", "PetriNets")]) + prompt + \
  131. delete + prompt + list_menu([("abc", "PetriNets")]) + prompt + \
  132. rename + prompt + list_menu([("a", "PetriNets")]) + prompt + \
  133. delete + prompt + list_menu([]) + prompt,
  134. mode))
  135. def test_po_pn_interface_new_reload(self):
  136. self.pn_interface_new_reload("PO")
  137. def test_co_pn_interface_new_reload(self):
  138. self.pn_interface_new_reload("CO")
  139. def pn_interface_new_reload(self, mode):
  140. self.assertTrue(run_file(all_files,
  141. ["new", "PetriNets", "abc", "exit", "load", "abc"], init + new_full +
  142. prompt_menu + load_full, mode))
  143. def test_po_pn_interface_new_list_empty(self):
  144. self.pn_interface_new_list_empty("PO")
  145. def test_co_pn_interface_new_list_empty(self):
  146. self.pn_interface_new_list_empty("CO")
  147. def pn_interface_new_list_empty(self, mode):
  148. self.assertTrue(run_file(all_files,
  149. ["new", "PetriNets", "abc", "list"], init + new_full + list_model +
  150. prompt_model, mode))
  151. def test_po_pn_interface_instantiate_place(self):
  152. self.pn_interface_instantiate_place("PO")
  153. def test_co_pn_interface_instantiate_place(self):
  154. self.pn_interface_instantiate_place("CO")
  155. def pn_interface_instantiate_place(self, mode):
  156. self.assertTrue(run_file(all_files,
  157. ["new", "PetriNets", "abc",
  158. "instantiate", "Place", "p1", "attr_add", "p1", "tokens", 5],
  159. init + new_full + instantiate + instantiate_name +
  160. instantiate_tokens + instantiate_ok + prompt_model, mode))
  161. def test_po_pn_interface_instantiate_place_list(self):
  162. self.pn_interface_instantiate_place_list("PO")
  163. def test_co_pn_interface_instantiate_place_list(self):
  164. self.pn_interface_instantiate_place_list("CO")
  165. def pn_interface_instantiate_place_list(self, mode):
  166. self.assertTrue(run_file(all_files,
  167. ["new", "PetriNets", "abc",
  168. "instantiate", "Place", "p1", "attr_add", "p1", "tokens", 5,
  169. "list"],
  170. init + new_full + instantiate + instantiate_name +
  171. instantiate_tokens + instantiate_ok + prompt_model +
  172. list_model + list_p1, mode))
  173. def test_po_pn_interface_instantiate_place_read(self):
  174. self.pn_interface_instantiate_place_read("PO")
  175. def test_co_pn_interface_instantiate_place_read(self):
  176. self.pn_interface_instantiate_place_read("CO")
  177. def pn_interface_instantiate_place_read(self, mode):
  178. self.assertTrue(run_file(all_files,
  179. ["new", "PetriNets", "abc",
  180. "instantiate", "Place", "p1", "attr_add", "p1", "tokens", 5,
  181. "read", "p1"],
  182. init + new_full + instantiate + instantiate_name +
  183. instantiate_tokens + instantiate_ok + prompt_model + read +
  184. read_p1, mode))
  185. def test_po_pn_interface_instantiate_transition(self):
  186. self.pn_interface_instantiate_transition("PO")
  187. def test_co_pn_interface_instantiate_transition(self):
  188. self.pn_interface_instantiate_transition("CO")
  189. def pn_interface_instantiate_transition(self, mode):
  190. self.assertTrue(run_file(all_files,
  191. ["new", "PetriNets", "abc",
  192. "instantiate", "Transition", "t1"],
  193. init + new_full + instantiate + instantiate_name +
  194. instantiate_ok + prompt_model, mode))
  195. def test_po_pn_interface_instantiate_transition_read(self):
  196. self.pn_interface_instantiate_transition_read("PO")
  197. def test_co_pn_interface_instantiate_transition_read(self):
  198. self.pn_interface_instantiate_transition_read("CO")
  199. def pn_interface_instantiate_transition_read(self, mode):
  200. self.assertTrue(run_file(all_files,
  201. ["new", "PetriNets", "abc", "instantiate", "Transition", "t1", "read", "t1"],
  202. init + new_full + instantiate + instantiate_name +
  203. instantiate_ok + prompt_model + read + read_t1, mode))
  204. def test_po_pn_interface_instantiate_arcs(self):
  205. self.pn_interface_instantiate_arcs("PO")
  206. def test_co_pn_interface_instantiate_arcs(self):
  207. self.pn_interface_instantiate_arcs("CO")
  208. def pn_interface_instantiate_arcs(self, mode):
  209. self.assertTrue(run_file(all_files,
  210. do_instantiate_simple,
  211. did_instantiate_simple,
  212. mode))
  213. def test_po_pn_interface_instantiate_arcs_read(self):
  214. self.pn_interface_instantiate_arcs_read("PO")
  215. def test_co_pn_interface_instantiate_arcs_read(self):
  216. self.pn_interface_instantiate_arcs_read("CO")
  217. def pn_interface_instantiate_arcs_read(self, mode):
  218. self.assertTrue(run_file(all_files,
  219. do_instantiate_simple + [
  220. "read", "p1",
  221. "read", "p2",
  222. "read", "t1",
  223. "read", "p2t",
  224. "read", "t2p"],
  225. did_instantiate_simple +
  226. read_p1_full + read_p2_full + read_t1_full + read_p2t_full +
  227. read_t2p_full, mode))
  228. def test_po_pn_interface_instantiate_arcs_list(self):
  229. self.pn_interface_instantiate_arcs_list("PO")
  230. def test_co_pn_interface_instantiate_arcs_list(self):
  231. self.pn_interface_instantiate_arcs_list("CO")
  232. def pn_interface_instantiate_arcs_list(self, mode):
  233. self.assertTrue(run_file(all_files,
  234. do_instantiate_simple + ["list"],
  235. did_instantiate_simple +
  236. list_model + list_t1 + list_p1 + list_p2 + list_p2t + list_t2p +
  237. prompt_model, mode))
  238. def test_po_pn_interface_enabled_empty(self):
  239. self.pn_interface_enabled_empty("PO")
  240. def test_co_pn_interface_enabled_empty(self):
  241. self.pn_interface_enabled_empty("CO")
  242. def pn_interface_enabled_empty(self, mode):
  243. self.assertTrue(run_file(all_files,
  244. ["new", "PetriNets", "abc", "enabled"],
  245. init + new_full + enabled + prompt_model, mode))
  246. def test_po_pn_interface_enabled(self):
  247. self.pn_interface_enabled("PO")
  248. def test_co_pn_interface_enabled(self):
  249. self.pn_interface_enabled("CO")
  250. def pn_interface_enabled(self, mode):
  251. self.assertTrue(run_file(all_files,
  252. do_instantiate_simple + ["enabled"],
  253. did_instantiate_simple + enabled +
  254. enabled_t1 + prompt_model, mode))
  255. def test_po_pn_interface_fire(self):
  256. self.pn_interface_fire("PO")
  257. def test_co_pn_interface_fire(self):
  258. self.pn_interface_fire("CO")
  259. def pn_interface_fire(self, mode):
  260. self.assertTrue(run_file(all_files,
  261. do_instantiate_simple + ["fire", "t1"],
  262. did_instantiate_simple + ["fire", "t1"],
  263. init + new_full + instantiate_transition + instantiate_place +
  264. instantiate_place + instantiate_arc + instantiate_arc + fire +
  265. fire_t1 + fire_finish + prompt_model, mode))
  266. def test_po_pn_interface_fire_place(self):
  267. self.pn_interface_fire_place("PO")
  268. def test_co_pn_interface_fire_place(self):
  269. self.pn_interface_fire_place("CO")
  270. def pn_interface_fire_place(self, mode):
  271. self.assertTrue(run_file(all_files,
  272. ["new", "PetriNets", "abc",
  273. "instantiate", "Place", "p1", "attr_add", "p1", "tokens", 5,
  274. "fire", "p1"],
  275. init + new_full + instantiate_place + fire + fire_no_enable +
  276. prompt_model, mode))
  277. def test_po_pn_interface_fire_unknown(self):
  278. self.pn_interface_fire_unknown("PO")
  279. def test_co_pn_interface_fire_unknown(self):
  280. self.pn_interface_fire_unknown("CO")
  281. def pn_interface_fire_unknown(self, mode):
  282. self.assertTrue(run_file(all_files,
  283. ["new", "PetriNets", "abc", "fire", "t1"],
  284. init + new_full + fire + fire_unknown + prompt_model, mode))
  285. def test_po_pn_interface_verify_OK(self):
  286. self.pn_interface_verify_OK("PO")
  287. def test_co_pn_interface_verify_OK(self):
  288. self.pn_interface_verify_OK("CO")
  289. def pn_interface_verify_OK(self, mode):
  290. self.assertTrue(run_file(all_files,
  291. do_instantiate_simple + ["verify"],
  292. did_instantiate_simple + verify_full, mode))
  293. def test_po_pn_interface_verify_fail_tokens(self):
  294. self.pn_interface_verify_fail_tokens("PO")
  295. def test_co_pn_interface_verify_fail_tokens(self):
  296. self.pn_interface_verify_fail_tokens("CO")
  297. def pn_interface_verify_fail_tokens(self, mode):
  298. self.assertTrue(run_file(all_files,
  299. do_instantiate_simple + ["modify", "p1", "tokens", -5, "verify"],
  300. did_instantiate_simple + verify_fail_tokens, mode))
  301. def test_po_pn_interface_verify_fail_weight(self):
  302. self.pn_interface_verify_fail_weight("PO")
  303. def test_co_pn_interface_verify_fail_weight(self):
  304. self.pn_interface_verify_fail_weight("CO")
  305. def pn_interface_verify_fail_weight(self, mode):
  306. self.assertTrue(run_file(all_files,
  307. do_instantiate_simple + ["modify", "p2t", "weight", -2, "verify"],
  308. did_instantiate_simple + verify_fail_weight, mode))
  309. def test_po_pn_interface_verify_fail_structure(self):
  310. self.pn_interface_verify_fail_structure("PO")
  311. def test_co_pn_interface_verify_fail_structure(self):
  312. self.pn_interface_verify_fail_structure("CO")
  313. def pn_interface_verify_fail_structure(self, mode):
  314. self.assertTrue(run_file(all_files,
  315. ["new", "PetriNets", "abc",
  316. "instantiate", "Transition", "t1",
  317. "instantiate", "Place", "p1", "attr_add", "p1", "tokens", 5,
  318. "instantiate", "P2T", "p2t", "t1", "p1", "attr_add", "p2t", "weight", 2, "verify"],
  319. init + new_full + instantiate_transition +
  320. instantiate_place + instantiate_arc + verify_fail_structure,
  321. mode))
  322. def test_po_pn_interface_types(self):
  323. self.pn_interface_types("PO")
  324. def test_co_pn_interface_types(self):
  325. self.pn_interface_types("CO")
  326. def pn_interface_types(self, mode):
  327. self.assertTrue(run_file(all_files,
  328. ["new", "PetriNets", "abc", "types"], init + new_full + types_full, mode))
  329. def test_po_pn_interface_modify_place(self):
  330. self.pn_interface_modify_place("PO")
  331. def test_co_pn_interface_modify_place(self):
  332. self.pn_interface_modify_place("CO")
  333. def pn_interface_modify_place(self, mode):
  334. self.assertTrue(run_file(all_files,
  335. ["new", "PetriNets", "abc",
  336. "instantiate", "Place", "p1", "attr_add", "p1", "tokens", 5,
  337. "read", "p1",
  338. "modify", "p1", "tokens", 1, "read", "p1"],
  339. init + new_full + instantiate + instantiate_name +
  340. instantiate_tokens + instantiate_ok + prompt_model + read +
  341. read_p1 + prompt_menu + modify_place_full + read + read_p1_1 +
  342. prompt_menu, mode))