pn_interface.alc 18 KB

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