pn_interface.alc 35 KB

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