pn_interface.alc 15 KB

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