pn_interface.alc 35 KB

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