modelling.alc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. include "primitives.alh"
  2. include "io.alh"
  3. include "object_operations.alh"
  4. include "constructors.alh"
  5. include "metamodels.alh"
  6. include "library.alh"
  7. Element global_models = ?
  8. String function instantiated_name(element : Element, original : String):
  9. if (original == ""):
  10. return "__" + cast_id2s(element)!
  11. else:
  12. return original!
  13. Element function instantiate_bottom():
  14. // Just create a new node that serves as the basis for everything
  15. // We don't know anything about the model yet, so just create an empty one
  16. Element new_model
  17. // The actual root node of the model
  18. new_model = create_node()
  19. // Add an empty model and empty type mapping
  20. dict_add(new_model, "model", create_node())
  21. dict_add(new_model, "type_mapping", create_node())
  22. // Return the created model
  23. return new_model!
  24. String function model_add_node(model : Element, name : String):
  25. // Adds a new node to the specified model with the desired name
  26. // This is a bottom operation, as it doesn't take any type
  27. Element new_node
  28. String actual_name
  29. new_node = create_node()
  30. actual_name = instantiated_name(new_node, name)
  31. dict_add(model["model"], actual_name, new_node)
  32. return actual_name!
  33. String function model_add_value(model : Element, name : String, value : Element):
  34. // Similar to model_add_node, but add a value as well
  35. String actual_name
  36. actual_name = instantiated_name(value, name)
  37. dict_add(model["model"], actual_name, value)
  38. return actual_name!
  39. String function model_add_edge(model : Element, name : String, source : String, destination : String):
  40. // Add an edge between the source and destination nodes
  41. // Nodes are specified using their string representation previously defined
  42. Element new_edge
  43. String actual_name
  44. if (bool_not(dict_in(model["model"], source))):
  45. log("In link " + name)
  46. log("ERROR: source of link unknown: " + source)
  47. log("Destination: " + destination)
  48. return ""!
  49. if (bool_not(dict_in(model["model"], destination))):
  50. log("In link " + name)
  51. log("ERROR: destination of link unknown: " + destination)
  52. log("Source: " + source)
  53. return ""!
  54. new_edge = create_edge(model["model"][source], model["model"][destination])
  55. actual_name = instantiated_name(new_edge, name)
  56. dict_add(model["model"], actual_name, new_edge)
  57. return actual_name!
  58. Void function retype_model(model : Element, metamodel : Element):
  59. // Remove the type mapping and add a new one for the specified metamodel
  60. dict_delete(model, "type_mapping")
  61. dict_add(model, "type_mapping", create_node())
  62. dict_add(model, "metamodel", metamodel)
  63. return!
  64. Void function retype(model : Element, element : String, type : String):
  65. // Retype a model, deleting any previous type the element had
  66. // The type string is evaluated in the metamodel previously specified
  67. if (dict_in_node(model["type_mapping"], model["model"][element])):
  68. dict_delete_node(model["type_mapping"], model["model"][element])
  69. dict_add(model["type_mapping"], model["model"][element], model["metamodel"]["model"][type])
  70. return!
  71. Element function instantiate_model(metamodel : Element):
  72. // Instantiate a model
  73. // Basically create an untyped model and retype it
  74. Element model
  75. model = instantiate_bottom()
  76. retype_model(model, metamodel)
  77. return model!
  78. String function instantiate_node(model : Element, type_name : String, instance_name : String):
  79. // Create a node typed by a node from the metamodel
  80. // Basically create a node and type it immediately
  81. String actual_name
  82. actual_name = model_add_node(model, instance_name)
  83. retype(model, actual_name, type_name)
  84. return actual_name!
  85. String function instantiate_value(model : Element, type_name : String, instance_name : String, value : Element):
  86. // Create a node typed by a node from the metamodel
  87. // Basically create a node and type it immediately
  88. String actual_name
  89. actual_name = model_add_value(model, instance_name, value)
  90. retype(model, actual_name, type_name)
  91. return actual_name!
  92. String function find_attribute_type(model : Element, elem : String, name : String):
  93. String mm_elem
  94. String direct_type
  95. direct_type = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][elem]))
  96. mm_elem = find_attribute_definer(model["metamodel"], direct_type, name)
  97. if (value_eq(mm_elem, "")):
  98. // Couldn't find element, so is not allowed!
  99. return ""!
  100. else:
  101. return reverseKeyLookup(model["metamodel"]["model"], dict_read_edge(model["metamodel"]["model"][mm_elem], name))!
  102. Element function get_superclasses(model : Element, name : String):
  103. Element result
  104. Integer i
  105. Integer j
  106. Integer num_edges
  107. Element edge
  108. Element elem
  109. Element nodes
  110. nodes = create_node()
  111. set_add(nodes, model["model"][name])
  112. // Initialize empty set
  113. result = create_node()
  114. i = 0
  115. while (0 < list_len(nodes)):
  116. elem = set_pop(nodes)
  117. create_edge(result, reverseKeyLookup(model["model"], elem))
  118. // Read out all outgoing edges
  119. num_edges = read_nr_out(elem)
  120. j = 0
  121. while (j < num_edges):
  122. edge = read_out(elem, j)
  123. if (element_eq(dict_read_node(model["type_mapping"], edge), model["inheritance"])):
  124. set_add(nodes, read_edge_dst(edge))
  125. j = j + 1
  126. return result!
  127. String function find_attribute_definer(model : Element, elem_name : String, name : String):
  128. Element superclasses
  129. Element current
  130. superclasses = get_superclasses(model, elem_name)
  131. while (list_len(superclasses) > 0):
  132. current = set_pop(superclasses)
  133. if (dict_in(model["model"][current], name)):
  134. return current!
  135. return ""!
  136. Void function instantiate_attribute(model : Element, element : String, attribute_name : String, value : Element):
  137. // Instantiate an attribute of something that needs to be instantiated
  138. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  139. String attr_type
  140. String attr_name
  141. attr_type = find_attribute_type(model, element, attribute_name)
  142. if (attr_type == ""):
  143. log("Could not find attribute " + cast_v2s(attribute_name))
  144. return!
  145. // Make a copy of the value, as it is likely that this value is reused later on
  146. value = create_value(value)
  147. attr_name = model_add_value(model, (element + ".") + attribute_name, value)
  148. retype(model, attr_name, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  149. instantiate_link(model, attr_type, "", element, attr_name)
  150. return!
  151. Void function instantiate_attribute_ref(model : Element, element : String, attribute_name : String, ref : String):
  152. // Instantiate an attribute of something that needs to be instantiated
  153. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  154. String attr_type
  155. String attr_name
  156. attr_type = find_attribute_type(model, element, attribute_name)
  157. if (attr_type == ""):
  158. log("Could not find attribute " + cast_v2s(attribute_name))
  159. return!
  160. instantiate_link(model, attr_type, "", element, ref)
  161. return!
  162. Void function instantiate_attribute_code(model : Element, element : String, attribute_name : String, code : Element):
  163. String ref
  164. ref = add_AL(model, code)
  165. instantiate_existing_attribute(model, element, attribute_name, ref)
  166. return!
  167. Void function instantiate_existing_attribute(model : Element, element : String, attribute_name : String, attr_ref : String):
  168. // Instantiate an attribute of something that needs to be instantiated
  169. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  170. String attr_type
  171. String attr_name
  172. attr_type = find_attribute_type(model, element, attribute_name)
  173. if (attr_type == ""):
  174. log("Could not find attribute " + cast_v2s(attribute_name))
  175. return!
  176. // Make a copy of the value, as it is likely that this value is reused later on
  177. retype(model, attr_ref, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  178. instantiate_link(model, attr_type, "", element, attr_ref)
  179. return!
  180. String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String):
  181. // Create a typed link between two nodes
  182. String actual_name
  183. actual_name = model_add_edge(model, name, source, destination)
  184. if (type == ""):
  185. // Have to find the type ourselves, as it isn't defined
  186. Element out
  187. Element in
  188. Element options
  189. out = selectPossibleOutgoing(model, source, create_node())
  190. in = selectPossibleIncoming(model, destination, create_node())
  191. options = set_overlap(out, in)
  192. if (read_nr_out(options) == 1):
  193. type = set_pop(options)
  194. elif (read_nr_out(options) == 0):
  195. log("ERROR: cannot find possible link between entries")
  196. return ""!
  197. else:
  198. log("ERROR: too many possible links between entries")
  199. return ""!
  200. retype(model, actual_name, type)
  201. return actual_name!
  202. Void function define_inheritance(model : Element, inheritance_name : String):
  203. // Set the inheritance link to the one defined in our own metamodel, given by the specified name
  204. dict_add(model, "inheritance", model["metamodel"]["model"][inheritance_name])
  205. return!
  206. Void function model_delete_element(model : Element, name : String):
  207. // Remove the link
  208. // 1) from the type mapping
  209. dict_delete_node(model["type_mapping"], model["model"][name])
  210. // 2) from the model
  211. delete_element(model["model"][name])
  212. return!
  213. String function model_define_attribute(model : Element, elem : String, name : String, type : String):
  214. // Create the necessary links to make it an attribute
  215. String edge_name
  216. edge_name = instantiate_link(model, "Association", "", elem, type)
  217. instantiate_attribute(model, edge_name, "name", name)
  218. return edge_name!
  219. Element function read_attribute(model : Element, element : String, attribute : String):
  220. Integer i
  221. Integer count
  222. Element edge
  223. Element edge_type
  224. Element elem
  225. Element typing
  226. elem = model["model"][element]
  227. typing = model["type_mapping"]
  228. count = read_nr_out(elem)
  229. i = 0
  230. while (i < count):
  231. edge = read_out(elem, i)
  232. if (dict_in_node(typing, edge)):
  233. edge_type = dict_read_node(typing, edge)
  234. if (element_eq(edge_type, dict_read_edge(read_edge_src(edge_type), attribute))):
  235. return read_edge_dst(edge)!
  236. i = i + 1
  237. return read_root()!
  238. Void function unset_attribute(model : Element, element : String, attribute : String):
  239. // Removes an attribute if it exists
  240. String attr_type
  241. Element attr_links
  242. String attr_link
  243. attr_type = find_attribute_type(model, element, attribute)
  244. attr_links = allOutgoingAssociationInstances(model, element, attr_type)
  245. while (list_len(attr_links) > 0):
  246. attr_link = set_pop(attr_links)
  247. dict_delete_node(model["type_mapping"], read_edge_dst(model["model"][attr_link]))
  248. dict_delete_node(model["type_mapping"], model["model"][attr_link])
  249. dict_delete_node(model["model"], reverseKeyLookup(model["model"], read_edge_dst(model["model"][attr_link])))
  250. delete_element(model["model"][attr_link])
  251. return!
  252. Void function add_AL_links(model : Element, list : Element, element : Element, type: String, linkname : String, expected_type : String):
  253. if (bool_not(dict_in(element, linkname))):
  254. return!
  255. Element link
  256. link = dict_read_edge(element, linkname)
  257. // The link
  258. dict_add(model["model"], "__" + cast_id2s(link), link)
  259. dict_add(model["type_mapping"], link, model["metamodel"]["model"][(type + "_") + linkname])
  260. // The name link
  261. link = read_out(link, 0)
  262. dict_add(model["model"], "__" + cast_id2s(link), link)
  263. dict_add(model["type_mapping"], link, model["metamodel"]["model"]["to_str"])
  264. // The name node
  265. link = read_edge_dst(link)
  266. if (bool_not(set_in_node(model["model"], link))):
  267. dict_add(model["model"], "__" + cast_id2s(link), link)
  268. dict_add(model["type_mapping"], link, model["metamodel"]["model"]["String"])
  269. // Now add the destination to the worker list
  270. Element node
  271. node = create_node()
  272. list_append(node, element[linkname])
  273. list_append(node, expected_type)
  274. set_add(list, node)
  275. return!
  276. String function add_AL(model : Element, element : Element):
  277. Element todo
  278. Element node
  279. Element work_node
  280. Element elem
  281. String type
  282. todo = create_node()
  283. node = create_node()
  284. list_append(node, element)
  285. list_append(node, "funcdef")
  286. set_add(todo, node)
  287. while (0 < dict_len(todo)):
  288. work_node = set_pop(todo)
  289. elem = list_read(work_node, 0)
  290. type = list_read(work_node, 1)
  291. if (bool_not(set_in_node(model["model"], elem))):
  292. // Determine the type if we don't know it
  293. if (type == ""):
  294. if (is_physical_action(elem)):
  295. type = cast_a2s(elem)
  296. else:
  297. type = "Any"
  298. // Add the node itself
  299. dict_add(model["model"], "__" + cast_id2s(elem), elem)
  300. dict_add(model["type_mapping"], elem, model["metamodel"]["model"][type])
  301. // Now add its edges
  302. if (type == "if"):
  303. add_AL_links(model, todo, elem, type, "cond", "")
  304. add_AL_links(model, todo, elem, type, "then", "")
  305. add_AL_links(model, todo, elem, type, "else", "")
  306. add_AL_links(model, todo, elem, type, "next", "")
  307. elif (type == "while"):
  308. add_AL_links(model, todo, elem, type, "cond", "")
  309. add_AL_links(model, todo, elem, type, "body", "")
  310. add_AL_links(model, todo, elem, type, "next", "")
  311. elif (type == "assign"):
  312. add_AL_links(model, todo, elem, type, "var", "")
  313. add_AL_links(model, todo, elem, type, "value", "")
  314. add_AL_links(model, todo, elem, type, "next", "")
  315. elif (type == "break"):
  316. add_AL_links(model, todo, elem, type, "while", "while")
  317. elif (type == "continue"):
  318. add_AL_links(model, todo, elem, type, "while", "while")
  319. elif (type == "return"):
  320. add_AL_links(model, todo, elem, type, "value", "")
  321. elif (type == "resolve"):
  322. add_AL_links(model, todo, elem, type, "var", "")
  323. elif (type == "access"):
  324. add_AL_links(model, todo, elem, type, "var", "")
  325. elif (type == "constant"):
  326. add_AL_links(model, todo, elem, type, "node", "")
  327. elif (type == "output"):
  328. add_AL_links(model, todo, elem, type, "node", "")
  329. add_AL_links(model, todo, elem, type, "next", "")
  330. elif (type == "global"):
  331. add_AL_links(model, todo, elem, type, "var", "String")
  332. add_AL_links(model, todo, elem, type, "next", "")
  333. elif (type == "param"):
  334. add_AL_links(model, todo, elem, type, "name", "String")
  335. add_AL_links(model, todo, elem, type, "value", "")
  336. add_AL_links(model, todo, elem, type, "next_param", "param")
  337. elif (type == "funcdef"):
  338. add_AL_links(model, todo, elem, type, "body", "")
  339. add_AL_links(model, todo, elem, type, "next", "")
  340. elif (type == "call"):
  341. add_AL_links(model, todo, elem, type, "func", "")
  342. add_AL_links(model, todo, elem, type, "params", "param")
  343. add_AL_links(model, todo, elem, type, "last_param", "param")
  344. add_AL_links(model, todo, elem, type, "next", "")
  345. return reverseKeyLookup(model["model"], element)!
  346. Void function add_constraint(model : Element, element : String, constraint : Action):
  347. // Add local constraints to an element
  348. Element attr_type
  349. String link_name
  350. String constraint_name
  351. constraint_name = add_AL(model, constraint)
  352. attr_type = find_attribute_type(model, element, "constraint")
  353. instantiate_link(model, attr_type, "", element, constraint_name)
  354. return!
  355. Void function construct_model():
  356. String command
  357. while (True):
  358. command = input()
  359. if (command == "instantiate_bottom"):
  360. Element m
  361. m = instantiate_bottom()
  362. dict_add(global_models, input(), m)
  363. elif (command == "add_node"):
  364. model_add_node(global_models[input()], input())
  365. elif (command == "add_value"):
  366. model_add_value(global_models[input()], input(), input())
  367. elif (command == "add_edge"):
  368. model_add_edge(global_models[input()], input(), input(), input())
  369. elif (command == "exit"):
  370. return!
  371. elif (command == "retype_model"):
  372. retype_model(global_models[input()], global_models[input()])
  373. elif (command == "retype"):
  374. retype(global_models[input()], input(), input())
  375. elif (command == "instantiate_model"):
  376. Element m
  377. m = instantiate_model(global_models[input()])
  378. dict_add(global_models, input(), m)
  379. elif (command == "instantiate_node"):
  380. instantiate_node(global_models[input()], input(), input())
  381. elif (command == "instantiate_attribute"):
  382. instantiate_attribute(global_models[input()], input(), input(), input())
  383. elif (command == "instantiate_attribute_ref"):
  384. instantiate_attribute_ref(global_models[input()], input(), input(), input())
  385. elif (command == "instantiate_link"):
  386. instantiate_link(global_models[input()], input(), input(), input(), input())
  387. elif (command == "define_inheritance"):
  388. define_inheritance(global_models[input()], input())
  389. elif (command == "add_constraint"):
  390. add_constraint(global_models[input()], input(), construct_function())
  391. elif (command == "initialize_SCD"):
  392. initialize_SCD(input())
  393. elif (command == "initialize_bottom"):
  394. initialize_bottom(input())
  395. elif (command == "export_node"):
  396. String local_name
  397. String location
  398. local_name = input()
  399. location = input()
  400. log("Exporting to location " + location)
  401. log(" name: " + local_name)
  402. log("Export element: " + cast_e2s(global_models[local_name]))
  403. export_node(location, global_models[local_name])
  404. elif (command == "import_node"):
  405. Element m
  406. command = input()
  407. m = import_node(command)
  408. if (element_eq(m, read_root())):
  409. log("Error: import not found for " + command)
  410. else:
  411. dict_add(global_models, input(), m)
  412. else:
  413. log("Modelling error: did not understand command " + command)
  414. Element function model_fuse(models : Element):
  415. Element new_model
  416. Element tagged_model
  417. String model_name
  418. Element model
  419. Element keys
  420. String key
  421. Element selected_MM
  422. String type
  423. // Read out some data first
  424. tagged_model = set_pop(models)
  425. set_add(models, tagged_model)
  426. model = list_read(tagged_model, 1)
  427. selected_MM = model["metamodel"]
  428. new_model = instantiate_model(selected_MM)
  429. models = set_copy(models)
  430. while (read_nr_out(models)):
  431. tagged_model = set_pop(models)
  432. model_name = list_read(tagged_model, 0)
  433. model = list_read(tagged_model, 1)
  434. // Add all elements from 'model', but prepend it with the 'model_name'
  435. keys = set_to_list(dict_keys(model["model"]))
  436. while (read_nr_out(keys) > 0):
  437. key = list_pop(keys, 0)
  438. type = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][key]))
  439. if (is_edge(model["model"][key])):
  440. String src
  441. String dst
  442. src = (model_name + "/") + reverseKeyLookup(model["model"], read_edge_src(model["model"][key]))
  443. dst = (model_name + "/") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][key]))
  444. if (bool_and(dict_in(new_model["model"], src), dict_in(new_model["model"], dst))):
  445. instantiate_link(new_model, type, (model_name + "/") + key, (model_name + "/") + src, (model_name + "/") + dst)
  446. else:
  447. list_append(keys, key)
  448. elif (has_value(model["model"][key])):
  449. instantiate_value(new_model, type, (model_name + "/") + key, model["model"][key])
  450. else:
  451. instantiate_node(new_model, type, (model_name + "/") + key)
  452. return new_model!