pn_interface.alc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. include "metamodels.alh"
  8. include "modelling.alh"
  9. Element function pn_operations():
  10. Element ops
  11. ops = create_node()
  12. dict_add(ops, "fire", petrinet_fire)
  13. dict_add(ops, "enabled", petrinet_enabled)
  14. return ops
  15. Element function petrinet_enabled(model : Element):
  16. Element set_enabled
  17. set_enabled = petrinet_enabled_set(model)
  18. output("Enabled transitions:")
  19. while (0 < read_nr_out(set_enabled)):
  20. output(getName(model, set_pop(set_enabled)))
  21. return model
  22. Element function petrinet_enabled_set(model : Element):
  23. Element all_transitions
  24. all_transitions = allInstances(model, model["metamodel"]["model"]["Transition"])
  25. Element enabled_transitions
  26. enabled_transitions = create_node()
  27. Element under_study
  28. Element in_arcs
  29. Element arc_under_study
  30. Boolean enabled
  31. while (0 < read_nr_out(all_transitions)):
  32. under_study = set_pop(all_transitions)
  33. enabled = True
  34. // Find all incoming transitions
  35. in_arcs = allIncomingAssociationInstances(model, under_study, model["metamodel"]["model"]["P2T"])
  36. while (0 < read_nr_out(in_arcs)):
  37. arc_under_study = set_pop(in_arcs)
  38. Integer present_tokens
  39. Integer required_tokens
  40. required_tokens = readAttribute(model, arc_under_study, "weight")
  41. present_tokens = readAttribute(model, read_edge_src(arc_under_study), "tokens")
  42. if (present_tokens < required_tokens):
  43. // Less tokens than required, so disable the transition completely
  44. enabled = False
  45. if (enabled):
  46. set_add(enabled_transitions, under_study)
  47. return enabled_transitions
  48. Element function petrinet_fire(model : Element):
  49. output("Transition to fire?")
  50. Element transition
  51. transition = input()
  52. if (dict_in(model["model"], transition)):
  53. transition = model["model"][transition]
  54. if (set_in(petrinet_enabled_set(model), transition)):
  55. Element workset
  56. Element working_place
  57. Element working_arc
  58. Integer new_value
  59. // Consume tokens
  60. workset = allIncomingAssociationInstances(model, transition, model["metamodel"]["model"]["P2T"])
  61. while (0 < read_nr_out(workset)):
  62. working_arc = set_pop(workset)
  63. working_place = read_edge_src(working_arc)
  64. new_value = integer_subtraction(readAttribute(model, working_place, "tokens"), readAttribute(model, working_arc, "weight"))
  65. unset_attribute(model, getName(model, working_place), "tokens")
  66. instantiate_attribute(model, getName(model, working_place), "tokens", new_value)
  67. //setAttribute(model, working_place, "tokens", integer_subtraction(readAttribute(model, working_place, "tokens"), readAttribute(model, working_arc, "weight")))
  68. output(((" " + getName(model, working_place)) + ": ") + cast_i2s(readAttribute(model, working_place, "tokens")))
  69. // Add tokens
  70. workset = allOutgoingAssociationInstances(model, transition, model["metamodel"]["model"]["T2P"])
  71. while (0 < read_nr_out(workset)):
  72. working_arc = set_pop(workset)
  73. working_place = read_edge_dst(working_arc)
  74. new_value = integer_addition(readAttribute(model, working_place, "tokens"), readAttribute(model, working_arc, "weight"))
  75. unset_attribute(model, getName(model, working_place), "tokens")
  76. instantiate_attribute(model, getName(model, working_place), "tokens", new_value)
  77. //setAttribute(model, working_place, "tokens", integer_addition(readAttribute(model, working_place, "tokens"), readAttribute(model, working_arc, "weight")))
  78. output(((" " + getName(model, working_place)) + ": ") + cast_i2s(readAttribute(model, working_place, "tokens")))
  79. output("Transition fired!")
  80. else:
  81. output("Cannot fire if not enabled; aborting")
  82. else:
  83. output("Unknown transition; aborting")
  84. return model
  85. Element function petrinet_loaded(model : Element):
  86. String cmd
  87. Element attr_list_pn
  88. Element attr_keys_pn
  89. String attr_key_pn
  90. Element metamodel_element_pn
  91. String typename
  92. Boolean bottom
  93. Element other_metamodel
  94. bottom = False
  95. other_metamodel = create_node()
  96. dict_add(other_metamodel, "model", model["model"])
  97. dict_add(other_metamodel, "type_mapping", create_node())
  98. dict_add(other_metamodel, "metamodel", import_node("models/LTM_bottom"))
  99. dict_add(other_metamodel, "inheritance", other_metamodel["metamodel"]["model"]["__Inheritance"])
  100. output("Model loaded, ready for commands!")
  101. output("Use 'help' command for a list of possible commands")
  102. while (True):
  103. output("Please give your command.")
  104. cmd = input()
  105. if (cmd == "help"):
  106. output("Generic model operations:")
  107. output(" instantiate -- Create a new model element")
  108. output(" delete -- Delete an existing element")
  109. output(" modify -- Modify attribute of an existing element")
  110. output(" rename -- Rename an existing element")
  111. output(" list -- Prints the list of elements in the model")
  112. output(" types -- Prints the list of elements that can be instantiated")
  113. output(" read -- Prints the current state of a model element")
  114. output(" verify -- Check whether the model conforms to the metamodel")
  115. output(" retype -- Change the type of an element")
  116. output(" switch -- Switch between conformance bottom and the linguistic metamodel")
  117. output(" exit -- Unload the model and go back to the loading prompt")
  118. if (bool_not(bottom)):
  119. output("Model-specific operations:")
  120. Element specific_ops
  121. specific_ops = dict_keys(pn_operations())
  122. String specific_op
  123. while (0 < dict_len(specific_ops)):
  124. specific_op = set_pop(specific_ops)
  125. output(" " + specific_op)
  126. elif (cmd == "exit"):
  127. return model
  128. elif (cmd == "instantiate"):
  129. String mm_type_name
  130. output("Type to instantiate?")
  131. mm_type_name = input()
  132. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  133. metamodel_element_pn = model["metamodel"]["model"][mm_type_name]
  134. String element_name
  135. output("Name of new element?")
  136. element_name = input()
  137. if (dict_in(model["model"], element_name)):
  138. output("Element already exists; aborting")
  139. else:
  140. Boolean cnt
  141. cnt = True
  142. Element additional_opts
  143. additional_opts = create_node()
  144. if (is_edge(metamodel_element_pn)):
  145. output("Source name?")
  146. String src_name
  147. src_name = input()
  148. if (dict_in(model["model"], src_name)):
  149. list_append(additional_opts, model["model"][src_name])
  150. output("Destination name?")
  151. String dst_name
  152. dst_name = input()
  153. if (dict_in(model["model"], dst_name)):
  154. list_append(additional_opts, model["model"][dst_name])
  155. else:
  156. output("Unknown destination; aborting")
  157. cnt = False
  158. else:
  159. output("Unknown source; aborting")
  160. cnt = False
  161. if (has_value(metamodel_element_pn)):
  162. // It is a value
  163. output("Value?")
  164. list_append(additional_opts, input())
  165. if (cnt):
  166. //attr_list_pn = getAttributeList(model, metamodel_element_pn)
  167. //attr_keys_pn = dict_keys(attr_list_pn)
  168. //Element attrs
  169. //attrs = create_node()
  170. //while (0 < read_nr_out(attr_keys_pn)):
  171. // attr_key_pn = set_pop(attr_keys_pn)
  172. // output(((attr_key_pn + " : ") + cast_e2s(attr_list_pn[attr_key_pn])) + "?")
  173. // dict_add(attrs, attr_key_pn, input())
  174. //Element resulting_element
  175. //resulting_element = instantiate_model_lib(model, metamodel_element_pn, element_name, additional_opts, create_node(), attrs)
  176. output("Instantiation successful!")
  177. else:
  178. output("Unknown type specified; aborting")
  179. elif (cmd == "delete"):
  180. output("What is the name of the element you want to delete?")
  181. cmd = input()
  182. if (dict_in(model["model"], cmd)):
  183. delete_element(model["model"][cmd])
  184. output("Deleted!")
  185. else:
  186. output("No such element; aborting")
  187. elif (cmd == "modify"):
  188. output("Element to modify?")
  189. cmd = input()
  190. if (dict_in(model["model"], cmd)):
  191. Element mod
  192. mod = model["model"][cmd]
  193. metamodel_element_pn = dict_read_node(model["type_mapping"], mod)
  194. attr_list_pn = getAttributeList(model, metamodel_element_pn)
  195. attr_keys_pn = dict_keys(attr_list_pn)
  196. while (0 < read_nr_out(attr_keys_pn)):
  197. attr_key_pn = set_pop(attr_keys_pn)
  198. output(((" " + attr_key_pn) + " : ") + cast_e2s(attr_list_pn[attr_key_pn]))
  199. output("Attribute to modify?")
  200. String attr_name
  201. attr_name = input()
  202. if (set_in(dict_keys(getAttributeList(model, metamodel_element_pn)), attr_name)):
  203. output("New value?")
  204. //setAttribute(model, mod, attr_name, input())
  205. output("Modified!")
  206. else:
  207. output("Unknown attribute; aborting")
  208. else:
  209. output("Element does not exist; aborting")
  210. elif (cmd == "rename"):
  211. output("Old name?")
  212. String old_name_e
  213. old_name_e = input()
  214. if (dict_in(model["model"], old_name_e)):
  215. output("New name?")
  216. String new_name_e
  217. new_name_e = input()
  218. if (dict_in(model["model"], new_name_e)):
  219. output("New name already used; aborting")
  220. else:
  221. dict_add(model["model"], new_name_e, model["model"][old_name_e])
  222. dict_delete(model["model"], old_name_e)
  223. output("Rename complete!")
  224. else:
  225. output("Unknown element; aborting")
  226. elif (cmd == "list"):
  227. Element keys_m
  228. keys_m = dict_keys(model["model"])
  229. output("List of all elements:")
  230. String v_m
  231. while (read_nr_out(keys_m) > 0):
  232. v_m = set_pop(keys_m)
  233. // Filter out anonymous objects
  234. typename = getName(model["metamodel"], dict_read_node(model["type_mapping"], model["model"][v_m]))
  235. if (bool_not(string_startswith(v_m, "__"))):
  236. output(((" " + v_m) + " : ") + typename)
  237. elif (cmd == "read"):
  238. output("Element to read?")
  239. cmd = input()
  240. if (dict_in(model["model"], cmd)):
  241. Element read_elem
  242. read_elem = model["model"][cmd]
  243. metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
  244. output("Name: " + cmd)
  245. output("Type: " + getName(model["metamodel"], metamodel_element_pn))
  246. if (is_edge(read_elem)):
  247. output("Source: " + getName(model, read_edge_src(read_elem)))
  248. output("Destination: " + getName(model, read_edge_dst(read_elem)))
  249. if (cast_v2s(read_elem) != "None"):
  250. output("Value: " + cast_v2s(read_elem))
  251. output("Defines attributes:")
  252. attr_list_pn = getInstantiatableAttributes(model, read_elem)
  253. attr_keys_pn = dict_keys(attr_list_pn)
  254. while (0 < read_nr_out(attr_keys_pn)):
  255. attr_key_pn = set_pop(attr_keys_pn)
  256. output((((" " + attr_key_pn) + " : ") + cast_e2s(attr_list_pn[attr_key_pn])))
  257. output("Attributes:")
  258. attr_list_pn = getAttributeList(model, metamodel_element_pn)
  259. attr_keys_pn = dict_keys(attr_list_pn)
  260. while (0 < read_nr_out(attr_keys_pn)):
  261. attr_key_pn = set_pop(attr_keys_pn)
  262. output(((((" " + attr_key_pn) + " : ") + cast_e2s(attr_list_pn[attr_key_pn])) + " = ") + cast_v2s(readAttribute(model, read_elem, attr_key_pn)))
  263. else:
  264. output("Unknown element; aborting")
  265. elif (cmd == "verify"):
  266. output(conformance_scd(model))
  267. elif (cmd == "types"):
  268. Element keys_t
  269. keys_t = dict_keys(model["metamodel"]["model"])
  270. output("List of types:")
  271. String v_t
  272. while (read_nr_out(keys_t) > 0):
  273. v_t = set_pop(keys_t)
  274. if (bool_not(string_startswith(v_t, "__"))):
  275. output(string_join((" " + v_t) + " : ", getName(model["metamodel"]["metamodel"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
  276. elif (cmd == "retype"):
  277. output("Element to retype?")
  278. String elementname
  279. elementname = input()
  280. if (dict_in(model["model"], elementname)):
  281. output("New type")
  282. typename = input()
  283. if (dict_in(model["metamodel"]["model"], typename)):
  284. // OK, do the retyping
  285. // First try removing the previous type if it exists
  286. dict_delete(model["type_mapping"], model["model"][elementname])
  287. // Now add the new type
  288. dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename])
  289. output("Retyped!")
  290. else:
  291. output("Unknown type; aborting")
  292. else:
  293. output("Unknown element; aborting")
  294. elif (cmd == "switch"):
  295. bottom = bool_not(bottom)
  296. Element tmp_model
  297. tmp_model = model
  298. model = other_metamodel
  299. other_metamodel = tmp_model
  300. if (bottom):
  301. // The type mapping we are using is probably not complete for our model
  302. // so we completely recreate it from the model we have.
  303. output("Switching to conformance bottom mode!")
  304. generate_bottom_type_mapping(model)
  305. else:
  306. // We already switched the models and such, so we are already done!
  307. output("Switching to linguistic metamodel!")
  308. elif (bool_and(dict_in(pn_operations(), cmd), bool_not(bottom))):
  309. // A model-specific operation, so execute that one
  310. Element specific_op
  311. specific_op = dict_read(pn_operations(), cmd)
  312. specific_op(model)
  313. else:
  314. output("Unknown command; aborting")
  315. output("Use command 'help' to get a list of available commands")
  316. Element function initial_prompt():
  317. output("Welcome to the Model Management Interface, running live on the Modelverse!")
  318. output("Use 'help' command for a list of possible commands")
  319. String command
  320. Element root
  321. Element metamodel
  322. String name
  323. Element my_model
  324. String mm_name
  325. root = create_metamodels()
  326. while (True):
  327. output("Please give your command.")
  328. command = input()
  329. if (command == "help"):
  330. output("Currently no model is loaded, so your operations are limited to:")
  331. output(" new -- Create a new model and save it for future use")
  332. output(" load -- Load a previously made model")
  333. output(" rename -- Rename a previously made model")
  334. output(" delete -- Delete a previously made model")
  335. output(" list -- Show a list of all stored models")
  336. output(" help -- Show a list of possible commands")
  337. elif (command == "new"):
  338. output("Metamodel to instantiate?")
  339. mm_name = input()
  340. if (dict_in(root, mm_name)):
  341. output("Name of model?")
  342. name = input()
  343. if (dict_in(root, name)):
  344. output("Model exists; aborting")
  345. else:
  346. //my_model = instantiate_new_model(root[mm_name], create_node())
  347. //dict_add(root, name, my_model)
  348. petrinet_loaded(my_model)
  349. else:
  350. output("Unknown metamodel; aborting")
  351. elif (command == "load"):
  352. output("Model to load?")
  353. name = input()
  354. if (dict_in(root, name)):
  355. my_model = root[name]
  356. petrinet_loaded(my_model)
  357. else:
  358. output("Model not found; aborting")
  359. elif (command == "list"):
  360. Element keys
  361. String m_menu_list
  362. keys = dict_keys(root)
  363. output("Found models:")
  364. while (read_nr_out(keys) > 0):
  365. m_menu_list = set_pop(keys)
  366. output(((" " + m_menu_list) + " : ") + reverseNameLookup(root, root[m_menu_list]["metamodel"]))
  367. elif (command == "delete"):
  368. output("Model to delete?")
  369. name = input()
  370. if (dict_in(root, name)):
  371. dict_delete(root, name)
  372. output("Deleted!")
  373. else:
  374. output("Model not found; aborting")
  375. elif (command == "rename"):
  376. output("Old name?")
  377. String old_name
  378. old_name = input()
  379. if (dict_in(root, old_name)):
  380. output("New name?")
  381. String new_name
  382. new_name = input()
  383. if (dict_in(root, new_name)):
  384. output("Model exists; aborting")
  385. else:
  386. dict_add(root, new_name, root[old_name])
  387. dict_delete(root, old_name)
  388. output("Rename complete!")
  389. else:
  390. output("Model not found; aborting")
  391. else:
  392. output("Command not recognized, use 'help' for a list of possible commands")
  393. Void function main():
  394. initial_prompt()