pn_interface.alc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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(" constrain -- Add a constraint function to the model")
  112. output(" rename -- Rename an existing element")
  113. output(" modify -- Modify the attributes of an element")
  114. output(" list -- Prints the list of elements in the model")
  115. output(" types -- Prints the list of elements that can be instantiated")
  116. output(" read -- Prints the current state of a model element")
  117. output(" verify -- Check whether the model conforms to the metamodel")
  118. output(" retype -- Change the type of an element")
  119. output(" switch -- Switch between conformance bottom and the linguistic metamodel")
  120. output(" exit -- Unload the model and go back to the loading prompt")
  121. if (bool_not(bottom)):
  122. output("Model-specific operations:")
  123. Element specific_ops
  124. specific_ops = dict_keys(pn_operations())
  125. String specific_op
  126. while (0 < dict_len(specific_ops)):
  127. specific_op = set_pop(specific_ops)
  128. output(" " + specific_op)
  129. elif (cmd == "exit"):
  130. return model
  131. elif (cmd == "instantiate"):
  132. String mm_type_name
  133. output("Type to instantiate?")
  134. mm_type_name = input()
  135. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  136. String element_name
  137. output("Name of new element?")
  138. element_name = input()
  139. if (dict_in(model["model"], element_name)):
  140. output("Element already exists; aborting")
  141. else:
  142. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  143. output("Source name?")
  144. String src_name
  145. src_name = input()
  146. if (dict_in(model["model"], src_name)):
  147. output("Destination name?")
  148. String dst_name
  149. dst_name = input()
  150. if (dict_in(model["model"], dst_name)):
  151. instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
  152. output("Instantiation successful!")
  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. output("Instantiation successful!")
  160. else:
  161. output("Unknown type specified; aborting")
  162. elif (cmd == "set_inheritance"):
  163. String inh_name
  164. output("Which link in the metamodel is the inheritance link?")
  165. inh_name = input()
  166. if (dict_in(model["metamodel"]["model"], inh_name)):
  167. dict_add(model, "inheritance", model["metamodel"]["model"][inh_name])
  168. output("Set inheritance link!")
  169. else:
  170. output("Element not found in metamodel; aborting")
  171. elif (cmd == "constrain"):
  172. output("Element to constrain (empty for global)?")
  173. String model_name
  174. model_name = input()
  175. if (model_name == ""):
  176. // Global constraint
  177. output("Give input to function constructors for GLOBAL constraint!")
  178. set_model_constraints(model, construct_function())
  179. elif (dict_in(model["model"], model_name)):
  180. // Local constraint for this model
  181. output("Give input to function constructors for LOCAL constraint!")
  182. add_constraint(model, model_name, construct_function())
  183. output("Added constraint to model!")
  184. else:
  185. // Local constraint, but model not found
  186. output("Unknown model; aborting")
  187. elif (cmd == "modify"):
  188. String model_name
  189. output("Element to modify?")
  190. model_name = input()
  191. if (dict_in(model["model"], model_name)):
  192. Element attrs
  193. attrs = getAttributeList(model, model_name)
  194. String attr_name
  195. output("Attribute to modify?")
  196. attr_name = input()
  197. if (set_in(dict_keys(attrs), attr_name)):
  198. output("New value?")
  199. unset_attribute(model, model_name, attr_name)
  200. instantiate_attribute(model, model_name, attr_name, input())
  201. output("Modified!")
  202. else:
  203. output("No such attribute!")
  204. else:
  205. output("No such model!")
  206. elif (cmd == "attr_add"):
  207. String model_name
  208. output("Which model do you want to assign an attribute to?")
  209. model_name = input()
  210. if (dict_in(model["model"], model_name)):
  211. Element attrs
  212. attrs = getAttributeList(model, model_name)
  213. String attr_name
  214. output("Which attribute do you wish to assign?")
  215. attr_name = input()
  216. if (set_in(dict_keys(attrs), attr_name)):
  217. output("Value of attribute?")
  218. instantiate_attribute(model, model_name, attr_name, input())
  219. output("Added attribute!")
  220. else:
  221. output("No such attribute!")
  222. else:
  223. output("No such model!")
  224. elif (cmd == "attr_del"):
  225. String model_name
  226. output("Which model do you want to remove an attribute of?")
  227. model_name = input()
  228. if (dict_in(model["model"], model_name)):
  229. Element attrs
  230. attrs = getAttributeList(model, model_name)
  231. String attr_name
  232. output("Which attribute do you want to delete?")
  233. attr_name = input()
  234. if (set_in(dict_keys(attrs), attr_name)):
  235. unset_attribute(model, model_name, attr_name)
  236. output("Attribute deleted!")
  237. else:
  238. output("No such attribute!")
  239. else:
  240. output("No such model!")
  241. elif (cmd == "delete"):
  242. output("What is the name of the element you want to delete?")
  243. cmd = input()
  244. if (dict_in(model["model"], cmd)):
  245. model_delete_element(model, cmd)
  246. output("Deleted!")
  247. else:
  248. output("No such element; aborting")
  249. elif (cmd == "rename"):
  250. output("Old name?")
  251. String old_name_e
  252. old_name_e = input()
  253. if (dict_in(model["model"], old_name_e)):
  254. output("New name?")
  255. String new_name_e
  256. new_name_e = input()
  257. if (dict_in(model["model"], new_name_e)):
  258. output("New name already used; aborting")
  259. else:
  260. dict_add(model["model"], new_name_e, model["model"][old_name_e])
  261. dict_delete(model["model"], old_name_e)
  262. output("Rename complete!")
  263. else:
  264. output("Unknown element; aborting")
  265. elif (cmd == "list"):
  266. Element keys_m
  267. keys_m = dict_keys(model["model"])
  268. output("List of all elements:")
  269. String v_m
  270. while (read_nr_out(keys_m) > 0):
  271. v_m = set_pop(keys_m)
  272. // Filter out anonymous objects
  273. if (bool_not(string_startswith(v_m, "__"))):
  274. typename = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][v_m]))
  275. output(((" " + v_m) + " : ") + typename)
  276. elif (cmd == "read"):
  277. output("Element to read?")
  278. cmd = input()
  279. if (dict_in(model["model"], cmd)):
  280. Element read_elem
  281. read_elem = model["model"][cmd]
  282. metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
  283. output("Name: " + cmd)
  284. output("Type: " + reverseKeyLookup(model["metamodel"]["model"], metamodel_element_pn))
  285. if (is_edge(read_elem)):
  286. output("Source: " + reverseKeyLookup(model["model"], read_edge_src(read_elem)))
  287. output("Destination: " + reverseKeyLookup(model["model"], read_edge_dst(read_elem)))
  288. if (cast_v2s(read_elem) != "None"):
  289. output("Value: " + cast_v2s(read_elem))
  290. output("Defines attributes:")
  291. attr_list_pn = getInstantiatableAttributes(model, read_elem)
  292. attr_keys_pn = dict_keys(attr_list_pn)
  293. while (0 < read_nr_out(attr_keys_pn)):
  294. attr_key_pn = set_pop(attr_keys_pn)
  295. output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])))
  296. output("Attributes:")
  297. attr_list_pn = getAttributeList(model, cmd)
  298. attr_keys_pn = dict_keys(attr_list_pn)
  299. while (0 < read_nr_out(attr_keys_pn)):
  300. attr_key_pn = set_pop(attr_keys_pn)
  301. 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)))
  302. else:
  303. output("Unknown element; aborting")
  304. elif (cmd == "verify"):
  305. output(conformance_scd(model))
  306. elif (cmd == "types"):
  307. Element keys_t
  308. keys_t = dict_keys(model["metamodel"]["model"])
  309. output("List of types:")
  310. String v_t
  311. while (read_nr_out(keys_t) > 0):
  312. v_t = set_pop(keys_t)
  313. if (bool_not(string_startswith(v_t, "__"))):
  314. output(string_join((" " + v_t) + " : ", reverseKeyLookup(model["metamodel"]["metamodel"]["model"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
  315. elif (cmd == "retype"):
  316. output("Element to retype?")
  317. String elementname
  318. elementname = input()
  319. if (dict_in(model["model"], elementname)):
  320. output("New type")
  321. typename = input()
  322. if (dict_in(model["metamodel"]["model"], typename)):
  323. // OK, do the retyping
  324. // First try removing the previous type if it exists
  325. dict_delete(model["type_mapping"], model["model"][elementname])
  326. // Now add the new type
  327. dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename])
  328. output("Retyped!")
  329. else:
  330. output("Unknown type; aborting")
  331. else:
  332. output("Unknown element; aborting")
  333. elif (cmd == "switch"):
  334. bottom = bool_not(bottom)
  335. Element tmp_model
  336. tmp_model = model
  337. model = other_metamodel
  338. other_metamodel = tmp_model
  339. if (bottom):
  340. // The type mapping we are using is probably not complete for our model
  341. // so we completely recreate it from the model we have.
  342. output("Switching to conformance bottom mode!")
  343. generate_bottom_type_mapping(model)
  344. else:
  345. // We already switched the models and such, so we are already done!
  346. output("Switching to linguistic metamodel!")
  347. elif (bool_and(dict_in(pn_operations(), cmd), bool_not(bottom))):
  348. // A model-specific operation, so execute that one
  349. Element specific_op
  350. specific_op = dict_read(pn_operations(), cmd)
  351. specific_op(model)
  352. else:
  353. output("Unknown command: " + cast_v2s(cmd))
  354. output("Use command 'help' to get a list of available commands")
  355. Element function initial_prompt():
  356. output("Welcome to the Model Management Interface, running live on the Modelverse!")
  357. output("Use 'help' command for a list of possible commands")
  358. String command
  359. Element root
  360. Element metamodel
  361. String name
  362. Element my_model
  363. String mm_name
  364. root = create_metamodels()
  365. while (True):
  366. output("Please give your command.")
  367. command = input()
  368. if (command == "help"):
  369. output("Currently no model is loaded, so your operations are limited to:")
  370. output(" new -- Create a new model and save it for future use")
  371. output(" load -- Load a previously made model")
  372. output(" rename -- Rename a previously made model")
  373. output(" delete -- Delete a previously made model")
  374. output(" list -- Show a list of all stored models")
  375. output(" help -- Show a list of possible commands")
  376. output(" load_AL-- Type an AL model")
  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. else:
  432. output("Command not recognized, use 'help' for a list of possible commands")
  433. Void function main():
  434. initial_prompt()