pn_interface.alc 15 KB

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