modelling.alc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. Element inheritance
  111. nodes = create_node()
  112. set_add(nodes, model["model"][name])
  113. inheritance = model["metamodel"]["model"]["Inheritance"]
  114. // Initialize empty set
  115. result = create_node()
  116. i = 0
  117. while (list_len(nodes) > 0):
  118. elem = set_pop(nodes)
  119. if (bool_not(set_in(result, reverseKeyLookup(model["model"], elem)))):
  120. create_edge(result, reverseKeyLookup(model["model"], elem))
  121. // Read out all outgoing edges
  122. num_edges = read_nr_out(elem)
  123. j = 0
  124. while (j < num_edges):
  125. edge = read_out(elem, j)
  126. if (element_eq(dict_read_node(model["type_mapping"], edge), inheritance)):
  127. set_add(nodes, read_edge_dst(edge))
  128. j = j + 1
  129. return result!
  130. String function find_attribute_definer(model : Element, elem_name : String, name : String):
  131. Element superclasses
  132. String current
  133. Integer nr_out
  134. Element out
  135. String name_attr
  136. superclasses = get_superclasses(model, elem_name)
  137. while (list_len(superclasses) > 0):
  138. current = set_pop(superclasses)
  139. nr_out = read_nr_out(model["model"][current])
  140. while (nr_out > 0):
  141. nr_out = nr_out - 1
  142. out = read_out(model["model"][current], nr_out)
  143. name_attr = read_attribute(model, reverseKeyLookup(model["model"], out), "name")
  144. if (name_attr == name):
  145. return current!
  146. return ""!
  147. Void function instantiate_attribute(model : Element, element : String, attribute_name : String, value : Element):
  148. // Instantiate an attribute of something that needs to be instantiated
  149. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  150. String attr_type
  151. String attr_name
  152. attr_type = find_attribute_type(model, element, attribute_name)
  153. if (attr_type == ""):
  154. log("Could not find attribute " + cast_v2s(attribute_name))
  155. return!
  156. // Make a copy of the value, as it is likely that this value is reused later on
  157. value = create_value(value)
  158. attr_name = model_add_value(model, (element + ".") + attribute_name, value)
  159. retype(model, attr_name, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  160. instantiate_link(model, attr_type, "", element, attr_name)
  161. return!
  162. Void function instantiate_attribute_ref(model : Element, element : String, attribute_name : String, ref : String):
  163. // Instantiate an attribute of something that needs to be instantiated
  164. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  165. String attr_type
  166. String attr_name
  167. attr_type = find_attribute_type(model, element, attribute_name)
  168. if (attr_type == ""):
  169. log("Could not find attribute " + cast_v2s(attribute_name))
  170. return!
  171. instantiate_link(model, attr_type, "", element, ref)
  172. return!
  173. Void function instantiate_attribute_code(model : Element, element : String, attribute_name : String, code : Element):
  174. String ref
  175. ref = add_AL(model, code)
  176. instantiate_existing_attribute(model, element, attribute_name, ref)
  177. return!
  178. Void function instantiate_existing_attribute(model : Element, element : String, attribute_name : String, attr_ref : String):
  179. // Instantiate an attribute of something that needs to be instantiated
  180. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  181. String attr_type
  182. String attr_name
  183. attr_type = find_attribute_type(model, element, attribute_name)
  184. if (attr_type == ""):
  185. log("Could not find attribute " + cast_v2s(attribute_name))
  186. return!
  187. // Make a copy of the value, as it is likely that this value is reused later on
  188. retype(model, attr_ref, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  189. instantiate_link(model, attr_type, "", element, attr_ref)
  190. return!
  191. String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String):
  192. // Create a typed link between two nodes
  193. String actual_name
  194. actual_name = model_add_edge(model, name, source, destination)
  195. if (type == ""):
  196. // Have to find the type ourselves, as it isn't defined
  197. Element out
  198. Element in
  199. Element options
  200. options = allowedAssociationsBetween(model, source, destination)
  201. if (read_nr_out(options) == 1):
  202. type = set_pop(options)
  203. elif (read_nr_out(options) == 0):
  204. log("ERROR: cannot find possible link between entries")
  205. return ""!
  206. else:
  207. log("ERROR: too many possible links between entries")
  208. return ""!
  209. retype(model, actual_name, type)
  210. return actual_name!
  211. Void function model_delete_element(model : Element, name : String):
  212. // Remove the link
  213. // 1) from the type mapping
  214. dict_delete_node(model["type_mapping"], model["model"][name])
  215. // 2) from the model
  216. delete_element(model["model"][name])
  217. return!
  218. String function model_define_attribute(model : Element, elem : String, name : String, type : String):
  219. // Create the necessary links to make it an attribute
  220. String edge_name
  221. edge_name = instantiate_link(model, "Association", "", elem, type)
  222. instantiate_attribute(model, edge_name, "name", name)
  223. return edge_name!
  224. Element function read_attribute(model : Element, element : String, attribute : String):
  225. Integer i
  226. Integer count
  227. Element edge
  228. Element edge_type
  229. Element elem
  230. Element typing
  231. elem = model["model"][element]
  232. typing = model["type_mapping"]
  233. count = read_nr_out(elem)
  234. i = 0
  235. while (i < count):
  236. edge = read_out(elem, i)
  237. if (dict_in_node(typing, edge)):
  238. edge_type = dict_read_node(typing, edge)
  239. if (element_eq(edge_type, dict_read_edge(read_edge_src(edge_type), attribute))):
  240. return read_edge_dst(edge)!
  241. i = i + 1
  242. return read_root()!
  243. Void function unset_attribute(model : Element, element : String, attribute : String):
  244. // Removes an attribute if it exists
  245. String attr_type
  246. Element attr_links
  247. String attr_link
  248. attr_type = find_attribute_type(model, element, attribute)
  249. attr_links = allOutgoingAssociationInstances(model, element, attr_type)
  250. while (list_len(attr_links) > 0):
  251. attr_link = set_pop(attr_links)
  252. dict_delete_node(model["type_mapping"], read_edge_dst(model["model"][attr_link]))
  253. dict_delete_node(model["type_mapping"], model["model"][attr_link])
  254. dict_delete_node(model["model"], reverseKeyLookup(model["model"], read_edge_dst(model["model"][attr_link])))
  255. delete_element(model["model"][attr_link])
  256. return!
  257. Void function add_AL_links(model : Element, list : Element, element : Element, type: String, linkname : String, expected_type : String):
  258. if (bool_not(dict_in(element, linkname))):
  259. return!
  260. Element link
  261. link = dict_read_edge(element, linkname)
  262. // The link
  263. dict_add(model["model"], "__" + cast_id2s(link), link)
  264. dict_add(model["type_mapping"], link, model["metamodel"]["model"][(type + "_") + linkname])
  265. // The name link
  266. link = read_out(link, 0)
  267. dict_add(model["model"], "__" + cast_id2s(link), link)
  268. dict_add(model["type_mapping"], link, model["metamodel"]["model"]["to_str"])
  269. // The name node
  270. link = read_edge_dst(link)
  271. if (bool_not(set_in_node(model["model"], link))):
  272. dict_add(model["model"], "__" + cast_id2s(link), link)
  273. dict_add(model["type_mapping"], link, model["metamodel"]["model"]["String"])
  274. // Now add the destination to the worker list
  275. Element node
  276. node = create_node()
  277. list_append(node, element[linkname])
  278. list_append(node, expected_type)
  279. set_add(list, node)
  280. return!
  281. String function add_AL(model : Element, element : Element):
  282. Element todo
  283. Element node
  284. Element work_node
  285. Element elem
  286. String type
  287. todo = create_node()
  288. node = create_node()
  289. list_append(node, element)
  290. list_append(node, "funcdef")
  291. set_add(todo, node)
  292. while (0 < dict_len(todo)):
  293. work_node = set_pop(todo)
  294. elem = list_read(work_node, 0)
  295. type = list_read(work_node, 1)
  296. if (bool_not(set_in_node(model["model"], elem))):
  297. // Determine the type if we don't know it
  298. if (type == ""):
  299. if (is_physical_action(elem)):
  300. type = cast_a2s(elem)
  301. else:
  302. type = "Any"
  303. // Add the node itself
  304. dict_add(model["model"], "__" + cast_id2s(elem), elem)
  305. dict_add(model["type_mapping"], elem, model["metamodel"]["model"][type])
  306. // Now add its edges
  307. if (type == "if"):
  308. add_AL_links(model, todo, elem, type, "cond", "")
  309. add_AL_links(model, todo, elem, type, "then", "")
  310. add_AL_links(model, todo, elem, type, "else", "")
  311. add_AL_links(model, todo, elem, type, "next", "")
  312. elif (type == "while"):
  313. add_AL_links(model, todo, elem, type, "cond", "")
  314. add_AL_links(model, todo, elem, type, "body", "")
  315. add_AL_links(model, todo, elem, type, "next", "")
  316. elif (type == "assign"):
  317. add_AL_links(model, todo, elem, type, "var", "")
  318. add_AL_links(model, todo, elem, type, "value", "")
  319. add_AL_links(model, todo, elem, type, "next", "")
  320. elif (type == "break"):
  321. add_AL_links(model, todo, elem, type, "while", "while")
  322. elif (type == "continue"):
  323. add_AL_links(model, todo, elem, type, "while", "while")
  324. elif (type == "return"):
  325. add_AL_links(model, todo, elem, type, "value", "")
  326. elif (type == "resolve"):
  327. add_AL_links(model, todo, elem, type, "var", "")
  328. elif (type == "access"):
  329. add_AL_links(model, todo, elem, type, "var", "")
  330. elif (type == "constant"):
  331. add_AL_links(model, todo, elem, type, "node", "")
  332. elif (type == "output"):
  333. add_AL_links(model, todo, elem, type, "node", "")
  334. add_AL_links(model, todo, elem, type, "next", "")
  335. elif (type == "global"):
  336. add_AL_links(model, todo, elem, type, "var", "String")
  337. add_AL_links(model, todo, elem, type, "next", "")
  338. elif (type == "param"):
  339. add_AL_links(model, todo, elem, type, "name", "String")
  340. add_AL_links(model, todo, elem, type, "value", "")
  341. add_AL_links(model, todo, elem, type, "next_param", "param")
  342. elif (type == "funcdef"):
  343. add_AL_links(model, todo, elem, type, "body", "")
  344. add_AL_links(model, todo, elem, type, "next", "")
  345. elif (type == "call"):
  346. add_AL_links(model, todo, elem, type, "func", "")
  347. add_AL_links(model, todo, elem, type, "params", "param")
  348. add_AL_links(model, todo, elem, type, "last_param", "param")
  349. add_AL_links(model, todo, elem, type, "next", "")
  350. return reverseKeyLookup(model["model"], element)!
  351. Void function add_constraint(model : Element, element : String, constraint : Action):
  352. // Add local constraints to an element
  353. Element attr_type
  354. String link_name
  355. String constraint_name
  356. constraint_name = add_AL(model, constraint)
  357. attr_type = find_attribute_type(model, element, "constraint")
  358. instantiate_link(model, attr_type, "", element, constraint_name)
  359. return!
  360. Void function construct_model():
  361. String command
  362. while (True):
  363. command = input()
  364. if (command == "instantiate_bottom"):
  365. Element m
  366. m = instantiate_bottom()
  367. dict_add(global_models, input(), m)
  368. elif (command == "add_node"):
  369. model_add_node(global_models[input()], input())
  370. elif (command == "add_value"):
  371. model_add_value(global_models[input()], input(), input())
  372. elif (command == "add_edge"):
  373. model_add_edge(global_models[input()], input(), input(), input())
  374. elif (command == "exit"):
  375. return!
  376. elif (command == "retype_model"):
  377. retype_model(global_models[input()], global_models[input()])
  378. elif (command == "retype"):
  379. retype(global_models[input()], input(), input())
  380. elif (command == "instantiate_model"):
  381. Element m
  382. m = instantiate_model(global_models[input()])
  383. dict_add(global_models, input(), m)
  384. elif (command == "instantiate_node"):
  385. instantiate_node(global_models[input()], input(), input())
  386. elif (command == "instantiate_attribute"):
  387. instantiate_attribute(global_models[input()], input(), input(), input())
  388. elif (command == "instantiate_attribute_ref"):
  389. instantiate_attribute_ref(global_models[input()], input(), input(), input())
  390. elif (command == "instantiate_attribute_code"):
  391. instantiate_attribute_code(global_models[input()], input(), input(), construct_function())
  392. elif (command == "instantiate_link"):
  393. instantiate_link(global_models[input()], input(), input(), input(), input())
  394. elif (command == "add_constraint"):
  395. add_constraint(global_models[input()], input(), construct_function())
  396. elif (command == "initialize_SCD"):
  397. initialize_SCD(input())
  398. elif (command == "initialize_bottom"):
  399. initialize_bottom(input())
  400. elif (command == "export_node"):
  401. String local_name
  402. String location
  403. local_name = input()
  404. location = input()
  405. export_node(location, global_models[local_name])
  406. elif (command == "import_node"):
  407. Element m
  408. command = input()
  409. m = import_node(command)
  410. if (element_eq(m, read_root())):
  411. log("Error: import not found for " + command)
  412. else:
  413. dict_add(global_models, input(), m)
  414. else:
  415. log("Modelling error: did not understand command " + command)