test_pn_interface.py 14 KB

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