pn_interface.alc 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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. Element pivots
  69. Boolean function node_match(transf_model : Element, transf_elem : Element, host_model : Element, host_elem : Element):
  70. //type "equality"
  71. String type1
  72. type1 = getTypeName(transf_model, transf_elem)
  73. String type2
  74. type2 = string_join(getTypeName(host_model, host_elem), "_LHS")
  75. if (type1 != type2):
  76. return False
  77. //semantic check
  78. Element func
  79. func = transf_model["semantic_checks"][read_attribute(transf_model, getName(transf_model,transf_elem), "function")]
  80. //pivot check
  81. String pivotInName
  82. pivotInName = read_attribute(transf_model, getName(transf_model, transf_elem), "pivotIn")
  83. if(func(host_model, host_elem)):
  84. if( pivotInName == ""):
  85. return True
  86. else:
  87. if(set_in(pivots[pivotInName], getName(host_model,host_elem))):
  88. return True
  89. else:
  90. return False
  91. return False
  92. Integer function are_connected(model: Element, el1:Element, el2:Element):
  93. if ( bool_not(is_edge(el1)) ):
  94. if(bool_not(is_edge(el2))):
  95. return 0 //not connected, no edge inbetween
  96. elif(is_edge(el2)):
  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. if( element_eq(read_edge_src(el1) , el2)):
  105. return 2 //incoming
  106. elif( element_eq(read_edge_dst(el1) , el2)):
  107. return 1 //outgoing
  108. else:
  109. return 0 //edge is not connected
  110. return 0 // if the function doesn't support it, assume disconnected
  111. Element function read_outgoing_assoc(model:Element, el1:Element, allowed:Element):
  112. Element result
  113. result = create_node()
  114. if(is_edge(el1)):
  115. set_add(result, read_edge_dst(el1))
  116. return result
  117. else:
  118. Integer length
  119. length = read_nr_out(el1)
  120. log(cast_i2s(length))
  121. Integer counter
  122. counter = 0
  123. Element edge
  124. while (counter < length):
  125. edge = read_out(el1, counter)
  126. //check if edge is part of the model
  127. if (set_in_node(allowed, edge)):
  128. //check if it is not attribute related
  129. if(bool_not(isAttributeRelated(model, edge))):
  130. set_add(result, edge)
  131. counter = counter + 1
  132. return result
  133. return create_node()
  134. Element function read_incoming_assoc(model:Element, el1:Element, allowed:Element):
  135. Element result
  136. result = create_node()
  137. if(is_edge(el1)):
  138. set_add(result, read_edge_src(el1))
  139. return result
  140. else:
  141. //get possible types of associations
  142. Integer length
  143. length = read_nr_in(model["typemapping"][el1])
  144. Integer counter
  145. counter = 0
  146. Element edge
  147. while (counter < length):
  148. edge = read_in(el1, counter)
  149. //check if edge is part of the model
  150. if (set_in_node(allowed, edge)):
  151. //check if it is not attribute related
  152. if(bool_not(isAttributeRelated(model, edge))):
  153. set_add(result, edge)
  154. counter = counter + 1
  155. return result
  156. return create_node()
  157. Element function new_SearchState(OldSearchState : Element, TransfModel : Element, NewTransfElem: Element, HostModel : Element, NewHostElem : Element):
  158. //some variables
  159. Element nss // new searchstate
  160. nss = create_node()
  161. Integer index // used for list looping
  162. Element elem // reusable loop variable
  163. Element ol //old list = old pattern out
  164. Element nl //new list = new N pattern out
  165. Element allowedP
  166. allowedP = OldSearchState["V_pattern"]
  167. Element allowedH
  168. allowedH = OldSearchState["V_host"]
  169. //new MATCHES
  170. dict_add(nss, "matches", OldSearchState["matches"]) //hopefully works as a reference
  171. //new CURMATCH
  172. Element OldCurMatchKeys // for looping trough old curmatch (for copying)
  173. OldCurMatchKeys = dict_keys(OldSearchState["curmatch"])
  174. Element new_curmatch //the new curmatch
  175. new_curmatch = create_node()
  176. Element new_curmatch_pattern //keep a set of all matches pattern nodes
  177. new_curmatch_pattern = create_node()
  178. Element new_curmatch_host //keep a set of all matched host nodes
  179. new_curmatch_host = create_node()
  180. while (integer_lt(0, list_len(OldCurMatchKeys))): //copying the old curmatch
  181. index = set_pop(OldCurMatchKeys)
  182. dict_add(new_curmatch, index, OldSearchState["curmatch"][index])
  183. set_add(new_curmatch_pattern, OldSearchState["curmatch"][index]["pn"] )
  184. set_add(new_curmatch_host, OldSearchState["curmatch"][index]["hn"] )
  185. Element newPair // the new pair to be added
  186. newPair = create_node()
  187. dict_add(newPair, "pn", NewTransfElem) //PatternNode
  188. set_add(new_curmatch_pattern, NewTransfElem)
  189. dict_add(newPair, "hn", NewHostElem) // HostNode
  190. set_add(new_curmatch_host, NewHostElem)
  191. Integer plabel
  192. plabel = read_attribute(TransfModel, getName(TransfModel, NewTransfElem), "plabel")
  193. dict_add(new_curmatch, plabel, newPair) //add pair to new curmatch
  194. dict_add(nss, "curmatch", new_curmatch) //add curmatch to state
  195. //new CURMATCH_pattern and host (constructed during the previous loop)
  196. dict_add(nss, "curmatch_pattern", new_curmatch_pattern)
  197. dict_add(nss, "curmatch_host", new_curmatch_pattern)
  198. //new N_PATTERN_OUT (copy everything but the new match elements)
  199. ol = OldSearchState["N_pattern_out"]
  200. nl = create_node()
  201. index = 0
  202. while (index < list_len(ol)): //loop trough old list
  203. elem = list_read(ol, index)
  204. index = index + 1
  205. if(bool_not(element_eq(elem, NewTransfElem))):
  206. set_add(nl, elem)
  207. ol = read_outgoing_assoc(TransfModel, NewTransfElem, allowedP)
  208. while (read_nr_out(ol)>0):
  209. elem = set_pop(ol)
  210. if (bool_not(set_in_node(new_curmatch_pattern, elem))):
  211. list_append(nl,elem)
  212. dict_add(nss, "N_pattern_out", nl)
  213. //new N_HOST_OUT (copy everything but the new match elements)
  214. ol = OldSearchState["N_host_out"]
  215. nl = create_node()
  216. index = 0
  217. while (index < list_len(ol)): //loop trough old list
  218. elem = list_read(ol, index)
  219. index = index + 1
  220. if(bool_not(element_eq(elem, NewHostElem))):
  221. list_append(nl, elem)
  222. ol = read_outgoing_assoc(HostModel, NewHostElem, allowedH)
  223. while (read_nr_out(ol)>0):
  224. elem = set_pop(ol)
  225. if (bool_not(set_in_node(new_curmatch_host, elem))):
  226. list_append(nl,elem)
  227. dict_add(nss, "N_host_out", nl)
  228. //new N_PATTERN_IN (copy everything but the new match elements)
  229. ol = OldSearchState["N_pattern_in"]
  230. nl = create_node()
  231. index = 0
  232. while (index < list_len(ol)): //loop trough old list
  233. elem = list_read(ol, index)
  234. index = index + 1
  235. if(bool_not(element_eq(elem, NewTransfElem))):
  236. list_append(nl, elem)
  237. ol = read_incoming_assoc(TransfModel, NewTransfElem, allowedP)
  238. while (read_nr_out(ol)>0):
  239. elem = set_pop(ol)
  240. if (bool_not(set_in_node(new_curmatch_pattern, elem))):
  241. list_append(nl,elem)
  242. dict_add(nss, "N_pattern_in", nl)
  243. //new N_HOST_IN (copy everything but the new match elements
  244. ol = OldSearchState["N_host_in"]
  245. nl = create_node()
  246. index = 0
  247. while (index < list_len(ol)): //loop trough old list
  248. elem = list_read(ol, index)
  249. index = index + 1
  250. if(bool_not(element_eq(elem, NewHostElem))):
  251. list_append(nl, elem)
  252. ol = read_incoming_assoc(HostModel, NewHostElem, allowedH)
  253. while (read_nr_out(ol) > 0):
  254. elem = set_pop(ol)
  255. if (bool_not(set_in_node(new_curmatch_host, elem))):
  256. list_append(nl,elem)
  257. dict_add(nss, "N_host_in", nl)
  258. //new V_PATTERN
  259. dict_add(nss, "V_pattern", allowedP)
  260. //new V_HOST
  261. dict_add(nss, "V_host", allowedH)
  262. // copy pattern_size
  263. dict_add(nss, "pattern_size", OldSearchState["pattern_size"])
  264. return nss
  265. Element function destruct(nss:Element):
  266. log("destructing")
  267. //destruct current_match
  268. Element todelete
  269. while (read_nr_out(nss["curmatch"])>0):
  270. todelete = set_pop(nss["curmatch"])
  271. delete_element(todelete)
  272. delete_element(nss["curmatch_pattern"])
  273. delete_element(nss["curmatch_host"])
  274. delete_element(nss["N_pattern_out"])
  275. delete_element(nss["N_pattern_in"])
  276. delete_element(nss["N_host_in"])
  277. delete_element(nss["N_host_out"])
  278. delete_element(nss)
  279. log("end destructing")
  280. return 0
  281. Element function transf_vf2(SearchState: Element, transf_m : Element, host_m : Element):
  282. log("transf_vf2")
  283. // get the current match from the search-state
  284. Element current_match
  285. current_match = dict_read(SearchState,"curmatch")
  286. //if match is complete: add to matches and return
  287. Integer pattern_size
  288. pattern_size = SearchState["pattern_size"]
  289. if pattern_size == list_len(current_match):
  290. Element OldCurMatchKeys // for looping trough old curmatch (for copying)
  291. OldCurMatchKeys = dict_keys(SearchState["curmatch"])
  292. Element new_curmatch //the new curmatch
  293. new_curmatch = create_node()
  294. Element deepcopy
  295. Integer index
  296. while (integer_lt(0, list_len(OldCurMatchKeys))): //copying the old curmatch
  297. index = set_pop(OldCurMatchKeys)
  298. deepcopy = create_node()
  299. dict_add(deepcopy, "pn", SearchState["curmatch"][index]["pn"])
  300. dict_add(deepcopy, "hn", SearchState["curmatch"][index]["hn"])
  301. dict_add(new_curmatch, index, deepcopy)
  302. list_append(SearchState["matches"], new_curmatch)
  303. //log("MATCHED")
  304. return SearchState
  305. //feasability checks
  306. if (list_len(SearchState["N_pattern_out"]) > list_len(SearchState["N_host_out"])):
  307. return SearchState
  308. if (list_len(SearchState["N_pattern_in"]) > list_len(SearchState["N_host_in"])):
  309. return SearchState
  310. //initialise some variables
  311. Integer index2 //an index used for looping trough host_list
  312. Element host_list // the set with host nodes to be considered
  313. Element pattern_node // node to be tested from the patterns side
  314. Element host_node //node to be tested from the hosts side
  315. Element nss //variable for storing next searchstate
  316. //search candidate
  317. if ( list_len(SearchState["N_pattern_out"]) > 0 ):
  318. //check for candidates in (N_pattern_out, N_host_out)
  319. host_list = SearchState["N_host_out"]
  320. pattern_node = list_read(SearchState["N_pattern_out"], 0)
  321. index2 = 0
  322. while (index2 < list_len(host_list)):
  323. host_node = list_read(host_list, index2)
  324. index2= index2 + 1
  325. if(node_match(transf_m, pattern_node, host_m, host_node)):
  326. nss = new_SearchState(SearchState, transf_m, pattern_node, host_m, host_node)
  327. nss = transf_vf2(nss, transf_m, host_m)
  328. dict_delete(SearchState, "matches")
  329. dict_add(SearchState, "matches", nss["matches"])
  330. destruct(nss)
  331. elif ( list_len(SearchState["N_pattern_in"]) > 0 ):
  332. //check for candidates in (N_pattern_in, N_host_in)
  333. host_list = SearchState["N_host_in"]
  334. pattern_node = list_read(SearchState["N_pattern_in"], 0)
  335. index2 = 0
  336. while (index2 < list_len(host_list)):
  337. host_node = list_read(host_list, index2)
  338. index2= index2 + 1
  339. if(node_match(transf_m, pattern_node, host_m, host_node)):
  340. nss = new_SearchState(SearchState, transf_m, pattern_node, host_m, host_node)
  341. nss = transf_vf2(nss, transf_m, host_m)
  342. dict_delete(SearchState, "matches")
  343. dict_add(SearchState, "matches", nss["matches"])
  344. destruct(nss)
  345. elif ( list_len(SearchState["V_pattern"]) > 0 ):
  346. //check for candidates in (V_pattern, V_host))
  347. host_list = SearchState["V_host"]
  348. pattern_node = set_pop(SearchState["V_pattern"])
  349. //list_read(SearchState["V_pattern"], 0)
  350. index2 = 0
  351. while (index2 < list_len(host_list)):
  352. host_node = list_read(host_list, index2)
  353. index2= index2 + 1
  354. if(node_match(transf_m, pattern_node, host_m, host_node)):
  355. nss = new_SearchState(SearchState, transf_m, pattern_node, host_m, host_node)
  356. nss = transf_vf2(nss, transf_m, host_m)
  357. dict_delete(SearchState, "matches")
  358. dict_add(SearchState, "matches", nss["matches"])
  359. destruct(nss)
  360. else:
  361. log("no candidate found")
  362. log(" end transf_vf2")
  363. return SearchState
  364. // given a pattern, this function will return all matches in a model
  365. Element function tf_matchAll(tf_ma_transformation: Element, tf_ma_pattern : Element, tf_ma_model: Element ):
  366. //timing info
  367. Integer starttime
  368. starttime = current_time()
  369. //find all nodes in the pattern
  370. Element tf_ma_metamodelTransf //transformation metamodel
  371. tf_ma_metamodelTransf = tf_ma_transformation["metamodel"]["model"]
  372. Element tf_ma_pattern_edges //all pre2ram edges
  373. tf_ma_pattern_edges = allOutgoingAssociationInstances(tf_ma_transformation, tf_ma_pattern, dict_read(tf_ma_metamodelTransf, "pre2ram"))
  374. Element tf_ma_ramified_nodes //dict in the form plabel->node
  375. tf_ma_ramified_nodes = create_node()
  376. //get the destinations
  377. while (integer_lt(0, list_len(tf_ma_pattern_edges))):
  378. list_append(tf_ma_ramified_nodes, read_edge_dst(set_pop(tf_ma_pattern_edges)))
  379. //find all nodes in the graph that are not related to attributes: ie a userdefined type
  380. Element hostmodel_keys //keys of all elements in the model
  381. hostmodel_keys = dict_keys(tf_ma_model["model"])
  382. Element host_nodes //all nodes in the host graph
  383. host_nodes = create_node()
  384. Element host_model_key
  385. while (integer_lt(0, read_nr_out(hostmodel_keys))):
  386. host_model_key = set_pop(hostmodel_keys)
  387. if bool_not(isAttributeRelated(tf_ma_model, tf_ma_model["model"][host_model_key])):
  388. list_append(host_nodes, tf_ma_model["model"][host_model_key])
  389. // initialise the start-state
  390. //initialise an start state
  391. Element SearchState
  392. SearchState = create_node()
  393. dict_add(SearchState, "matches", create_node())
  394. dict_add(SearchState, "curmatch", create_node())
  395. dict_add(SearchState, "curmatch_pattern", create_node())
  396. dict_add(SearchState, "curmatch_host", create_node())
  397. dict_add(SearchState, "N_pattern_out", create_node())
  398. dict_add(SearchState, "N_host_out", create_node())
  399. dict_add(SearchState, "N_pattern_in", create_node())
  400. dict_add(SearchState, "N_host_in", create_node())
  401. dict_add(SearchState, "V_pattern", tf_ma_ramified_nodes)
  402. dict_add(SearchState, "V_host", host_nodes)
  403. dict_add(SearchState, "pattern_size", list_len(tf_ma_ramified_nodes))
  404. //call vf2 for the matching
  405. SearchState = transf_vf2(SearchState, tf_ma_transformation, tf_ma_model)
  406. //timing info
  407. log(string_join("matching took ", cast_i2s( current_time() - starttime)))
  408. delete_element(tf_ma_ramified_nodes)
  409. delete_element(host_nodes)
  410. Element matches
  411. matches = SearchState["matches"]
  412. destruct(SearchState)
  413. return matches
  414. Element function mergeLHSNAC(tf_er_lhs : Element, tf_er_nac: Element):
  415. //timing info
  416. Integer starttime
  417. starttime = current_time()
  418. Element final_matches
  419. final_matches = create_node()
  420. Element lhs_match
  421. Element nac_match
  422. Element lhs_match_keys
  423. Element lhs_match_key
  424. Boolean allow_match
  425. Integer nac_index
  426. Boolean plabel_match_exists
  427. while(list_len(tf_er_lhs) > 0):
  428. lhs_match = set_pop(tf_er_lhs)
  429. allow_match=True
  430. nac_index = 0
  431. while(bool_and(nac_index < read_nr_out(tf_er_nac), allow_match)):
  432. nac_match = list_read(tf_er_nac, nac_index)
  433. nac_index = nac_index + 1
  434. lhs_match_keys = dict_keys(lhs_match)
  435. plabel_match_exists = False //we assume that the nac prohibits all
  436. while (bool_and(read_nr_out(lhs_match_keys)>0, allow_match)):
  437. lhs_match_key = set_pop(lhs_match_keys)
  438. if dict_in(nac_match, lhs_match_key):
  439. plabel_match_exists = True //the assumption is disproved, which means we can continue looking
  440. if (element_eq(nac_match[lhs_match_key]["hn"],lhs_match[lhs_match_key]["hn"])):
  441. allow_match = False
  442. if bool_not(plabel_match_exists): //if the assumption is not disproved, the nac prohibits all matches and no matches should remain after merge
  443. return create_node()
  444. if (allow_match):
  445. list_append(final_matches, lhs_match)
  446. //timing info
  447. log(string_join("merging LHS with NAC took ", cast_i2s( current_time() - starttime)))
  448. return final_matches
  449. Element function extractPivots(final_matches : Element, tf_er_transformation : Element, tf_er_model : Element):
  450. Integer match_index
  451. match_index = 0
  452. Element lhs_match
  453. Element lhs_match_keys
  454. Element lhs_match_key
  455. String pivotOutName
  456. Element pivotSet
  457. pivotSet = create_node()
  458. while (match_index < read_nr_out(final_matches)):
  459. lhs_match = list_read(final_matches, match_index)
  460. match_index = match_index +1
  461. lhs_match_keys = dict_keys(lhs_match)
  462. while (read_nr_out(lhs_match_keys) > 0):
  463. lhs_match_key = set_pop(lhs_match_keys)
  464. pivotOutName = read_attribute(tf_er_transformation, getName(tf_er_transformation, lhs_match[lhs_match_key]["pn"]), "pivotOut")
  465. if(pivotOutName != ""):
  466. if ( bool_not(set_in(pivotSet, pivotOutName))):
  467. set_add(pivotSet, pivotOutName)
  468. if (dict_in(pivots, pivotOutName)):
  469. dict_delete(pivots, pivotOutName)
  470. dict_add(pivots, pivotOutName, create_node())
  471. set_add(pivots[pivotOutName], getName(tf_er_model,lhs_match[lhs_match_key]["hn"]))
  472. return 0
  473. Element function cast_set2list(sset:Element):
  474. Element rreturnlist //this name must be made weird, otherwise compilation error
  475. rreturnlist = create_node()
  476. while (read_nr_out(sset) > 0):
  477. list_append(rreturnlist, set_pop(sset))
  478. return rreturnlist
  479. Element function rhs_rewrite(final_matches : Element,plabel_to_rhsnode:Element, tf_er_transformation:Element, tf_er_model:Element ):
  480. //timing info
  481. Integer starttime
  482. starttime = current_time()
  483. //loop over all matches
  484. Element rhs_plabels
  485. Element rhs_plabel
  486. Element rhs_action
  487. Element rhs_to_host //keep this mapping, so that we know wich nodes in the hostmodel new edges connect
  488. Element lhs_match_keys
  489. Element lhs_match_key
  490. Element host_match_node
  491. Element lhs_match
  492. Integer source_plabel
  493. Integer destination_plabel
  494. Integer index
  495. while (read_nr_out(final_matches)>0):
  496. lhs_match = set_pop(final_matches)
  497. //get set of plables
  498. rhs_plabels = dict_keys(plabel_to_rhsnode)
  499. //loop trough every node in the match
  500. lhs_match_keys = dict_keys(lhs_match)
  501. rhs_to_host = create_node()
  502. while (read_nr_out(lhs_match_keys) > 0):
  503. lhs_match_key = set_pop(lhs_match_keys)
  504. host_match_node = lhs_match[lhs_match_key]["hn"]
  505. if set_in(rhs_plabels, lhs_match_key): //if node matches plabel with rhs
  506. //run function
  507. rhs_action = read_attribute(tf_er_transformation,getName(tf_er_transformation, plabel_to_rhsnode[lhs_match_key]), "function")
  508. rhs_action = tf_er_transformation["actions"][rhs_action]
  509. rhs_action(tf_er_model, host_match_node)
  510. dict_add(rhs_to_host, lhs_match_key, host_match_node)
  511. //remove key from rhs_plabels
  512. set_remove(rhs_plabels, lhs_match_key)
  513. else: // if node doesn't match plabel with rhs
  514. model_delete_element(tf_er_model, getName(tf_er_model, host_match_node))
  515. //for each remaining rhs-key (it is not in the match, so should be added)
  516. rhs_plabels = cast_set2list(rhs_plabels)
  517. while (read_nr_out(rhs_plabels) > 0):
  518. //rhs_plabel = set_pop(rhs_plabels)
  519. index = 0
  520. while(index < read_nr_out(rhs_plabels)):
  521. rhs_plabel = list_read(rhs_plabels, index)
  522. //if it is a node, than we can simply add it
  523. if (bool_not(is_edge(plabel_to_rhsnode[rhs_plabel]))):
  524. host_match_node = instantiate_node(tf_er_model, string_substr(getTypeName(tf_er_transformation, plabel_to_rhsnode[rhs_plabel]), 0,-4), "")
  525. //run the function
  526. rhs_action = read_attribute(tf_er_transformation,getName(tf_er_transformation, plabel_to_rhsnode[rhs_plabel]), "function")
  527. rhs_action = tf_er_transformation["actions"][rhs_action]
  528. rhs_action(tf_er_model, tf_er_model["model"][host_match_node])
  529. list_delete(rhs_plabels,index)
  530. else:
  531. //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
  532. source_plabel = read_attribute(tf_er_transformation, getName(tf_er_transformation, read_edge_src(plabel_to_rhsnode[rhs_plabel]) ) , "plabel")
  533. destination_plabel = read_attribute(tf_er_transformation, getName(tf_er_transformation, read_edge_dst(plabel_to_rhsnode[rhs_plabel]) ) , "plabel")
  534. if(bool_and(dict_in(rhs_to_host, source_plabel), dict_in(rhs_to_host, destination_plabel))):
  535. rhs_action = instantiate_link(tf_er_model, string_substr(getTypeName(tf_er_transformation, plabel_to_rhsnode[rhs_plabel]), 0,-4), "", getName(tf_er_transformation, read_edge_src(plabel_to_rhsnode[rhs_plabel])), getName(tf_er_transformation, read_edge_dst(plabel_to_rhsnode[rhs_plabel])))
  536. //run function
  537. rhs_action = read_attribute(tf_er_transformation,getName(tf_er_transformation, plabel_to_rhsnode[rhs_plabel]), "function")
  538. rhs_action = tf_er_transformation["actions"][rhs_action]
  539. rhs_action(tf_er_model, tf_er_model["model"][host_match_node])
  540. list_delete(rhs_plabels,index)
  541. else:
  542. index = index + 1
  543. //timing info
  544. log(string_join("rewriting took ", cast_i2s( current_time() - starttime)))
  545. return 0
  546. Element function tf_executeFRule(tf_er_transformation: Element,tf_er_rule: Element, tf_er_model: Element):
  547. //find LHS
  548. Element tf_er_lhs
  549. tf_er_lhs = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, dict_read(dict_read(dict_read(tf_er_transformation, "metamodel"), "model"), "lhs"))
  550. Integer nr_of_lhs_nodes
  551. nr_of_lhs_nodes = read_nr_out(tf_er_lhs)
  552. //match lhses
  553. tf_er_lhs = tf_matchAll(tf_er_transformation, read_edge_dst(set_pop(tf_er_lhs)), tf_er_model)
  554. Integer nr_of_lhs_matches
  555. nr_of_lhs_matches = list_len(tf_er_lhs)
  556. //find NACs if at least one lhs is found (otherwise not needed)
  557. Integer nr_of_nac_matches
  558. nr_of_nac_matches = 0
  559. Element tf_er_nac
  560. tf_er_nac = create_node()
  561. if(bool_or(nr_of_lhs_matches > 0, nr_of_lhs_nodes==0)):
  562. Element tf_er_nac_
  563. tf_er_nac_ = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, dict_read(dict_read(dict_read(tf_er_transformation, "metamodel"), "model"), "nac"))
  564. Element partial_nac_matches
  565. while (read_nr_out(tf_er_nac_) > 0):
  566. partial_nac_matches = tf_matchAll(tf_er_transformation, read_edge_dst(set_pop(tf_er_nac_)), tf_er_model)
  567. while (read_nr_out(partial_nac_matches)>0):
  568. list_append(tf_er_nac, set_pop(partial_nac_matches))
  569. nr_of_nac_matches = list_len(tf_er_nac)
  570. if( nr_of_lhs_nodes == 0):
  571. if (nr_of_nac_matches == 0):
  572. return True
  573. else:
  574. return False
  575. //remove from lhs matches that share a node with a nac match
  576. Element final_matches
  577. final_matches = mergeLHSNAC(tf_er_lhs, tf_er_nac)
  578. if( read_nr_out(final_matches) == 0 ):
  579. return False
  580. //extract pivots from the matches
  581. extractPivots(final_matches, tf_er_transformation, tf_er_model)
  582. //rhs part
  583. //create plabel_to_rhsnode
  584. Element RHS
  585. RHS = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, tf_er_transformation["metamodel"]["model"]["rhs"])
  586. if(read_nr_out(RHS) > 0):
  587. RHS = read_edge_dst(set_pop(RHS))
  588. Element metamodelTransf //transformation metamodel
  589. metamodelTransf = tf_er_transformation["metamodel"]["model"]
  590. Element post2ram // type of the post2ram edge
  591. post2ram = dict_read(metamodelTransf, "post2ram")
  592. post2ram = allOutgoingAssociationInstances(tf_er_transformation, RHS, post2ram)
  593. Element plabel_to_rhsnode //dict in the form plabel->node
  594. plabel_to_rhsnode = create_node()
  595. Element rhs_node
  596. Integer rhs_plabel
  597. while (integer_lt(0, list_len(post2ram))):
  598. rhs_node = read_edge_dst(set_pop(post2ram))
  599. rhs_plabel = read_attribute(tf_er_transformation, getName(tf_er_transformation, rhs_node), "plabel")
  600. dict_add(plabel_to_rhsnode,rhs_plabel, rhs_node)
  601. rhs_rewrite(final_matches ,plabel_to_rhsnode, tf_er_transformation, tf_er_model )
  602. //clean up matches
  603. Element todelete
  604. Element todelete2
  605. while (read_nr_out(tf_er_nac) >0):
  606. todelete2 = set_pop(tf_er_nac)
  607. while (read_nr_out(todelete2)>0):
  608. todelete = set_pop(todelete2)
  609. delete_element(todelete)
  610. while (read_nr_out(tf_er_lhs) >0):
  611. todelete2 = set_pop(tf_er_nac)
  612. while (read_nr_out(todelete2)>0):
  613. todelete = set_pop(todelete2)
  614. delete_element(todelete)
  615. return True
  616. Integer function modulo(a:Integer,b:Integer):
  617. return a - integer_division(a,b)*b
  618. Integer RandomNumberState
  619. Integer function getRandomNumber(min:Integer, max:Integer):
  620. //returns a random integer in min and max range, never max
  621. if(min==max):
  622. return min
  623. if( bool_not(is_physical_int(RandomNumberState))):
  624. RandomNumberState = 4 // xkcd.com/221/
  625. RandomNumberState = modulo((8121*RandomNumberState) + 28411, 134456)
  626. return modulo(RandomNumberState , (max-min))+min
  627. Element function list_select_random(list:Element):
  628. return list_read(list, getRandomNumber(0,read_nr_out(list)))
  629. Element function tf_executeARule(tf_er_transformation: Element,tf_er_rule: Element, tf_er_model: Element):
  630. //find LHS
  631. Element tf_er_lhs
  632. tf_er_lhs = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, dict_read(dict_read(dict_read(tf_er_transformation, "metamodel"), "model"), "lhs"))
  633. Integer nr_of_lhs_nodes
  634. nr_of_lhs_nodes = read_nr_out(tf_er_lhs)
  635. tf_er_lhs = tf_matchAll(tf_er_transformation, read_edge_dst(set_pop(tf_er_lhs)), tf_er_model)
  636. Integer nr_of_lhs_matches
  637. nr_of_lhs_matches = list_len(tf_er_lhs)
  638. //find NACs if at least one lhs is found (otherwise not needed)
  639. Integer nr_of_nac_matches
  640. nr_of_nac_matches = 0
  641. Element tf_er_nac
  642. tf_er_nac = create_node()
  643. if(bool_or(nr_of_lhs_matches > 0, nr_of_lhs_nodes==0)):
  644. Element tf_er_nac_
  645. tf_er_nac_ = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, dict_read(dict_read(dict_read(tf_er_transformation, "metamodel"), "model"), "nac"))
  646. Element partial_nac_matches
  647. while (read_nr_out(tf_er_nac_) > 0):
  648. partial_nac_matches = tf_matchAll(tf_er_transformation, read_edge_dst(set_pop(tf_er_nac_)), tf_er_model)
  649. while (read_nr_out(partial_nac_matches)>0):
  650. list_append(tf_er_nac, set_pop(partial_nac_matches))
  651. nr_of_nac_matches = list_len(tf_er_nac)
  652. if( nr_of_lhs_nodes == 0):
  653. if (nr_of_nac_matches == 0):
  654. return True
  655. else:
  656. return False
  657. //remove from lhs matches that share a node with a nac match
  658. Element final_matches
  659. final_matches = mergeLHSNAC(tf_er_lhs, tf_er_nac)
  660. if( read_nr_out(final_matches) == 0 ):
  661. return False
  662. //select only one match
  663. Element selected
  664. selected = list_select_random(final_matches)
  665. final_matches = create_node()
  666. list_append(final_matches, selected)
  667. //extract pivots from the matches
  668. extractPivots(final_matches, tf_er_transformation, tf_er_model)
  669. //rhs part
  670. //create plabel_to_rhsnode
  671. Element RHS
  672. RHS = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, tf_er_transformation["metamodel"]["model"]["rhs"])
  673. if(read_nr_out(RHS) > 0):
  674. RHS = read_edge_dst(set_pop(RHS))
  675. Element metamodelTransf //transformation metamodel
  676. metamodelTransf = tf_er_transformation["metamodel"]["model"]
  677. Element post2ram // type of the post2ram edge
  678. post2ram = dict_read(metamodelTransf, "post2ram")
  679. post2ram = allOutgoingAssociationInstances(tf_er_transformation, RHS, post2ram)
  680. Element plabel_to_rhsnode //dict in the form plabel->node
  681. plabel_to_rhsnode = create_node()
  682. Element rhs_node
  683. Integer rhs_plabel
  684. while (integer_lt(0, list_len(post2ram))):
  685. rhs_node = read_edge_dst(set_pop(post2ram))
  686. rhs_plabel = read_attribute(tf_er_transformation, getName(tf_er_transformation, rhs_node), "plabel")
  687. dict_add(plabel_to_rhsnode,rhs_plabel, rhs_node)
  688. rhs_rewrite(final_matches ,plabel_to_rhsnode, tf_er_transformation, tf_er_model )
  689. return True
  690. Element function executeRuleBlock(transformation_model : Element,hostmodel:Element, ruleblock : Element):
  691. //execute the correct ruletype
  692. String typename
  693. typename = getTypeName(transformation_model, ruleblock)
  694. Boolean succes
  695. succes = False
  696. Element Rule
  697. Integer curtime
  698. curtime = current_time()
  699. if (typename == "RuleBlockRoot"):
  700. // do nothing
  701. succes = True
  702. elif (typename == "ARule"):
  703. //run Arule
  704. Rule = read_edge_dst(set_pop(allOutgoingAssociationInstances(transformation_model, ruleblock, transformation_model["metamodel"]["model"]["getrule"])))
  705. succes = tf_executeARule(transformation_model, Rule, hostmodel)
  706. elif (typename == "FRule"):
  707. //run Frule
  708. Rule = read_edge_dst(set_pop(allOutgoingAssociationInstances(transformation_model, ruleblock, transformation_model["metamodel"]["model"]["getrule"])))
  709. succes = tf_executeFRule(transformation_model, Rule, hostmodel)
  710. else:
  711. log("RuleType is not supported yet")
  712. log(string_join(string_join(string_join("= running RuleBlock: ",getName(transformation_model, Rule)), " took "),cast_i2s(current_time() - curtime)))
  713. //Return the next
  714. Element next
  715. if succes:
  716. next = allOutgoingAssociationInstances(transformation_model, ruleblock, transformation_model["metamodel"]["model"]["next_onSucces"])
  717. else:
  718. next = allOutgoingAssociationInstances(transformation_model, ruleblock, transformation_model["metamodel"]["model"]["next_onFailure"])
  719. if (read_nr_out(next) == 0):
  720. next = transformation_model
  721. else:
  722. next = read_edge_dst(set_pop(next))
  723. return next
  724. Element function tf_transform(tf_tf_transformation: Element, tf_tf_model: Element):
  725. //create a list of all rules
  726. Element ruleblock
  727. ruleblock = allInstances(tf_tf_transformation, tf_tf_transformation["metamodel"]["model"]["RuleBlockRoot"])
  728. if(read_nr_out(ruleblock) > 0):
  729. ruleblock = set_pop(ruleblock)
  730. pivots = create_node()
  731. Boolean continue
  732. continue = True
  733. while (continue):
  734. ruleblock = executeRuleBlock(tf_tf_transformation,tf_tf_model, ruleblock)
  735. if(element_eq(ruleblock,tf_tf_transformation)):
  736. log("finished")
  737. continue = False
  738. else:
  739. log("root ruleblock not found")
  740. return 0
  741. Element function test_simple_petrinet1(root:Element):
  742. //create a petrinet for testing purposes
  743. // P1 -> T1 -> P2 -> T2
  744. // m:1 w:1 w:2 m:0 w:1
  745. // l1 l2 l3
  746. //////////////////////////////////
  747. //initialise an empty petrinet
  748. Element my_model
  749. my_model = instantiate_model(root["PetriNets"])
  750. dict_add(root, "pnt_test1", my_model)
  751. // init t1
  752. instantiate_node(my_model, "Transition", "t1")
  753. // init p1
  754. instantiate_node(my_model, "Place", "p1")
  755. instantiate_attribute(my_model, "p1","tokens",1)
  756. // init link1
  757. instantiate_link(my_model, "P2T", "p1_to_t1", "p1", "t1")
  758. instantiate_attribute(my_model, "p1_to_t1", "weight", 1)
  759. //init p2
  760. instantiate_node(my_model, "Place", "p2")
  761. instantiate_attribute(my_model, "p2","tokens",0)
  762. //init link2
  763. instantiate_link(my_model, "T2P", "t1_to_p2", "t1", "p2")
  764. instantiate_attribute(my_model, "t1_to_p2", "weight", 2)
  765. //init t2
  766. instantiate_node(my_model, "Transition", "t2")
  767. //init link3
  768. instantiate_link(my_model, "P2T", "p2_to_t2", "p2", "t2")
  769. instantiate_attribute(my_model, "p2_to_t2", "weight", 1)
  770. return my_model
  771. Element function petrinet_transformation(root: Element):
  772. // Create A transformation model for petrinets (useful for testing)
  773. //////////////////////////////////
  774. //initialise an empty transformation model for petrinet
  775. Element my_model
  776. my_model = instantiate_model(root["PetriNets_MTransformation"])
  777. define_inheritance(my_model, "l3")
  778. dict_add(root, "transf_petrinet_fire", my_model)
  779. ////////////////////////////
  780. // root node
  781. instantiate_node(my_model, "Transformation", "PetrinetFire")
  782. instantiate_attribute(my_model, "PetrinetFire", "name", "PetriNetFire")
  783. ////////////////////////////
  784. // rule: find transitions
  785. //rule: find_transitions
  786. instantiate_node(my_model, "Rule", "find_transitions")
  787. instantiate_attribute(my_model, "find_transitions", "name", "find_transitions")
  788. instantiate_attribute(my_model, "find_transitions", "precedence", 1)
  789. //connect to transformation
  790. instantiate_link(my_model, "transformation_to_rule", "", "PetrinetFire", "find_transitions")
  791. //precondition: lhs_precondition
  792. instantiate_node(my_model, "Precondition","lhs_precondition")
  793. //connect to rule
  794. instantiate_link(my_model, "lhs", "", "find_transitions", "lhs_precondition")
  795. //Transition_LHS: lhs_find_all
  796. instantiate_node(my_model, "Transition_LHS","lhs_find_all")
  797. instantiate_attribute(my_model, "lhs_find_all", "plabel", 1)
  798. instantiate_attribute(my_model, "lhs_find_all", "function", "always_true")
  799. instantiate_attribute(my_model, "lhs_find_all", "pivotOut", "enabled")
  800. instantiate_attribute(my_model, "lhs_find_all", "pivotIn", "")
  801. //connect to rule
  802. instantiate_link(my_model, "pre2ram", "", "lhs_precondition", "lhs_find_all")
  803. //precondition: nac_precondition
  804. instantiate_node(my_model, "Precondition","nac_precondition")
  805. //connect to rule
  806. instantiate_link(my_model, "nac", "", "find_transitions", "nac_precondition")
  807. //Transition_LHS: nac_transition_place_before
  808. instantiate_node(my_model, "Transition_LHS","nac_transition_before")
  809. instantiate_attribute(my_model, "nac_transition_before", "plabel", 1)
  810. instantiate_attribute(my_model, "nac_transition_before", "function", "always_true")
  811. instantiate_attribute(my_model, "nac_transition_before", "pivotOut", "")
  812. instantiate_attribute(my_model, "nac_transition_before", "pivotIn", "")
  813. //connect to rule
  814. instantiate_link(my_model, "pre2ram", "", "nac_precondition", "nac_transition_before")
  815. //Place_LHS: nac_place_before
  816. instantiate_node(my_model, "Place_LHS","nac_place_before")
  817. instantiate_attribute(my_model, "nac_place_before", "plabel", 2)
  818. instantiate_attribute(my_model, "nac_place_before", "function", "always_true")
  819. instantiate_attribute(my_model, "nac_place_before", "pivotOut", "")
  820. instantiate_attribute(my_model, "nac_place_before", "pivotIn", "")
  821. //connect to rule
  822. instantiate_link(my_model, "pre2ram", "", "nac_precondition", "nac_place_before")
  823. //P2T_LHS: nac_ft_p2t
  824. instantiate_link(my_model, "P2T_LHS", "nac_p2tlhs_before", "nac_place_before", "nac_transition_before")
  825. instantiate_attribute(my_model, "nac_p2tlhs_before", "plabel", 3)
  826. instantiate_attribute(my_model, "nac_p2tlhs_before", "function", "is_not_consumable")
  827. instantiate_attribute(my_model, "nac_p2tlhs_before", "pivotOut", "")
  828. instantiate_attribute(my_model, "nac_p2tlhs_before", "pivotIn", "")
  829. //connect to rule
  830. instantiate_link(my_model, "pre2ram", "", "nac_precondition", "nac_p2tlhs_before")
  831. ////////////////////////////
  832. // rule: consume tokens
  833. //rule: consume_tokens
  834. instantiate_node(my_model, "Rule", "consume_tokens")
  835. instantiate_attribute(my_model, "consume_tokens", "name", "consume_tokens")
  836. instantiate_attribute(my_model, "consume_tokens", "precedence", 2)
  837. //connect to transformation
  838. instantiate_link(my_model, "transformation_to_rule", "", "PetrinetFire", "consume_tokens")
  839. //precondition: lhs_precondition
  840. instantiate_node(my_model, "Precondition","ct_lhs_precondition")
  841. //connect to rule
  842. instantiate_link(my_model, "lhs", "", "consume_tokens", "ct_lhs_precondition")
  843. //Transition_LHS: ct_lhs_transition
  844. instantiate_node(my_model, "Transition_LHS","ct_lhs_transition")
  845. instantiate_attribute(my_model, "ct_lhs_transition", "plabel", 1)
  846. instantiate_attribute(my_model, "ct_lhs_transition", "function", "always_true")
  847. instantiate_attribute(my_model, "ct_lhs_transition", "pivotOut", "")
  848. instantiate_attribute(my_model, "ct_lhs_transition", "pivotIn", "enabled")
  849. //connect to rule
  850. instantiate_link(my_model, "pre2ram", "", "ct_lhs_precondition", "ct_lhs_transition")
  851. //Place_LHS: ct_lhs_place
  852. instantiate_node(my_model, "Place_LHS","ct_lhs_place")
  853. instantiate_attribute(my_model, "ct_lhs_place", "plabel", 2)
  854. instantiate_attribute(my_model, "ct_lhs_place", "function", "always_true")
  855. instantiate_attribute(my_model, "ct_lhs_place", "pivotOut", "")
  856. instantiate_attribute(my_model, "ct_lhs_place", "pivotIn", "")
  857. //connect to rule
  858. instantiate_link(my_model, "pre2ram", "", "ct_lhs_precondition", "ct_lhs_place")
  859. //P2T_LHS: ct_lhs_link
  860. instantiate_link(my_model, "P2T_LHS", "ct_lhs_link", "ct_lhs_place", "ct_lhs_transition")
  861. instantiate_attribute(my_model, "ct_lhs_link", "plabel", 3)
  862. instantiate_attribute(my_model, "ct_lhs_link", "function", "always_true")
  863. instantiate_attribute(my_model, "ct_lhs_link", "pivotOut", "")
  864. instantiate_attribute(my_model, "ct_lhs_link", "pivotIn", "")
  865. //connect to rule
  866. instantiate_link(my_model, "pre2ram", "", "ct_lhs_precondition", "ct_lhs_link")
  867. //precondition: rhs_postcondition
  868. instantiate_node(my_model, "Postcondition","ct_rhs_postcondition")
  869. //connect to rule
  870. instantiate_link(my_model, "rhs", "", "consume_tokens", "ct_rhs_postcondition")
  871. //Transition_RHS: ct_rhs_transition
  872. instantiate_node(my_model, "Transition_RHS","ct_rhs_transition")
  873. instantiate_attribute(my_model, "ct_rhs_transition", "plabel", 1)
  874. instantiate_attribute(my_model, "ct_rhs_transition", "function", "do_nothing")
  875. instantiate_attribute(my_model, "ct_rhs_transition", "pivotOut", "")
  876. instantiate_attribute(my_model, "ct_rhs_transition", "pivotIn", "")
  877. //connect to rule
  878. instantiate_link(my_model, "post2ram", "", "ct_rhs_postcondition", "ct_rhs_transition")
  879. //Place_RHS: ct_rhs_place
  880. instantiate_node(my_model, "Place_RHS","ct_rhs_place")
  881. instantiate_attribute(my_model, "ct_rhs_place", "plabel", 2)
  882. instantiate_attribute(my_model, "ct_rhs_place", "function", "do_nothing")
  883. instantiate_attribute(my_model, "ct_rhs_place", "pivotOut", "")
  884. instantiate_attribute(my_model, "ct_rhs_place", "pivotIn", "")
  885. //connect to rule
  886. instantiate_link(my_model, "post2ram", "", "ct_rhs_postcondition", "ct_rhs_place")
  887. //P2T_RHS: ct_rhs_link
  888. instantiate_link(my_model, "P2T_RHS", "ct_rhs_link", "ct_rhs_place", "ct_rhs_transition")
  889. instantiate_attribute(my_model, "ct_rhs_link", "plabel", 3)
  890. instantiate_attribute(my_model, "ct_rhs_link", "function", "remove_tokens")
  891. instantiate_attribute(my_model, "ct_rhs_link", "pivotOut", "")
  892. instantiate_attribute(my_model, "ct_rhs_link", "pivotIn", "")
  893. //connect to rule
  894. instantiate_link(my_model, "post2ram", "", "ct_rhs_postcondition", "ct_rhs_link")
  895. ////////////////////////////
  896. // rule: produce tokens
  897. //rule: produce_tokens
  898. instantiate_node(my_model, "Rule", "produce_tokens")
  899. instantiate_attribute(my_model, "produce_tokens", "name", "produce_tokens")
  900. instantiate_attribute(my_model, "produce_tokens", "precedence", 3)
  901. //connect to transformation
  902. instantiate_link(my_model, "transformation_to_rule", "", "PetrinetFire", "produce_tokens")
  903. //precondition: lhs_precondition
  904. instantiate_node(my_model, "Precondition","pt_lhs_precondition")
  905. //connect to rule
  906. instantiate_link(my_model, "lhs", "", "produce_tokens", "pt_lhs_precondition")
  907. //Transition_LHS: pt_lhs_transition
  908. instantiate_node(my_model, "Transition_LHS","pt_lhs_transition")
  909. instantiate_attribute(my_model, "pt_lhs_transition", "plabel", 1)
  910. instantiate_attribute(my_model, "pt_lhs_transition", "function", "always_true")
  911. instantiate_attribute(my_model, "pt_lhs_transition", "pivotOut", "")
  912. instantiate_attribute(my_model, "pt_lhs_transition", "pivotIn", "enabled")
  913. //connect to rule
  914. instantiate_link(my_model, "pre2ram", "", "pt_lhs_precondition", "pt_lhs_transition")
  915. //Place_LHS: pt_lhs_place
  916. instantiate_node(my_model, "Place_LHS","pt_lhs_place")
  917. instantiate_attribute(my_model, "pt_lhs_place", "plabel", 2)
  918. instantiate_attribute(my_model, "pt_lhs_place", "function", "always_true")
  919. instantiate_attribute(my_model, "pt_lhs_place", "pivotOut", "")
  920. instantiate_attribute(my_model, "pt_lhs_place", "pivotIn", "")
  921. //connect to rule
  922. instantiate_link(my_model, "pre2ram", "", "pt_lhs_precondition", "pt_lhs_place")
  923. //P2T_LHS: pt_lhs_link
  924. instantiate_link(my_model, "T2P_LHS", "pt_lhs_link", "pt_lhs_transition", "pt_lhs_place")
  925. instantiate_attribute(my_model, "pt_lhs_link", "plabel", 3)
  926. instantiate_attribute(my_model, "pt_lhs_link", "function", "always_true")
  927. instantiate_attribute(my_model, "pt_lhs_link", "pivotOut", "")
  928. instantiate_attribute(my_model, "pt_lhs_link", "pivotIn", "")
  929. //connect to rule
  930. instantiate_link(my_model, "pre2ram", "", "pt_lhs_precondition", "pt_lhs_link")
  931. //precondition: rhs_postcondition
  932. instantiate_node(my_model, "Postcondition","pt_rhs_postcondition")
  933. //connect to rule
  934. instantiate_link(my_model, "rhs", "", "produce_tokens", "pt_rhs_postcondition")
  935. //Transition_RHS: pt_rhs_transition
  936. instantiate_node(my_model, "Transition_RHS","pt_rhs_transition")
  937. instantiate_attribute(my_model, "pt_rhs_transition", "plabel", 1)
  938. instantiate_attribute(my_model, "pt_rhs_transition", "function", "do_nothing")
  939. instantiate_attribute(my_model, "pt_rhs_transition", "pivotOut", "")
  940. instantiate_attribute(my_model, "pt_rhs_transition", "pivotIn", "")
  941. //connect to rule
  942. instantiate_link(my_model, "post2ram", "", "pt_rhs_postcondition", "pt_rhs_transition")
  943. //Place_RHS: pt_rhs_place
  944. instantiate_node(my_model, "Place_RHS","pt_rhs_place")
  945. instantiate_attribute(my_model, "pt_rhs_place", "plabel", 2)
  946. instantiate_attribute(my_model, "pt_rhs_place", "function", "do_nothing")
  947. instantiate_attribute(my_model, "pt_rhs_place", "pivotOut", "")
  948. instantiate_attribute(my_model, "pt_rhs_place", "pivotIn", "")
  949. //connect to rule
  950. instantiate_link(my_model, "post2ram", "", "pt_rhs_postcondition", "pt_rhs_place")
  951. //P2T_RHS: ct_rhs_link
  952. instantiate_link(my_model, "T2P_RHS", "pt_rhs_link", "pt_rhs_transition", "pt_rhs_place")
  953. instantiate_attribute(my_model, "pt_rhs_link", "plabel", 3)
  954. instantiate_attribute(my_model, "pt_rhs_link", "function", "add_tokens")
  955. instantiate_attribute(my_model, "pt_rhs_link", "pivotOut", "")
  956. instantiate_attribute(my_model, "pt_rhs_link", "pivotIn", "")
  957. //connect to rule
  958. instantiate_link(my_model, "post2ram", "", "pt_rhs_postcondition", "pt_rhs_link")
  959. ////////////////////////////
  960. // rule ordering part
  961. //start node
  962. instantiate_node(my_model, "RuleBlockRoot", "rule_root")
  963. //ARule: enabled
  964. instantiate_node(my_model, "ARule", "arule1")
  965. //connect to startnode
  966. instantiate_link(my_model, "next_onSucces", "", "rule_root", "arule1")
  967. //connect to rule
  968. instantiate_link(my_model, "getrule","", "arule1", "find_transitions")
  969. //FRule: consume
  970. instantiate_node(my_model, "FRule", "frule2")
  971. //connect to arule1
  972. instantiate_link(my_model, "next_onSucces", "", "arule1", "frule2")
  973. //connect to rule
  974. instantiate_link(my_model, "getrule","", "frule2", "consume_tokens")
  975. //Frule: produce
  976. instantiate_node(my_model, "FRule", "frule3")
  977. //connect to frule2
  978. instantiate_link(my_model, "next_onSucces", "", "frule2", "frule3")
  979. //connect to rule
  980. instantiate_link(my_model, "getrule","", "frule3", "produce_tokens")
  981. //create circularity link
  982. //instantiate_link(my_model, "next_onSucces", "", "frule3", "arule1")
  983. //if you cannot consume, still try to produce (for source transitions)
  984. //instantiate_link(my_model, "next_onFailure", "", "frule2", "frule3")
  985. //if you cannot produce, still check for other transitions (it might have consumed already)
  986. //instantiate_link(my_model, "next_onFailure", "", "frule3", "arule1")
  987. ////////////////////////////
  988. // semantic checks
  989. Element semantic_checks
  990. semantic_checks = create_node()
  991. dict_add(semantic_checks, "always_true", always_true)
  992. dict_add(semantic_checks, "is_not_consumable", is_not_consumable)
  993. dict_add(my_model, "semantic_checks", semantic_checks)
  994. Element actions
  995. actions = create_node()
  996. dict_add(actions, "do_nothing", do_nothing)
  997. dict_add(actions, "add_tokens", add_tokens)
  998. dict_add(actions, "remove_tokens", remove_tokens)
  999. dict_add(my_model, "actions", actions)
  1000. return my_model
  1001. Boolean function is_not_consumable(model : Element, matched_node : Element):
  1002. //get place before
  1003. Element place_before
  1004. place_before = read_edge_src(matched_node)
  1005. //get tokens from place
  1006. Integer tokens
  1007. tokens = read_attribute(model, getName(model, place_before), "tokens")
  1008. //get weight from matched_node
  1009. Integer weight
  1010. weight = read_attribute(model, getName(model, matched_node), "weight")
  1011. return weight > tokens
  1012. Boolean function always_true(model : Element, self : Element):
  1013. return True
  1014. Element function do_nothing(model: Element, self : Element):
  1015. return 0
  1016. Element function remove_tokens(model:Element, self: Element):
  1017. //get place before
  1018. Element place_before
  1019. place_before = read_edge_src(self)
  1020. //get tokens from place
  1021. Integer tokens
  1022. tokens = read_attribute(model, getName(model, place_before), "tokens")
  1023. //get weight from matched_node
  1024. Integer weight
  1025. weight = read_attribute(model, getName(model, self), "weight")
  1026. //rewrite tokens
  1027. unset_attribute(model, getName(model, place_before), "tokens")
  1028. instantiate_attribute(model, getName(model, place_before), "tokens", tokens-weight)
  1029. return 0
  1030. Element function add_tokens(model:Element, self: Element):
  1031. //get place before
  1032. Element place_after
  1033. place_after = read_edge_dst(self)
  1034. //get tokens from place
  1035. Integer tokens
  1036. tokens = read_attribute(model, getName(model, place_after), "tokens")
  1037. //get weight from matched_node
  1038. Integer weight
  1039. weight = read_attribute(model, getName(model, self), "weight")
  1040. //rewrite tokens
  1041. unset_attribute(model, getName(model, place_after), "tokens")
  1042. instantiate_attribute(model, getName(model, place_after), "tokens", tokens+weight)
  1043. return 0
  1044. Element function pn_operations():
  1045. Element ops
  1046. ops = create_node()
  1047. dict_add(ops, "fire", petrinet_fire)
  1048. dict_add(ops, "enabled", petrinet_enabled)
  1049. return ops
  1050. Element function petrinet_enabled(model : Element):
  1051. Element set_enabled
  1052. set_enabled = petrinet_enabled_set(model)
  1053. output("Enabled transitions:")
  1054. while (0 < read_nr_out(set_enabled)):
  1055. output(getName(model, set_pop(set_enabled)))
  1056. return model
  1057. Element function petrinet_enabled_set(model : Element):
  1058. Element all_transitions
  1059. all_transitions = allInstances(model, model["metamodel"]["model"]["Transition"])
  1060. Element enabled_transitions
  1061. enabled_transitions = create_node()
  1062. Element under_study
  1063. Element in_arcs
  1064. Element arc_under_study
  1065. Boolean enabled
  1066. while (0 < read_nr_out(all_transitions)):
  1067. under_study = set_pop(all_transitions)
  1068. enabled = True
  1069. // Find all incoming transitions
  1070. in_arcs = allIncomingAssociationInstances(model, under_study, model["metamodel"]["model"]["P2T"])
  1071. while (0 < read_nr_out(in_arcs)):
  1072. arc_under_study = set_pop(in_arcs)
  1073. Integer present_tokens
  1074. Integer required_tokens
  1075. required_tokens = read_attribute(model, getName(model, arc_under_study), "weight")
  1076. present_tokens = read_attribute(model, getName(model, read_edge_src(arc_under_study)), "tokens")
  1077. if (present_tokens < required_tokens):
  1078. // Less tokens than required, so disable the transition completely
  1079. enabled = False
  1080. if (enabled):
  1081. set_add(enabled_transitions, under_study)
  1082. return enabled_transitions
  1083. Element function petrinet_fire(model : Element):
  1084. output("Transition to fire?")
  1085. Element transition
  1086. transition = input()
  1087. if (dict_in(model["model"], transition)):
  1088. transition = model["model"][transition]
  1089. if (set_in(petrinet_enabled_set(model), transition)):
  1090. Element workset
  1091. Element working_place
  1092. Element working_arc
  1093. Integer new_value
  1094. // Consume tokens
  1095. workset = allIncomingAssociationInstances(model, transition, model["metamodel"]["model"]["P2T"])
  1096. while (0 < read_nr_out(workset)):
  1097. working_arc = set_pop(workset)
  1098. working_place = read_edge_src(working_arc)
  1099. new_value = integer_subtraction(read_attribute(model, getName(model, working_place), "tokens"), read_attribute(model, getName(model, working_arc), "weight"))
  1100. unset_attribute(model, getName(model, working_place), "tokens")
  1101. instantiate_attribute(model, getName(model, working_place), "tokens", new_value)
  1102. output(((" " + getName(model, working_place)) + ": ") + cast_i2s(read_attribute(model, getName(model, working_place), "tokens")))
  1103. // Add tokens
  1104. workset = allOutgoingAssociationInstances(model, transition, model["metamodel"]["model"]["T2P"])
  1105. while (0 < read_nr_out(workset)):
  1106. working_arc = set_pop(workset)
  1107. working_place = read_edge_dst(working_arc)
  1108. new_value = integer_addition(read_attribute(model, getName(model, working_place), "tokens"), read_attribute(model, getName(model, working_arc), "weight"))
  1109. unset_attribute(model, getName(model, working_place), "tokens")
  1110. instantiate_attribute(model, getName(model, working_place), "tokens", new_value)
  1111. output(((" " + getName(model, working_place)) + ": ") + cast_i2s(read_attribute(model, getName(model, working_place), "tokens")))
  1112. output("Transition fired!")
  1113. else:
  1114. output("Cannot fire if not enabled; aborting")
  1115. else:
  1116. output("Unknown transition; aborting")
  1117. return model
  1118. Element function petrinet_loaded(model : Element):
  1119. String cmd
  1120. Element attr_list_pn
  1121. Element attr_keys_pn
  1122. String attr_key_pn
  1123. Element metamodel_element_pn
  1124. String typename
  1125. Boolean bottom
  1126. Element other_metamodel
  1127. bottom = False
  1128. other_metamodel = create_node()
  1129. dict_add(other_metamodel, "model", model["model"])
  1130. dict_add(other_metamodel, "type_mapping", create_node())
  1131. dict_add(other_metamodel, "metamodel", import_node("models/LTM_bottom"))
  1132. dict_add(other_metamodel, "inheritance", other_metamodel["metamodel"]["model"]["__Inheritance"])
  1133. output("Model loaded, ready for commands!")
  1134. output("Use 'help' command for a list of possible commands")
  1135. while (True):
  1136. output("Please give your command.")
  1137. cmd = input()
  1138. if (cmd == "help"):
  1139. output("Generic model operations:")
  1140. output(" instantiate -- Create a new model element")
  1141. output(" delete -- Delete an existing element")
  1142. output(" attr_add -- Add an attribute to an element")
  1143. output(" attr_del -- Delete an attribute of an element")
  1144. output(" rename -- Rename an existing element")
  1145. output(" modify -- Modify the attributes of an element")
  1146. output(" list -- Prints the list of elements in the model")
  1147. output(" types -- Prints the list of elements that can be instantiated")
  1148. output(" read -- Prints the current state of a model element")
  1149. output(" verify -- Check whether the model conforms to the metamodel")
  1150. output(" retype -- Change the type of an element")
  1151. output(" switch -- Switch between conformance bottom and the linguistic metamodel")
  1152. output(" exit -- Unload the model and go back to the loading prompt")
  1153. if (bool_not(bottom)):
  1154. output("Model-specific operations:")
  1155. Element specific_ops
  1156. specific_ops = dict_keys(pn_operations())
  1157. String specific_op
  1158. while (0 < dict_len(specific_ops)):
  1159. specific_op = set_pop(specific_ops)
  1160. output(" " + specific_op)
  1161. elif (cmd == "exit"):
  1162. return model
  1163. elif (cmd == "instantiate"):
  1164. String mm_type_name
  1165. output("Type to instantiate?")
  1166. mm_type_name = input()
  1167. if (dict_in(model["metamodel"]["model"], mm_type_name)):
  1168. String element_name
  1169. output("Name of new element?")
  1170. element_name = input()
  1171. if (dict_in(model["model"], element_name)):
  1172. output("Element already exists; aborting")
  1173. else:
  1174. if (is_edge(model["metamodel"]["model"][mm_type_name])):
  1175. output("Source name?")
  1176. String src_name
  1177. src_name = input()
  1178. if (dict_in(model["model"], src_name)):
  1179. output("Destination name?")
  1180. String dst_name
  1181. dst_name = input()
  1182. if (dict_in(model["model"], dst_name)):
  1183. instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
  1184. if (dict_in(model["model"], element_name)):
  1185. output("Instantiation successful!")
  1186. else:
  1187. output("Instantiation error!")
  1188. else:
  1189. output("Unknown destination; aborting")
  1190. else:
  1191. output("Unknown source; aborting")
  1192. else:
  1193. instantiate_node(model, mm_type_name, element_name)
  1194. if (dict_in(model["model"], element_name)):
  1195. output("Instantiation successful!")
  1196. else:
  1197. output("Instantiation error!")
  1198. else:
  1199. output("Unknown type specified; aborting")
  1200. elif (cmd == "modify"):
  1201. String model_name
  1202. output("Element to modify?")
  1203. model_name = input()
  1204. if (dict_in(model["model"], model_name)):
  1205. Element attrs
  1206. attrs = getAttributeList(model, model_name)
  1207. String attr_name
  1208. output("Attribute to modify?")
  1209. attr_name = input()
  1210. if (set_in(dict_keys(attrs), attr_name)):
  1211. output("New value?")
  1212. unset_attribute(model, model_name, attr_name)
  1213. instantiate_attribute(model, model_name, attr_name, input())
  1214. output("Modified!")
  1215. else:
  1216. output("No such attribute!")
  1217. else:
  1218. output("No such model!")
  1219. elif (cmd == "attr_add"):
  1220. String model_name
  1221. output("Which model do you want to assign an attribute to?")
  1222. model_name = input()
  1223. if (dict_in(model["model"], model_name)):
  1224. Element attrs
  1225. attrs = getAttributeList(model, model_name)
  1226. String attr_name
  1227. output("Which attribute do you wish to assign?")
  1228. attr_name = input()
  1229. if (set_in(dict_keys(attrs), attr_name)):
  1230. output("Value of attribute?")
  1231. instantiate_attribute(model, model_name, attr_name, input())
  1232. output("Added attribute!")
  1233. else:
  1234. output("No such attribute!")
  1235. else:
  1236. output("No such model!")
  1237. elif (cmd == "attr_del"):
  1238. String model_name
  1239. output("Which model do you want to remove an attribute of?")
  1240. model_name = input()
  1241. if (dict_in(model["model"], model_name)):
  1242. Element attrs
  1243. attrs = getAttributeList(model, model_name)
  1244. output(print_dict(attrs))
  1245. String attr_name
  1246. output("Which attribute do you want to delete?")
  1247. attr_name = input()
  1248. if (set_in(dict_keys(attrs), attr_name)):
  1249. unset_attribute(model, model_name, attr_name)
  1250. else:
  1251. output("No such attribute!")
  1252. else:
  1253. output("No such model!")
  1254. elif (cmd == "delete"):
  1255. output("What is the name of the element you want to delete?")
  1256. cmd = input()
  1257. if (dict_in(model["model"], cmd)):
  1258. model_delete_element(model, cmd)
  1259. output("Deleted!")
  1260. else:
  1261. output("No such element; aborting")
  1262. elif (cmd == "rename"):
  1263. output("Old name?")
  1264. String old_name_e
  1265. old_name_e = input()
  1266. if (dict_in(model["model"], old_name_e)):
  1267. output("New name?")
  1268. String new_name_e
  1269. new_name_e = input()
  1270. if (dict_in(model["model"], new_name_e)):
  1271. output("New name already used; aborting")
  1272. else:
  1273. dict_add(model["model"], new_name_e, model["model"][old_name_e])
  1274. dict_delete(model["model"], old_name_e)
  1275. output("Rename complete!")
  1276. else:
  1277. output("Unknown element; aborting")
  1278. elif (cmd == "list"):
  1279. Element keys_m
  1280. keys_m = dict_keys(model["model"])
  1281. output("List of all elements:")
  1282. String v_m
  1283. while (read_nr_out(keys_m) > 0):
  1284. v_m = set_pop(keys_m)
  1285. // Filter out anonymous objects
  1286. typename = getName(model["metamodel"], dict_read_node(model["type_mapping"], model["model"][v_m]))
  1287. if (bool_not(string_startswith(v_m, "__"))):
  1288. output(((" " + v_m) + " : ") + typename)
  1289. elif (cmd == "read"):
  1290. output("Element to read?")
  1291. cmd = input()
  1292. if (dict_in(model["model"], cmd)):
  1293. Element read_elem
  1294. read_elem = model["model"][cmd]
  1295. metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
  1296. output("Name: " + cmd)
  1297. output("Type: " + getName(model["metamodel"], metamodel_element_pn))
  1298. if (is_edge(read_elem)):
  1299. output("Source: " + getName(model, read_edge_src(read_elem)))
  1300. output("Destination: " + getName(model, read_edge_dst(read_elem)))
  1301. if (cast_v2s(read_elem) != "None"):
  1302. output("Value: " + cast_v2s(read_elem))
  1303. output("Defines attributes:")
  1304. attr_list_pn = getInstantiatableAttributes(model, read_elem)
  1305. attr_keys_pn = dict_keys(attr_list_pn)
  1306. while (0 < read_nr_out(attr_keys_pn)):
  1307. attr_key_pn = set_pop(attr_keys_pn)
  1308. output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])))
  1309. output("Attributes:")
  1310. attr_list_pn = getAttributeList(model, cmd)
  1311. attr_keys_pn = dict_keys(attr_list_pn)
  1312. while (0 < read_nr_out(attr_keys_pn)):
  1313. attr_key_pn = set_pop(attr_keys_pn)
  1314. 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)))
  1315. else:
  1316. output("Unknown element; aborting")
  1317. elif (cmd == "verify"):
  1318. output(conformance_scd(model))
  1319. elif (cmd == "types"):
  1320. Element keys_t
  1321. keys_t = dict_keys(model["metamodel"]["model"])
  1322. output("List of types:")
  1323. String v_t
  1324. while (read_nr_out(keys_t) > 0):
  1325. v_t = set_pop(keys_t)
  1326. if (bool_not(string_startswith(v_t, "__"))):
  1327. output(string_join((" " + v_t) + " : ", getName(model["metamodel"]["metamodel"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
  1328. elif (cmd == "retype"):
  1329. output("Element to retype?")
  1330. String elementname
  1331. elementname = input()
  1332. if (dict_in(model["model"], elementname)):
  1333. output("New type")
  1334. typename = input()
  1335. if (dict_in(model["metamodel"]["model"], typename)):
  1336. // OK, do the retyping
  1337. // First try removing the previous type if it exists
  1338. dict_delete(model["type_mapping"], model["model"][elementname])
  1339. // Now add the new type
  1340. dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename])
  1341. output("Retyped!")
  1342. else:
  1343. output("Unknown type; aborting")
  1344. else:
  1345. output("Unknown element; aborting")
  1346. elif (cmd == "switch"):
  1347. bottom = bool_not(bottom)
  1348. Element tmp_model
  1349. tmp_model = model
  1350. model = other_metamodel
  1351. other_metamodel = tmp_model
  1352. if (bottom):
  1353. // The type mapping we are using is probably not complete for our model
  1354. // so we completely recreate it from the model we have.
  1355. output("Switching to conformance bottom mode!")
  1356. generate_bottom_type_mapping(model)
  1357. else:
  1358. // We already switched the models and such, so we are already done!
  1359. output("Switching to linguistic metamodel!")
  1360. elif (bool_and(dict_in(pn_operations(), cmd), bool_not(bottom))):
  1361. // A model-specific operation, so execute that one
  1362. Element specific_op
  1363. specific_op = dict_read(pn_operations(), cmd)
  1364. specific_op(model)
  1365. else:
  1366. output("Unknown command; aborting")
  1367. output("Use command 'help' to get a list of available commands")
  1368. Element function initial_prompt():
  1369. output("Welcome to the Model Management Interface, running live on the Modelverse!")
  1370. output("Use 'help' command for a list of possible commands")
  1371. String command
  1372. Element root
  1373. Element metamodel
  1374. String name
  1375. Element my_model
  1376. String mm_name
  1377. root = create_metamodels()
  1378. while (True):
  1379. output("Please give your command.")
  1380. command = input()
  1381. if (command == "help"):
  1382. output("Currently no model is loaded, so your operations are limited to:")
  1383. output(" new -- Create a new model and save it for future use")
  1384. output(" load -- Load a previously made model")
  1385. output(" rename -- Rename a previously made model")
  1386. output(" delete -- Delete a previously made model")
  1387. output(" list -- Show a list of all stored models")
  1388. output(" help -- Show a list of possible commands")
  1389. elif (command == "new"):
  1390. output("Metamodel to instantiate?")
  1391. mm_name = input()
  1392. if (dict_in(root, mm_name)):
  1393. output("Name of model?")
  1394. name = input()
  1395. if (dict_in(root, name)):
  1396. output("Model exists; aborting")
  1397. else:
  1398. my_model = instantiate_model(root[mm_name])
  1399. dict_add(root, name, my_model)
  1400. petrinet_loaded(my_model)
  1401. else:
  1402. output("Unknown metamodel; aborting")
  1403. elif (command == "load"):
  1404. output("Model to load?")
  1405. name = input()
  1406. if (dict_in(root, name)):
  1407. my_model = root[name]
  1408. petrinet_loaded(my_model)
  1409. else:
  1410. output("Model not found; aborting")
  1411. elif (command == "list"):
  1412. Element keys
  1413. String m_menu_list
  1414. keys = dict_keys(root)
  1415. output("Found models:")
  1416. while (read_nr_out(keys) > 0):
  1417. m_menu_list = set_pop(keys)
  1418. output(((" " + m_menu_list) + " : ") + reverseNameLookup(root, root[m_menu_list]["metamodel"]))
  1419. elif (command == "delete"):
  1420. output("Model to delete?")
  1421. name = input()
  1422. if (dict_in(root, name)):
  1423. dict_delete(root, name)
  1424. output("Deleted!")
  1425. else:
  1426. output("Model not found; aborting")
  1427. elif (command == "rename"):
  1428. output("Old name?")
  1429. String old_name
  1430. old_name = input()
  1431. if (dict_in(root, old_name)):
  1432. output("New name?")
  1433. String new_name
  1434. new_name = input()
  1435. if (dict_in(root, new_name)):
  1436. output("Model exists; aborting")
  1437. else:
  1438. dict_add(root, new_name, root[old_name])
  1439. dict_delete(root, old_name)
  1440. output("Rename complete!")
  1441. else:
  1442. output("Model not found; aborting")
  1443. else:
  1444. output("Command not recognized, use 'help' for a list of possible commands")
  1445. Void function main():
  1446. //initial_prompt()
  1447. log("==========================================================================")
  1448. Integer init_time
  1449. init_time = current_time()
  1450. Element root
  1451. root = create_metamodels()
  1452. log(string_join("metamodel creation took ", cast_i2s(current_time() - init_time)))
  1453. init_time = current_time()
  1454. Element transf
  1455. transf = petrinet_transformation(root)
  1456. log(string_join("transform model creation took ", cast_i2s(current_time() - init_time)))
  1457. init_time = current_time()
  1458. Element test1
  1459. test1 = test_simple_petrinet1(root)
  1460. log(string_join("simple petrinet model creation took ", cast_i2s(current_time() - init_time)))
  1461. init_time = current_time()
  1462. log(conformance_scd(transf))
  1463. log(string_join("transformation conformance checking took ", cast_i2s(current_time() - init_time)))
  1464. init_time = current_time()
  1465. tf_transform(transf, test1)
  1466. log(string_join("transformation execution took ", cast_i2s(current_time() - init_time)))
  1467. init_time = current_time()
  1468. log("==========================================================================")
  1469. printNode(test1, test1["model"]["p1"])
  1470. printNode(test1, test1["model"]["p2"])
  1471. log("=== done")
  1472. return