pn_interface.alc 16 KB

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