pn_interface.alc 15 KB

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