modelling.alc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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(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, instance_name, type_name)
  84. return actual_name
  85. String function find_attribute_type(model : Element, elem : String, name : String):
  86. String mm_elem
  87. String direct_type
  88. direct_type = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][elem]))
  89. mm_elem = find_attribute_definer(model["metamodel"], direct_type, name)
  90. if (value_eq(mm_elem, "")):
  91. // Couldn't find element, so is not allowed!
  92. return ""
  93. else:
  94. return reverseKeyLookup(model["metamodel"]["model"], dict_read_edge(model["metamodel"]["model"][mm_elem], name))
  95. Element function get_superclasses(model : Element, name : String):
  96. Element result
  97. Integer i
  98. Integer j
  99. Integer num_edges
  100. Element edge
  101. Element elem
  102. Element nodes
  103. nodes = create_node()
  104. set_add(nodes, model["model"][name])
  105. // Initialize empty set
  106. result = create_node()
  107. i = 0
  108. while (0 < list_len(nodes)):
  109. elem = set_pop(nodes)
  110. create_edge(result, reverseKeyLookup(model["model"], elem))
  111. // Read out all outgoing edges
  112. num_edges = read_nr_out(elem)
  113. j = 0
  114. while (j < num_edges):
  115. edge = read_out(elem, j)
  116. if (element_eq(dict_read_node(model["type_mapping"], edge), model["inheritance"])):
  117. set_add(nodes, read_edge_dst(edge))
  118. j = j + 1
  119. return result
  120. String function find_attribute_definer(model : Element, elem_name : String, name : String):
  121. Element superclasses
  122. Element current
  123. superclasses = get_superclasses(model, elem_name)
  124. while (list_len(superclasses) > 0):
  125. current = set_pop(superclasses)
  126. if (dict_in(model["model"][current], name)):
  127. return current
  128. return ""
  129. Void function instantiate_attribute(model : Element, element : String, attribute_name : String, value : Element):
  130. // Instantiate an attribute of something that needs to be instantiated
  131. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  132. String attr_type
  133. String attr_name
  134. attr_type = find_attribute_type(model, element, attribute_name)
  135. if (attr_type == ""):
  136. log("Could not find attribute!")
  137. return
  138. attr_name = model_add_value(model, "", value)
  139. retype(model, attr_name, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  140. instantiate_link(model, attr_type, "", element, attr_name)
  141. return
  142. String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String):
  143. // Create a typed link between two nodes
  144. String actual_name
  145. actual_name = model_add_edge(model, name, source, destination)
  146. retype(model, actual_name, type)
  147. return actual_name
  148. Void function define_inheritance(model : Element, inheritance_name : String):
  149. // Set the inheritance link to the one defined in our own metamodel, given by the specified name
  150. dict_add(model, "inheritance", model["metamodel"]["model"][inheritance_name])
  151. return
  152. Void function model_delete_element(model : Element, name : String):
  153. // Remove the link
  154. // 1) from the type mapping
  155. dict_delete(model["type_mapping"], model["model"][name])
  156. // 2) from the model
  157. delete_element(model["model"][name])
  158. return
  159. Element function read_attribute(model : Element, element : String, attribute : String):
  160. Integer i
  161. Integer count
  162. Element edge
  163. Element edge_type
  164. Element elem
  165. Element typing
  166. elem = model["model"][element]
  167. typing = model["type_mapping"]
  168. count = read_nr_out(elem)
  169. i = 0
  170. while (i < count):
  171. edge = read_out(elem, i)
  172. if (dict_in_node(typing, edge)):
  173. edge_type = dict_read_node(typing, edge)
  174. if (element_eq(edge_type, dict_read_edge(read_edge_src(edge_type), attribute))):
  175. return read_edge_dst(edge)
  176. i = i + 1
  177. return read_root()
  178. Void function unset_attribute(model : Element, element : String, attribute : String):
  179. // Removes an attribute if it exists
  180. String attr_type
  181. Element attr_links
  182. String attr_link
  183. attr_type = find_attribute_type(model, element, attribute)
  184. attr_links = allOutgoingAssociationInstances(model, element, attr_type)
  185. while (list_len(attr_links) > 0):
  186. attr_link = set_pop(attr_links)
  187. dict_delete(model["type_mapping"], read_edge_dst(model["model"][attr_link]))
  188. dict_delete(model["type_mapping"], model["model"][attr_link])
  189. delete_element(read_edge_dst(model["model"][attr_link]))
  190. return
  191. Void function add_AL_links(model : Element, list : Element, element : Element, type: String, linkname : String, expected_type : String):
  192. if (bool_not(dict_in(element, linkname))):
  193. return
  194. Element link
  195. link = dict_read_edge(element, linkname)
  196. // The link
  197. dict_add(model["model"], "__" + cast_id2s(link), link)
  198. dict_add(model["type_mapping"], link, model["metamodel"]["model"][(type + "_") + linkname])
  199. // The name link
  200. link = read_out(link, 0)
  201. dict_add(model["model"], "__" + cast_id2s(link), link)
  202. dict_add(model["type_mapping"], link, model["metamodel"]["model"]["to_str"])
  203. // The name node
  204. link = read_edge_dst(link)
  205. if (bool_not(set_in_node(model["model"], link))):
  206. dict_add(model["model"], "__" + cast_id2s(link), link)
  207. dict_add(model["type_mapping"], link, model["metamodel"]["model"]["String"])
  208. // Now add the destination to the worker list
  209. Element node
  210. node = create_node()
  211. list_append(node, element[linkname])
  212. list_append(node, expected_type)
  213. set_add(list, node)
  214. return
  215. String function add_AL(model : Element, element : Element):
  216. log("Adding constraint: " + cast_e2s(element))
  217. Element todo
  218. Element node
  219. Element work_node
  220. Element elem
  221. String type
  222. todo = create_node()
  223. node = create_node()
  224. list_append(node, element)
  225. list_append(node, "funcdef")
  226. set_add(todo, node)
  227. while (0 < dict_len(todo)):
  228. work_node = set_pop(todo)
  229. elem = list_read(work_node, 0)
  230. type = list_read(work_node, 1)
  231. if (bool_not(set_in_node(model["model"], elem))):
  232. log("Adding")
  233. // Determine the type if we don't know it
  234. if (type == ""):
  235. if (is_physical_action(elem)):
  236. type = cast_a2s(elem)
  237. else:
  238. type = "Any"
  239. // Add the node itself
  240. dict_add(model["model"], "__" + cast_id2s(elem), elem)
  241. dict_add(model["type_mapping"], elem, model["metamodel"]["model"][type])
  242. // Now add its edges
  243. if (type == "if"):
  244. add_AL_links(model, todo, elem, type, "cond", "")
  245. add_AL_links(model, todo, elem, type, "then", "")
  246. add_AL_links(model, todo, elem, type, "else", "")
  247. add_AL_links(model, todo, elem, type, "next", "")
  248. elif (type == "while"):
  249. add_AL_links(model, todo, elem, type, "cond", "")
  250. add_AL_links(model, todo, elem, type, "body", "")
  251. add_AL_links(model, todo, elem, type, "next", "")
  252. elif (type == "assign"):
  253. add_AL_links(model, todo, elem, type, "var", "")
  254. add_AL_links(model, todo, elem, type, "value", "")
  255. add_AL_links(model, todo, elem, type, "next", "")
  256. elif (type == "break"):
  257. add_AL_links(model, todo, elem, type, "while", "while")
  258. elif (type == "continue"):
  259. add_AL_links(model, todo, elem, type, "while", "while")
  260. elif (type == "return"):
  261. add_AL_links(model, todo, elem, type, "value", "")
  262. elif (type == "resolve"):
  263. add_AL_links(model, todo, elem, type, "var", "")
  264. elif (type == "access"):
  265. add_AL_links(model, todo, elem, type, "var", "")
  266. elif (type == "constant"):
  267. add_AL_links(model, todo, elem, type, "node", "")
  268. elif (type == "output"):
  269. add_AL_links(model, todo, elem, type, "node", "")
  270. add_AL_links(model, todo, elem, type, "next", "")
  271. elif (type == "global"):
  272. add_AL_links(model, todo, elem, type, "var", "String")
  273. add_AL_links(model, todo, elem, type, "next", "")
  274. elif (type == "param"):
  275. add_AL_links(model, todo, elem, type, "name", "String")
  276. add_AL_links(model, todo, elem, type, "value", "")
  277. add_AL_links(model, todo, elem, type, "next_param", "param")
  278. elif (type == "funcdef"):
  279. add_AL_links(model, todo, elem, type, "body", "")
  280. add_AL_links(model, todo, elem, type, "next", "")
  281. elif (type == "call"):
  282. add_AL_links(model, todo, elem, type, "func", "")
  283. add_AL_links(model, todo, elem, type, "params", "param")
  284. add_AL_links(model, todo, elem, type, "last_param", "param")
  285. add_AL_links(model, todo, elem, type, "next", "")
  286. else:
  287. log("Already in there")
  288. return reverseKeyLookup(model["model"], element)
  289. Void function add_constraint(model : Element, element : String, constraint : Action):
  290. // Add local constraints to an element
  291. Element attr_type
  292. String link_name
  293. String constraint_name
  294. constraint_name = add_AL(model, constraint)
  295. attr_type = find_attribute_type(model, element, "constraint")
  296. instantiate_link(model, attr_type, "", element, constraint_name)
  297. return
  298. Void function construct_model():
  299. String command
  300. while (True):
  301. command = input()
  302. if (command == "instantiate_bottom"):
  303. Element m
  304. m = instantiate_bottom()
  305. dict_add(global_models, input(), m)
  306. elif (command == "add_node"):
  307. model_add_node(global_models[input()], input())
  308. elif (command == "add_value"):
  309. model_add_value(global_models[input()], input(), input())
  310. elif (command == "add_edge"):
  311. model_add_edge(global_models[input()], input(), input(), input())
  312. elif (command == "exit"):
  313. return
  314. elif (command == "retype_model"):
  315. retype_model(global_models[input()], global_models[input()])
  316. elif (command == "retype"):
  317. retype(global_models[input()], input(), input())
  318. elif (command == "instantiate_model"):
  319. Element m
  320. m = instantiate_model(global_models[input()])
  321. dict_add(global_models, input(), m)
  322. elif (command == "instantiate_node"):
  323. instantiate_node(global_models[input()], input(), input())
  324. elif (command == "instantiate_attribute"):
  325. instantiate_attribute(global_models[input()], input(), input(), input())
  326. elif (command == "instantiate_link"):
  327. instantiate_link(global_models[input()], input(), input(), input(), input())
  328. elif (command == "define_inheritance"):
  329. define_inheritance(global_models[input()], input())
  330. elif (command == "add_constraint"):
  331. add_constraint(global_models[input()], input(), construct_function())
  332. elif (command == "initialize_SCD"):
  333. initialize_SCD(input())
  334. elif (command == "initialize_bottom"):
  335. initialize_bottom(input())
  336. elif (command == "export_node"):
  337. String local_name
  338. String location
  339. local_name = input()
  340. location = input()
  341. export_node(location, global_models[local_name])
  342. elif (command == "import_node"):
  343. Element m
  344. m = import_node(input())
  345. dict_add(global_models, input(), m)
  346. else:
  347. log("Modelling error: did not understand command " + command)