pn_interface.alc 48 KB

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