pn_interface.alc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. //helper function that returns the type of an attribute in string format
  10. Element function find_attribute_type_s(model : Element, elem : String, name : String):
  11. Element mm_elem
  12. Element direct_type
  13. direct_type = dict_read_node(model["type_mapping"], model["model"][elem])
  14. mm_elem = find_attribute_definer(model["metamodel"], direct_type, name)
  15. if (element_eq(mm_elem, read_root())):
  16. // Couldn't find element, so is not allowed!
  17. log("Undefined attribute!")
  18. return read_root()
  19. else:
  20. return getName(model["metamodel"], read_edge_dst( dict_read_edge(mm_elem, name)))
  21. //given a model and a node, it will return the name of it's type in the metamodel
  22. String function getTypeName(model:Element, node:Element):
  23. return getName(model["metamodel"], dict_read_node(model["type_mapping"], node))
  24. //given a model and a node it will return the name of it's type in the metametamodel
  25. String function getMetaTypeName(model:Element, node:Element):
  26. return getTypeName(model["metamodel"], dict_read_node(model["type_mapping"], node))
  27. Boolean function isAttributeRelated(model:Element, node:Element):
  28. if(getMetaTypeName(model, node) == "Type"):
  29. return True
  30. elif(getMetaTypeName(model, node) == "l5"):
  31. if(getMetaTypeName(model, read_edge_dst(node)) == "Type" ):
  32. return True
  33. return False
  34. //function to print information about a node (attributes, type and name)
  35. Element function printNode(tf_pn_model: Element, tf_pn_node: Element):
  36. //print header with name, metatype
  37. //print node_name
  38. String tf_pn_node_name
  39. tf_pn_node_name = getName(tf_pn_model, tf_pn_node)
  40. log(string_join("--name: ",tf_pn_node_name) )
  41. //print node type
  42. log(string_join("type: ", getTypeName(tf_pn_model, tf_pn_node) ))
  43. //print node metatype
  44. String metatype
  45. metatype = getMetaTypeName(tf_pn_model, tf_pn_node)
  46. log(string_join("metatype: ", metatype))
  47. if (isAttributeRelated(tf_pn_model, tf_pn_node)):
  48. log(">attribute related")
  49. //print attributes
  50. log("attributes:")
  51. //get the list of attributes
  52. Element tf_pn_attributelist
  53. tf_pn_attributelist = dict_keys(getAttributeList(tf_pn_model, tf_pn_node_name))
  54. String tf_pn_attributename
  55. String tf_pn_attributetype
  56. while (integer_lt(0, read_nr_out(tf_pn_attributelist))):
  57. //print attribute name and its value
  58. tf_pn_attributename = set_pop(tf_pn_attributelist)
  59. tf_pn_attributetype = find_attribute_type_s(tf_pn_model, tf_pn_node_name, tf_pn_attributename)
  60. if( tf_pn_attributetype == "String"):
  61. log(string_join(string_join(string_join(" ",tf_pn_attributename), " : "),read_attribute(tf_pn_model, tf_pn_node_name, tf_pn_attributename)))
  62. elif (tf_pn_attributetype == "Integer"):
  63. log(string_join(string_join(string_join(" ",tf_pn_attributename), " : "),cast_i2s(read_attribute(tf_pn_model, tf_pn_node_name, tf_pn_attributename))))
  64. else:
  65. log(string_join(tf_pn_attributename, " : "))
  66. log(read_attribute(tf_pn_model, tf_pn_node_name, tf_pn_attributename))
  67. return 0
  68. Boolean function node_match(transf_model : Element, transf_elem : Element, host_model : Element, host_elem : Element):
  69. //TODO maybe get rid of LHS postfix? maybe not
  70. if (getTypeName(transf_model, transf_elem) != string_join(getTypeName(host_model, host_elem), "_LHS")):
  71. return False
  72. //TODO attributecheck (need to be able to define function for this)
  73. //TODO semantic check
  74. return True
  75. Element function new_SearchState(OldSearchState : Element, TransfModel : Element, NewTransfElem: Element, HostModel : Element, NewHostElem : Element):
  76. //some variables
  77. Element nss // new searchstate
  78. nss = create_node()
  79. Integer index // used for list looping
  80. Element elem // reusable loop variable
  81. //new MATCHES
  82. dict_add(nss, "matches", OldSearchState["matches"]) //hopefully works as a reference
  83. //new CURMATCH
  84. Element OldCurMatchKeys // for looping trough old curmatch (for copying)
  85. OldCurMatchKeys = dict_keys(OldSearchState["curmatch"])
  86. Element new_curmatch //the new curmatch
  87. new_curmatch = create_node()
  88. Element oldkey // loop variable
  89. while (integer_lt(0, list_len(OldCurMatchKeys))): //copying the old curmatch
  90. oldkey = set_pop(OldCurMatchKeys)
  91. dict_add(new_curmatch, oldkey, OldSearchState["curmatch"][oldkey]) //WARNING dict_read_node needed???
  92. Element newPair // the new pair to be added
  93. newPair = create_node()
  94. dict_add(newPair, "pn", newTransfElem) //PatternNode
  95. dict_add(newPair, "hn", newHostElem) // HostNode
  96. Integer plabel
  97. plabel = read_attribute(TransfModel, getName(TransfModel, NewTransfElem), "plabel")
  98. dict_add(new_curmatch, plabel, newPair) //add pair to new curmatch
  99. dict_add(nss, "curmatch", new_curmatch) //add curmatch to state
  100. //new N_PATTERN_OUT (copy everything but the new match elements)
  101. Element ol //old list = old pattern out
  102. Element nl //new list = new N pattern out
  103. ol = OldSearchState["N_pattern_out"]
  104. nl = create_node()
  105. index = 0
  106. while (index < list_len(ol)): //loop trough old list
  107. elem = list_read(ol, index)
  108. index = index + 1
  109. if(elem != NewTransfElem): //WARNING is this the right check?
  110. list_append(nl, elem)
  111. dict_add(nss, "N_pattern_out", nl)
  112. //new N_HOST_OUT (copy everything but the new match elements)
  113. ol = OldSearchState["N_host_out"]
  114. nl = create_node()
  115. index = 0
  116. while (index < list_len(ol)): //loop trough old list
  117. elem = list_read(ol, index)
  118. index = index + 1
  119. if(elem != NewHostElem): //WARNING is this the right check?
  120. list_append(nl, elem)
  121. dict_add(nss, "N_host_out", nl)
  122. //new N_PATTERN_IN (copy everything but the new match elements)
  123. ol = OldSearchState["N_pattern_in"]
  124. nl = create_node()
  125. index = 0
  126. while (index < list_len(ol)): //loop trough old list
  127. elem = list_read(ol, index)
  128. index = index + 1
  129. if(elem != NewTransfElem): //WARNING is this the right check?
  130. list_append(nl, elem)
  131. dict_add(nss, "N_pattern_in", nl)
  132. //new N_HOST_IN (copy everything but the new match elements
  133. ol = OldSearchState["N_host_in"]
  134. nl = create_node()
  135. index = 0
  136. while (index < list_len(ol)): //loop trough old list
  137. elem = list_read(ol, index)
  138. index = index + 1
  139. if(elem != NewHostElem): //WARNING is this the right check?
  140. list_append(nl, elem)
  141. dict_add(nss, "N_host_in", nl)
  142. //new V_PATTERN
  143. //copy everything but:
  144. // - new match elements
  145. // - outgoing nodes from new match elements
  146. // - incoming nodes from new match elements
  147. //TODO
  148. dict_add(nss, "V_pattern", nl)
  149. //new V_PATTERN
  150. //copy everything but:
  151. // - new match elements
  152. // - outgoing nodes from new match elements
  153. // - incoming nodes from new match elements
  154. //TODO
  155. dict_add(nss, "V_host", nl)
  156. // copy pattern_size
  157. dict_add(nss, "pattern_size", dict_read(OldSearchState["pattern_size"])
  158. return nss
  159. Element function transf_vf2(SearchState: Element, transf_m : Element, host_m : Element):
  160. // get the current match from the search-state
  161. Element current_match
  162. current_match = SearchState["curmatch"]
  163. //if match is complete: add to matches and return
  164. if SearchState["pattern_size"] == list_len(current_match):
  165. //TODO is it needed to check if match already exists?
  166. set_add(SearchState["matches"], SearchState["curmatch"])
  167. return
  168. //TODO feasability check (if not feasible: return early on!!!)
  169. //initialise some variables
  170. Integer index1 //an index used for looping trough pattern_list
  171. Integer index2 //an index used for looping trough host_list
  172. Element pattern_list //the set with pattern nodes to be considered
  173. Element host_list // the set with host nodes to be considered
  174. Element pattern_node // node to be tested from the patterns side
  175. Element host_node //node to be tested from the hosts side
  176. //check for candidates in (N_pattern_out, N_host_out)
  177. pattern_list = SearchState["N_pattern_out"]
  178. host_list = SearchState["N_host_out"]
  179. index1 = 0
  180. Element nss //variable for storing next searchstate
  181. while ( index1 < list_len(pattern_list) ):
  182. pattern_node = list_read(pattern_list, index1)
  183. index1 = index1 + 1
  184. index2 = 0
  185. while (index2 < list_len(host_list)):
  186. host_node = list_read(host_list, index2)
  187. index2= index2 + 1
  188. if(node_match(transf_m, pattern_node, host_m, host_node)):
  189. nss = new_SearchState(SearchState, transf_m, pattern_node, host_m, host_node)
  190. //TODO recursive call (nodes added to new current match, reorder Nout Nin V)
  191. //TODO repeat above loop for in
  192. //TODO repeat above loop for V
  193. return 0
  194. // given a pattern, this function will return all matches in a model
  195. Element function tf_matchAll(tf_ma_transformation: Element, tf_ma_pattern : Element, tf_ma_model: Element ):
  196. //find all nodes in the pattern
  197. log("=== finding all nodes in pattern")
  198. Element tf_ma_metamodelTransf //transformation metamodel
  199. tf_ma_metamodelTransf = tf_ma_transformation["metamodel"]["model"]
  200. Element pre2ram_et // type of the pre2ram edge
  201. pre2ram_et = dict_read(tf_ma_metamodelTransf, "pre2ram")
  202. Element tf_ma_pattern_edges //all pre2ram edges
  203. tf_ma_pattern_edges = allOutgoingAssociationInstances(tf_ma_transformation, tf_ma_pattern, pre2ram_et)
  204. Element tf_ma_ramified_nodes //dict in the form plabel->node
  205. Element tf_ma_ramified_node
  206. while (integer_lt(0, list_len(tf_ma_pattern_edges))):
  207. tf_ma_ramified_node = read_edge_dst(set_pop(tf_ma_pattern_edges))
  208. //printNode(tf_ma_transformation, tf_ma_ramified_node)
  209. list_append(tf_ma_ramified_nodes, tf_ma_ramified_node)
  210. //find all nodes in the graph that are not related to attributes: ie a userdefined type
  211. log("=== finding nodes in host")
  212. Element hostmodel_mm //metamodel of hostgraph
  213. hostmodel_mm = tf_ma_model["metamodel"]["model"]
  214. Element hostmodel_keys //keys of all elements in the model
  215. hostmodel_keys = dict_keys(tf_ma_model["model"])
  216. Element host_nodes //all nodes in the host graph
  217. Element host_model_key
  218. while (integer_lt(0, read_nr_out(hostmodel_keys))):
  219. host_model_key = set_pop(hostmodel_keys)
  220. if bool_not(isAttributeRelated(tf_ma_model, tf_ma_model["model"][host_model_key])):
  221. //printNode(tf_ma_model, tf_ma_model["model"][host_model_key])
  222. list_append(host_nodes, tf_ma_model["model"][host_model_key])
  223. // initialise the start-state
  224. log("=== creating searchstate")
  225. //initialise an start state
  226. Element SearchState
  227. SearchState = create_node()
  228. dict_add(SearchState, "matches", create_node())
  229. dict_add(SearchState, "curmatch", create_node())
  230. dict_add(SearchState, "N_pattern_out", create_node())
  231. dict_add(SearchState, "N_host_out", create_node())
  232. dict_add(SearchState, "N_pattern_in", create_node())
  233. dict_add(SearchState, "N_host_in", create_node())
  234. dict_add(SearchState, "V_pattern", tf_ma_ramified_nodes)
  235. dict_add(SearchState, "V_host", host_nodes)
  236. dict_add(SearchState, "pattern_size", list_len(tf_ma_ramified_nodes))
  237. //call vf2 for the matching
  238. log("calling vf2")
  239. SearchState = transf_vf2(SearchState, tf_ma_pattern, tf_ma_model)
  240. return dict_read(SearchState,"matches")
  241. Element function tf_executeRule(tf_er_transformation: Element,tf_er_rule: Element, tf_er_model: Element):
  242. Element tf_er_lhs
  243. // get the lhs of the rule
  244. tf_er_lhs = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, dict_read(dict_read(dict_read(tf_er_transformation, "metamodel"), "model"), "lhs"))
  245. // find the matches of the rule
  246. tf_matchAll(tf_er_transformation, read_edge_dst(set_pop(tf_er_lhs)), tf_er_model)
  247. return 0
  248. Element function tf_transform(tf_tf_transformation: Element, tf_tf_model: Element):
  249. //create a list of all rules
  250. Element tf_tf_rules
  251. tf_tf_rules = allInstances(tf_tf_transformation, dict_read(dict_read(dict_read(tf_tf_transformation, "metamodel"), "model"), "Rule"))
  252. //execute rules in random order
  253. Element tf_tf_rule
  254. while (integer_lt(0, read_nr_out(tf_tf_rules))):
  255. tf_tf_rule = set_pop(tf_tf_rules)
  256. //tf_printNode(tf_tf_transformation, tf_tf_rule)
  257. tf_executeRule(tf_tf_transformation, tf_tf_rule, tf_tf_model)
  258. return 0
  259. Element function test_simple_petrinet1(root:Element):
  260. //create a petrinet for testing purposes
  261. // P1 -> T1 -> P2
  262. // 1 1 2 0
  263. //////////////////////////////////
  264. //initialise an empty petrinet
  265. Element my_model
  266. my_model = instantiate_model(root["PetriNets"])
  267. dict_add(root, "pnt_test1", my_model)
  268. // init t1
  269. instantiate_node(my_model, "Transition", "t1")
  270. // init p1
  271. instantiate_node(my_model, "Place", "p1")
  272. instantiate_attribute(my_model, "p1","tokens",1)
  273. // init link1
  274. instantiate_link(my_model, "P2T", "l1", "p1", "t1")
  275. instantiate_attribute(my_model, "l1", "weight", 1)
  276. //init p2
  277. instantiate_node(my_model, "Place", "p2")
  278. instantiate_attribute(my_model, "p2","tokens",0)
  279. //init link2
  280. instantiate_link(my_model, "T2P", "l2", "p1", "t1")
  281. instantiate_attribute(my_model, "l2", "weight", 2)
  282. return my_model
  283. Element function petrinet_transformation(root: Element):
  284. // Create A transformation model for petrinets (useful for testing)
  285. //////////////////////////////////
  286. //initialise an empty transformation model for petrinet
  287. Element my_model
  288. my_model = instantiate_model(root["PetriNets_MTransformation"])
  289. define_inheritance(my_model, "l3")
  290. dict_add(root, "transf_petrinet_fire", my_model)
  291. ////////////////////////////
  292. // root node
  293. instantiate_node(my_model, "Transformation", "PetrinetFire")
  294. instantiate_attribute(my_model, "PetrinetFire", "name", "PetriNetFire")
  295. ////////////////////////////
  296. // rule: find transitions
  297. //rule: find_transitions
  298. instantiate_node(my_model, "Rule", "find_transitions")
  299. instantiate_attribute(my_model, "find_transitions", "name", "find_transitions")
  300. instantiate_attribute(my_model, "find_transitions", "precedence", 1)
  301. //connect to transformation
  302. instantiate_link(my_model, "transformation_to_rule", "", "PetrinetFire", "find_transitions")
  303. //precondition: lhs_precondition
  304. instantiate_node(my_model, "Precondition","lhs_precondition")
  305. //connect to rule
  306. instantiate_link(my_model, "lhs", "", "find_transitions", "lhs_precondition")
  307. //Transition_LHS: lhs_find_all
  308. instantiate_node(my_model, "Transition_LHS","lhs_find_all")
  309. instantiate_attribute(my_model, "lhs_find_all", "plabel", 1)
  310. //connect to rule
  311. instantiate_link(my_model, "pre2ram", "", "lhs_precondition", "lhs_find_all")
  312. //precondition: nac_precondition
  313. instantiate_node(my_model, "Precondition","nac_precondition")
  314. //connect to rule
  315. instantiate_link(my_model, "nac", "", "find_transitions", "nac_precondition")
  316. //Transition_LHS: nac_transition_place_before
  317. instantiate_node(my_model, "Transition_LHS","nac_transition_place_before")
  318. instantiate_attribute(my_model, "nac_transition_place_before", "plabel", 1)
  319. //connect to rule
  320. instantiate_link(my_model, "pre2ram", "", "nac_precondition", "nac_transition_place_before")
  321. //Place_LHS: nac_place_before
  322. instantiate_node(my_model, "Place_LHS","nac_place_before")
  323. instantiate_attribute(my_model, "nac_place_before", "plabel", 2)
  324. 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)")
  325. //connect to rule
  326. instantiate_link(my_model, "pre2ram", "", "nac_precondition", "nac_place_before")
  327. //P2T_LHS: nac_ft_p2t
  328. instantiate_link(my_model, "P2T_LHS", "nac_p2tlhs_before", "nac_place_before", "nac_transition_place_before")
  329. instantiate_attribute(my_model, "nac_p2tlhs_before", "plabel", 3)
  330. instantiate_attribute(my_model, "nac_p2tlhs_before", "weight", "lambda x: True")
  331. //connect to rule
  332. instantiate_link(my_model, "pre2ram", "", "nac_precondition", "nac_p2tlhs_before")
  333. return my_model
  334. Element function pn_operations():
  335. Element ops
  336. ops = create_node()
  337. dict_add(ops, "fire", petrinet_fire)
  338. dict_add(ops, "enabled", petrinet_enabled)
  339. return ops
  340. Element function petrinet_enabled(model : Element):
  341. Element set_enabled
  342. set_enabled = petrinet_enabled_set(model)
  343. output("Enabled transitions:")
  344. while (0 < read_nr_out(set_enabled)):
  345. output(getName(model, set_pop(set_enabled)))
  346. return model
  347. Element function petrinet_enabled_set(model : Element):
  348. Element all_transitions
  349. all_transitions = allInstances(model, model["metamodel"]["model"]["Transition"])
  350. Element enabled_transitions
  351. enabled_transitions = create_node()
  352. Element under_study
  353. Element in_arcs
  354. Element arc_under_study
  355. Boolean enabled
  356. while (0 < read_nr_out(all_transitions)):
  357. under_study = set_pop(all_transitions)
  358. enabled = True
  359. // Find all incoming transitions
  360. in_arcs = allIncomingAssociationInstances(model, under_study, model["metamodel"]["model"]["P2T"])
  361. while (0 < read_nr_out(in_arcs)):
  362. arc_under_study = set_pop(in_arcs)
  363. Integer present_tokens
  364. Integer required_tokens
  365. required_tokens = read_attribute(model, getName(model, arc_under_study), "weight")
  366. present_tokens = read_attribute(model, getName(model, read_edge_src(arc_under_study)), "tokens")
  367. if (present_tokens < required_tokens):
  368. // Less tokens than required, so disable the transition completely
  369. enabled = False
  370. if (enabled):
  371. set_add(enabled_transitions, under_study)
  372. return enabled_transitions
  373. Element function petrinet_fire(model : Element):
  374. output("Transition to fire?")
  375. Element transition
  376. transition = input()
  377. if (dict_in(model["model"], transition)):
  378. transition = model["model"][transition]
  379. if (set_in(petrinet_enabled_set(model), transition)):
  380. Element workset
  381. Element working_place
  382. Element working_arc
  383. Integer new_value
  384. // Consume tokens
  385. workset = allIncomingAssociationInstances(model, transition, model["metamodel"]["model"]["P2T"])
  386. while (0 < read_nr_out(workset)):
  387. working_arc = set_pop(workset)
  388. working_place = read_edge_src(working_arc)
  389. new_value = integer_subtraction(read_attribute(model, getName(model, working_place), "tokens"), read_attribute(model, getName(model, working_arc), "weight"))
  390. unset_attribute(model, getName(model, working_place), "tokens")
  391. instantiate_attribute(model, getName(model, working_place), "tokens", new_value)
  392. output(((" " + getName(model, working_place)) + ": ") + cast_i2s(read_attribute(model, getName(model, working_place), "tokens")))
  393. // Add tokens
  394. workset = allOutgoingAssociationInstances(model, transition, model["metamodel"]["model"]["T2P"])
  395. while (0 < read_nr_out(workset)):
  396. working_arc = set_pop(workset)
  397. working_place = read_edge_dst(working_arc)
  398. new_value = integer_addition(read_attribute(model, getName(model, working_place), "tokens"), read_attribute(model, getName(model, working_arc), "weight"))
  399. unset_attribute(model, getName(model, working_place), "tokens")
  400. instantiate_attribute(model, getName(model, working_place), "tokens", new_value)
  401. output(((" " + getName(model, working_place)) + ": ") + cast_i2s(read_attribute(model, getName(model, working_place), "tokens")))
  402. output("Transition fired!")
  403. else:
  404. output("Cannot fire if not enabled; aborting")
  405. else:
  406. output("Unknown transition; aborting")
  407. return model
  408. Element function petrinet_loaded(model : Element):
  409. String cmd
  410. Element attr_list_pn
  411. Element attr_keys_pn
  412. String attr_key_pn
  413. Element metamodel_element_pn
  414. String typename
  415. Boolean bottom
  416. Element other_metamodel
  417. bottom = False
  418. other_metamodel = create_node()
  419. dict_add(other_metamodel, "model", model["model"])
  420. dict_add(other_metamodel, "type_mapping", create_node())
  421. dict_add(other_metamodel, "metamodel", import_node("models/LTM_bottom"))
  422. dict_add(other_metamodel, "inheritance", other_metamodel["metamodel"]["model"]["__Inheritance"])
  423. output("Model loaded, ready for commands!")
  424. output("Use 'help' command for a list of possible commands")
  425. while (True):
  426. output("Please give your command.")
  427. cmd = input()
  428. if (cmd == "help"):
  429. output("Generic model operations:")
  430. output(" instantiate -- Create a new model element")
  431. output(" delete -- Delete an existing element")
  432. output(" attr_add -- Add an attribute to an element")
  433. output(" attr_del -- Delete an attribute of an element")
  434. output(" rename -- Rename an existing element")
  435. output(" modify -- Modify the attributes of an element")
  436. output(" list -- Prints the list of elements in the model")
  437. output(" types -- Prints the list of elements that can be instantiated")
  438. output(" read -- Prints the current state of a model element")
  439. output(" verify -- Check whether the model conforms to the metamodel")
  440. output(" retype -- Change the type of an element")
  441. output(" switch -- Switch between conformance bottom and the linguistic metamodel")
  442. output(" exit -- Unload the model and go back to the loading prompt")
  443. if (bool_not(bottom)):
  444. output("Model-specific operations:")
  445. Element specific_ops
  446. specific_ops = dict_keys(pn_operations())
  447. String specific_op
  448. while (0 < dict_len(specific_ops)):
  449. specific_op = set_pop(specific_ops)
  450. output(" " + specific_op)
  451. elif (cmd == "exit"):
  452. return model
  453. elif (cmd == "instantiate"):
  454. String mm_type_name
  455. output("Type to instantiate?")
  456. mm_type_name = input()
  457. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  458. String element_name
  459. output("Name of new element?")
  460. element_name = input()
  461. if (dict_in(model["model"], element_name)):
  462. output("Element already exists; aborting")
  463. else:
  464. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  465. output("Source name?")
  466. String src_name
  467. src_name = input()
  468. if (dict_in(model["model"], src_name)):
  469. output("Destination name?")
  470. String dst_name
  471. dst_name = input()
  472. if (dict_in(model["model"], dst_name)):
  473. instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
  474. if (dict_in(model["model"], element_name)):
  475. output("Instantiation successful!")
  476. else:
  477. output("Instantiation error!")
  478. else:
  479. output("Unknown destination; aborting")
  480. else:
  481. output("Unknown source; aborting")
  482. else:
  483. instantiate_node(model, mm_type_name, element_name)
  484. if (dict_in(model["model"], element_name)):
  485. output("Instantiation successful!")
  486. else:
  487. output("Instantiation error!")
  488. else:
  489. output("Unknown type specified; aborting")
  490. elif (cmd == "modify"):
  491. String model_name
  492. output("Element to modify?")
  493. model_name = input()
  494. if (dict_in(model["model"], model_name)):
  495. Element attrs
  496. attrs = getAttributeList(model, model_name)
  497. String attr_name
  498. output("Attribute to modify?")
  499. attr_name = input()
  500. if (set_in(dict_keys(attrs), attr_name)):
  501. output("New value?")
  502. unset_attribute(model, model_name, attr_name)
  503. instantiate_attribute(model, model_name, attr_name, input())
  504. output("Modified!")
  505. else:
  506. output("No such attribute!")
  507. else:
  508. output("No such model!")
  509. elif (cmd == "attr_add"):
  510. String model_name
  511. output("Which model do you want to assign an attribute to?")
  512. model_name = input()
  513. if (dict_in(model["model"], model_name)):
  514. Element attrs
  515. attrs = getAttributeList(model, model_name)
  516. String attr_name
  517. output("Which attribute do you wish to assign?")
  518. attr_name = input()
  519. if (set_in(dict_keys(attrs), attr_name)):
  520. output("Value of attribute?")
  521. instantiate_attribute(model, model_name, attr_name, input())
  522. output("Added attribute!")
  523. else:
  524. output("No such attribute!")
  525. else:
  526. output("No such model!")
  527. elif (cmd == "attr_del"):
  528. String model_name
  529. output("Which model do you want to remove an attribute of?")
  530. model_name = input()
  531. if (dict_in(model["model"], model_name)):
  532. Element attrs
  533. attrs = getAttributeList(model, model_name)
  534. output(print_dict(attrs))
  535. String attr_name
  536. output("Which attribute do you want to delete?")
  537. attr_name = input()
  538. if (set_in(dict_keys(attrs), attr_name)):
  539. unset_attribute(model, model_name, attr_name)
  540. else:
  541. output("No such attribute!")
  542. else:
  543. output("No such model!")
  544. elif (cmd == "delete"):
  545. output("What is the name of the element you want to delete?")
  546. cmd = input()
  547. if (dict_in(model["model"], cmd)):
  548. model_delete_element(model, cmd)
  549. output("Deleted!")
  550. else:
  551. output("No such element; aborting")
  552. elif (cmd == "rename"):
  553. output("Old name?")
  554. String old_name_e
  555. old_name_e = input()
  556. if (dict_in(model["model"], old_name_e)):
  557. output("New name?")
  558. String new_name_e
  559. new_name_e = input()
  560. if (dict_in(model["model"], new_name_e)):
  561. output("New name already used; aborting")
  562. else:
  563. dict_add(model["model"], new_name_e, model["model"][old_name_e])
  564. dict_delete(model["model"], old_name_e)
  565. output("Rename complete!")
  566. else:
  567. output("Unknown element; aborting")
  568. elif (cmd == "list"):
  569. Element keys_m
  570. keys_m = dict_keys(model["model"])
  571. output("List of all elements:")
  572. String v_m
  573. while (read_nr_out(keys_m) > 0):
  574. v_m = set_pop(keys_m)
  575. // Filter out anonymous objects
  576. typename = getName(model["metamodel"], dict_read_node(model["type_mapping"], model["model"][v_m]))
  577. if (bool_not(string_startswith(v_m, "__"))):
  578. output(((" " + v_m) + " : ") + typename)
  579. elif (cmd == "read"):
  580. output("Element to read?")
  581. cmd = input()
  582. if (dict_in(model["model"], cmd)):
  583. Element read_elem
  584. read_elem = model["model"][cmd]
  585. metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
  586. output("Name: " + cmd)
  587. output("Type: " + getName(model["metamodel"], metamodel_element_pn))
  588. if (is_edge(read_elem)):
  589. output("Source: " + getName(model, read_edge_src(read_elem)))
  590. output("Destination: " + getName(model, read_edge_dst(read_elem)))
  591. if (cast_v2s(read_elem) != "None"):
  592. output("Value: " + cast_v2s(read_elem))
  593. output("Defines attributes:")
  594. attr_list_pn = getInstantiatableAttributes(model, read_elem)
  595. attr_keys_pn = dict_keys(attr_list_pn)
  596. while (0 < read_nr_out(attr_keys_pn)):
  597. attr_key_pn = set_pop(attr_keys_pn)
  598. output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])))
  599. output("Attributes:")
  600. attr_list_pn = getAttributeList(model, cmd)
  601. attr_keys_pn = dict_keys(attr_list_pn)
  602. while (0 < read_nr_out(attr_keys_pn)):
  603. attr_key_pn = set_pop(attr_keys_pn)
  604. 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)))
  605. else:
  606. output("Unknown element; aborting")
  607. elif (cmd == "verify"):
  608. output(conformance_scd(model))
  609. elif (cmd == "types"):
  610. Element keys_t
  611. keys_t = dict_keys(model["metamodel"]["model"])
  612. output("List of types:")
  613. String v_t
  614. while (read_nr_out(keys_t) > 0):
  615. v_t = set_pop(keys_t)
  616. if (bool_not(string_startswith(v_t, "__"))):
  617. output(string_join((" " + v_t) + " : ", getName(model["metamodel"]["metamodel"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
  618. elif (cmd == "retype"):
  619. output("Element to retype?")
  620. String elementname
  621. elementname = input()
  622. if (dict_in(model["model"], elementname)):
  623. output("New type")
  624. typename = input()
  625. if (dict_in(model["metamodel"]["model"], typename)):
  626. // OK, do the retyping
  627. // First try removing the previous type if it exists
  628. dict_delete(model["type_mapping"], model["model"][elementname])
  629. // Now add the new type
  630. dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename])
  631. output("Retyped!")
  632. else:
  633. output("Unknown type; aborting")
  634. else:
  635. output("Unknown element; aborting")
  636. elif (cmd == "switch"):
  637. bottom = bool_not(bottom)
  638. Element tmp_model
  639. tmp_model = model
  640. model = other_metamodel
  641. other_metamodel = tmp_model
  642. if (bottom):
  643. // The type mapping we are using is probably not complete for our model
  644. // so we completely recreate it from the model we have.
  645. output("Switching to conformance bottom mode!")
  646. generate_bottom_type_mapping(model)
  647. else:
  648. // We already switched the models and such, so we are already done!
  649. output("Switching to linguistic metamodel!")
  650. elif (bool_and(dict_in(pn_operations(), cmd), bool_not(bottom))):
  651. // A model-specific operation, so execute that one
  652. Element specific_op
  653. specific_op = dict_read(pn_operations(), cmd)
  654. specific_op(model)
  655. else:
  656. output("Unknown command; aborting")
  657. output("Use command 'help' to get a list of available commands")
  658. Element function initial_prompt():
  659. output("Welcome to the Model Management Interface, running live on the Modelverse!")
  660. output("Use 'help' command for a list of possible commands")
  661. String command
  662. Element root
  663. Element metamodel
  664. String name
  665. Element my_model
  666. String mm_name
  667. root = create_metamodels()
  668. while (True):
  669. output("Please give your command.")
  670. command = input()
  671. if (command == "help"):
  672. output("Currently no model is loaded, so your operations are limited to:")
  673. output(" new -- Create a new model and save it for future use")
  674. output(" load -- Load a previously made model")
  675. output(" rename -- Rename a previously made model")
  676. output(" delete -- Delete a previously made model")
  677. output(" list -- Show a list of all stored models")
  678. output(" help -- Show a list of possible commands")
  679. elif (command == "new"):
  680. output("Metamodel to instantiate?")
  681. mm_name = input()
  682. if (dict_in(root, mm_name)):
  683. output("Name of model?")
  684. name = input()
  685. if (dict_in(root, name)):
  686. output("Model exists; aborting")
  687. else:
  688. my_model = instantiate_model(root[mm_name])
  689. dict_add(root, name, my_model)
  690. petrinet_loaded(my_model)
  691. else:
  692. output("Unknown metamodel; aborting")
  693. elif (command == "load"):
  694. output("Model to load?")
  695. name = input()
  696. if (dict_in(root, name)):
  697. my_model = root[name]
  698. petrinet_loaded(my_model)
  699. else:
  700. output("Model not found; aborting")
  701. elif (command == "list"):
  702. Element keys
  703. String m_menu_list
  704. keys = dict_keys(root)
  705. output("Found models:")
  706. while (read_nr_out(keys) > 0):
  707. m_menu_list = set_pop(keys)
  708. output(((" " + m_menu_list) + " : ") + reverseNameLookup(root, root[m_menu_list]["metamodel"]))
  709. elif (command == "delete"):
  710. output("Model to delete?")
  711. name = input()
  712. if (dict_in(root, name)):
  713. dict_delete(root, name)
  714. output("Deleted!")
  715. else:
  716. output("Model not found; aborting")
  717. elif (command == "rename"):
  718. output("Old name?")
  719. String old_name
  720. old_name = input()
  721. if (dict_in(root, old_name)):
  722. output("New name?")
  723. String new_name
  724. new_name = input()
  725. if (dict_in(root, new_name)):
  726. output("Model exists; aborting")
  727. else:
  728. dict_add(root, new_name, root[old_name])
  729. dict_delete(root, old_name)
  730. output("Rename complete!")
  731. else:
  732. output("Model not found; aborting")
  733. else:
  734. output("Command not recognized, use 'help' for a list of possible commands")
  735. Void function main():
  736. //initial_prompt()
  737. log("==========================================================================")
  738. log("=== creating metamodels")
  739. Element root
  740. root = create_metamodels()
  741. log("=== load instantiation of transformation metamodel")
  742. Element transf
  743. transf = petrinet_transformation(root)
  744. log("=== load test1")
  745. Element test1
  746. test1 = test_simple_petrinet1(root)
  747. log("=== check conformance of transformation")
  748. log(conformance_scd(transf))
  749. log("=== run transform")
  750. tf_transform(transf, test1)
  751. log("=== done")
  752. log("==========================================================================")
  753. return