pn_interface.alc 16 KB

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