pn_interface.alc 35 KB

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