pn_interface.alc 39 KB

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