pn_interface.alc 14 KB

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