modelling.alc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. include "primitives.alh"
  2. include "io.alh"
  3. include "object_operations.alh"
  4. String function instantiated_name(element : Element, original : String):
  5. if (original == ""):
  6. return "__" + cast_id2s(element)
  7. else:
  8. return original
  9. Element function instantiate_bottom():
  10. // Just create a new node that serves as the basis for everything
  11. // We don't know anything about the model yet, so just create an empty one
  12. Element new_model
  13. // The actual root node of the model
  14. new_model = create_node()
  15. // Add an empty model and empty type mapping
  16. dict_add(new_model, "model", create_node())
  17. dict_add(new_model, "type_mapping", create_node())
  18. // Return the created model
  19. return new_model
  20. String function model_add_node(model : Element, name : String):
  21. // Adds a new node to the specified model with the desired name
  22. // This is a bottom operation, as it doesn't take any type
  23. Element new_node
  24. String actual_name
  25. new_node = create_node()
  26. actual_name = instantiated_name(new_node, name)
  27. dict_add(model["model"], actual_name, new_node)
  28. return actual_name
  29. String function model_add_value(model : Element, name : String, value : Element):
  30. // Similar to model_add_node, but add a value as well
  31. String actual_name
  32. actual_name = instantiated_name(value, name)
  33. dict_add(model["model"], actual_name, value)
  34. return actual_name
  35. String function model_add_edge(model : Element, name : String, source : String, destination : String):
  36. // Add an edge between the source and destination nodes
  37. // Nodes are specified using their string representation previously defined
  38. Element new_edge
  39. String actual_name
  40. if (bool_not(dict_in(model["model"], source))):
  41. log("In link " + name)
  42. log("ERROR: source of link unknown: " + source)
  43. log("Destination: " + destination)
  44. return ""
  45. if (bool_not(dict_in(model["model"], destination))):
  46. log("In link " + name)
  47. log("ERROR: destination of link unknown: " + destination)
  48. log("Source: " + source)
  49. return ""
  50. new_edge = create_edge(model["model"][source], model["model"][destination])
  51. actual_name = instantiated_name(new_edge, name)
  52. dict_add(model["model"], actual_name, new_edge)
  53. return actual_name
  54. Void function retype_model(model : Element, metamodel : Element):
  55. // Remove the type mapping and add a new one for the specified metamodel
  56. dict_delete(model, "type_mapping")
  57. dict_add(model, "type_mapping", create_node())
  58. dict_add(model, "metamodel", metamodel)
  59. return
  60. Void function retype(model : Element, element : String, type : String):
  61. // Retype a model, deleting any previous type the element had
  62. // The type string is evaluated in the metamodel previously specified
  63. if (dict_in_node(model["type_mapping"], model["model"][element])):
  64. dict_delete(model["type_mapping"], model["model"][element])
  65. dict_add(model["type_mapping"], model["model"][element], model["metamodel"]["model"][type])
  66. return
  67. Element function instantiate_model(metamodel : Element):
  68. // Instantiate a model
  69. // Basically create an untyped model and retype it
  70. Element model
  71. model = instantiate_bottom()
  72. retype_model(model, metamodel)
  73. return model
  74. String function instantiate_node(model : Element, type_name : String, instance_name : String):
  75. // Create a node typed by a node from the metamodel
  76. // Basically create a node and type it immediately
  77. String actual_name
  78. actual_name = model_add_node(model, instance_name)
  79. retype(model, instance_name, type_name)
  80. return actual_name
  81. String function find_attribute_type(model : Element, elem : String, name : String):
  82. String mm_elem
  83. String direct_type
  84. direct_type = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][elem]))
  85. mm_elem = find_attribute_definer(model["metamodel"], direct_type, name)
  86. if (value_eq(mm_elem, "")):
  87. // Couldn't find element, so is not allowed!
  88. return ""
  89. else:
  90. return reverseKeyLookup(model["metamodel"]["model"], dict_read_edge(model["metamodel"]["model"][mm_elem], name))
  91. Element function get_superclasses(model : Element, name : String):
  92. Element result
  93. Integer i
  94. Integer num_edges
  95. Element edge
  96. Element elem
  97. elem = model["model"][name]
  98. // Initialize empty set
  99. result = create_node()
  100. i = 0
  101. // Read out all outgoing edges
  102. num_edges = read_nr_out(elem)
  103. while (i < num_edges):
  104. edge = read_out(elem, i)
  105. if (element_eq(dict_read_node(model["type_mapping"], edge), model["inheritance"])):
  106. create_edge(result, reverseKeyLookup(model["model"], read_edge_dst(edge)))
  107. i = i + 1
  108. return result
  109. String function find_attribute_definer(model : Element, elem_name : String, name : String):
  110. if (dict_in(model["model"][elem_name], name)):
  111. // Try in the current class definition
  112. return elem_name
  113. else:
  114. // Not found, so go to all superclasses and try there
  115. Element superclasses
  116. Element current
  117. Element found
  118. superclasses = get_superclasses(model, elem_name)
  119. while (list_len(superclasses) > 0):
  120. current = set_pop(superclasses)
  121. found = find_attribute_definer(model, current, name)
  122. if (bool_not(value_eq(found, ""))):
  123. // Found something!
  124. return current
  125. // Error
  126. return ""
  127. Void function instantiate_attribute(model : Element, element : String, attribute_name : String, value : Element):
  128. // Instantiate an attribute of something that needs to be instantiated
  129. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  130. String attr_type
  131. String attr_name
  132. attr_type = find_attribute_type(model, element, attribute_name)
  133. if (attr_type == ""):
  134. log("Could not find attribute!")
  135. return
  136. attr_name = model_add_value(model, "", value)
  137. retype(model, attr_name, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  138. instantiate_link(model, attr_type, "", element, attr_name)
  139. return
  140. String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String):
  141. // Create a typed link between two nodes
  142. String actual_name
  143. actual_name = model_add_edge(model, name, source, destination)
  144. retype(model, actual_name, type)
  145. return actual_name
  146. Void function define_inheritance(model : Element, inheritance_name : String):
  147. // Set the inheritance link to the one defined in our own metamodel, given by the specified name
  148. dict_add(model, "inheritance", model["metamodel"]["model"][inheritance_name])
  149. return
  150. Void function model_delete_element(model : Element, name : String):
  151. // Remove the link
  152. // 1) from the type mapping
  153. dict_delete(model["type_mapping"], model["model"][name])
  154. // 2) from the model
  155. delete_element(model["model"][name])
  156. return
  157. Element function read_attribute(model : Element, element : String, attribute : String):
  158. Integer i
  159. Integer count
  160. Element edge
  161. Element edge_type
  162. Element elem
  163. elem = model["model"][element]
  164. count = read_nr_out(elem)
  165. i = 0
  166. while (i < count):
  167. edge = read_out(elem, i)
  168. if (dict_in_node(model["type_mapping"], edge)):
  169. edge_type = dict_read_node(model["type_mapping"], edge)
  170. if (element_eq(edge_type, dict_read_edge(read_edge_src(edge_type), attribute))):
  171. return read_edge_dst(edge)
  172. i = i + 1
  173. return read_root()
  174. Void function unset_attribute(model : Element, element : String, attribute : String):
  175. // Removes an attribute if it exists
  176. String attr_type
  177. Element attr_links
  178. String attr_link
  179. attr_type = find_attribute_type(model, element, attribute)
  180. attr_links = allOutgoingAssociationInstances(model, element, attr_type)
  181. while (list_len(attr_links) > 0):
  182. attr_link = set_pop(attr_links)
  183. model_delete_element(model, attr_link)
  184. return
  185. Void function add_AL_links(model : Element, list : Element, element : Element, linkname : String, expected_type : String):
  186. if (bool_not(dict_in(element, linkname))):
  187. return
  188. Element link
  189. link = dict_read_edge(element, linkname)
  190. // The link
  191. dict_add(model["model"], "__" + cast_id2s(link), link)
  192. dict_add(model["type_mapping"], link, model["metamodel"]["model"]["dict_link"])
  193. // The name link
  194. link = read_out(link, 0)
  195. dict_add(model["model"], "__" + cast_id2s(link), link)
  196. dict_add(model["type_mapping"], link, model["metamodel"]["model"]["to_str"])
  197. // The name node
  198. link = read_edge_dst(link)
  199. if (bool_not(set_in_node(model["model"], link))):
  200. dict_add(model["model"], "__" + cast_id2s(link), link)
  201. dict_add(model["type_mapping"], link, model["metamodel"]["model"]["String"])
  202. // Now add the destination to the worker list
  203. Element node
  204. node = create_node()
  205. list_append(node, element[linkname])
  206. list_append(node, expected_type)
  207. set_add(list, node)
  208. return
  209. String function add_AL(model : Element, element : Element):
  210. Element todo
  211. Element node
  212. Element work_node
  213. Element elem
  214. String type
  215. todo = create_node()
  216. node = create_node()
  217. list_append(node, element)
  218. list_append(node, "funcdef")
  219. set_add(todo, node)
  220. while (0 < dict_len(todo)):
  221. work_node = set_pop(todo)
  222. elem = list_read(work_node, 0)
  223. type = list_read(work_node, 1)
  224. if (bool_not(set_in_node(model["model"], elem))):
  225. // Determine the type if we don't know it
  226. if (type == ""):
  227. if (is_physical_action(elem)):
  228. type = cast_a2s(elem)
  229. else:
  230. type = "Any"
  231. // Add the node itself
  232. dict_add(model["model"], "__" + cast_id2s(elem), elem)
  233. dict_add(model["type_mapping"], elem, model["metamodel"]["model"][type])
  234. // Now add its edges
  235. if (type == "if"):
  236. add_AL_links(model, todo, elem, "cond", "")
  237. add_AL_links(model, todo, elem, "true", "")
  238. add_AL_links(model, todo, elem, "false", "")
  239. add_AL_links(model, todo, elem, "next", "")
  240. elif (type == "while"):
  241. add_AL_links(model, todo, elem, "cond", "")
  242. add_AL_links(model, todo, elem, "body", "")
  243. add_AL_links(model, todo, elem, "next", "")
  244. elif (type == "assign"):
  245. add_AL_links(model, todo, elem, "var", "")
  246. add_AL_links(model, todo, elem, "value", "")
  247. add_AL_links(model, todo, elem, "next", "")
  248. elif (type == "break"):
  249. add_AL_links(model, todo, elem, "while", "while")
  250. elif (type == "continue"):
  251. add_AL_links(model, todo, elem, "while", "while")
  252. elif (type == "return"):
  253. add_AL_links(model, todo, elem, "value", "")
  254. elif (type == "resolve"):
  255. add_AL_links(model, todo, elem, "var", "")
  256. elif (type == "access"):
  257. add_AL_links(model, todo, elem, "var", "")
  258. elif (type == "constant"):
  259. add_AL_links(model, todo, elem, "node", "")
  260. elif (type == "output"):
  261. add_AL_links(model, todo, elem, "node", "")
  262. elif (type == "global"):
  263. add_AL_links(model, todo, elem, "var", "String")
  264. elif (type == "param"):
  265. add_AL_links(model, todo, elem, "name", "String")
  266. add_AL_links(model, todo, elem, "value", "")
  267. add_AL_links(model, todo, elem, "next_param", "param")
  268. elif (type == "funcdef"):
  269. add_AL_links(model, todo, elem, "body", "")
  270. elif (type == "call"):
  271. add_AL_links(model, todo, elem, "func", "")
  272. add_AL_links(model, todo, elem, "params", "param")
  273. add_AL_links(model, todo, elem, "last_param", "param")
  274. else:
  275. log("Unknown type found in AL parser: " + type)
  276. return reverseKeyLookup(model["model"], element)
  277. Void function add_constraint(model : Element, element : String):
  278. // Add local constraints to an element
  279. Action constraint
  280. Element attr_type
  281. String link_name
  282. String constraint_name
  283. constraint = construct_function()
  284. constraint_name = add_AL(model, constraint)
  285. attr_type = find_attribute_type(model, element, "constraint")
  286. link_name = instantiate_link(model, reverseKeyLookup(model["model"], attr_type), "", element, constraint_name)
  287. return
  288. Void function construct_model():
  289. String command
  290. while (True):
  291. command = input()
  292. if (command == "instantiate_bottom"):
  293. output(instantiate_bottom())
  294. elif (command == "add_node"):
  295. model_add_node(input(), input())
  296. elif (command == "add_value"):
  297. model_add_value(input(), input(), input())
  298. elif (command == "add_edge"):
  299. model_add_edge(input(), input(), input(), input())
  300. elif (command == "exit"):
  301. return
  302. elif (command == "retype_model"):
  303. retype_model(input(), input())
  304. elif (command == "retype"):
  305. retype(input(), input(), input())
  306. elif (command == "instantiate_model"):
  307. output(instantiate_model(input()))
  308. elif (command == "instantiate_node"):
  309. instantiate_node(input(), input(), input())
  310. elif (command == "instantiate_attribute"):
  311. instantiate_attribute(input(), input(), input(), input())
  312. elif (command == "instantiate_link"):
  313. instantiate_link(input(), input(), input(), input(), input())
  314. elif (command == "define_inheritance"):
  315. define_inheritance(input(), input())
  316. elif (command == "add_constraint"):
  317. add_constraint(input(), input())
  318. else:
  319. log("Modelling error: did not understand command " + command)