pn_interface.alc 15 KB

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