pn_interface.alc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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", scd_model["Class"], scd_model["Type"])
  23. instantiate_bottom_edge(scd, "__Name", scd_model["Attribute"], scd_model["__String"])
  24. instantiate_bottom_edge(scd, "Association", scd_model["Class"], scd_model["Class"])
  25. instantiate_bottom_edge(scd, "Inheritance", scd_model["Class"], scd_model["Class"])
  26. instantiate_bottom_edge(scd, "__name_edge", scd_model["__Name"], scd_model["__name"])
  27. instantiate_bottom_edge(scd, "__inh_1", scd_model["Association"], scd_model["Class"])
  28. instantiate_bottom_edge(scd, "__inh_2", scd_model["Attribute"], scd_model["Class"])
  29. instantiate_bottom_value(scd, "__attribute", "attribute")
  30. instantiate_bottom_edge(scd, "__attribute_edge", scd_model["Attribute"], 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, scd_model["Class"], scd_model["Class"])
  35. dict_add(mapping, scd_model["Type"], scd_model["Type"])
  36. dict_add(mapping, scd_model["__String"], scd_model["Type"])
  37. dict_add(mapping, scd_model["Attribute"], scd_model["Attribute"])
  38. dict_add(mapping, scd_model["__Name"], scd_model["Attribute"])
  39. dict_add(mapping, scd_model["Association"], scd_model["Association"])
  40. dict_add(mapping, scd_model["Inheritance"], scd_model["Association"])
  41. dict_add(mapping, scd_model["__inh_1"], scd_model["Inheritance"])
  42. dict_add(mapping, scd_model["__inh_2"], scd_model["Inheritance"])
  43. dict_add(mapping, scd_model["__name"], scd_model["__String"])
  44. dict_add(mapping, scd_model["__name_edge"], scd_model["__Name"])
  45. dict_add(mapping, scd_model["__attribute"], scd_model["__String"])
  46. dict_add(mapping, scd_model["__attribute_edge"], scd_model["__Name"])
  47. retype(scd, scd, 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 modeletamodel
  51. modeletamodel = instantiate_new_model(scd, scd_model["Inheritance"])
  52. Element tokens
  53. tokens = create_node()
  54. dict_add(tokens, "tokens", Integer)
  55. instantiate_model_lib(modeletamodel, scd_model["Class"], "Place", create_node(), tokens, create_node())
  56. instantiate_model_lib(modeletamodel, 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, modeletamodel["model"]["Place"])
  63. list_append(p2t_links, modeletamodel["model"]["Transition"])
  64. Element t2p_links
  65. t2p_links = create_node()
  66. list_append(t2p_links, modeletamodel["model"]["Transition"])
  67. list_append(t2p_links, modeletamodel["model"]["Place"])
  68. instantiate_model_lib(modeletamodel, scd_model["Association"], "P2T", p2t_links, weight, create_node())
  69. instantiate_model_lib(modeletamodel, scd_model["Association"], "T2P", t2p_links, weight, create_node())
  70. set_model_constraints(modeletamodel, petrinet_constraints)
  71. export_node("models/PetriNets", modeletamodel)
  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", mm_bottom_model["Node"], mm_bottom_model["Node"])
  86. instantiate_bottom_edge(mm_bottom, "__Inheritance", mm_bottom_model["Node"], mm_bottom_model["Node"])
  87. instantiate_bottom_edge(mm_bottom, "__inh_1", mm_bottom_model["Type"], mm_bottom_model["Node"])
  88. instantiate_bottom_edge(mm_bottom, "__inh_2", mm_bottom_model["Integer"], mm_bottom_model["Node"])
  89. instantiate_bottom_edge(mm_bottom, "__inh_3", mm_bottom_model["Action"], mm_bottom_model["Node"])
  90. instantiate_bottom_edge(mm_bottom, "__inh_4", mm_bottom_model["Float"], mm_bottom_model["Node"])
  91. instantiate_bottom_edge(mm_bottom, "__inh_5", mm_bottom_model["String"], mm_bottom_model["Node"])
  92. instantiate_bottom_edge(mm_bottom, "__inh_6", mm_bottom_model["Boolean"], mm_bottom_model["Node"])
  93. instantiate_bottom_edge(mm_bottom, "__inh_7", mm_bottom_model["Edge"], mm_bottom_model["Node"])
  94. // Retype it with itself
  95. mapping = create_node()
  96. dict_add(mapping, mm_bottom_model["Node"], mm_bottom_model["Node"])
  97. dict_add(mapping, mm_bottom_model["Type"], mm_bottom_model["Type"])
  98. dict_add(mapping, mm_bottom_model["Integer"], mm_bottom_model["Type"])
  99. dict_add(mapping, mm_bottom_model["Float"], mm_bottom_model["Type"])
  100. dict_add(mapping, mm_bottom_model["String"], mm_bottom_model["Type"])
  101. dict_add(mapping, mm_bottom_model["Boolean"], mm_bottom_model["Type"])
  102. dict_add(mapping, mm_bottom_model["Action"], mm_bottom_model["Type"])
  103. dict_add(mapping, mm_bottom_model["Edge"], mm_bottom_model["Edge"])
  104. dict_add(mapping, mm_bottom_model["__Inheritance"], mm_bottom_model["Edge"])
  105. dict_add(mapping, mm_bottom_model["__inh_1"], mm_bottom_model["__Inheritance"])
  106. dict_add(mapping, mm_bottom_model["__inh_2"], mm_bottom_model["__Inheritance"])
  107. dict_add(mapping, mm_bottom_model["__inh_3"], mm_bottom_model["__Inheritance"])
  108. dict_add(mapping, mm_bottom_model["__inh_4"], mm_bottom_model["__Inheritance"])
  109. dict_add(mapping, mm_bottom_model["__inh_5"], mm_bottom_model["__Inheritance"])
  110. dict_add(mapping, mm_bottom_model["__inh_6"], mm_bottom_model["__Inheritance"])
  111. dict_add(mapping, mm_bottom_model["__inh_7"], mm_bottom_model["__Inheritance"])
  112. retype(mm_bottom, mm_bottom, mm_bottom_model["__Inheritance"], mapping)
  113. export_node("models/LTM_bottom", mm_bottom)
  114. return dict_read(dict_read(read_root(), "__hierarchy"), "models")
  115. String function petrinet_constraints(model : Element):
  116. // Check places to have positive number of tokens
  117. Element all_elems
  118. Element elem_constraint
  119. all_elems = allInstances(model, model["metamodel"]["model"]["Place"])
  120. while (0 < read_nr_out(all_elems)):
  121. elem_constraint = set_pop(all_elems)
  122. if (integer_lt(readAttribute(model, elem_constraint, "tokens"), 0)):
  123. return "Negative number of tokens in Place " + getName(model, elem_constraint)
  124. // Check P2T transitions to have positive weight
  125. all_elems = allInstances(model, model["metamodel"]["model"]["P2T"])
  126. while (0 < read_nr_out(all_elems)):
  127. elem_constraint = set_pop(all_elems)
  128. if (integer_lt(readAttribute(model, elem_constraint, "weight"), 0)):
  129. return "Negative weight in arc " + getName(model, elem_constraint)
  130. // Check T2P transitions to have positive weight
  131. all_elems = allInstances(model, model["metamodel"]["model"]["T2P"])
  132. while (0 < read_nr_out(all_elems)):
  133. elem_constraint = set_pop(all_elems)
  134. if (integer_lt(readAttribute(model, elem_constraint, "weight"), 0)):
  135. return "Negative weight in arc " + getName(model, 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(model : Element):
  144. Element set_enabled
  145. set_enabled = petrinet_enabled_set(model)
  146. output("Enabled transitions:")
  147. while (0 < read_nr_out(set_enabled)):
  148. output(getName(model, set_pop(set_enabled)))
  149. return model
  150. Element function petrinet_enabled_set(model : Element):
  151. Element all_transitions
  152. all_transitions = allInstances(model, model["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(model, under_study, model["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(model, arc_under_study, "weight")
  169. present_tokens = readAttribute(model, 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(model : Element):
  177. output("Transition to fire?")
  178. Element transition
  179. transition = input()
  180. if (dict_in(model["model"], transition)):
  181. transition = model["model"][transition]
  182. if (set_in(petrinet_enabled_set(model), transition)):
  183. Element workset
  184. Element working_place
  185. Element working_arc
  186. // Consume tokens
  187. workset = allIncomingAssociationInstances(model, transition, model["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(model, working_place, "tokens", integer_subtraction(readAttribute(model, working_place, "tokens"), readAttribute(model, working_arc, "weight")))
  192. output(((" " + getName(model, working_place)) + ": ") + cast_i2s(readAttribute(model, working_place, "tokens")))
  193. // Add tokens
  194. workset = allOutgoingAssociationInstances(model, transition, model["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(model, working_place, "tokens", integer_addition(readAttribute(model, working_place, "tokens"), readAttribute(model, working_arc, "weight")))
  199. output(((" " + getName(model, working_place)) + ": ") + cast_i2s(readAttribute(model, 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 model
  206. Element function petrinet_loaded(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", 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", 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 model
  249. elif (cmd == "instantiate"):
  250. String mm_type_name
  251. output("Type to instantiate?")
  252. mm_type_name = input()
  253. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  254. metamodel_element_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(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(model["model"], src_name)):
  270. list_append(additional_opts, model["model"][src_name])
  271. output("Destination name?")
  272. String dst_name
  273. dst_name = input()
  274. if (dict_in(model["model"], dst_name)):
  275. list_append(additional_opts, 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(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(attr_list_pn[attr_key_pn])) + "?")
  294. dict_add(attrs, attr_key_pn, input())
  295. Element resulting_element
  296. resulting_element = instantiate_model_lib(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(model["model"], cmd)):
  304. delete_element(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(model["model"], cmd)):
  312. Element mod
  313. mod = model["model"][cmd]
  314. metamodel_element_pn = dict_read_node(model["type_mapping"], mod)
  315. attr_list_pn = getAttributeList(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(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(model, metamodel_element_pn)), attr_name)):
  324. output("New value?")
  325. setAttribute(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(model["model"], old_name_e)):
  336. output("New name?")
  337. String new_name_e
  338. new_name_e = input()
  339. if (dict_in(model["model"], new_name_e)):
  340. output("New name already used; aborting")
  341. else:
  342. dict_add(model["model"], new_name_e, model["model"][old_name_e])
  343. dict_delete(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(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(model["metamodel"], dict_read_node(model["type_mapping"], 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(model["model"], cmd)):
  362. Element read_elem
  363. read_elem = model["model"][cmd]
  364. metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
  365. output("Name: " + cmd)
  366. output("Type: " + getName(model["metamodel"], metamodel_element_pn))
  367. if (is_edge(read_elem)):
  368. output("Source: " + getName(model, read_edge_src(read_elem)))
  369. output("Destination: " + getName(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(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(attr_list_pn[attr_key_pn])))
  378. output("Attributes:")
  379. attr_list_pn = getAttributeList(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(attr_list_pn[attr_key_pn])) + " = ") + cast_v2s(readAttribute(model, read_elem, attr_key_pn)))
  384. else:
  385. output("Unknown element; aborting")
  386. elif (cmd == "verify"):
  387. output(conformance_scd(model))
  388. elif (cmd == "types"):
  389. Element keys_t
  390. keys_t = dict_keys(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(model["metamodel"]["metamodel"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
  397. elif (cmd == "retype"):
  398. output("Element to retype?")
  399. String elementname
  400. elementname = input()
  401. if (dict_in(model["model"], elementname)):
  402. output("New type")
  403. typename = input()
  404. if (dict_in(model["metamodel"]["model"], typename)):
  405. // OK, do the retyping
  406. // First try removing the previous type if it exists
  407. dict_delete(model["type_mapping"], model["model"][elementname])
  408. // Now add the new type
  409. dict_add(model["type_mapping"], model["model"][elementname], 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 = model
  419. 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(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(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. root = create_metamodels()
  447. while (True):
  448. output("Please give your command.")
  449. command = input()
  450. if (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 (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(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 (command == "load"):
  473. output("Model to load?")
  474. name = input()
  475. if (dict_in(root, name)):
  476. my_model = root[name]
  477. petrinet_loaded(my_model)
  478. else:
  479. output("Model not found; aborting")
  480. elif (command == "list"):
  481. Element keys
  482. String m_menu_list
  483. keys = dict_keys(root)
  484. output("Found models:")
  485. while (read_nr_out(keys) > 0):
  486. m_menu_list = set_pop(keys)
  487. output(((" " + m_menu_list) + " : ") + reverseNameLookup(root, root[m_menu_list]["metamodel"]))
  488. elif (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 (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, 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()