pn_interface.alc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 (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 "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 (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 "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 (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 "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 (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 (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 (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 (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 (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(((" " + 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 (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(((" " + 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. String 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 (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. String specific_op
  244. while (0 < dict_len(specific_ops)):
  245. specific_op = set_pop(specific_ops)
  246. output(" " + specific_op)
  247. elif (cmd == "exit"):
  248. return pn_model
  249. elif (cmd == "instantiate"):
  250. String mm_type_name
  251. output("Type to instantiate?")
  252. mm_type_name = input()
  253. if (dict_in(dict_read(dict_read(pn_model, "metamodel"), "model"), mm_type_name)):
  254. metamodel_element_pn = dict_read(dict_read(dict_read(pn_model, "metamodel"), "model"), mm_type_name)
  255. String element_name
  256. output("Name of new element?")
  257. element_name = input()
  258. if (dict_in(dict_read(pn_model, "model"), element_name)):
  259. output("Element already exists; aborting")
  260. else:
  261. Boolean cnt
  262. cnt = True
  263. Element additional_opts
  264. additional_opts = create_node()
  265. if (is_edge(metamodel_element_pn)):
  266. output("Source name?")
  267. String src_name
  268. src_name = input()
  269. if (dict_in(dict_read(pn_model, "model"), src_name)):
  270. list_append(additional_opts, dict_read(dict_read(pn_model, "model"), src_name))
  271. output("Destination name?")
  272. String dst_name
  273. dst_name = input()
  274. if (dict_in(dict_read(pn_model, "model"), dst_name)):
  275. list_append(additional_opts, dict_read(dict_read(pn_model, "model"), dst_name))
  276. else:
  277. output("Unknown destination; aborting")
  278. cnt = False
  279. else:
  280. output("Unknown source; aborting")
  281. cnt = False
  282. if (type_eq(typeof(metamodel_element_pn), Type)):
  283. // It is a value
  284. output("Value?")
  285. list_append(additional_opts, input())
  286. if (cnt):
  287. attr_list_pn = getAttributeList(pn_model, metamodel_element_pn)
  288. attr_keys_pn = dict_keys(attr_list_pn)
  289. Element attrs
  290. attrs = create_node()
  291. while (0 < read_nr_out(attr_keys_pn)):
  292. attr_key_pn = set_pop(attr_keys_pn)
  293. output(((attr_key_pn + " : ") + cast_t2s(dict_read(attr_list_pn, attr_key_pn))) + "?")
  294. dict_add(attrs, attr_key_pn, input())
  295. Element resulting_element
  296. resulting_element = instantiate_model_lib(pn_model, metamodel_element_pn, element_name, additional_opts, create_node(), attrs)
  297. output("Instantiation successful!")
  298. else:
  299. output("Unknown type specified; aborting")
  300. elif (cmd == "delete"):
  301. output("What is the name of the element you want to delete?")
  302. cmd = input()
  303. if (dict_in(dict_read(pn_model, "model"), cmd)):
  304. delete_element(dict_read(dict_read(pn_model, "model"), cmd))
  305. output("Deleted!")
  306. else:
  307. output("No such element; aborting")
  308. elif (cmd == "modify"):
  309. output("Element to modify?")
  310. cmd = input()
  311. if (dict_in(dict_read(pn_model, "model"), cmd)):
  312. Element mod
  313. mod = dict_read(dict_read(pn_model, "model"), cmd)
  314. metamodel_element_pn = dict_read_node(dict_read(pn_model, "type_mapping"), mod)
  315. attr_list_pn = getAttributeList(pn_model, metamodel_element_pn)
  316. attr_keys_pn = dict_keys(attr_list_pn)
  317. while (0 < read_nr_out(attr_keys_pn)):
  318. attr_key_pn = set_pop(attr_keys_pn)
  319. output(((" " + attr_key_pn) + " : ") + cast_t2s(dict_read(attr_list_pn, attr_key_pn)))
  320. output("Attribute to modify?")
  321. String attr_name
  322. attr_name = input()
  323. if (set_in(dict_keys(getAttributeList(pn_model, metamodel_element_pn)), attr_name)):
  324. output("New value?")
  325. setAttribute(pn_model, mod, attr_name, input())
  326. output("Modified!")
  327. else:
  328. output("Unknown attribute; aborting")
  329. else:
  330. output("Element does not exist; aborting")
  331. elif (cmd == "rename"):
  332. output("Old name?")
  333. String old_name_e
  334. old_name_e = input()
  335. if (dict_in(dict_read(pn_model, "model"), old_name_e)):
  336. output("New name?")
  337. String new_name_e
  338. new_name_e = input()
  339. if (dict_in(dict_read(pn_model, "model"), new_name_e)):
  340. output("New name already used; aborting")
  341. else:
  342. dict_add(dict_read(pn_model, "model"), new_name_e, dict_read(dict_read(pn_model, "model"), old_name_e))
  343. dict_delete(dict_read(pn_model, "model"), old_name_e)
  344. output("Rename complete!")
  345. else:
  346. output("Unknown element; aborting")
  347. elif (cmd == "list"):
  348. Element keys_m
  349. keys_m = dict_keys(dict_read(pn_model, "model"))
  350. output("List of all elements:")
  351. String v_m
  352. while (read_nr_out(keys_m) > 0):
  353. v_m = set_pop(keys_m)
  354. // Filter out anonymous objects
  355. 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)))
  356. if (bool_not(string_startswith(v_m, "__"))):
  357. output(((" " + v_m) + " : ") + typename)
  358. elif (cmd == "read"):
  359. output("Element to read?")
  360. cmd = input()
  361. if (dict_in(dict_read(pn_model, "model"), cmd)):
  362. Element read_elem
  363. read_elem = dict_read(dict_read(pn_model, "model"), cmd)
  364. metamodel_element_pn = dict_read_node(dict_read(pn_model, "type_mapping"), read_elem)
  365. output("Name: " + cmd)
  366. output("Type: " + getName(dict_read(pn_model, "metamodel"), metamodel_element_pn))
  367. if (is_edge(read_elem)):
  368. output("Source: " + getName(pn_model, read_edge_src(read_elem)))
  369. output("Destination: " + getName(pn_model, read_edge_dst(read_elem)))
  370. if (cast_v2s(read_elem) != "None"):
  371. output("Value: " + cast_v2s(read_elem))
  372. output("Defines attributes:")
  373. attr_list_pn = getInstantiatableAttributes(pn_model, read_elem)
  374. attr_keys_pn = dict_keys(attr_list_pn)
  375. while (0 < read_nr_out(attr_keys_pn)):
  376. attr_key_pn = set_pop(attr_keys_pn)
  377. output((((" " + attr_key_pn) + " : ") + cast_t2s(dict_read(attr_list_pn, attr_key_pn))))
  378. output("Attributes:")
  379. attr_list_pn = getAttributeList(pn_model, metamodel_element_pn)
  380. attr_keys_pn = dict_keys(attr_list_pn)
  381. while (0 < read_nr_out(attr_keys_pn)):
  382. attr_key_pn = set_pop(attr_keys_pn)
  383. output(((((" " + attr_key_pn) + " : ") + cast_t2s(dict_read(attr_list_pn, attr_key_pn))) + " = ") + cast_v2s(readAttribute(pn_model, read_elem, attr_key_pn)))
  384. else:
  385. output("Unknown element; aborting")
  386. elif (cmd == "verify"):
  387. output(conformance_scd(pn_model))
  388. elif (cmd == "types"):
  389. Element keys_t
  390. keys_t = dict_keys(dict_read(dict_read(pn_model, "metamodel"), "model"))
  391. output("List of types:")
  392. String v_t
  393. while (read_nr_out(keys_t) > 0):
  394. v_t = set_pop(keys_t)
  395. if (bool_not(string_startswith(v_t, "__"))):
  396. output(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)))))
  397. elif (cmd == "retype"):
  398. output("Element to retype?")
  399. String elementname
  400. elementname = input()
  401. if (dict_in(dict_read(pn_model, "model"), elementname)):
  402. output("New type")
  403. typename = input()
  404. if (dict_in(dict_read(dict_read(pn_model, "metamodel"), "model"), typename)):
  405. // OK, do the retyping
  406. // First try removing the previous type if it exists
  407. dict_delete(dict_read(pn_model, "type_mapping"), dict_read(dict_read(pn_model, "model"), elementname))
  408. // Now add the new type
  409. 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))
  410. output("Retyped!")
  411. else:
  412. output("Unknown type; aborting")
  413. else:
  414. output("Unknown element; aborting")
  415. elif (cmd == "switch"):
  416. bottom = bool_not(bottom)
  417. Element tmp_model
  418. tmp_model = pn_model
  419. pn_model = other_metamodel
  420. other_metamodel = tmp_model
  421. if (bottom):
  422. // The type mapping we are using is probably not complete for our model
  423. // so we completely recreate it from the model we have.
  424. output("Switching to conformance bottom mode!")
  425. generate_bottom_type_mapping(pn_model)
  426. else:
  427. // We already switched the models and such, so we are already done!
  428. output("Switching to linguistic metamodel!")
  429. elif (bool_and(dict_in(pn_operations(), cmd), bool_not(bottom))):
  430. // A model-specific operation, so execute that one
  431. Element specific_op
  432. specific_op = dict_read(pn_operations(), cmd)
  433. specific_op(pn_model)
  434. else:
  435. output("Unknown command; aborting")
  436. output("Use command 'help' to get a list of available commands")
  437. Element function initial_prompt():
  438. output("Welcome to the Model Management Interface, running live on the Modelverse!")
  439. output("Use 'help' command for a list of possible commands")
  440. String command
  441. Element root
  442. Element metamodel
  443. String name
  444. Element my_model
  445. String mm_name
  446. create_metamodels()
  447. dict_add(dict_read(read_root(), "__hierarchy"), "models", create_node())
  448. root = dict_read(dict_read(read_root(), "__hierarchy"), "models")
  449. while (True):
  450. output("Please give your command.")
  451. command = input()
  452. if (command == "help"):
  453. output("Currently no model is loaded, so your operations are limited to:")
  454. output(" new -- Create a new model and save it for future use")
  455. output(" load -- Load a previously made model")
  456. output(" rename -- Rename a previously made model")
  457. output(" delete -- Delete a previously made model")
  458. output(" list -- Show a list of all stored models")
  459. output(" help -- Show a list of possible commands")
  460. elif (command == "new"):
  461. output("Metamodel to instantiate?")
  462. mm_name = input()
  463. if (dict_in(root, mm_name)):
  464. output("Name of model?")
  465. name = input()
  466. if (dict_in(root, name)):
  467. output("Model exists; aborting")
  468. else:
  469. my_model = instantiate_new_model(dict_read(root, mm_name), create_node())
  470. dict_add(root, name, my_model)
  471. petrinet_loaded(my_model)
  472. else:
  473. output("Unknown metamodel; aborting")
  474. elif (command == "load"):
  475. output("Model to load?")
  476. name = input()
  477. if (dict_in(root, name)):
  478. my_model = dict_read(root, name)
  479. petrinet_loaded(my_model)
  480. else:
  481. output("Model not found; aborting")
  482. elif (command == "list"):
  483. Element keys
  484. keys = dict_keys(root)
  485. output("Found models:")
  486. String m_menu_list
  487. while (read_nr_out(keys) > 0):
  488. m_menu_list = set_pop(keys)
  489. output(((" " + m_menu_list) + " : ") + reverseNameLookup(root, dict_read(dict_read(root, m_menu_list), "metamodel")))
  490. elif (command == "delete"):
  491. output("Model to delete?")
  492. name = input()
  493. if (dict_in(root, name)):
  494. dict_delete(root, name)
  495. output("Deleted!")
  496. else:
  497. output("Model not found; aborting")
  498. elif (command == "rename"):
  499. output("Old name?")
  500. String old_name
  501. old_name = input()
  502. if (dict_in(root, old_name)):
  503. output("New name?")
  504. String new_name
  505. new_name = input()
  506. if (dict_in(root, new_name)):
  507. output("Model exists; aborting")
  508. else:
  509. dict_add(root, new_name, dict_read(root, old_name))
  510. dict_delete(root, old_name)
  511. output("Rename complete!")
  512. else:
  513. output("Model not found; aborting")
  514. else:
  515. output("Command not recognized, use 'help' for a list of possible commands")
  516. Void function main():
  517. initial_prompt()