pn_interface.alc 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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):
  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. //TODO RHS
  378. return 0
  379. Element function tf_transform(tf_tf_transformation: Element, tf_tf_model: Element):
  380. //create a list of all rules
  381. Element tf_tf_rules
  382. tf_tf_rules = allInstances(tf_tf_transformation, dict_read(dict_read(dict_read(tf_tf_transformation, "metamodel"), "model"), "Rule"))
  383. //TODO give order to rules
  384. //execute rules in random order
  385. Element tf_tf_rule
  386. while (integer_lt(0, read_nr_out(tf_tf_rules))):
  387. tf_tf_rule = set_pop(tf_tf_rules)
  388. //tf_printNode(tf_tf_transformation, tf_tf_rule)
  389. tf_executeRule(tf_tf_transformation, tf_tf_rule, tf_tf_model)
  390. return 0
  391. Element function test_simple_petrinet1(root:Element):
  392. //create a petrinet for testing purposes
  393. // P1 -> T1 -> P2 -> T2
  394. // 1 1 2 0 1
  395. //////////////////////////////////
  396. //initialise an empty petrinet
  397. Element my_model
  398. my_model = instantiate_model(root["PetriNets"])
  399. dict_add(root, "pnt_test1", my_model)
  400. // init t1
  401. instantiate_node(my_model, "Transition", "t1")
  402. // init p1
  403. instantiate_node(my_model, "Place", "p1")
  404. instantiate_attribute(my_model, "p1","tokens",1)
  405. // init link1
  406. instantiate_link(my_model, "P2T", "p1_to_t1", "p1", "t1")
  407. instantiate_attribute(my_model, "p1_to_t1", "weight", 1)
  408. //init p2
  409. instantiate_node(my_model, "Place", "p2")
  410. instantiate_attribute(my_model, "p2","tokens",0)
  411. //init link2
  412. instantiate_link(my_model, "T2P", "t1_to_p2", "t1", "p2")
  413. instantiate_attribute(my_model, "t1_to_p2", "weight", 2)
  414. //init t2
  415. instantiate_node(my_model, "Transition", "t2")
  416. //init link3
  417. instantiate_link(my_model, "P2T", "p2_to_t2", "p2", "t2")
  418. instantiate_attribute(my_model, "p2_to_t2", "weight", 1)
  419. return my_model
  420. Element function petrinet_transformation(root: Element):
  421. // Create A transformation model for petrinets (useful for testing)
  422. //////////////////////////////////
  423. //initialise an empty transformation model for petrinet
  424. Element my_model
  425. my_model = instantiate_model(root["PetriNets_MTransformation"])
  426. define_inheritance(my_model, "l3")
  427. dict_add(root, "transf_petrinet_fire", my_model)
  428. ////////////////////////////
  429. // root node
  430. instantiate_node(my_model, "Transformation", "PetrinetFire")
  431. instantiate_attribute(my_model, "PetrinetFire", "name", "PetriNetFire")
  432. ////////////////////////////
  433. // rule: find transitions
  434. //rule: find_transitions
  435. instantiate_node(my_model, "Rule", "find_transitions")
  436. instantiate_attribute(my_model, "find_transitions", "name", "find_transitions")
  437. instantiate_attribute(my_model, "find_transitions", "precedence", 1)
  438. //connect to transformation
  439. instantiate_link(my_model, "transformation_to_rule", "", "PetrinetFire", "find_transitions")
  440. //precondition: lhs_precondition
  441. instantiate_node(my_model, "Precondition","lhs_precondition")
  442. //connect to rule
  443. instantiate_link(my_model, "lhs", "", "find_transitions", "lhs_precondition")
  444. //Transition_LHS: lhs_find_all
  445. instantiate_node(my_model, "Transition_LHS","lhs_find_all")
  446. instantiate_attribute(my_model, "lhs_find_all", "plabel", 1)
  447. instantiate_attribute(my_model, "lhs_find_all", "function", "always_true")
  448. //connect to rule
  449. instantiate_link(my_model, "pre2ram", "", "lhs_precondition", "lhs_find_all")
  450. //precondition: nac_precondition
  451. instantiate_node(my_model, "Precondition","nac_precondition")
  452. //connect to rule
  453. instantiate_link(my_model, "nac", "", "find_transitions", "nac_precondition")
  454. //Transition_LHS: nac_transition_place_before
  455. instantiate_node(my_model, "Transition_LHS","nac_transition_before")
  456. instantiate_attribute(my_model, "nac_transition_before", "plabel", 1)
  457. instantiate_attribute(my_model, "nac_transition_before", "function", "always_true")
  458. //connect to rule
  459. instantiate_link(my_model, "pre2ram", "", "nac_precondition", "nac_transition_before")
  460. //Place_LHS: nac_place_before
  461. instantiate_node(my_model, "Place_LHS","nac_place_before")
  462. instantiate_attribute(my_model, "nac_place_before", "plabel", 2)
  463. instantiate_attribute(my_model, "nac_place_before", "function", "always_true")
  464. //connect to rule
  465. instantiate_link(my_model, "pre2ram", "", "nac_precondition", "nac_place_before")
  466. //P2T_LHS: nac_ft_p2t
  467. instantiate_link(my_model, "P2T_LHS", "nac_p2tlhs_before", "nac_place_before", "nac_transition_before")
  468. instantiate_attribute(my_model, "nac_p2tlhs_before", "plabel", 3)
  469. instantiate_attribute(my_model, "nac_p2tlhs_before", "function", "is_not_consumable")
  470. //connect to rule
  471. instantiate_link(my_model, "pre2ram", "", "nac_precondition", "nac_p2tlhs_before")
  472. Element semantic_checks
  473. semantic_checks = create_node()
  474. dict_add(semantic_checks, "always_true", always_true)
  475. dict_add(semantic_checks, "is_not_consumable", is_not_consumable)
  476. dict_add(my_model, "semantic_checks", semantic_checks)
  477. return my_model
  478. Boolean function is_not_consumable(model : Element, matched_node : Element):
  479. //get place before
  480. Element place_before
  481. place_before = read_edge_src(matched_node)
  482. //get tokens from place
  483. Integer tokens
  484. tokens = read_attribute(model, getName(model, place_before), "tokens")
  485. //get weight from matched_node
  486. Integer weight
  487. weight = read_attribute(model, getName(model, matched_node), "weight")
  488. return weight > tokens
  489. Boolean function always_true(model : Element, self : Element):
  490. return True
  491. Element function pn_operations():
  492. Element ops
  493. ops = create_node()
  494. dict_add(ops, "fire", petrinet_fire)
  495. dict_add(ops, "enabled", petrinet_enabled)
  496. return ops
  497. Element function petrinet_enabled(model : Element):
  498. Element set_enabled
  499. set_enabled = petrinet_enabled_set(model)
  500. output("Enabled transitions:")
  501. while (0 < read_nr_out(set_enabled)):
  502. output(getName(model, set_pop(set_enabled)))
  503. return model
  504. Element function petrinet_enabled_set(model : Element):
  505. Element all_transitions
  506. all_transitions = allInstances(model, model["metamodel"]["model"]["Transition"])
  507. Element enabled_transitions
  508. enabled_transitions = create_node()
  509. Element under_study
  510. Element in_arcs
  511. Element arc_under_study
  512. Boolean enabled
  513. while (0 < read_nr_out(all_transitions)):
  514. under_study = set_pop(all_transitions)
  515. enabled = True
  516. // Find all incoming transitions
  517. in_arcs = allIncomingAssociationInstances(model, under_study, model["metamodel"]["model"]["P2T"])
  518. while (0 < read_nr_out(in_arcs)):
  519. arc_under_study = set_pop(in_arcs)
  520. Integer present_tokens
  521. Integer required_tokens
  522. required_tokens = read_attribute(model, getName(model, arc_under_study), "weight")
  523. present_tokens = read_attribute(model, getName(model, read_edge_src(arc_under_study)), "tokens")
  524. if (present_tokens < required_tokens):
  525. // Less tokens than required, so disable the transition completely
  526. enabled = False
  527. if (enabled):
  528. set_add(enabled_transitions, under_study)
  529. return enabled_transitions
  530. Element function petrinet_fire(model : Element):
  531. output("Transition to fire?")
  532. Element transition
  533. transition = input()
  534. if (dict_in(model["model"], transition)):
  535. transition = model["model"][transition]
  536. if (set_in(petrinet_enabled_set(model), transition)):
  537. Element workset
  538. Element working_place
  539. Element working_arc
  540. Integer new_value
  541. // Consume tokens
  542. workset = allIncomingAssociationInstances(model, transition, model["metamodel"]["model"]["P2T"])
  543. while (0 < read_nr_out(workset)):
  544. working_arc = set_pop(workset)
  545. working_place = read_edge_src(working_arc)
  546. new_value = integer_subtraction(read_attribute(model, getName(model, working_place), "tokens"), read_attribute(model, getName(model, working_arc), "weight"))
  547. unset_attribute(model, getName(model, working_place), "tokens")
  548. instantiate_attribute(model, getName(model, working_place), "tokens", new_value)
  549. output(((" " + getName(model, working_place)) + ": ") + cast_i2s(read_attribute(model, getName(model, working_place), "tokens")))
  550. // Add tokens
  551. workset = allOutgoingAssociationInstances(model, transition, model["metamodel"]["model"]["T2P"])
  552. while (0 < read_nr_out(workset)):
  553. working_arc = set_pop(workset)
  554. working_place = read_edge_dst(working_arc)
  555. new_value = integer_addition(read_attribute(model, getName(model, working_place), "tokens"), read_attribute(model, getName(model, working_arc), "weight"))
  556. unset_attribute(model, getName(model, working_place), "tokens")
  557. instantiate_attribute(model, getName(model, working_place), "tokens", new_value)
  558. output(((" " + getName(model, working_place)) + ": ") + cast_i2s(read_attribute(model, getName(model, working_place), "tokens")))
  559. output("Transition fired!")
  560. else:
  561. output("Cannot fire if not enabled; aborting")
  562. else:
  563. output("Unknown transition; aborting")
  564. return model
  565. Element function petrinet_loaded(model : Element):
  566. String cmd
  567. Element attr_list_pn
  568. Element attr_keys_pn
  569. String attr_key_pn
  570. Element metamodel_element_pn
  571. String typename
  572. Boolean bottom
  573. Element other_metamodel
  574. bottom = False
  575. other_metamodel = create_node()
  576. dict_add(other_metamodel, "model", model["model"])
  577. dict_add(other_metamodel, "type_mapping", create_node())
  578. dict_add(other_metamodel, "metamodel", import_node("models/LTM_bottom"))
  579. dict_add(other_metamodel, "inheritance", other_metamodel["metamodel"]["model"]["__Inheritance"])
  580. output("Model loaded, ready for commands!")
  581. output("Use 'help' command for a list of possible commands")
  582. while (True):
  583. output("Please give your command.")
  584. cmd = input()
  585. if (cmd == "help"):
  586. output("Generic model operations:")
  587. output(" instantiate -- Create a new model element")
  588. output(" delete -- Delete an existing element")
  589. output(" attr_add -- Add an attribute to an element")
  590. output(" attr_del -- Delete an attribute of an element")
  591. output(" rename -- Rename an existing element")
  592. output(" modify -- Modify the attributes of an element")
  593. output(" list -- Prints the list of elements in the model")
  594. output(" types -- Prints the list of elements that can be instantiated")
  595. output(" read -- Prints the current state of a model element")
  596. output(" verify -- Check whether the model conforms to the metamodel")
  597. output(" retype -- Change the type of an element")
  598. output(" switch -- Switch between conformance bottom and the linguistic metamodel")
  599. output(" exit -- Unload the model and go back to the loading prompt")
  600. if (bool_not(bottom)):
  601. output("Model-specific operations:")
  602. Element specific_ops
  603. specific_ops = dict_keys(pn_operations())
  604. String specific_op
  605. while (0 < dict_len(specific_ops)):
  606. specific_op = set_pop(specific_ops)
  607. output(" " + specific_op)
  608. elif (cmd == "exit"):
  609. return model
  610. elif (cmd == "instantiate"):
  611. String mm_type_name
  612. output("Type to instantiate?")
  613. mm_type_name = input()
  614. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  615. String element_name
  616. output("Name of new element?")
  617. element_name = input()
  618. if (dict_in(model["model"], element_name)):
  619. output("Element already exists; aborting")
  620. else:
  621. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  622. output("Source name?")
  623. String src_name
  624. src_name = input()
  625. if (dict_in(model["model"], src_name)):
  626. output("Destination name?")
  627. String dst_name
  628. dst_name = input()
  629. if (dict_in(model["model"], dst_name)):
  630. instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
  631. if (dict_in(model["model"], element_name)):
  632. output("Instantiation successful!")
  633. else:
  634. output("Instantiation error!")
  635. else:
  636. output("Unknown destination; aborting")
  637. else:
  638. output("Unknown source; aborting")
  639. else:
  640. instantiate_node(model, mm_type_name, element_name)
  641. if (dict_in(model["model"], element_name)):
  642. output("Instantiation successful!")
  643. else:
  644. output("Instantiation error!")
  645. else:
  646. output("Unknown type specified; aborting")
  647. elif (cmd == "modify"):
  648. String model_name
  649. output("Element to modify?")
  650. model_name = input()
  651. if (dict_in(model["model"], model_name)):
  652. Element attrs
  653. attrs = getAttributeList(model, model_name)
  654. String attr_name
  655. output("Attribute to modify?")
  656. attr_name = input()
  657. if (set_in(dict_keys(attrs), attr_name)):
  658. output("New value?")
  659. unset_attribute(model, model_name, attr_name)
  660. instantiate_attribute(model, model_name, attr_name, input())
  661. output("Modified!")
  662. else:
  663. output("No such attribute!")
  664. else:
  665. output("No such model!")
  666. elif (cmd == "attr_add"):
  667. String model_name
  668. output("Which model do you want to assign an attribute to?")
  669. model_name = input()
  670. if (dict_in(model["model"], model_name)):
  671. Element attrs
  672. attrs = getAttributeList(model, model_name)
  673. String attr_name
  674. output("Which attribute do you wish to assign?")
  675. attr_name = input()
  676. if (set_in(dict_keys(attrs), attr_name)):
  677. output("Value of attribute?")
  678. instantiate_attribute(model, model_name, attr_name, input())
  679. output("Added attribute!")
  680. else:
  681. output("No such attribute!")
  682. else:
  683. output("No such model!")
  684. elif (cmd == "attr_del"):
  685. String model_name
  686. output("Which model do you want to remove an attribute of?")
  687. model_name = input()
  688. if (dict_in(model["model"], model_name)):
  689. Element attrs
  690. attrs = getAttributeList(model, model_name)
  691. output(print_dict(attrs))
  692. String attr_name
  693. output("Which attribute do you want to delete?")
  694. attr_name = input()
  695. if (set_in(dict_keys(attrs), attr_name)):
  696. unset_attribute(model, model_name, attr_name)
  697. else:
  698. output("No such attribute!")
  699. else:
  700. output("No such model!")
  701. elif (cmd == "delete"):
  702. output("What is the name of the element you want to delete?")
  703. cmd = input()
  704. if (dict_in(model["model"], cmd)):
  705. model_delete_element(model, cmd)
  706. output("Deleted!")
  707. else:
  708. output("No such element; aborting")
  709. elif (cmd == "rename"):
  710. output("Old name?")
  711. String old_name_e
  712. old_name_e = input()
  713. if (dict_in(model["model"], old_name_e)):
  714. output("New name?")
  715. String new_name_e
  716. new_name_e = input()
  717. if (dict_in(model["model"], new_name_e)):
  718. output("New name already used; aborting")
  719. else:
  720. dict_add(model["model"], new_name_e, model["model"][old_name_e])
  721. dict_delete(model["model"], old_name_e)
  722. output("Rename complete!")
  723. else:
  724. output("Unknown element; aborting")
  725. elif (cmd == "list"):
  726. Element keys_m
  727. keys_m = dict_keys(model["model"])
  728. output("List of all elements:")
  729. String v_m
  730. while (read_nr_out(keys_m) > 0):
  731. v_m = set_pop(keys_m)
  732. // Filter out anonymous objects
  733. typename = getName(model["metamodel"], dict_read_node(model["type_mapping"], model["model"][v_m]))
  734. if (bool_not(string_startswith(v_m, "__"))):
  735. output(((" " + v_m) + " : ") + typename)
  736. elif (cmd == "read"):
  737. output("Element to read?")
  738. cmd = input()
  739. if (dict_in(model["model"], cmd)):
  740. Element read_elem
  741. read_elem = model["model"][cmd]
  742. metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
  743. output("Name: " + cmd)
  744. output("Type: " + getName(model["metamodel"], metamodel_element_pn))
  745. if (is_edge(read_elem)):
  746. output("Source: " + getName(model, read_edge_src(read_elem)))
  747. output("Destination: " + getName(model, read_edge_dst(read_elem)))
  748. if (cast_v2s(read_elem) != "None"):
  749. output("Value: " + cast_v2s(read_elem))
  750. output("Defines attributes:")
  751. attr_list_pn = getInstantiatableAttributes(model, read_elem)
  752. attr_keys_pn = dict_keys(attr_list_pn)
  753. while (0 < read_nr_out(attr_keys_pn)):
  754. attr_key_pn = set_pop(attr_keys_pn)
  755. output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])))
  756. output("Attributes:")
  757. attr_list_pn = getAttributeList(model, cmd)
  758. attr_keys_pn = dict_keys(attr_list_pn)
  759. while (0 < read_nr_out(attr_keys_pn)):
  760. attr_key_pn = set_pop(attr_keys_pn)
  761. 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)))
  762. else:
  763. output("Unknown element; aborting")
  764. elif (cmd == "verify"):
  765. output(conformance_scd(model))
  766. elif (cmd == "types"):
  767. Element keys_t
  768. keys_t = dict_keys(model["metamodel"]["model"])
  769. output("List of types:")
  770. String v_t
  771. while (read_nr_out(keys_t) > 0):
  772. v_t = set_pop(keys_t)
  773. if (bool_not(string_startswith(v_t, "__"))):
  774. output(string_join((" " + v_t) + " : ", getName(model["metamodel"]["metamodel"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
  775. elif (cmd == "retype"):
  776. output("Element to retype?")
  777. String elementname
  778. elementname = input()
  779. if (dict_in(model["model"], elementname)):
  780. output("New type")
  781. typename = input()
  782. if (dict_in(model["metamodel"]["model"], typename)):
  783. // OK, do the retyping
  784. // First try removing the previous type if it exists
  785. dict_delete(model["type_mapping"], model["model"][elementname])
  786. // Now add the new type
  787. dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename])
  788. output("Retyped!")
  789. else:
  790. output("Unknown type; aborting")
  791. else:
  792. output("Unknown element; aborting")
  793. elif (cmd == "switch"):
  794. bottom = bool_not(bottom)
  795. Element tmp_model
  796. tmp_model = model
  797. model = other_metamodel
  798. other_metamodel = tmp_model
  799. if (bottom):
  800. // The type mapping we are using is probably not complete for our model
  801. // so we completely recreate it from the model we have.
  802. output("Switching to conformance bottom mode!")
  803. generate_bottom_type_mapping(model)
  804. else:
  805. // We already switched the models and such, so we are already done!
  806. output("Switching to linguistic metamodel!")
  807. elif (bool_and(dict_in(pn_operations(), cmd), bool_not(bottom))):
  808. // A model-specific operation, so execute that one
  809. Element specific_op
  810. specific_op = dict_read(pn_operations(), cmd)
  811. specific_op(model)
  812. else:
  813. output("Unknown command; aborting")
  814. output("Use command 'help' to get a list of available commands")
  815. Element function initial_prompt():
  816. output("Welcome to the Model Management Interface, running live on the Modelverse!")
  817. output("Use 'help' command for a list of possible commands")
  818. String command
  819. Element root
  820. Element metamodel
  821. String name
  822. Element my_model
  823. String mm_name
  824. root = create_metamodels()
  825. while (True):
  826. output("Please give your command.")
  827. command = input()
  828. if (command == "help"):
  829. output("Currently no model is loaded, so your operations are limited to:")
  830. output(" new -- Create a new model and save it for future use")
  831. output(" load -- Load a previously made model")
  832. output(" rename -- Rename a previously made model")
  833. output(" delete -- Delete a previously made model")
  834. output(" list -- Show a list of all stored models")
  835. output(" help -- Show a list of possible commands")
  836. elif (command == "new"):
  837. output("Metamodel to instantiate?")
  838. mm_name = input()
  839. if (dict_in(root, mm_name)):
  840. output("Name of model?")
  841. name = input()
  842. if (dict_in(root, name)):
  843. output("Model exists; aborting")
  844. else:
  845. my_model = instantiate_model(root[mm_name])
  846. dict_add(root, name, my_model)
  847. petrinet_loaded(my_model)
  848. else:
  849. output("Unknown metamodel; aborting")
  850. elif (command == "load"):
  851. output("Model to load?")
  852. name = input()
  853. if (dict_in(root, name)):
  854. my_model = root[name]
  855. petrinet_loaded(my_model)
  856. else:
  857. output("Model not found; aborting")
  858. elif (command == "list"):
  859. Element keys
  860. String m_menu_list
  861. keys = dict_keys(root)
  862. output("Found models:")
  863. while (read_nr_out(keys) > 0):
  864. m_menu_list = set_pop(keys)
  865. output(((" " + m_menu_list) + " : ") + reverseNameLookup(root, root[m_menu_list]["metamodel"]))
  866. elif (command == "delete"):
  867. output("Model to delete?")
  868. name = input()
  869. if (dict_in(root, name)):
  870. dict_delete(root, name)
  871. output("Deleted!")
  872. else:
  873. output("Model not found; aborting")
  874. elif (command == "rename"):
  875. output("Old name?")
  876. String old_name
  877. old_name = input()
  878. if (dict_in(root, old_name)):
  879. output("New name?")
  880. String new_name
  881. new_name = input()
  882. if (dict_in(root, new_name)):
  883. output("Model exists; aborting")
  884. else:
  885. dict_add(root, new_name, root[old_name])
  886. dict_delete(root, old_name)
  887. output("Rename complete!")
  888. else:
  889. output("Model not found; aborting")
  890. else:
  891. output("Command not recognized, use 'help' for a list of possible commands")
  892. Void function main():
  893. //initial_prompt()
  894. log("==========================================================================")
  895. log("=== creating metamodels")
  896. Element root
  897. root = create_metamodels()
  898. log("=== load instantiation of transformation metamodel")
  899. Element transf
  900. transf = petrinet_transformation(root)
  901. log("=== load test1")
  902. Element test1
  903. test1 = test_simple_petrinet1(root)
  904. log("=== check conformance of transformation")
  905. log(conformance_scd(transf))
  906. log("=== run transform")
  907. tf_transform(transf, test1)
  908. log("=== done")
  909. log("==========================================================================")
  910. return