pn_interface.alc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. String element_name
  132. output("Name of new element?")
  133. element_name = input()
  134. if (dict_in(model["model"], element_name)):
  135. output("Element already exists; aborting")
  136. else:
  137. if (is_edge(metamodel_element_pn)):
  138. output("Source name?")
  139. String src_name
  140. src_name = input()
  141. if (dict_in(model["model"], src_name)):
  142. output("Destination name?")
  143. String dst_name
  144. dst_name = input()
  145. if (dict_in(model["model"], dst_name)):
  146. instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
  147. output("Instantiation successful!")
  148. if (dict_in(model["model"], element_name)):
  149. output("Instantiation successful!")
  150. else:
  151. output("Instantiation error!")
  152. else:
  153. output("Unknown destination; aborting")
  154. else:
  155. output("Unknown source; aborting")
  156. else:
  157. instantiate_node(model, mm_type_name, element_name)
  158. if (dict_in(model["model"], element_name)):
  159. output("Instantiation successful!")
  160. else:
  161. output("Instantiation error!")
  162. else:
  163. output("Unknown type specified; aborting")
  164. elif (cmd == "attr_add"):
  165. String model_name
  166. output("Which model do you want to assign an attribute to?")
  167. model_name = input()
  168. if (dict_in(model["model"], model_name)):
  169. Element attrs
  170. attrs = getAttributeList(model, model_name)
  171. output(print_dict(attrs))
  172. String attr_name
  173. output("Which attribute do you wish to assign?")
  174. attr_name = input()
  175. if (set_in(dict_keys(attrs), attr_name)):
  176. output("Value of attribute?")
  177. instantiate_attribute(model, model_name, attr_name, input())
  178. output("Added attribute!")
  179. else:
  180. output("No such attribute!")
  181. else:
  182. output("No such model!")
  183. elif (cmd == "delete"):
  184. output("What is the name of the element you want to delete?")
  185. cmd = input()
  186. if (dict_in(model["model"], cmd)):
  187. delete_element(model["model"][cmd])
  188. output("Deleted!")
  189. else:
  190. output("No such element; aborting")
  191. elif (cmd == "rename"):
  192. output("Old name?")
  193. String old_name_e
  194. old_name_e = input()
  195. if (dict_in(model["model"], old_name_e)):
  196. output("New name?")
  197. String new_name_e
  198. new_name_e = input()
  199. if (dict_in(model["model"], new_name_e)):
  200. output("New name already used; aborting")
  201. else:
  202. dict_add(model["model"], new_name_e, model["model"][old_name_e])
  203. dict_delete(model["model"], old_name_e)
  204. output("Rename complete!")
  205. else:
  206. output("Unknown element; aborting")
  207. elif (cmd == "list"):
  208. Element keys_m
  209. keys_m = dict_keys(model["model"])
  210. output("List of all elements:")
  211. String v_m
  212. while (read_nr_out(keys_m) > 0):
  213. v_m = set_pop(keys_m)
  214. // Filter out anonymous objects
  215. typename = getName(model["metamodel"], dict_read_node(model["type_mapping"], model["model"][v_m]))
  216. if (bool_not(string_startswith(v_m, "__"))):
  217. output(((" " + v_m) + " : ") + typename)
  218. elif (cmd == "read"):
  219. output("Element to read?")
  220. cmd = input()
  221. if (dict_in(model["model"], cmd)):
  222. Element read_elem
  223. read_elem = model["model"][cmd]
  224. metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
  225. output("Name: " + cmd)
  226. output("Type: " + getName(model["metamodel"], metamodel_element_pn))
  227. if (is_edge(read_elem)):
  228. output("Source: " + getName(model, read_edge_src(read_elem)))
  229. output("Destination: " + getName(model, read_edge_dst(read_elem)))
  230. if (cast_v2s(read_elem) != "None"):
  231. output("Value: " + cast_v2s(read_elem))
  232. output("Defines attributes:")
  233. attr_list_pn = getInstantiatableAttributes(model, read_elem)
  234. attr_keys_pn = dict_keys(attr_list_pn)
  235. while (0 < read_nr_out(attr_keys_pn)):
  236. attr_key_pn = set_pop(attr_keys_pn)
  237. output((((" " + attr_key_pn) + " : ") + cast_e2s(attr_list_pn[attr_key_pn])))
  238. output("Attributes:")
  239. attr_list_pn = getAttributeList(model, read_elem)
  240. attr_keys_pn = dict_keys(attr_list_pn)
  241. while (0 < read_nr_out(attr_keys_pn)):
  242. attr_key_pn = set_pop(attr_keys_pn)
  243. output(((((" " + attr_key_pn) + " : ") + cast_e2s(attr_list_pn[attr_key_pn])) + " = ") + cast_v2s(readAttribute(model, read_elem, attr_key_pn)))
  244. else:
  245. output("Unknown element; aborting")
  246. elif (cmd == "verify"):
  247. output(conformance_scd(model))
  248. elif (cmd == "types"):
  249. Element keys_t
  250. keys_t = dict_keys(model["metamodel"]["model"])
  251. output("List of types:")
  252. String v_t
  253. while (read_nr_out(keys_t) > 0):
  254. v_t = set_pop(keys_t)
  255. if (bool_not(string_startswith(v_t, "__"))):
  256. output(string_join((" " + v_t) + " : ", getName(model["metamodel"]["metamodel"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
  257. elif (cmd == "retype"):
  258. output("Element to retype?")
  259. String elementname
  260. elementname = input()
  261. if (dict_in(model["model"], elementname)):
  262. output("New type")
  263. typename = input()
  264. if (dict_in(model["metamodel"]["model"], typename)):
  265. // OK, do the retyping
  266. // First try removing the previous type if it exists
  267. dict_delete(model["type_mapping"], model["model"][elementname])
  268. // Now add the new type
  269. dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename])
  270. output("Retyped!")
  271. else:
  272. output("Unknown type; aborting")
  273. else:
  274. output("Unknown element; aborting")
  275. elif (cmd == "switch"):
  276. bottom = bool_not(bottom)
  277. Element tmp_model
  278. tmp_model = model
  279. model = other_metamodel
  280. other_metamodel = tmp_model
  281. if (bottom):
  282. // The type mapping we are using is probably not complete for our model
  283. // so we completely recreate it from the model we have.
  284. output("Switching to conformance bottom mode!")
  285. generate_bottom_type_mapping(model)
  286. else:
  287. // We already switched the models and such, so we are already done!
  288. output("Switching to linguistic metamodel!")
  289. elif (bool_and(dict_in(pn_operations(), cmd), bool_not(bottom))):
  290. // A model-specific operation, so execute that one
  291. Element specific_op
  292. specific_op = dict_read(pn_operations(), cmd)
  293. specific_op(model)
  294. else:
  295. output("Unknown command; aborting")
  296. output("Use command 'help' to get a list of available commands")
  297. Element function initial_prompt():
  298. output("Welcome to the Model Management Interface, running live on the Modelverse!")
  299. output("Use 'help' command for a list of possible commands")
  300. String command
  301. Element root
  302. Element metamodel
  303. String name
  304. Element my_model
  305. String mm_name
  306. root = create_metamodels()
  307. while (True):
  308. output("Please give your command.")
  309. command = input()
  310. if (command == "help"):
  311. output("Currently no model is loaded, so your operations are limited to:")
  312. output(" new -- Create a new model and save it for future use")
  313. output(" load -- Load a previously made model")
  314. output(" rename -- Rename a previously made model")
  315. output(" delete -- Delete a previously made model")
  316. output(" list -- Show a list of all stored models")
  317. output(" help -- Show a list of possible commands")
  318. elif (command == "new"):
  319. output("Metamodel to instantiate?")
  320. mm_name = input()
  321. if (dict_in(root, mm_name)):
  322. output("Name of model?")
  323. name = input()
  324. if (dict_in(root, name)):
  325. output("Model exists; aborting")
  326. else:
  327. my_model = instantiate_model(root[mm_name])
  328. petrinet_loaded(my_model)
  329. else:
  330. output("Unknown metamodel; aborting")
  331. elif (command == "load"):
  332. output("Model to load?")
  333. name = input()
  334. if (dict_in(root, name)):
  335. my_model = root[name]
  336. petrinet_loaded(my_model)
  337. else:
  338. output("Model not found; aborting")
  339. elif (command == "list"):
  340. Element keys
  341. String m_menu_list
  342. keys = dict_keys(root)
  343. output("Found models:")
  344. while (read_nr_out(keys) > 0):
  345. m_menu_list = set_pop(keys)
  346. output(((" " + m_menu_list) + " : ") + reverseNameLookup(root, root[m_menu_list]["metamodel"]))
  347. elif (command == "delete"):
  348. output("Model to delete?")
  349. name = input()
  350. if (dict_in(root, name)):
  351. dict_delete(root, name)
  352. output("Deleted!")
  353. else:
  354. output("Model not found; aborting")
  355. elif (command == "rename"):
  356. output("Old name?")
  357. String old_name
  358. old_name = input()
  359. if (dict_in(root, old_name)):
  360. output("New name?")
  361. String new_name
  362. new_name = input()
  363. if (dict_in(root, new_name)):
  364. output("Model exists; aborting")
  365. else:
  366. dict_add(root, new_name, root[old_name])
  367. dict_delete(root, old_name)
  368. output("Rename complete!")
  369. else:
  370. output("Model not found; aborting")
  371. else:
  372. output("Command not recognized, use 'help' for a list of possible commands")
  373. Void function main():
  374. initial_prompt()