pn_interface.alc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. include "primitives.alh"
  2. include "constructors.alh"
  3. include "object_operations.alh"
  4. include "library.alh"
  5. include "conformance_scd.alh"
  6. include "io.alh"
  7. Element function create_metamodels():
  8. // TODO
  9. // For the moment, we make the assumption that it doesn't exist yet.
  10. // We create both the Simple Class Diagrams model, retype it, and then make the metamodel as an instance of this
  11. // In the future, this should already exist though...
  12. if (bool_not(dict_in(dict_read(read_root(), "__hierarchy"), "models"))):
  13. Element scd
  14. Element scd_model
  15. scd = create_node()
  16. scd_model = create_node()
  17. dict_add(scd, "model", scd_model)
  18. instantiate_bottom_node(scd, "Class")
  19. instantiate_bottom_value(scd, "Type", Type)
  20. instantiate_bottom_value(scd, "__String", String)
  21. instantiate_bottom_value(scd, "__name", "name")
  22. instantiate_bottom_edge(scd, "Attribute", dict_read(scd_model, "Class"), dict_read(scd_model, "Type"))
  23. instantiate_bottom_edge(scd, "__Name", dict_read(scd_model, "Attribute"), dict_read(scd_model, "__String"))
  24. instantiate_bottom_edge(scd, "Association", dict_read(scd_model, "Class"), dict_read(scd_model, "Class"))
  25. instantiate_bottom_edge(scd, "Inheritance", dict_read(scd_model, "Class"), dict_read(scd_model, "Class"))
  26. instantiate_bottom_edge(scd, "__name_edge", dict_read(scd_model, "__Name"), dict_read(scd_model, "__name"))
  27. instantiate_bottom_edge(scd, "__inh_1", dict_read(scd_model, "Association"), dict_read(scd_model, "Class"))
  28. instantiate_bottom_edge(scd, "__inh_2", dict_read(scd_model, "Attribute"), dict_read(scd_model, "Class"))
  29. instantiate_bottom_value(scd, "__attribute", "attribute")
  30. instantiate_bottom_edge(scd, "__attribute_edge", dict_read(scd_model, "Attribute"), dict_read(scd_model, "__attribute"))
  31. // Instantiated, now retype to make sure that everything is correct
  32. Element mapping
  33. mapping = create_node()
  34. dict_add(mapping, dict_read(scd_model, "Class"), dict_read(scd_model, "Class"))
  35. dict_add(mapping, dict_read(scd_model, "Type"), dict_read(scd_model, "Type"))
  36. dict_add(mapping, dict_read(scd_model, "__String"), dict_read(scd_model, "Type"))
  37. dict_add(mapping, dict_read(scd_model, "Attribute"), dict_read(scd_model, "Attribute"))
  38. dict_add(mapping, dict_read(scd_model, "__Name"), dict_read(scd_model, "Attribute"))
  39. dict_add(mapping, dict_read(scd_model, "Association"), dict_read(scd_model, "Association"))
  40. dict_add(mapping, dict_read(scd_model, "Inheritance"), dict_read(scd_model, "Association"))
  41. dict_add(mapping, dict_read(scd_model, "__inh_1"), dict_read(scd_model, "Inheritance"))
  42. dict_add(mapping, dict_read(scd_model, "__inh_2"), dict_read(scd_model, "Inheritance"))
  43. dict_add(mapping, dict_read(scd_model, "__name"), dict_read(scd_model, "__String"))
  44. dict_add(mapping, dict_read(scd_model, "__name_edge"), dict_read(scd_model, "__Name"))
  45. dict_add(mapping, dict_read(scd_model, "__attribute"), dict_read(scd_model, "__String"))
  46. dict_add(mapping, dict_read(scd_model, "__attribute_edge"), dict_read(scd_model, "__Name"))
  47. retype(scd, scd, dict_read(scd_model, "Inheritance"), mapping)
  48. export_node("models/SimpleClassDiagrams", scd)
  49. // TODO this code is also very dirty as there is no nice "dictionary" and "list" syntax yet
  50. Element pn_metamodel
  51. pn_metamodel = instantiate_new_model(scd, dict_read(scd_model, "Inheritance"))
  52. Element tokens
  53. tokens = create_node()
  54. dict_add(tokens, "tokens", Integer)
  55. instantiate_model_lib(pn_metamodel, dict_read(scd_model, "Class"), "Place", create_node(), tokens, create_node())
  56. instantiate_model_lib(pn_metamodel, dict_read(scd_model, "Class"), "Transition", create_node(), create_node(), create_node())
  57. Element weight
  58. weight = create_node()
  59. dict_add(weight, "weight", Integer)
  60. Element p2t_links
  61. p2t_links = create_node()
  62. list_append(p2t_links, dict_read(dict_read(pn_metamodel, "model"), "Place"))
  63. list_append(p2t_links, dict_read(dict_read(pn_metamodel, "model"), "Transition"))
  64. Element t2p_links
  65. t2p_links = create_node()
  66. list_append(t2p_links, dict_read(dict_read(pn_metamodel, "model"), "Transition"))
  67. list_append(t2p_links, dict_read(dict_read(pn_metamodel, "model"), "Place"))
  68. instantiate_model_lib(pn_metamodel, dict_read(scd_model, "Association"), "P2T", p2t_links, weight, create_node())
  69. instantiate_model_lib(pn_metamodel, dict_read(scd_model, "Association"), "T2P", t2p_links, weight, create_node())
  70. set_model_constraints(pn_metamodel, petrinet_constraints)
  71. export_node("models/PetriNets", pn_metamodel)
  72. // Also create conformance bottom metamodel
  73. Element mm_bottom
  74. Element mm_bottom_model
  75. mm_bottom = create_node()
  76. mm_bottom_model = create_node()
  77. dict_add(mm_bottom, "model", mm_bottom_model)
  78. instantiate_bottom_node(mm_bottom, "Node")
  79. instantiate_bottom_value(mm_bottom, "Type", Type)
  80. instantiate_bottom_value(mm_bottom, "Integer", Integer)
  81. instantiate_bottom_value(mm_bottom, "Action", Action)
  82. instantiate_bottom_value(mm_bottom, "Float", Float)
  83. instantiate_bottom_value(mm_bottom, "String", String)
  84. instantiate_bottom_value(mm_bottom, "Boolean", Boolean)
  85. instantiate_bottom_edge(mm_bottom, "Edge", dict_read(mm_bottom_model, "Node"), dict_read(mm_bottom_model, "Node"))
  86. instantiate_bottom_edge(mm_bottom, "__Inheritance", dict_read(mm_bottom_model, "Node"), dict_read(mm_bottom_model, "Node"))
  87. instantiate_bottom_edge(mm_bottom, "__inh_1", dict_read(mm_bottom_model, "Type"), dict_read(mm_bottom_model, "Node"))
  88. instantiate_bottom_edge(mm_bottom, "__inh_2", dict_read(mm_bottom_model, "Integer"), dict_read(mm_bottom_model, "Node"))
  89. instantiate_bottom_edge(mm_bottom, "__inh_3", dict_read(mm_bottom_model, "Action"), dict_read(mm_bottom_model, "Node"))
  90. instantiate_bottom_edge(mm_bottom, "__inh_4", dict_read(mm_bottom_model, "Float"), dict_read(mm_bottom_model, "Node"))
  91. instantiate_bottom_edge(mm_bottom, "__inh_5", dict_read(mm_bottom_model, "String"), dict_read(mm_bottom_model, "Node"))
  92. instantiate_bottom_edge(mm_bottom, "__inh_6", dict_read(mm_bottom_model, "Boolean"), dict_read(mm_bottom_model, "Node"))
  93. instantiate_bottom_edge(mm_bottom, "__inh_7", dict_read(mm_bottom_model, "Edge"), dict_read(mm_bottom_model, "Node"))
  94. // Retype it with itself
  95. mapping = create_node()
  96. dict_add(mapping, dict_read(mm_bottom_model, "Node"), dict_read(mm_bottom_model, "Node"))
  97. dict_add(mapping, dict_read(mm_bottom_model, "Type"), dict_read(mm_bottom_model, "Type"))
  98. dict_add(mapping, dict_read(mm_bottom_model, "Integer"), dict_read(mm_bottom_model, "Type"))
  99. dict_add(mapping, dict_read(mm_bottom_model, "Float"), dict_read(mm_bottom_model, "Type"))
  100. dict_add(mapping, dict_read(mm_bottom_model, "String"), dict_read(mm_bottom_model, "Type"))
  101. dict_add(mapping, dict_read(mm_bottom_model, "Boolean"), dict_read(mm_bottom_model, "Type"))
  102. dict_add(mapping, dict_read(mm_bottom_model, "Action"), dict_read(mm_bottom_model, "Type"))
  103. dict_add(mapping, dict_read(mm_bottom_model, "Edge"), dict_read(mm_bottom_model, "Edge"))
  104. dict_add(mapping, dict_read(mm_bottom_model, "__Inheritance"), dict_read(mm_bottom_model, "Edge"))
  105. dict_add(mapping, dict_read(mm_bottom_model, "__inh_1"), dict_read(mm_bottom_model, "__Inheritance"))
  106. dict_add(mapping, dict_read(mm_bottom_model, "__inh_2"), dict_read(mm_bottom_model, "__Inheritance"))
  107. dict_add(mapping, dict_read(mm_bottom_model, "__inh_3"), dict_read(mm_bottom_model, "__Inheritance"))
  108. dict_add(mapping, dict_read(mm_bottom_model, "__inh_4"), dict_read(mm_bottom_model, "__Inheritance"))
  109. dict_add(mapping, dict_read(mm_bottom_model, "__inh_5"), dict_read(mm_bottom_model, "__Inheritance"))
  110. dict_add(mapping, dict_read(mm_bottom_model, "__inh_6"), dict_read(mm_bottom_model, "__Inheritance"))
  111. dict_add(mapping, dict_read(mm_bottom_model, "__inh_7"), dict_read(mm_bottom_model, "__Inheritance"))
  112. retype(mm_bottom, mm_bottom, dict_read(mm_bottom_model, "__Inheritance"), mapping)
  113. export_node("models/LTM_bottom", mm_bottom)
  114. return 0
  115. String function petrinet_constraints(pn_mo : Element):
  116. // Check places to have positive number of tokens
  117. Element all_elems
  118. Element elem_constraint
  119. all_elems = allInstances(pn_mo, dict_read(dict_read(dict_read(pn_mo, "metamodel"), "model"), "Place"))
  120. while (integer_lt(0, read_nr_out(all_elems))):
  121. elem_constraint = set_pop(all_elems)
  122. if (integer_lt(readAttribute(pn_mo, elem_constraint, "tokens"), 0)):
  123. return string_join("Negative number of tokens in Place ", getName(pn_mo, elem_constraint))
  124. // Check P2T transitions to have positive weight
  125. all_elems = allInstances(pn_mo, dict_read(dict_read(dict_read(pn_mo, "metamodel"), "model"), "P2T"))
  126. while (integer_lt(0, read_nr_out(all_elems))):
  127. elem_constraint = set_pop(all_elems)
  128. if (integer_lt(readAttribute(pn_mo, elem_constraint, "weight"), 0)):
  129. return string_join("Negative weight in arc ", getName(pn_mo, elem_constraint))
  130. // Check T2P transitions to have positive weight
  131. all_elems = allInstances(pn_mo, dict_read(dict_read(dict_read(pn_mo, "metamodel"), "model"), "T2P"))
  132. while (integer_lt(0, read_nr_out(all_elems))):
  133. elem_constraint = set_pop(all_elems)
  134. if (integer_lt(readAttribute(pn_mo, elem_constraint, "weight"), 0)):
  135. return string_join("Negative weight in arc ", getName(pn_mo, elem_constraint))
  136. return "OK"
  137. Element function pn_operations():
  138. Element ops
  139. ops = create_node()
  140. dict_add(ops, "fire", petrinet_fire)
  141. dict_add(ops, "enabled", petrinet_enabled)
  142. return ops
  143. Element function petrinet_enabled(pn_m : Element):
  144. Element set_enabled
  145. set_enabled = petrinet_enabled_set(pn_m)
  146. output("Enabled transitions:")
  147. while (integer_lt(0, read_nr_out(set_enabled))):
  148. output(getName(pn_m, set_pop(set_enabled)))
  149. return pn_m
  150. Element function petrinet_enabled_set(pn_mod : Element):
  151. Element all_transitions
  152. all_transitions = allInstances(pn_mod, dict_read(dict_read(dict_read(pn_mod, "metamodel"), "model"), "Transition"))
  153. Element enabled_transitions
  154. enabled_transitions = create_node()
  155. Element under_study
  156. Element in_arcs
  157. Element arc_under_study
  158. Boolean enabled
  159. while (integer_lt(0, read_nr_out(all_transitions))):
  160. under_study = set_pop(all_transitions)
  161. enabled = True
  162. // Find all incoming transitions
  163. in_arcs = allIncomingAssociationInstances(pn_mod, under_study, dict_read(dict_read(dict_read(pn_mod, "metamodel"), "model"), "P2T"))
  164. while (integer_lt(0, read_nr_out(in_arcs))):
  165. arc_under_study = set_pop(in_arcs)
  166. Integer present_tokens
  167. Integer required_tokens
  168. required_tokens = readAttribute(pn_mod, arc_under_study, "weight")
  169. present_tokens = readAttribute(pn_mod, read_edge_src(arc_under_study), "tokens")
  170. if (integer_lt(present_tokens, required_tokens)):
  171. // Less tokens than required, so disable the transition completely
  172. enabled = False
  173. if (enabled):
  174. set_add(enabled_transitions, under_study)
  175. return enabled_transitions
  176. Element function petrinet_fire(pn_mode : Element):
  177. output("Transition to fire?")
  178. Element transition
  179. transition = input()
  180. if (dict_in(dict_read(pn_mode, "model"), transition)):
  181. transition = dict_read(dict_read(pn_mode, "model"), transition)
  182. if (set_in(petrinet_enabled_set(pn_mode), transition)):
  183. Element workset
  184. Element working_place
  185. Element working_arc
  186. // Consume tokens
  187. workset = allIncomingAssociationInstances(pn_mode, transition, dict_read(dict_read(dict_read(pn_mode, "metamodel"), "model"), "P2T"))
  188. while (integer_lt(0, read_nr_out(workset))):
  189. working_arc = set_pop(workset)
  190. working_place = read_edge_src(working_arc)
  191. setAttribute(pn_mode, working_place, "tokens", integer_subtraction(readAttribute(pn_mode, working_place, "tokens"), readAttribute(pn_mode, working_arc, "weight")))
  192. output(string_join(string_join(string_join(" ", getName(pn_mode, working_place)), ": "), cast_i2s(readAttribute(pn_mode, working_place, "tokens"))))
  193. // Add tokens
  194. workset = allOutgoingAssociationInstances(pn_mode, transition, dict_read(dict_read(dict_read(pn_mode, "metamodel"), "model"), "T2P"))
  195. while (integer_lt(0, read_nr_out(workset))):
  196. working_arc = set_pop(workset)
  197. working_place = read_edge_dst(working_arc)
  198. setAttribute(pn_mode, working_place, "tokens", integer_addition(readAttribute(pn_mode, working_place, "tokens"), readAttribute(pn_mode, working_arc, "weight")))
  199. output(string_join(string_join(string_join(" ", getName(pn_mode, working_place)), ": "), cast_i2s(readAttribute(pn_mode, working_place, "tokens"))))
  200. output("Transition fired!")
  201. else:
  202. output("Cannot fire if not enabled; aborting")
  203. else:
  204. output("Unknown transition; aborting")
  205. return pn_mode
  206. Element function petrinet_loaded(pn_model : Element):
  207. String cmd
  208. Element attr_list_pn
  209. Element attr_keys_pn
  210. Element attr_key_pn
  211. Element metamodel_element_pn
  212. String typename
  213. Boolean bottom
  214. Element other_metamodel
  215. bottom = False
  216. other_metamodel = create_node()
  217. dict_add(other_metamodel, "model", dict_read(pn_model, "model"))
  218. dict_add(other_metamodel, "type_mapping", create_node())
  219. dict_add(other_metamodel, "metamodel", import_node("models/LTM_bottom"))
  220. dict_add(other_metamodel, "inheritance", dict_read(dict_read(dict_read(other_metamodel, "metamodel"), "model"), "__Inheritance"))
  221. output("Model loaded, ready for commands!")
  222. output("Use 'help' command for a list of possible commands")
  223. while (True):
  224. output("Please give your command.")
  225. cmd = input()
  226. if (string_eq(cmd, "help")):
  227. output("Generic model operations:")
  228. output(" instantiate -- Create a new model element")
  229. output(" delete -- Delete an existing element")
  230. output(" modify -- Modify attribute of an existing element")
  231. output(" rename -- Rename an existing element")
  232. output(" list -- Prints the list of elements in the model")
  233. output(" types -- Prints the list of elements that can be instantiated")
  234. output(" read -- Prints the current state of a model element")
  235. output(" verify -- Check whether the model conforms to the metamodel")
  236. output(" retype -- Change the type of an element")
  237. output(" switch -- Switch between conformance bottom and the linguistic metamodel")
  238. output(" exit -- Unload the model and go back to the loading prompt")
  239. if (bool_not(bottom)):
  240. output("Model-specific operations:")
  241. Element specific_ops
  242. specific_ops = dict_keys(pn_operations())
  243. while (integer_lt(0, dict_len(specific_ops))):
  244. output(string_join(" ", set_pop(specific_ops)))
  245. elif (string_eq(cmd, "exit")):
  246. return pn_model
  247. elif (string_eq(cmd, "instantiate")):
  248. String mm_type_name
  249. output("Type to instantiate?")
  250. mm_type_name = input()
  251. if (dict_in(dict_read(dict_read(pn_model, "metamodel"), "model"), mm_type_name)):
  252. metamodel_element_pn = dict_read(dict_read(dict_read(pn_model, "metamodel"), "model"), mm_type_name)
  253. String element_name
  254. output("Name of new element?")
  255. element_name = input()
  256. if (dict_in(dict_read(pn_model, "model"), element_name)):
  257. output("Element already exists; aborting")
  258. else:
  259. Boolean cnt
  260. cnt = True
  261. Element additional_opts
  262. additional_opts = create_node()
  263. if (is_edge(metamodel_element_pn)):
  264. output("Source name?")
  265. String src_name
  266. src_name = input()
  267. if (dict_in(dict_read(pn_model, "model"), src_name)):
  268. list_append(additional_opts, dict_read(dict_read(pn_model, "model"), src_name))
  269. output("Destination name?")
  270. String dst_name
  271. dst_name = input()
  272. if (dict_in(dict_read(pn_model, "model"), dst_name)):
  273. list_append(additional_opts, dict_read(dict_read(pn_model, "model"), dst_name))
  274. else:
  275. output("Unknown destination; aborting")
  276. cnt = False
  277. else:
  278. output("Unknown source; aborting")
  279. cnt = False
  280. if (type_eq(typeof(metamodel_element_pn), Type)):
  281. // It is a value
  282. output("Value?")
  283. list_append(additional_opts, input())
  284. if (cnt):
  285. attr_list_pn = getAttributeList(pn_model, metamodel_element_pn)
  286. attr_keys_pn = dict_keys(attr_list_pn)
  287. Element attrs
  288. attrs = create_node()
  289. while (integer_lt(0, read_nr_out(attr_keys_pn))):
  290. attr_key_pn = set_pop(attr_keys_pn)
  291. output(string_join(string_join(string_join(attr_key_pn, " : "), cast_t2s(dict_read(attr_list_pn, attr_key_pn))), "?"))
  292. dict_add(attrs, attr_key_pn, input())
  293. Element resulting_element
  294. resulting_element = instantiate_model_lib(pn_model, metamodel_element_pn, element_name, additional_opts, create_node(), attrs)
  295. output("Instantiation successful!")
  296. else:
  297. output("Unknown type specified; aborting")
  298. elif (string_eq(cmd, "delete")):
  299. output("What is the name of the element you want to delete?")
  300. cmd = input()
  301. if (dict_in(dict_read(pn_model, "model"), cmd)):
  302. delete_element(dict_read(dict_read(pn_model, "model"), cmd))
  303. output("Deleted!")
  304. else:
  305. output("No such element; aborting")
  306. elif (string_eq(cmd, "modify")):
  307. output("Element to modify?")
  308. cmd = input()
  309. if (dict_in(dict_read(pn_model, "model"), cmd)):
  310. Element mod
  311. mod = dict_read(dict_read(pn_model, "model"), cmd)
  312. metamodel_element_pn = dict_read_node(dict_read(pn_model, "type_mapping"), mod)
  313. attr_list_pn = getAttributeList(pn_model, metamodel_element_pn)
  314. attr_keys_pn = dict_keys(attr_list_pn)
  315. while (integer_lt(0, read_nr_out(attr_keys_pn))):
  316. attr_key_pn = set_pop(attr_keys_pn)
  317. output(string_join(string_join(string_join(" ", attr_key_pn), " : "), cast_t2s(dict_read(attr_list_pn, attr_key_pn))))
  318. output("Attribute to modify?")
  319. String attr_name
  320. attr_name = input()
  321. if (set_in(dict_keys(getAttributeList(pn_model, metamodel_element_pn)), attr_name)):
  322. output("New value?")
  323. setAttribute(pn_model, mod, attr_name, input())
  324. output("Modified!")
  325. else:
  326. output("Unknown attribute; aborting")
  327. else:
  328. output("Element does not exist; aborting")
  329. elif (string_eq(cmd, "rename")):
  330. output("Old name?")
  331. String old_name_e
  332. old_name_e = input()
  333. if (dict_in(dict_read(pn_model, "model"), old_name_e)):
  334. output("New name?")
  335. String new_name_e
  336. new_name_e = input()
  337. if (dict_in(dict_read(pn_model, "model"), new_name_e)):
  338. output("New name already used; aborting")
  339. else:
  340. dict_add(dict_read(pn_model, "model"), new_name_e, dict_read(dict_read(pn_model, "model"), old_name_e))
  341. dict_delete(dict_read(pn_model, "model"), old_name_e)
  342. output("Rename complete!")
  343. else:
  344. output("Unknown element; aborting")
  345. elif (string_eq(cmd, "list")):
  346. Element keys_m
  347. keys_m = dict_keys(dict_read(pn_model, "model"))
  348. output("List of all elements:")
  349. Element v_m
  350. while (integer_gt(read_nr_out(keys_m), 0)):
  351. v_m = set_pop(keys_m)
  352. // Filter out anonymous objects
  353. typename = getName(dict_read(pn_model, "metamodel"), dict_read_node(dict_read(pn_model, "type_mapping"), dict_read(dict_read(pn_model, "model"), v_m)))
  354. if (bool_not(string_startswith(v_m, "__"))):
  355. output(string_join(string_join(string_join(" ", v_m), " : "), typename))
  356. elif (string_eq(cmd, "read")):
  357. output("Element to read?")
  358. cmd = input()
  359. if (dict_in(dict_read(pn_model, "model"), cmd)):
  360. Element read_elem
  361. read_elem = dict_read(dict_read(pn_model, "model"), cmd)
  362. metamodel_element_pn = dict_read_node(dict_read(pn_model, "type_mapping"), read_elem)
  363. output(string_join("Name: ", cmd))
  364. output(string_join("Type: ", getName(dict_read(pn_model, "metamodel"), metamodel_element_pn)))
  365. if (is_edge(read_elem)):
  366. output(string_join("Source: ", getName(pn_model, read_edge_src(read_elem))))
  367. output(string_join("Destination: ", getName(pn_model, read_edge_dst(read_elem))))
  368. if (string_neq(cast_v2s(read_elem), "None")):
  369. output(string_join("Value: ", cast_v2s(read_elem)))
  370. output("Defines attributes:")
  371. attr_list_pn = getInstantiatableAttributes(pn_model, read_elem)
  372. attr_keys_pn = dict_keys(attr_list_pn)
  373. while (integer_lt(0, read_nr_out(attr_keys_pn))):
  374. attr_key_pn = set_pop(attr_keys_pn)
  375. output(string_join(string_join(string_join(" ", attr_key_pn), " : "), cast_t2s(dict_read(attr_list_pn, attr_key_pn))))
  376. output("Attributes:")
  377. attr_list_pn = getAttributeList(pn_model, metamodel_element_pn)
  378. attr_keys_pn = dict_keys(attr_list_pn)
  379. while (integer_lt(0, read_nr_out(attr_keys_pn))):
  380. attr_key_pn = set_pop(attr_keys_pn)
  381. output(string_join(string_join(string_join(string_join(string_join(" ", attr_key_pn), " : "), cast_t2s(dict_read(attr_list_pn, attr_key_pn))), " = "), cast_v2s(readAttribute(pn_model, read_elem, attr_key_pn))))
  382. else:
  383. output("Unknown element; aborting")
  384. elif (string_eq(cmd, "verify")):
  385. output(conformance_scd(pn_model))
  386. elif (string_eq(cmd, "types")):
  387. Element keys_t
  388. keys_t = dict_keys(dict_read(dict_read(pn_model, "metamodel"), "model"))
  389. output("List of types:")
  390. Element v_t
  391. while (integer_gt(read_nr_out(keys_t), 0)):
  392. v_t = set_pop(keys_t)
  393. if (bool_not(string_startswith(v_t, "__"))):
  394. output(string_join(string_join(string_join(" ", v_t), " : "), getName(dict_read(dict_read(pn_model, "metamodel"), "metamodel"), dict_read_node(dict_read(dict_read(pn_model, "metamodel"), "type_mapping"), dict_read(dict_read(dict_read(pn_model, "metamodel"), "model"), v_t)))))
  395. elif (string_eq(cmd, "retype")):
  396. output("Element to retype?")
  397. String elementname
  398. elementname = input()
  399. if (dict_in(dict_read(pn_model, "model"), elementname)):
  400. output("New type")
  401. typename = input()
  402. if (dict_in(dict_read(dict_read(pn_model, "metamodel"), "model"), typename)):
  403. // OK, do the retyping
  404. // First try removing the previous type if it exists
  405. dict_delete(dict_read(pn_model, "type_mapping"), dict_read(dict_read(pn_model, "model"), elementname))
  406. // Now add the new type
  407. dict_add(dict_read(pn_model, "type_mapping"), dict_read(dict_read(pn_model, "model"), elementname), dict_read(dict_read(dict_read(pn_model, "metamodel"), "model"), typename))
  408. output("Retyped!")
  409. else:
  410. output("Unknown type; aborting")
  411. else:
  412. output("Unknown element; aborting")
  413. elif (string_eq(cmd, "switch")):
  414. bottom = bool_not(bottom)
  415. Element tmp_model
  416. tmp_model = pn_model
  417. pn_model = other_metamodel
  418. other_metamodel = tmp_model
  419. if (bottom):
  420. // The type mapping we are using is probably not complete for our model
  421. // so we completely recreate it from the model we have.
  422. output("Switching to conformance bottom mode!")
  423. generate_bottom_type_mapping(pn_model)
  424. else:
  425. // We already switched the models and such, so we are already done!
  426. output("Switching to linguistic metamodel!")
  427. elif (bool_and(dict_in(pn_operations(), cmd), bool_not(bottom))):
  428. // A model-specific operation, so execute that one
  429. Element specific_op
  430. specific_op = dict_read(pn_operations(), cmd)
  431. specific_op(pn_model)
  432. else:
  433. output("Unknown command; aborting")
  434. output("Use command 'help' to get a list of available commands")
  435. Element function initial_prompt():
  436. output("Welcome to the Model Management Interface, running live on the Modelverse!")
  437. output("Use 'help' command for a list of possible commands")
  438. String command
  439. Element root
  440. Element metamodel
  441. String name
  442. Element my_model
  443. String mm_name
  444. create_metamodels()
  445. dict_add(dict_read(read_root(), "__hierarchy"), "models", create_node())
  446. root = dict_read(dict_read(read_root(), "__hierarchy"), "models")
  447. while (True):
  448. output("Please give your command.")
  449. command = input()
  450. if (string_eq(command, "help")):
  451. output("Currently no model is loaded, so your operations are limited to:")
  452. output(" new -- Create a new model and save it for future use")
  453. output(" load -- Load a previously made model")
  454. output(" rename -- Rename a previously made model")
  455. output(" delete -- Delete a previously made model")
  456. output(" list -- Show a list of all stored models")
  457. output(" help -- Show a list of possible commands")
  458. elif (string_eq(command, "new")):
  459. output("Metamodel to instantiate?")
  460. mm_name = input()
  461. if (dict_in(root, mm_name)):
  462. output("Name of model?")
  463. name = input()
  464. if (dict_in(root, name)):
  465. output("Model exists; aborting")
  466. else:
  467. my_model = instantiate_new_model(dict_read(root, mm_name), create_node())
  468. dict_add(root, name, my_model)
  469. petrinet_loaded(my_model)
  470. else:
  471. output("Unknown metamodel; aborting")
  472. elif (string_eq(command, "load")):
  473. output("Model to load?")
  474. name = input()
  475. if (dict_in(root, name)):
  476. my_model = dict_read(root, name)
  477. petrinet_loaded(my_model)
  478. else:
  479. output("Model not found; aborting")
  480. elif (string_eq(command, "list")):
  481. Element keys
  482. keys = dict_keys(root)
  483. output("Found models:")
  484. while (integer_gt(read_nr_out(keys), 0)):
  485. Element m_menu_list
  486. m_menu_list = set_pop(keys)
  487. output(string_join(string_join(string_join(" ", m_menu_list), " : "), reverseNameLookup(root, dict_read(dict_read(root, m_menu_list), "metamodel"))))
  488. elif (string_eq(command, "delete")):
  489. output("Model to delete?")
  490. name = input()
  491. if (dict_in(root, name)):
  492. dict_delete(root, name)
  493. output("Deleted!")
  494. else:
  495. output("Model not found; aborting")
  496. elif (string_eq(command, "rename")):
  497. output("Old name?")
  498. String old_name
  499. old_name = input()
  500. if (dict_in(root, old_name)):
  501. output("New name?")
  502. String new_name
  503. new_name = input()
  504. if (dict_in(root, new_name)):
  505. output("Model exists; aborting")
  506. else:
  507. dict_add(root, new_name, dict_read(root, old_name))
  508. dict_delete(root, old_name)
  509. output("Rename complete!")
  510. else:
  511. output("Model not found; aborting")
  512. else:
  513. output("Command not recognized, use 'help' for a list of possible commands")
  514. Void function main():
  515. initial_prompt()