| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502 |
- include "primitives.alh"
- include "constructors.alh"
- include "object_operations.alh"
- include "library.alh"
- include "conformance_scd.alh"
- include "io.alh"
- include "metamodels.alh"
- include "modelling.alh"
- //helper function that returns the type of an attribute in string format
- Element function find_attribute_type_s(model : Element, elem : String, name : String):
- Element mm_elem
- Element direct_type
- direct_type = dict_read_node(model["type_mapping"], model["model"][elem])
- mm_elem = find_attribute_definer(model["metamodel"], direct_type, name)
- if (element_eq(mm_elem, read_root())):
- // Couldn't find element, so is not allowed!
- log("Undefined attribute!")
- return read_root()
- else:
- return getName(model["metamodel"], read_edge_dst( dict_read_edge(mm_elem, name)))
- //given a model and a node, it will return the name of it's type in the metamodel
- String function getTypeName(model:Element, node:Element):
- return getName(model["metamodel"], dict_read_node(model["type_mapping"], node))
- //given a model and a node it will return the name of it's type in the metametamodel
- String function getMetaTypeName(model:Element, node:Element):
- return getTypeName(model["metamodel"], dict_read_node(model["type_mapping"], node))
- Boolean function isAttributeRelated(model:Element, node:Element):
- if(getMetaTypeName(model, node) == "Type"):
- return True
- elif(getMetaTypeName(model, node) == "l5"):
- if(getMetaTypeName(model, read_edge_dst(node)) == "Type" ):
- return True
- return False
- //function to print information about a node (attributes, type and name)
- Element function printNode(tf_pn_model: Element, tf_pn_node: Element):
-
- //print header with name, metatype
-
- //print node_name
- String tf_pn_node_name
- tf_pn_node_name = getName(tf_pn_model, tf_pn_node)
- log(string_join("--name: ",tf_pn_node_name) )
- //print node type
- log(string_join("type: ", getTypeName(tf_pn_model, tf_pn_node) ))
- //print node metatype
- String metatype
- metatype = getMetaTypeName(tf_pn_model, tf_pn_node)
- log(string_join("metatype: ", metatype))
- if (isAttributeRelated(tf_pn_model, tf_pn_node)):
- log(">attribute related")
-
- //print attributes
-
- log("attributes:")
- //get the list of attributes
- Element tf_pn_attributelist
- tf_pn_attributelist = dict_keys(getAttributeList(tf_pn_model, tf_pn_node_name))
- String tf_pn_attributename
- String tf_pn_attributetype
- while (integer_lt(0, read_nr_out(tf_pn_attributelist))):
- //print attribute name and its value
- tf_pn_attributename = set_pop(tf_pn_attributelist)
- tf_pn_attributetype = find_attribute_type_s(tf_pn_model, tf_pn_node_name, tf_pn_attributename)
- if( tf_pn_attributetype == "String"):
- log(string_join(string_join(string_join(" ",tf_pn_attributename), " : "),read_attribute(tf_pn_model, tf_pn_node_name, tf_pn_attributename)))
- elif (tf_pn_attributetype == "Integer"):
- log(string_join(string_join(string_join(" ",tf_pn_attributename), " : "),cast_i2s(read_attribute(tf_pn_model, tf_pn_node_name, tf_pn_attributename))))
- else:
- log(string_join(tf_pn_attributename, " : "))
- log(read_attribute(tf_pn_model, tf_pn_node_name, tf_pn_attributename))
- return 0
-
- Boolean function node_match(transf_model : Element, transf_elem : Element, host_model : Element, host_elem : Element, pivots : Element):
- //type equality
- String type1
- type1 = getTypeName(transf_model, transf_elem)
- String type2
- type2 = string_join(getTypeName(host_model, host_elem), "_LHS")
-
- if (type1 != type2):
- return False
- //semantic check
- Element func
- func = transf_model["semantic_checks"][read_attribute(transf_model, getName(transf_model,transf_elem), "function")]
- //pivot check
- String pivotInName
- pivotInName = read_attribute(transf_model, getName(transf_model, transf_elem), "pivotIn")
- if(func(host_model, host_elem)):
- if( pivotInName == ""):
- return True
- else:
- if(set_in(pivots[pivotInName], getName(host_model,host_elem))):
- return True
- else:
- return False
- return False
- Integer function are_connected(model: Element, el1:Element, el2:Element):
- String el1_mmt
- String el2_mmt
- el1_mmt = getMetaTypeName(model, el1)
- el2_mmt = getMetaTypeName(model, el2)
- if ( el1_mmt == "Class" ):
- if(el2_mmt == "Class"):
- return 0 //not connected, no edge inbetween
- elif(el2_mmt == "l5"):
- if( element_eq(read_edge_src(el2) , el1)):
- return 1 //outgoing
- elif( element_eq(read_edge_dst(el2) , el1)):
- return 2 //incoming
- else:
- return 0 //the edge is not connected
- else:
- log("is_connected_to: unsupported type for second element")
- log(string_join("type = ", el2_mmt))
- elif (el1_mmt == "l5"):
- if( element_eq(read_edge_src(el1) , el2)):
- return 2 //incoming
- elif( element_eq(read_edge_dst(el1) , el2)):
- return 1 //outgoing
- else:
- return 0 //edge is not connected
- else:
- log("is_connected_to: unsupported type for first element")
- log(string_join("type = ", el1_mmt))
- return 0 // if the function doesn't support it, assume disconnected
- Element function new_SearchState(OldSearchState : Element, TransfModel : Element, NewTransfElem: Element, HostModel : Element, NewHostElem : Element):
- //some variables
- Element nss // new searchstate
- nss = create_node()
- Integer index // used for list looping
- Element elem // reusable loop variable
-
- //new MATCHES
- dict_add(nss, "matches", OldSearchState["matches"]) //hopefully works as a reference
-
- //new CURMATCH
- Element OldCurMatchKeys // for looping trough old curmatch (for copying)
- OldCurMatchKeys = dict_keys(OldSearchState["curmatch"])
- Element new_curmatch //the new curmatch
- new_curmatch = create_node()
- Element oldkey // loop variable
- while (integer_lt(0, list_len(OldCurMatchKeys))): //copying the old curmatch
- oldkey = set_pop(OldCurMatchKeys)
- dict_add(new_curmatch, oldkey, OldSearchState["curmatch"][oldkey]) //WARNING dict_read_node needed???
- Element newPair // the new pair to be added
- newPair = create_node()
- dict_add(newPair, "pn", NewTransfElem) //PatternNode
- dict_add(newPair, "hn", NewHostElem) // HostNode
- Integer plabel
- plabel = read_attribute(TransfModel, getName(TransfModel, NewTransfElem), "plabel")
- dict_add(new_curmatch, plabel, newPair) //add pair to new curmatch
- dict_add(nss, "curmatch", new_curmatch) //add curmatch to state
-
- //new N_PATTERN_OUT (copy everything but the new match elements)
- Element ol //old list = old pattern out
- Element nl //new list = new N pattern out
- ol = OldSearchState["N_pattern_out"]
- nl = create_node()
- index = 0
- while (index < list_len(ol)): //loop trough old list
- elem = list_read(ol, index)
- index = index + 1
- if(bool_not(element_eq(elem, NewTransfElem))):
- list_append(nl, elem)
- dict_add(nss, "N_pattern_out", nl)
-
- //new N_HOST_OUT (copy everything but the new match elements)
- ol = OldSearchState["N_host_out"]
- nl = create_node()
- index = 0
- while (index < list_len(ol)): //loop trough old list
- elem = list_read(ol, index)
- index = index + 1
- if(bool_not(element_eq(elem, NewHostElem))):
- list_append(nl, elem)
- dict_add(nss, "N_host_out", nl)
-
- //new N_PATTERN_IN (copy everything but the new match elements)
- ol = OldSearchState["N_pattern_in"]
- nl = create_node()
- index = 0
- while (index < list_len(ol)): //loop trough old list
- elem = list_read(ol, index)
- index = index + 1
- if(bool_not(element_eq(elem, NewTransfElem))):
- list_append(nl, elem)
- dict_add(nss, "N_pattern_in", nl)
-
- //new N_HOST_IN (copy everything but the new match elements
- ol = OldSearchState["N_host_in"]
- nl = create_node()
- index = 0
- while (index < list_len(ol)): //loop trough old list
- elem = list_read(ol, index)
- index = index + 1
- if(bool_not(element_eq(elem, NewHostElem))):
- list_append(nl, elem)
- dict_add(nss, "N_host_in", nl)
-
- //new V_PATTERN
- ol = OldSearchState["V_pattern"]
- nl = create_node()
- index = 0
- Integer connectedness
- while (index < list_len(ol)): //loop trough old list
- elem = list_read(ol, index)
- index = index + 1
- if(bool_not(element_eq(elem, NewTransfElem))):
- connectedness = are_connected(TransfModel, NewTransfElem, elem)
- if (connectedness == 0): // if not connected, keep in V
- list_append(nl, elem)
- elif (connectedness == 1): //if connected in outwards manner, don't keep in v and add to outlist
- list_append(nss["N_pattern_out"], elem)
- elif (connectedness == 2): //if connected in inwards manner, don't keep in v and add to inlist
- list_append(nss["N_pattern_in"], elem)
- else:
- log("new_SearchState: unsupported connectedness value")
- log(cast_i2s(connectedness))
- dict_add(nss, "V_pattern", nl)
-
- //new V_HOST
- ol = OldSearchState["V_host"]
- nl = create_node()
- index = 0
- while (index < list_len(ol)): //loop trough old list
- elem = list_read(ol, index)
- index = index + 1
- if(bool_not(element_eq(elem, NewHostElem))):
- connectedness = are_connected(HostModel, NewHostElem, elem)
- if (connectedness == 0): // if not connected, keep in V
- list_append(nl, elem)
- elif (connectedness == 1): //if connected in outwards manner, don't keep in v and add to outlist
- list_append(nss["N_host_out"], elem)
- elif (connectedness == 2): //if connected in inwards manner, don't keep in v and add to inlist
- list_append(nss["N_host_in"], elem)
- else:
- log("new_SearchState: unsupported connectedness value")
- log(cast_i2s(connectedness))
- dict_add(nss, "V_host", nl)
-
- // copy pattern_size
- dict_add(nss, "pattern_size", OldSearchState["pattern_size"])
-
- //copy pivots
- dict_add(nss, "pivots",OldSearchState["pivots"])
-
- return nss
- Element function transf_vf2(SearchState: Element, transf_m : Element, host_m : Element):
-
- // get the current match from the search-state
- Element current_match
- current_match = dict_read(SearchState,"curmatch")
- //if match is complete: add to matches and return
- Integer pattern_size
- pattern_size = SearchState["pattern_size"]
- if pattern_size == list_len(current_match):
- list_append(SearchState["matches"], SearchState["curmatch"])
- //log("MATCHED")
- return SearchState
- //feasability checks
- if (list_len(SearchState["N_pattern_out"]) > list_len(SearchState["N_host_out"])):
- log("not feasible1")
- return SearchState
- if (list_len(SearchState["N_pattern_in"]) > list_len(SearchState["N_host_in"])):
- log("not feasible2")
- return SearchState
- if (list_len(SearchState["V_pattern"]) > list_len(SearchState["V_host"])):
- log("not feasible3")
- return SearchState
- //initialise some variables
- Integer index2 //an index used for looping trough host_list
- Element host_list // the set with host nodes to be considered
- Element pattern_node // node to be tested from the patterns side
- Element host_node //node to be tested from the hosts side
- Element nss //variable for storing next searchstate
-
- //search candidate
- if ( list_len(SearchState["N_pattern_out"]) > 0 ):
- //check for candidates in (N_pattern_out, N_host_out)
- host_list = SearchState["N_host_out"]
- pattern_node = list_read(SearchState["N_pattern_out"], 0)
- index2 = 0
- while (index2 < list_len(host_list)):
- host_node = list_read(host_list, index2)
- index2= index2 + 1
- if(node_match(transf_m, pattern_node, host_m, host_node, SearchState["pivots"])):
- nss = new_SearchState(SearchState, transf_m, pattern_node, host_m, host_node)
- nss = transf_vf2(nss, transf_m, host_m)
- dict_delete(SearchState, "matches")
- dict_add(SearchState, "matches", nss["matches"])
- elif ( list_len(SearchState["N_pattern_in"]) > 0 ):
- //check for candidates in (N_pattern_in, N_host_in)
- host_list = SearchState["N_host_in"]
- pattern_node = list_read(SearchState["N_pattern_in"], 0)
- index2 = 0
- while (index2 < list_len(host_list)):
- host_node = list_read(host_list, index2)
- index2= index2 + 1
- if(node_match(transf_m, pattern_node, host_m, host_node, SearchState["pivots"])):
- nss = new_SearchState(SearchState, transf_m, pattern_node, host_m, host_node)
- nss = transf_vf2(nss, transf_m, host_m)
- dict_delete(SearchState, "matches")
- dict_add(SearchState, "matches", nss["matches"])
- elif ( list_len(SearchState["V_pattern"]) > 0 ):
- //check for candidates in (V_pattern, V_host))
- host_list = SearchState["V_host"]
- pattern_node = list_read(SearchState["V_pattern"], 0)
- index2 = 0
- while (index2 < list_len(host_list)):
- host_node = list_read(host_list, index2)
- index2= index2 + 1
- if(node_match(transf_m, pattern_node, host_m, host_node, SearchState["pivots"])):
- nss = new_SearchState(SearchState, transf_m, pattern_node, host_m, host_node)
- nss = transf_vf2(nss, transf_m, host_m)
- dict_delete(SearchState, "matches")
- dict_add(SearchState, "matches", nss["matches"])
- else:
- log("no candidate found")
- return SearchState
- // given a pattern, this function will return all matches in a model
- Element function tf_matchAll(tf_ma_transformation: Element, tf_ma_pattern : Element, tf_ma_model: Element, pivots:Element ):
-
- //find all nodes in the pattern
-
- Element tf_ma_metamodelTransf //transformation metamodel
- tf_ma_metamodelTransf = tf_ma_transformation["metamodel"]["model"]
- Element pre2ram_et // type of the pre2ram edge
- pre2ram_et = dict_read(tf_ma_metamodelTransf, "pre2ram")
- Element tf_ma_pattern_edges //all pre2ram edges
- tf_ma_pattern_edges = allOutgoingAssociationInstances(tf_ma_transformation, tf_ma_pattern, pre2ram_et)
- Element tf_ma_ramified_nodes //dict in the form plabel->node
- tf_ma_ramified_nodes = create_node()
- Element tf_ma_ramified_node
- while (integer_lt(0, list_len(tf_ma_pattern_edges))):
- tf_ma_ramified_node = read_edge_dst(set_pop(tf_ma_pattern_edges))
- list_append(tf_ma_ramified_nodes, tf_ma_ramified_node)
-
- //find all nodes in the graph that are not related to attributes: ie a userdefined type
-
- Element hostmodel_mm //metamodel of hostgraph
- hostmodel_mm = tf_ma_model["metamodel"]["model"]
- Element hostmodel_keys //keys of all elements in the model
- hostmodel_keys = dict_keys(tf_ma_model["model"])
- Element host_nodes //all nodes in the host graph
- host_nodes = create_node()
- Element host_model_key
- while (integer_lt(0, read_nr_out(hostmodel_keys))):
- host_model_key = set_pop(hostmodel_keys)
- if bool_not(isAttributeRelated(tf_ma_model, tf_ma_model["model"][host_model_key])):
- list_append(host_nodes, tf_ma_model["model"][host_model_key])
-
- // initialise the start-state
-
- //initialise an start state
- Element SearchState
- SearchState = create_node()
- dict_add(SearchState, "matches", create_node())
- dict_add(SearchState, "curmatch", create_node())
- dict_add(SearchState, "N_pattern_out", create_node())
- dict_add(SearchState, "N_host_out", create_node())
- dict_add(SearchState, "N_pattern_in", create_node())
- dict_add(SearchState, "N_host_in", create_node())
- dict_add(SearchState, "V_pattern", tf_ma_ramified_nodes)
- dict_add(SearchState, "V_host", host_nodes)
- dict_add(SearchState, "pattern_size", list_len(tf_ma_ramified_nodes))
- dict_add(SearchState, "pivots", pivots)
-
- //call vf2 for the matching
-
- SearchState = transf_vf2(SearchState, tf_ma_transformation, tf_ma_model)
- return SearchState["matches"]
- Element function mergeLHSNAC(tf_er_lhs : Element, tf_er_nac: Element):
- Element final_matches
- final_matches = create_node()
- Element lhs_match
- Element nac_match
- Element lhs_match_keys
- Element lhs_match_key
- Boolean allow_match
- Integer nac_index
- while(list_len(tf_er_lhs) > 0):
- lhs_match = set_pop(tf_er_lhs)
- allow_match=True
- nac_index = 0
- while(bool_and(nac_index < read_nr_out(tf_er_nac), allow_match)):
- nac_match = list_read(tf_er_nac, nac_index)
- nac_index = nac_index + 1
- lhs_match_keys = dict_keys(lhs_match)
- while (bool_and(read_nr_out(lhs_match_keys)>0, allow_match)):
- lhs_match_key = set_pop(lhs_match_keys)
- if dict_in(nac_match, lhs_match_key):
- if (element_eq(nac_match[lhs_match_key]["hn"],lhs_match[lhs_match_key]["hn"])):
- allow_match = False
- if (allow_match):
- list_append(final_matches, lhs_match)
- return final_matches
- Element function extractPivots(final_matches : Element, pivots: Element, tf_er_transformation : Element, tf_er_model : Element):
- Integer match_index
- match_index = 0
- Element pattern_match_node
- Element host_match_node
- Element lhs_match
- Element lhs_match_keys
- Element lhs_match_key
- String pivotOutName
- Element pivotSet
- pivotSet = create_node()
- while (match_index < read_nr_out(final_matches)):
- lhs_match = list_read(final_matches, match_index)
- match_index = match_index +1
- lhs_match_keys = dict_keys(lhs_match)
- while (read_nr_out(lhs_match_keys) > 0):
- lhs_match_key = set_pop(lhs_match_keys)
- pattern_match_node = lhs_match[lhs_match_key]["pn"]
- host_match_node = lhs_match[lhs_match_key]["hn"]
- pivotOutName = read_attribute(tf_er_transformation, getName(tf_er_transformation, pattern_match_node), "pivotOut")
- if(pivotOutName != ""):
- if ( bool_not(set_in(pivotSet, pivotOutName))):
- set_add(pivotSet, pivotOutName)
- if (dict_in(pivots, pivotOutName)):
- dict_delete(pivots, pivotOutName)
- dict_add(pivots, pivotOutName, create_node())
- set_add(pivots[pivotOutName], getName(tf_er_model,host_match_node))
- return 0
-
- Element function rhs_rewrite(final_matches : Element,plabel_to_rhsnode:Element, tf_er_transformation:Element, tf_er_model:Element ):
- //loop over all matches
- Integer match_index
- match_index = 0
- Element rhs_plabels
- Element rhs_plabel
- Element rhs_action
- String new_TypeName
- String new_MetaTypeName
- Element rhs_to_host //keep this mapping, so that we know wich nodes in the hostmodel new edges connect
- Element lhs_match_keys
- Element lhs_match_key
- Element host_match_node
- Element lhs_match
- Integer source_plabel
- Integer destination_plabel
- while (match_index < read_nr_out(final_matches)):
- lhs_match = list_read(final_matches, match_index)
- match_index = match_index +1
- //get set of plables
- rhs_plabels = dict_keys(plabel_to_rhsnode)
- //loop trough every node in the match
- lhs_match_keys = dict_keys(lhs_match)
- rhs_to_host = create_node()
- while (read_nr_out(lhs_match_keys) > 0):
- lhs_match_key = set_pop(lhs_match_keys)
- host_match_node = lhs_match[lhs_match_key]["hn"]
- if set_in(rhs_plabels, lhs_match_key): //if node matches plabel with rhs
- //run function
- rhs_action = read_attribute(tf_er_transformation,getName(tf_er_transformation, plabel_to_rhsnode[lhs_match_key]), "function")
- rhs_action = tf_er_transformation["actions"][rhs_action]
- rhs_action(tf_er_model, host_match_node)
- dict_add(rhs_to_host, lhs_match_key, host_match_node)
- //remove key from rhs_plabels
- set_remove(rhs_plabels, lhs_match_key)
- else: // if node doesn't match plabel with rhs
- model_delete_element(tf_er_model, getName(tf_er_model, host_match_node))
- //for each remaining rhs-key (it is not in the match, so should be added)
- while (read_nr_out(rhs_plabels) > 0):
- rhs_plabel = set_pop(rhs_plabels)
- //get type and metatype
- new_TypeName = string_substr(getTypeName(tf_er_transformation, plabel_to_rhsnode[rhs_plabel]), 0,-4)
- new_MetaTypeName = getMetaTypeName(tf_er_transformation, plabel_to_rhsnode[rhs_plabel])
- //if it is a node, than we can simply add it
- if (new_MetaTypeName == "Class"):
- host_match_node = instantiate_node(tf_er_model, new_TypeName, "")
- //run the function
- rhs_action = read_attribute(tf_er_transformation,getName(tf_er_transformation, plabel_to_rhsnode[rhs_plabel]), "function")
- rhs_action = tf_er_transformation["actions"][rhs_action]
- rhs_action(tf_er_model, tf_er_model["model"][host_match_node])
- elif (new_MetaTypeName == "l5"):
- //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
- source_plabel = read_attribute(tf_er_transformation, getName(tf_er_transformation, read_edge_src(plabel_to_rhsnode[rhs_plabel]) ) , "plabel")
- destination_plabel = read_attribute(tf_er_transformation, getName(tf_er_transformation, read_edge_dst(plabel_to_rhsnode[rhs_plabel]) ) , "plabel")
- if(bool_and(dict_in(rhs_to_host, source_plabel), dict_in(rhs_to_host, destination_plabel))):
- 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])))
- //run function
- rhs_action = read_attribute(tf_er_transformation,getName(tf_er_transformation, plabel_to_rhsnode[rhs_plabel]), "function")
- rhs_action = tf_er_transformation["actions"][rhs_action]
- rhs_action(tf_er_model, tf_er_model["model"][host_match_node])
- else:
- //TODO this is wrong and might cause an infinite loop
- set_add(rhs_plabels, rhs_plabel)
- else:
- log("unsupported metatype in rhs")
- return 0
-
- Element function tf_executeFRule(tf_er_transformation: Element,tf_er_rule: Element, tf_er_model: Element, pivots : Element):
-
- //find LHS
- //TODO if no lhs (is this allowed to happen?) // or empty lhs
- Element tf_er_lhs
- tf_er_lhs = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, dict_read(dict_read(dict_read(tf_er_transformation, "metamodel"), "model"), "lhs"))
- tf_er_lhs = tf_matchAll(tf_er_transformation, read_edge_dst(set_pop(tf_er_lhs)), tf_er_model, pivots)
- Integer nr_of_lhs_matches
- nr_of_lhs_matches = list_len(tf_er_lhs)
-
- //find NACs if at least one lhs is found (otherwise not needed)
- Integer nr_of_nac_matches
- nr_of_nac_matches = 0
- Element tf_er_nac
- tf_er_nac = create_node()
- if(nr_of_lhs_matches > 0):
- Element tf_er_nac_
- tf_er_nac_ = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, dict_read(dict_read(dict_read(tf_er_transformation, "metamodel"), "model"), "nac"))
- Element partial_nac_matches
- while (read_nr_out(tf_er_nac_) > 0):
- partial_nac_matches = tf_matchAll(tf_er_transformation, read_edge_dst(set_pop(tf_er_nac_)), tf_er_model, pivots)
- while (read_nr_out(partial_nac_matches)>0):
- list_append(tf_er_nac, set_pop(partial_nac_matches))
- nr_of_nac_matches = list_len(tf_er_nac)
-
- //remove from lhs matches that share a node with a nac match
- //TODO if nac is not related to match (no plabels match)
- Element final_matches
- final_matches = mergeLHSNAC(tf_er_lhs, tf_er_nac)
-
- //extract pivots from the matches
- extractPivots(final_matches, pivots, tf_er_transformation, tf_er_model)
- //rhs part
-
- //create plabel_to_rhsnode
- Element RHS
- RHS = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, tf_er_transformation["metamodel"]["model"]["rhs"])
- if(read_nr_out(RHS) > 0):
- RHS = read_edge_dst(set_pop(RHS))
- Element metamodelTransf //transformation metamodel
- metamodelTransf = tf_er_transformation["metamodel"]["model"]
- Element post2ram // type of the post2ram edge
- post2ram = dict_read(metamodelTransf, "post2ram")
- post2ram = allOutgoingAssociationInstances(tf_er_transformation, RHS, post2ram)
- Element plabel_to_rhsnode //dict in the form plabel->node
- plabel_to_rhsnode = create_node()
- Element rhs_node
- Integer rhs_plabel
- while (integer_lt(0, list_len(post2ram))):
- rhs_node = read_edge_dst(set_pop(post2ram))
- rhs_plabel = read_attribute(tf_er_transformation, getName(tf_er_transformation, rhs_node), "plabel")
- dict_add(plabel_to_rhsnode,rhs_plabel, rhs_node)
-
- rhs_rewrite(final_matches ,plabel_to_rhsnode, tf_er_transformation, tf_er_model )
-
- return pivots
- Integer function modulo(a:Integer,b:Integer):
- return a - integer_division(a,b)*b
- Integer RandomNumberState
- Integer function getRandomNumber(min:Integer, max:Integer):
- if( bool_not(is_physical_int(RandomNumberState))):
- RandomNumberState = 4 // xkcd.com/221/
- RandomNumberState = modulo((8121*RandomNumberState) + 28411, 134456)
- return modulo(RandomNumberState , (max-min)+1)+min
- Element function tf_executeARule(tf_er_transformation: Element,tf_er_rule: Element, tf_er_model: Element, pivots : Element):
-
- //find LHS
- //TODO if no lhs (is this allowed to happen?) // or empty lhs
- Element tf_er_lhs
- tf_er_lhs = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, dict_read(dict_read(dict_read(tf_er_transformation, "metamodel"), "model"), "lhs"))
- tf_er_lhs = tf_matchAll(tf_er_transformation, read_edge_dst(set_pop(tf_er_lhs)), tf_er_model, pivots)
- Integer nr_of_lhs_matches
- nr_of_lhs_matches = list_len(tf_er_lhs)
-
- //find NACs if at least one lhs is found (otherwise not needed)
- Integer nr_of_nac_matches
- nr_of_nac_matches = 0
- Element tf_er_nac
- tf_er_nac = create_node()
- if(nr_of_lhs_matches > 0):
- Element tf_er_nac_
- tf_er_nac_ = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, dict_read(dict_read(dict_read(tf_er_transformation, "metamodel"), "model"), "nac"))
- Element partial_nac_matches
- while (read_nr_out(tf_er_nac_) > 0):
- partial_nac_matches = tf_matchAll(tf_er_transformation, read_edge_dst(set_pop(tf_er_nac_)), tf_er_model, pivots)
- while (read_nr_out(partial_nac_matches)>0):
- list_append(tf_er_nac, set_pop(partial_nac_matches))
- nr_of_nac_matches = list_len(tf_er_nac)
-
- //remove from lhs matches that share a node with a nac match
- //TODO if nac is not related to match (no plabels match)
- Element final_matches
- final_matches = mergeLHSNAC(tf_er_lhs, tf_er_nac)
-
- //extract pivots from the matches
- extractPivots(final_matches, pivots, tf_er_transformation, tf_er_model)
- //rhs part
-
- //create plabel_to_rhsnode
- Element RHS
- RHS = allOutgoingAssociationInstances(tf_er_transformation, tf_er_rule, tf_er_transformation["metamodel"]["model"]["rhs"])
- if(read_nr_out(RHS) > 0):
- RHS = read_edge_dst(set_pop(RHS))
- Element metamodelTransf //transformation metamodel
- metamodelTransf = tf_er_transformation["metamodel"]["model"]
- Element post2ram // type of the post2ram edge
- post2ram = dict_read(metamodelTransf, "post2ram")
- post2ram = allOutgoingAssociationInstances(tf_er_transformation, RHS, post2ram)
- Element plabel_to_rhsnode //dict in the form plabel->node
- plabel_to_rhsnode = create_node()
- Element rhs_node
- Integer rhs_plabel
- while (integer_lt(0, list_len(post2ram))):
- rhs_node = read_edge_dst(set_pop(post2ram))
- rhs_plabel = read_attribute(tf_er_transformation, getName(tf_er_transformation, rhs_node), "plabel")
- dict_add(plabel_to_rhsnode,rhs_plabel, rhs_node)
-
- rhs_rewrite(final_matches ,plabel_to_rhsnode, tf_er_transformation, tf_er_model )
-
- return pivots
- Element function executeRuleBlock(transformation_model : Element,hostmodel:Element, ruleblock : Element, pivots:Element):
- //execute the correct ruletype
- //TODO define decent succes_value
- String typename
- typename = getTypeName(transformation_model, ruleblock)
- Boolean succes
- succes = False
- Element Rule
- if (typename == "RuleBlockRoot"):
- // do nothing
- log("start transformation")
- succes = True
- elif (typename == "ARule"):
- log("running arule")
- Rule = read_edge_dst(set_pop(allOutgoingAssociationInstances(transformation_model, ruleblock, transformation_model["metamodel"]["model"]["getrule"])))
- log(getName(transformation_model, Rule))
- pivots = tf_executeARule(transformation_model, Rule, hostmodel, pivots)
- succes = True
- elif (typename == "FRule"):
- log("running frule")
- Rule = read_edge_dst(set_pop(allOutgoingAssociationInstances(transformation_model, ruleblock, transformation_model["metamodel"]["model"]["getrule"])))
- log(getName(transformation_model, Rule))
- pivots = tf_executeFRule(transformation_model, Rule, hostmodel, pivots)
- succes = True
- else:
- log("RuleType is not supported yet")
- //Return the next
- Element next
- if succes:
- next = allOutgoingAssociationInstances(transformation_model, ruleblock, transformation_model["metamodel"]["model"]["next_onSucces"])
- else:
- next = allOutgoingAssociationInstances(transformation_model, ruleblock, transformation_model["metamodel"]["model"]["next_onFailure"])
- if (read_nr_out(next) == 0):
- log("No next defined")
- next = transformation_model
- else:
- next = read_edge_dst(set_pop(next))
-
- return next
- Element function tf_transform(tf_tf_transformation: Element, tf_tf_model: Element):
- //create a list of all rules
- Element ruleblock
- ruleblock = allInstances(tf_tf_transformation, tf_tf_transformation["metamodel"]["model"]["RuleBlockRoot"])
- if(read_nr_out(ruleblock) > 0):
- ruleblock = set_pop(ruleblock)
- Element pivots
- pivots = create_node()
- Boolean continue
- continue = True
- while (continue):
- ruleblock = executeRuleBlock(tf_tf_transformation,tf_tf_model, ruleblock, pivots)
- if(element_eq(ruleblock,tf_tf_transformation)):
- log("finished")
- continue = False
- else:
- log("root ruleblock not found")
- return 0
- Element function test_simple_petrinet1(root:Element):
- //create a petrinet for testing purposes
-
- // P1 -> T1 -> P2 -> T2
- // m:1 w:1 w:2 m:0 w:1
- // l1 l2 l3
-
- //////////////////////////////////
- //initialise an empty petrinet
- Element my_model
- my_model = instantiate_model(root["PetriNets"])
- dict_add(root, "pnt_test1", my_model)
-
- // init t1
- instantiate_node(my_model, "Transition", "t1")
- // init p1
- instantiate_node(my_model, "Place", "p1")
- instantiate_attribute(my_model, "p1","tokens",1)
- // init link1
- instantiate_link(my_model, "P2T", "p1_to_t1", "p1", "t1")
- instantiate_attribute(my_model, "p1_to_t1", "weight", 1)
- //init p2
- instantiate_node(my_model, "Place", "p2")
- instantiate_attribute(my_model, "p2","tokens",0)
- //init link2
- instantiate_link(my_model, "T2P", "t1_to_p2", "t1", "p2")
- instantiate_attribute(my_model, "t1_to_p2", "weight", 2)
- //init t2
- instantiate_node(my_model, "Transition", "t2")
- //init link3
- instantiate_link(my_model, "P2T", "p2_to_t2", "p2", "t2")
- instantiate_attribute(my_model, "p2_to_t2", "weight", 1)
-
-
- return my_model
-
-
- Element function petrinet_transformation(root: Element):
- // Create A transformation model for petrinets (useful for testing)
- //////////////////////////////////
- //initialise an empty transformation model for petrinet
- Element my_model
- my_model = instantiate_model(root["PetriNets_MTransformation"])
- define_inheritance(my_model, "l3")
- dict_add(root, "transf_petrinet_fire", my_model)
- ////////////////////////////
- // root node
- instantiate_node(my_model, "Transformation", "PetrinetFire")
- instantiate_attribute(my_model, "PetrinetFire", "name", "PetriNetFire")
- ////////////////////////////
- // rule: find transitions
- //rule: find_transitions
- instantiate_node(my_model, "Rule", "find_transitions")
- instantiate_attribute(my_model, "find_transitions", "name", "find_transitions")
- instantiate_attribute(my_model, "find_transitions", "precedence", 1)
- //connect to transformation
- instantiate_link(my_model, "transformation_to_rule", "", "PetrinetFire", "find_transitions")
- //precondition: lhs_precondition
- instantiate_node(my_model, "Precondition","lhs_precondition")
- //connect to rule
- instantiate_link(my_model, "lhs", "", "find_transitions", "lhs_precondition")
- //Transition_LHS: lhs_find_all
- instantiate_node(my_model, "Transition_LHS","lhs_find_all")
- instantiate_attribute(my_model, "lhs_find_all", "plabel", 1)
- instantiate_attribute(my_model, "lhs_find_all", "function", "always_true")
- instantiate_attribute(my_model, "lhs_find_all", "pivotOut", "enabled")
- instantiate_attribute(my_model, "lhs_find_all", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "pre2ram", "", "lhs_precondition", "lhs_find_all")
- //precondition: nac_precondition
- instantiate_node(my_model, "Precondition","nac_precondition")
- //connect to rule
- instantiate_link(my_model, "nac", "", "find_transitions", "nac_precondition")
- //Transition_LHS: nac_transition_place_before
- instantiate_node(my_model, "Transition_LHS","nac_transition_before")
- instantiate_attribute(my_model, "nac_transition_before", "plabel", 1)
- instantiate_attribute(my_model, "nac_transition_before", "function", "always_true")
- instantiate_attribute(my_model, "nac_transition_before", "pivotOut", "")
- instantiate_attribute(my_model, "nac_transition_before", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "pre2ram", "", "nac_precondition", "nac_transition_before")
- //Place_LHS: nac_place_before
- instantiate_node(my_model, "Place_LHS","nac_place_before")
- instantiate_attribute(my_model, "nac_place_before", "plabel", 2)
- instantiate_attribute(my_model, "nac_place_before", "function", "always_true")
- instantiate_attribute(my_model, "nac_place_before", "pivotOut", "")
- instantiate_attribute(my_model, "nac_place_before", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "pre2ram", "", "nac_precondition", "nac_place_before")
- //P2T_LHS: nac_ft_p2t
- instantiate_link(my_model, "P2T_LHS", "nac_p2tlhs_before", "nac_place_before", "nac_transition_before")
- instantiate_attribute(my_model, "nac_p2tlhs_before", "plabel", 3)
- instantiate_attribute(my_model, "nac_p2tlhs_before", "function", "is_not_consumable")
- instantiate_attribute(my_model, "nac_p2tlhs_before", "pivotOut", "")
- instantiate_attribute(my_model, "nac_p2tlhs_before", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "pre2ram", "", "nac_precondition", "nac_p2tlhs_before")
-
- ////////////////////////////
- // rule: consume tokens
- //rule: consume_tokens
- instantiate_node(my_model, "Rule", "consume_tokens")
- instantiate_attribute(my_model, "consume_tokens", "name", "consume_tokens")
- instantiate_attribute(my_model, "consume_tokens", "precedence", 2)
- //connect to transformation
- instantiate_link(my_model, "transformation_to_rule", "", "PetrinetFire", "consume_tokens")
- //precondition: lhs_precondition
- instantiate_node(my_model, "Precondition","ct_lhs_precondition")
- //connect to rule
- instantiate_link(my_model, "lhs", "", "consume_tokens", "ct_lhs_precondition")
-
- //Transition_LHS: ct_lhs_transition
- instantiate_node(my_model, "Transition_LHS","ct_lhs_transition")
- instantiate_attribute(my_model, "ct_lhs_transition", "plabel", 1)
- instantiate_attribute(my_model, "ct_lhs_transition", "function", "always_true")
- instantiate_attribute(my_model, "ct_lhs_transition", "pivotOut", "")
- instantiate_attribute(my_model, "ct_lhs_transition", "pivotIn", "enabled")
- //connect to rule
- instantiate_link(my_model, "pre2ram", "", "ct_lhs_precondition", "ct_lhs_transition")
-
- //Place_LHS: ct_lhs_place
- instantiate_node(my_model, "Place_LHS","ct_lhs_place")
- instantiate_attribute(my_model, "ct_lhs_place", "plabel", 2)
- instantiate_attribute(my_model, "ct_lhs_place", "function", "always_true")
- instantiate_attribute(my_model, "ct_lhs_place", "pivotOut", "")
- instantiate_attribute(my_model, "ct_lhs_place", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "pre2ram", "", "ct_lhs_precondition", "ct_lhs_place")
-
- //P2T_LHS: ct_lhs_link
- instantiate_link(my_model, "P2T_LHS", "ct_lhs_link", "ct_lhs_place", "ct_lhs_transition")
- instantiate_attribute(my_model, "ct_lhs_link", "plabel", 3)
- instantiate_attribute(my_model, "ct_lhs_link", "function", "always_true")
- instantiate_attribute(my_model, "ct_lhs_link", "pivotOut", "")
- instantiate_attribute(my_model, "ct_lhs_link", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "pre2ram", "", "ct_lhs_precondition", "ct_lhs_link")
-
- //precondition: rhs_postcondition
- instantiate_node(my_model, "Postcondition","ct_rhs_postcondition")
- //connect to rule
- instantiate_link(my_model, "rhs", "", "consume_tokens", "ct_rhs_postcondition")
-
- //Transition_RHS: ct_rhs_transition
- instantiate_node(my_model, "Transition_RHS","ct_rhs_transition")
- instantiate_attribute(my_model, "ct_rhs_transition", "plabel", 1)
- instantiate_attribute(my_model, "ct_rhs_transition", "function", "do_nothing")
- instantiate_attribute(my_model, "ct_rhs_transition", "pivotOut", "")
- instantiate_attribute(my_model, "ct_rhs_transition", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "post2ram", "", "ct_rhs_postcondition", "ct_rhs_transition")
-
- //Place_RHS: ct_rhs_place
- instantiate_node(my_model, "Place_RHS","ct_rhs_place")
- instantiate_attribute(my_model, "ct_rhs_place", "plabel", 2)
- instantiate_attribute(my_model, "ct_rhs_place", "function", "do_nothing")
- instantiate_attribute(my_model, "ct_rhs_place", "pivotOut", "")
- instantiate_attribute(my_model, "ct_rhs_place", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "post2ram", "", "ct_rhs_postcondition", "ct_rhs_place")
-
- //P2T_RHS: ct_rhs_link
- instantiate_link(my_model, "P2T_RHS", "ct_rhs_link", "ct_rhs_place", "ct_rhs_transition")
- instantiate_attribute(my_model, "ct_rhs_link", "plabel", 3)
- instantiate_attribute(my_model, "ct_rhs_link", "function", "remove_tokens")
- instantiate_attribute(my_model, "ct_rhs_link", "pivotOut", "")
- instantiate_attribute(my_model, "ct_rhs_link", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "post2ram", "", "ct_rhs_postcondition", "ct_rhs_link")
-
- ////////////////////////////
- // rule: produce tokens
- //rule: produce_tokens
- instantiate_node(my_model, "Rule", "produce_tokens")
- instantiate_attribute(my_model, "produce_tokens", "name", "produce_tokens")
- instantiate_attribute(my_model, "produce_tokens", "precedence", 3)
- //connect to transformation
- instantiate_link(my_model, "transformation_to_rule", "", "PetrinetFire", "produce_tokens")
- //precondition: lhs_precondition
- instantiate_node(my_model, "Precondition","pt_lhs_precondition")
- //connect to rule
- instantiate_link(my_model, "lhs", "", "produce_tokens", "pt_lhs_precondition")
-
- //Transition_LHS: pt_lhs_transition
- instantiate_node(my_model, "Transition_LHS","pt_lhs_transition")
- instantiate_attribute(my_model, "pt_lhs_transition", "plabel", 1)
- instantiate_attribute(my_model, "pt_lhs_transition", "function", "always_true")
- instantiate_attribute(my_model, "pt_lhs_transition", "pivotOut", "")
- instantiate_attribute(my_model, "pt_lhs_transition", "pivotIn", "enabled")
- //connect to rule
- instantiate_link(my_model, "pre2ram", "", "pt_lhs_precondition", "pt_lhs_transition")
-
- //Place_LHS: pt_lhs_place
- instantiate_node(my_model, "Place_LHS","pt_lhs_place")
- instantiate_attribute(my_model, "pt_lhs_place", "plabel", 2)
- instantiate_attribute(my_model, "pt_lhs_place", "function", "always_true")
- instantiate_attribute(my_model, "pt_lhs_place", "pivotOut", "")
- instantiate_attribute(my_model, "pt_lhs_place", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "pre2ram", "", "pt_lhs_precondition", "pt_lhs_place")
-
- //P2T_LHS: pt_lhs_link
- instantiate_link(my_model, "T2P_LHS", "pt_lhs_link", "pt_lhs_transition", "pt_lhs_place")
- instantiate_attribute(my_model, "pt_lhs_link", "plabel", 3)
- instantiate_attribute(my_model, "pt_lhs_link", "function", "always_true")
- instantiate_attribute(my_model, "pt_lhs_link", "pivotOut", "")
- instantiate_attribute(my_model, "pt_lhs_link", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "pre2ram", "", "pt_lhs_precondition", "pt_lhs_link")
-
- //precondition: rhs_postcondition
- instantiate_node(my_model, "Postcondition","pt_rhs_postcondition")
- //connect to rule
- instantiate_link(my_model, "rhs", "", "produce_tokens", "pt_rhs_postcondition")
-
- //Transition_RHS: pt_rhs_transition
- instantiate_node(my_model, "Transition_RHS","pt_rhs_transition")
- instantiate_attribute(my_model, "pt_rhs_transition", "plabel", 1)
- instantiate_attribute(my_model, "pt_rhs_transition", "function", "do_nothing")
- instantiate_attribute(my_model, "pt_rhs_transition", "pivotOut", "")
- instantiate_attribute(my_model, "pt_rhs_transition", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "post2ram", "", "pt_rhs_postcondition", "pt_rhs_transition")
-
- //Place_RHS: pt_rhs_place
- instantiate_node(my_model, "Place_RHS","pt_rhs_place")
- instantiate_attribute(my_model, "pt_rhs_place", "plabel", 2)
- instantiate_attribute(my_model, "pt_rhs_place", "function", "do_nothing")
- instantiate_attribute(my_model, "pt_rhs_place", "pivotOut", "")
- instantiate_attribute(my_model, "pt_rhs_place", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "post2ram", "", "pt_rhs_postcondition", "pt_rhs_place")
-
- //P2T_RHS: ct_rhs_link
- instantiate_link(my_model, "T2P_RHS", "pt_rhs_link", "pt_rhs_transition", "pt_rhs_place")
- instantiate_attribute(my_model, "pt_rhs_link", "plabel", 3)
- instantiate_attribute(my_model, "pt_rhs_link", "function", "add_tokens")
- instantiate_attribute(my_model, "pt_rhs_link", "pivotOut", "")
- instantiate_attribute(my_model, "pt_rhs_link", "pivotIn", "")
- //connect to rule
- instantiate_link(my_model, "post2ram", "", "pt_rhs_postcondition", "pt_rhs_link")
-
- ////////////////////////////
- // rule ordering part
-
- //start node
- instantiate_node(my_model, "RuleBlockRoot", "rule_root")
-
- //ARule: enabled
- instantiate_node(my_model, "ARule", "arule1")
- //connect to startnode
- instantiate_link(my_model, "next_onSucces", "", "rule_root", "arule1")
- //connect to rule
- instantiate_link(my_model, "getrule","", "arule1", "find_transitions")
-
- //FRule: consume
- instantiate_node(my_model, "FRule", "frule2")
- //connect to arule1
- instantiate_link(my_model, "next_onSucces", "", "arule1", "frule2")
- //connect to rule
- instantiate_link(my_model, "getrule","", "frule2", "consume_tokens")
-
- //Frule: produce
- instantiate_node(my_model, "FRule", "frule3")
- //connect to frule2
- instantiate_link(my_model, "next_onSucces", "", "frule2", "frule3")
- //connect to rule
- instantiate_link(my_model, "getrule","", "frule3", "produce_tokens")
-
-
- ////////////////////////////
- // semantic checks
-
- Element semantic_checks
- semantic_checks = create_node()
- dict_add(semantic_checks, "always_true", always_true)
- dict_add(semantic_checks, "is_not_consumable", is_not_consumable)
- dict_add(my_model, "semantic_checks", semantic_checks)
-
- Element actions
- actions = create_node()
- dict_add(actions, "do_nothing", do_nothing)
- dict_add(actions, "add_tokens", add_tokens)
- dict_add(actions, "remove_tokens", remove_tokens)
- dict_add(my_model, "actions", actions)
-
- return my_model
-
- Boolean function is_not_consumable(model : Element, matched_node : Element):
- //get place before
- Element place_before
- place_before = read_edge_src(matched_node)
- //get tokens from place
- Integer tokens
- tokens = read_attribute(model, getName(model, place_before), "tokens")
- //get weight from matched_node
- Integer weight
- weight = read_attribute(model, getName(model, matched_node), "weight")
- return weight > tokens
-
- Boolean function always_true(model : Element, self : Element):
- return True
- Element function do_nothing(model: Element, self : Element):
- return 0
- Element function remove_tokens(model:Element, self: Element):
- //get place before
- Element place_before
- place_before = read_edge_src(self)
- //get tokens from place
- Integer tokens
- tokens = read_attribute(model, getName(model, place_before), "tokens")
- //get weight from matched_node
- Integer weight
- weight = read_attribute(model, getName(model, self), "weight")
- //rewrite tokens
- unset_attribute(model, getName(model, place_before), "tokens")
- instantiate_attribute(model, getName(model, place_before), "tokens", tokens-weight)
- return 0
- Element function add_tokens(model:Element, self: Element):
- //get place before
- Element place_after
- place_after = read_edge_dst(self)
- //get tokens from place
- Integer tokens
- tokens = read_attribute(model, getName(model, place_after), "tokens")
- //get weight from matched_node
- Integer weight
- weight = read_attribute(model, getName(model, self), "weight")
- //rewrite tokens
- unset_attribute(model, getName(model, place_after), "tokens")
- instantiate_attribute(model, getName(model, place_after), "tokens", tokens+weight)
- return 0
-
-
-
- Element function pn_operations():
- Element ops
- ops = create_node()
- dict_add(ops, "fire", petrinet_fire)
- dict_add(ops, "enabled", petrinet_enabled)
- return ops
- Element function petrinet_enabled(model : Element):
- Element set_enabled
- set_enabled = petrinet_enabled_set(model)
- output("Enabled transitions:")
- while (0 < read_nr_out(set_enabled)):
- output(getName(model, set_pop(set_enabled)))
- return model
- Element function petrinet_enabled_set(model : Element):
- Element all_transitions
- all_transitions = allInstances(model, model["metamodel"]["model"]["Transition"])
- Element enabled_transitions
- enabled_transitions = create_node()
- Element under_study
- Element in_arcs
- Element arc_under_study
- Boolean enabled
- while (0 < read_nr_out(all_transitions)):
- under_study = set_pop(all_transitions)
- enabled = True
- // Find all incoming transitions
- in_arcs = allIncomingAssociationInstances(model, under_study, model["metamodel"]["model"]["P2T"])
- while (0 < read_nr_out(in_arcs)):
- arc_under_study = set_pop(in_arcs)
- Integer present_tokens
- Integer required_tokens
- required_tokens = read_attribute(model, getName(model, arc_under_study), "weight")
- present_tokens = read_attribute(model, getName(model, read_edge_src(arc_under_study)), "tokens")
- if (present_tokens < required_tokens):
- // Less tokens than required, so disable the transition completely
- enabled = False
- if (enabled):
- set_add(enabled_transitions, under_study)
- return enabled_transitions
- Element function petrinet_fire(model : Element):
- output("Transition to fire?")
- Element transition
- transition = input()
- if (dict_in(model["model"], transition)):
- transition = model["model"][transition]
- if (set_in(petrinet_enabled_set(model), transition)):
- Element workset
- Element working_place
- Element working_arc
- Integer new_value
- // Consume tokens
- workset = allIncomingAssociationInstances(model, transition, model["metamodel"]["model"]["P2T"])
- while (0 < read_nr_out(workset)):
- working_arc = set_pop(workset)
- working_place = read_edge_src(working_arc)
- new_value = integer_subtraction(read_attribute(model, getName(model, working_place), "tokens"), read_attribute(model, getName(model, working_arc), "weight"))
- unset_attribute(model, getName(model, working_place), "tokens")
- instantiate_attribute(model, getName(model, working_place), "tokens", new_value)
- output(((" " + getName(model, working_place)) + ": ") + cast_i2s(read_attribute(model, getName(model, working_place), "tokens")))
- // Add tokens
- workset = allOutgoingAssociationInstances(model, transition, model["metamodel"]["model"]["T2P"])
- while (0 < read_nr_out(workset)):
- working_arc = set_pop(workset)
- working_place = read_edge_dst(working_arc)
- new_value = integer_addition(read_attribute(model, getName(model, working_place), "tokens"), read_attribute(model, getName(model, working_arc), "weight"))
- unset_attribute(model, getName(model, working_place), "tokens")
- instantiate_attribute(model, getName(model, working_place), "tokens", new_value)
- output(((" " + getName(model, working_place)) + ": ") + cast_i2s(read_attribute(model, getName(model, working_place), "tokens")))
- output("Transition fired!")
- else:
- output("Cannot fire if not enabled; aborting")
- else:
- output("Unknown transition; aborting")
- return model
-
- Element function petrinet_loaded(model : Element):
- String cmd
- Element attr_list_pn
- Element attr_keys_pn
- String attr_key_pn
- Element metamodel_element_pn
- String typename
- Boolean bottom
- Element other_metamodel
- bottom = False
- other_metamodel = create_node()
- dict_add(other_metamodel, "model", model["model"])
- dict_add(other_metamodel, "type_mapping", create_node())
- dict_add(other_metamodel, "metamodel", import_node("models/LTM_bottom"))
- dict_add(other_metamodel, "inheritance", other_metamodel["metamodel"]["model"]["__Inheritance"])
- output("Model loaded, ready for commands!")
- output("Use 'help' command for a list of possible commands")
- while (True):
- output("Please give your command.")
- cmd = input()
- if (cmd == "help"):
- output("Generic model operations:")
- output(" instantiate -- Create a new model element")
- output(" delete -- Delete an existing element")
- output(" attr_add -- Add an attribute to an element")
- output(" attr_del -- Delete an attribute of an element")
- output(" rename -- Rename an existing element")
- output(" modify -- Modify the attributes of an element")
- output(" list -- Prints the list of elements in the model")
- output(" types -- Prints the list of elements that can be instantiated")
- output(" read -- Prints the current state of a model element")
- output(" verify -- Check whether the model conforms to the metamodel")
- output(" retype -- Change the type of an element")
- output(" switch -- Switch between conformance bottom and the linguistic metamodel")
- output(" exit -- Unload the model and go back to the loading prompt")
- if (bool_not(bottom)):
- output("Model-specific operations:")
- Element specific_ops
- specific_ops = dict_keys(pn_operations())
- String specific_op
- while (0 < dict_len(specific_ops)):
- specific_op = set_pop(specific_ops)
- output(" " + specific_op)
- elif (cmd == "exit"):
- return model
- elif (cmd == "instantiate"):
- String mm_type_name
- output("Type to instantiate?")
- mm_type_name = input()
- if (dict_in(model["metamodel"]["model"], mm_type_name)):
- String element_name
- output("Name of new element?")
- element_name = input()
- if (dict_in(model["model"], element_name)):
- output("Element already exists; aborting")
- else:
- if (is_edge(model["metamodel"]["model"][mm_type_name])):
- output("Source name?")
- String src_name
- src_name = input()
- if (dict_in(model["model"], src_name)):
- output("Destination name?")
- String dst_name
- dst_name = input()
- if (dict_in(model["model"], dst_name)):
- instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
- if (dict_in(model["model"], element_name)):
- output("Instantiation successful!")
- else:
- output("Instantiation error!")
- else:
- output("Unknown destination; aborting")
- else:
- output("Unknown source; aborting")
- else:
- instantiate_node(model, mm_type_name, element_name)
- if (dict_in(model["model"], element_name)):
- output("Instantiation successful!")
- else:
- output("Instantiation error!")
- else:
- output("Unknown type specified; aborting")
- elif (cmd == "modify"):
- String model_name
- output("Element to modify?")
- model_name = input()
- if (dict_in(model["model"], model_name)):
- Element attrs
- attrs = getAttributeList(model, model_name)
- String attr_name
- output("Attribute to modify?")
- attr_name = input()
- if (set_in(dict_keys(attrs), attr_name)):
- output("New value?")
- unset_attribute(model, model_name, attr_name)
- instantiate_attribute(model, model_name, attr_name, input())
- output("Modified!")
- else:
- output("No such attribute!")
- else:
- output("No such model!")
- elif (cmd == "attr_add"):
- String model_name
- output("Which model do you want to assign an attribute to?")
- model_name = input()
- if (dict_in(model["model"], model_name)):
- Element attrs
- attrs = getAttributeList(model, model_name)
- String attr_name
- output("Which attribute do you wish to assign?")
- attr_name = input()
- if (set_in(dict_keys(attrs), attr_name)):
- output("Value of attribute?")
- instantiate_attribute(model, model_name, attr_name, input())
- output("Added attribute!")
- else:
- output("No such attribute!")
- else:
- output("No such model!")
- elif (cmd == "attr_del"):
- String model_name
- output("Which model do you want to remove an attribute of?")
- model_name = input()
- if (dict_in(model["model"], model_name)):
- Element attrs
- attrs = getAttributeList(model, model_name)
- output(print_dict(attrs))
- String attr_name
- output("Which attribute do you want to delete?")
- attr_name = input()
- if (set_in(dict_keys(attrs), attr_name)):
- unset_attribute(model, model_name, attr_name)
- else:
- output("No such attribute!")
- else:
- output("No such model!")
- elif (cmd == "delete"):
- output("What is the name of the element you want to delete?")
- cmd = input()
- if (dict_in(model["model"], cmd)):
- model_delete_element(model, cmd)
- output("Deleted!")
- else:
- output("No such element; aborting")
- elif (cmd == "rename"):
- output("Old name?")
- String old_name_e
- old_name_e = input()
- if (dict_in(model["model"], old_name_e)):
- output("New name?")
- String new_name_e
- new_name_e = input()
- if (dict_in(model["model"], new_name_e)):
- output("New name already used; aborting")
- else:
- dict_add(model["model"], new_name_e, model["model"][old_name_e])
- dict_delete(model["model"], old_name_e)
- output("Rename complete!")
- else:
- output("Unknown element; aborting")
- elif (cmd == "list"):
- Element keys_m
- keys_m = dict_keys(model["model"])
- output("List of all elements:")
- String v_m
- while (read_nr_out(keys_m) > 0):
- v_m = set_pop(keys_m)
- // Filter out anonymous objects
- typename = getName(model["metamodel"], dict_read_node(model["type_mapping"], model["model"][v_m]))
- if (bool_not(string_startswith(v_m, "__"))):
- output(((" " + v_m) + " : ") + typename)
- elif (cmd == "read"):
- output("Element to read?")
- cmd = input()
- if (dict_in(model["model"], cmd)):
- Element read_elem
- read_elem = model["model"][cmd]
- metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
- output("Name: " + cmd)
- output("Type: " + getName(model["metamodel"], metamodel_element_pn))
- if (is_edge(read_elem)):
- output("Source: " + getName(model, read_edge_src(read_elem)))
- output("Destination: " + getName(model, read_edge_dst(read_elem)))
- if (cast_v2s(read_elem) != "None"):
- output("Value: " + cast_v2s(read_elem))
- output("Defines attributes:")
- attr_list_pn = getInstantiatableAttributes(model, read_elem)
- attr_keys_pn = dict_keys(attr_list_pn)
- while (0 < read_nr_out(attr_keys_pn)):
- attr_key_pn = set_pop(attr_keys_pn)
- output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])))
- output("Attributes:")
- attr_list_pn = getAttributeList(model, cmd)
- attr_keys_pn = dict_keys(attr_list_pn)
- while (0 < read_nr_out(attr_keys_pn)):
- attr_key_pn = set_pop(attr_keys_pn)
- 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)))
- else:
- output("Unknown element; aborting")
- elif (cmd == "verify"):
- output(conformance_scd(model))
- elif (cmd == "types"):
- Element keys_t
- keys_t = dict_keys(model["metamodel"]["model"])
- output("List of types:")
- String v_t
- while (read_nr_out(keys_t) > 0):
- v_t = set_pop(keys_t)
- if (bool_not(string_startswith(v_t, "__"))):
- output(string_join((" " + v_t) + " : ", getName(model["metamodel"]["metamodel"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
- elif (cmd == "retype"):
- output("Element to retype?")
- String elementname
- elementname = input()
- if (dict_in(model["model"], elementname)):
- output("New type")
- typename = input()
- if (dict_in(model["metamodel"]["model"], typename)):
- // OK, do the retyping
- // First try removing the previous type if it exists
- dict_delete(model["type_mapping"], model["model"][elementname])
- // Now add the new type
- dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename])
- output("Retyped!")
- else:
- output("Unknown type; aborting")
- else:
- output("Unknown element; aborting")
- elif (cmd == "switch"):
- bottom = bool_not(bottom)
- Element tmp_model
- tmp_model = model
- model = other_metamodel
- other_metamodel = tmp_model
- if (bottom):
- // The type mapping we are using is probably not complete for our model
- // so we completely recreate it from the model we have.
- output("Switching to conformance bottom mode!")
- generate_bottom_type_mapping(model)
- else:
- // We already switched the models and such, so we are already done!
- output("Switching to linguistic metamodel!")
- elif (bool_and(dict_in(pn_operations(), cmd), bool_not(bottom))):
- // A model-specific operation, so execute that one
- Element specific_op
- specific_op = dict_read(pn_operations(), cmd)
- specific_op(model)
- else:
- output("Unknown command; aborting")
- output("Use command 'help' to get a list of available commands")
- Element function initial_prompt():
- output("Welcome to the Model Management Interface, running live on the Modelverse!")
- output("Use 'help' command for a list of possible commands")
- String command
- Element root
- Element metamodel
- String name
- Element my_model
- String mm_name
- root = create_metamodels()
- while (True):
- output("Please give your command.")
- command = input()
- if (command == "help"):
- output("Currently no model is loaded, so your operations are limited to:")
- output(" new -- Create a new model and save it for future use")
- output(" load -- Load a previously made model")
- output(" rename -- Rename a previously made model")
- output(" delete -- Delete a previously made model")
- output(" list -- Show a list of all stored models")
- output(" help -- Show a list of possible commands")
- elif (command == "new"):
- output("Metamodel to instantiate?")
- mm_name = input()
- if (dict_in(root, mm_name)):
- output("Name of model?")
- name = input()
- if (dict_in(root, name)):
- output("Model exists; aborting")
- else:
- my_model = instantiate_model(root[mm_name])
- dict_add(root, name, my_model)
- petrinet_loaded(my_model)
- else:
- output("Unknown metamodel; aborting")
- elif (command == "load"):
- output("Model to load?")
- name = input()
- if (dict_in(root, name)):
- my_model = root[name]
- petrinet_loaded(my_model)
- else:
- output("Model not found; aborting")
- elif (command == "list"):
- Element keys
- String m_menu_list
- keys = dict_keys(root)
- output("Found models:")
- while (read_nr_out(keys) > 0):
- m_menu_list = set_pop(keys)
- output(((" " + m_menu_list) + " : ") + reverseNameLookup(root, root[m_menu_list]["metamodel"]))
- elif (command == "delete"):
- output("Model to delete?")
- name = input()
- if (dict_in(root, name)):
- dict_delete(root, name)
- output("Deleted!")
- else:
- output("Model not found; aborting")
- elif (command == "rename"):
- output("Old name?")
- String old_name
- old_name = input()
- if (dict_in(root, old_name)):
- output("New name?")
- String new_name
- new_name = input()
- if (dict_in(root, new_name)):
- output("Model exists; aborting")
- else:
- dict_add(root, new_name, root[old_name])
- dict_delete(root, old_name)
- output("Rename complete!")
- else:
- output("Model not found; aborting")
- else:
- output("Command not recognized, use 'help' for a list of possible commands")
- Void function main():
- //initial_prompt()
- log("==========================================================================")
- log("=== creating metamodels")
- Element root
- root = create_metamodels()
- log("=== load instantiation of transformation metamodel")
- Element transf
- transf = petrinet_transformation(root)
- log("=== load test1")
- Element test1
- test1 = test_simple_petrinet1(root)
- log("=== check conformance of transformation")
- log(conformance_scd(transf))
- log("=== run transform")
- tf_transform(transf, test1)
- log("=== printing some places")
- printNode(test1, test1["model"]["p1"])
- printNode(test1, test1["model"]["p2"])
- log("=== done")
- log("==========================================================================")
- return
|