modelling.alc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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("ERROR: source of link unknown")
  42. return ""
  43. if (bool_not(dict_in(model["model"], destination))):
  44. log("ERROR: destination of link unknown")
  45. return ""
  46. new_edge = create_edge(model["model"][source], model["model"][destination])
  47. actual_name = instantiated_name(new_edge, name)
  48. dict_add(model["model"], actual_name, new_edge)
  49. return actual_name
  50. Void function retype_model(model : Element, metamodel : Element):
  51. // Remove the type mapping and add a new one for the specified metamodel
  52. dict_delete(model, "type_mapping")
  53. dict_add(model, "type_mapping", create_node())
  54. dict_add(model, "metamodel", metamodel)
  55. return
  56. Void function retype(model : Element, element : String, type : String):
  57. // Retype a model, deleting any previous type the element had
  58. // The type string is evaluated in the metamodel previously specified
  59. if (dict_in_node(model["type_mapping"], model["model"][element])):
  60. dict_delete(model["type_mapping"], model["model"][element])
  61. dict_add(model["type_mapping"], model["model"][element], model["metamodel"]["model"][type])
  62. return
  63. Element function instantiate_model(metamodel : Element):
  64. // Instantiate a model
  65. // Basically create an untyped model and retype it
  66. Element model
  67. model = instantiate_bottom()
  68. retype_model(model, metamodel)
  69. return model
  70. String function instantiate_node(model : Element, type_name : String, instance_name : String):
  71. // Create a node typed by a node from the metamodel
  72. // Basically create a node and type it immediately
  73. String actual_name
  74. actual_name = model_add_node(model, instance_name)
  75. retype(model, instance_name, type_name)
  76. return actual_name
  77. String function find_attribute_type(model : Element, elem : String, name : String):
  78. String mm_elem
  79. String direct_type
  80. direct_type = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][elem]))
  81. mm_elem = find_attribute_definer(model["metamodel"], direct_type, name)
  82. if (value_eq(mm_elem, "")):
  83. // Couldn't find element, so is not allowed!
  84. return ""
  85. else:
  86. return reverseKeyLookup(model["metamodel"]["model"], dict_read_edge(model["metamodel"]["model"][mm_elem], name))
  87. Element function get_superclasses(model : Element, name : String):
  88. Element result
  89. Integer i
  90. Integer num_edges
  91. Element edge
  92. Element elem
  93. elem = model["model"][name]
  94. // Initialize empty set
  95. result = create_node()
  96. i = 0
  97. // Read out all outgoing edges
  98. num_edges = read_nr_out(elem)
  99. while (i < num_edges):
  100. edge = read_out(elem, i)
  101. if (element_eq(dict_read_node(model["type_mapping"], edge), model["inheritance"])):
  102. create_edge(result, reverseKeyLookup(model["model"], read_edge_dst(edge)))
  103. i = i + 1
  104. return result
  105. String function find_attribute_definer(model : Element, elem_name : String, name : String):
  106. if (dict_in(model["model"][elem_name], name)):
  107. // Try in the current class definition
  108. return elem_name
  109. else:
  110. // Not found, so go to all superclasses and try there
  111. Element superclasses
  112. Element current
  113. Element found
  114. superclasses = get_superclasses(model, elem_name)
  115. while (list_len(superclasses) > 0):
  116. current = set_pop(superclasses)
  117. found = find_attribute_definer(model, current, name)
  118. if (bool_not(value_eq(found, ""))):
  119. // Found something!
  120. return current
  121. // Error
  122. return ""
  123. Void function instantiate_attribute(model : Element, element : String, attribute_name : String, value : Element):
  124. // Instantiate an attribute of something that needs to be instantiated
  125. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  126. String attr_type
  127. String attr_name
  128. attr_type = find_attribute_type(model, element, attribute_name)
  129. if (attr_type == ""):
  130. log("Could not find attribute!")
  131. return
  132. attr_name = model_add_value(model, "", value)
  133. retype(model, attr_name, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  134. instantiate_link(model, attr_type, "", element, attr_name)
  135. return
  136. String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String):
  137. // Create a typed link between two nodes
  138. String actual_name
  139. actual_name = model_add_edge(model, name, source, destination)
  140. retype(model, actual_name, type)
  141. return actual_name
  142. Void function define_inheritance(model : Element, inheritance_name : String):
  143. // Set the inheritance link to the one defined in our own metamodel, given by the specified name
  144. dict_add(model, "inheritance", model["metamodel"]["model"][inheritance_name])
  145. return
  146. Void function model_delete_element(model : Element, name : String):
  147. // Remove the link
  148. // 1) from the type mapping
  149. dict_delete(model["type_mapping"], model["model"][name])
  150. // 2) from the model
  151. delete_element(model["model"][name])
  152. return
  153. Element function read_attribute(model : Element, element : String, attribute : String):
  154. Integer i
  155. Integer count
  156. Element edge
  157. Element edge_type
  158. Element elem
  159. elem = model["model"][element]
  160. count = read_nr_out(elem)
  161. i = 0
  162. while (i < count):
  163. edge = read_out(elem, i)
  164. if (dict_in_node(model["type_mapping"], edge)):
  165. edge_type = dict_read_node(model["type_mapping"], edge)
  166. if (element_eq(edge_type, dict_read_edge(read_edge_src(edge_type), attribute))):
  167. return read_edge_dst(edge)
  168. i = i + 1
  169. return read_root()
  170. Void function unset_attribute(model : Element, element : String, attribute : String):
  171. // Removes an attribute if it exists
  172. String attr_type
  173. Element attr_links
  174. String attr_link
  175. attr_type = find_attribute_type(model, element, attribute)
  176. attr_links = allOutgoingAssociationInstances(model, element, attr_type)
  177. while (list_len(attr_links) > 0):
  178. attr_link = set_pop(attr_links)
  179. model_delete_element(model, attr_link)
  180. return
  181. Void function recursive_add_AL(model : Element, element : Element):
  182. // TODO
  183. String name
  184. name = "__" + cast_id2s(element)
  185. if (dict_in(model["model"], name)):
  186. return
  187. else:
  188. // Add this element and call it for all its outgoing links
  189. dict_add(model["model"], name, element)
  190. if (is_physical_action(element)):
  191. dict_add(model["type_mapping"], element, model["metamodel"][cast_a2s(element)])
  192. else:
  193. // Either dealing with a param, a funcdef, or just some other kind of node that we have no clue about
  194. // TODO add this code
  195. return
  196. // Add all its outgoing links
  197. Integer counter
  198. Integer i
  199. Element elem
  200. counter = read_nr_out(element)
  201. i = 0
  202. while (i < counter):
  203. elem = read_out(element, i)
  204. // TODO add the link and all its attributes
  205. recursive_add_AL(model, elem)
  206. i = i + 1
  207. return
  208. String function type_action_code(model : Element, constraint : Action):
  209. recursive_add_AL(model, constraint)
  210. return reverseKeyLookup(model["model"], constraint)
  211. Void function add_constraint(model : Element, element : String):
  212. // Add local constraints to an element
  213. Action constraint
  214. Element attr_type
  215. String link_name
  216. String constraint_name
  217. constraint = construct_function()
  218. attr_type = find_attribute_type(model, element, "constraint")
  219. constraint_name = type_action_code(model, constraint)
  220. link_name = instantiate_link(model, attr_type, "", model["model"][element], constraint_name)
  221. return
  222. Void function construct_model():
  223. String command
  224. while (True):
  225. command = input()
  226. if (command == "instantiate_bottom"):
  227. output(instantiate_bottom())
  228. elif (command == "add_node"):
  229. model_add_node(input(), input())
  230. elif (command == "add_value"):
  231. model_add_value(input(), input(), input())
  232. elif (command == "add_edge"):
  233. model_add_edge(input(), input(), input(), input())
  234. elif (command == "exit"):
  235. return
  236. elif (command == "retype_model"):
  237. retype_model(input(), input())
  238. elif (command == "retype"):
  239. retype(input(), input(), input())
  240. elif (command == "instantiate_model"):
  241. output(instantiate_model(input()))
  242. elif (command == "instantiate_node"):
  243. instantiate_node(input(), input(), input())
  244. elif (command == "instantiate_attribute"):
  245. instantiate_attribute(input(), input(), input(), input())
  246. elif (command == "instantiate_link"):
  247. instantiate_link(input(), input(), input(), input(), input())
  248. elif (command == "define_inheritance"):
  249. define_inheritance(input(), input())
  250. elif (command == "add_constraint"):
  251. add_constraint(input(), input())
  252. else:
  253. log("Modelling error: did not understand command " + command)