modelling.alc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. new_edge = create_edge(model["model"][source], model["model"][destination])
  41. actual_name = instantiated_name(new_edge, name)
  42. dict_add(model["model"], actual_name, new_edge)
  43. return actual_name
  44. Void function retype_model(model : Element, metamodel : Element):
  45. // Remove the type mapping and add a new one for the specified metamodel
  46. dict_delete(model, "type_mapping")
  47. dict_add(model, "type_mapping", create_node())
  48. dict_add(model, "metamodel", metamodel)
  49. return
  50. Void function retype(model : Element, element : String, type : String):
  51. // Retype a model, deleting any previous type the element had
  52. // The type string is evaluated in the metamodel previously specified
  53. if (dict_in_node(model["type_mapping"], model["model"][element])):
  54. dict_delete(model["type_mapping"], model["model"][element])
  55. dict_add(model["type_mapping"], model["model"][element], model["metamodel"]["model"][type])
  56. return
  57. Element function instantiate_model(metamodel : Element):
  58. // Instantiate a model
  59. // Basically create an untyped model and retype it
  60. Element model
  61. model = instantiate_bottom()
  62. retype_model(model, metamodel)
  63. return model
  64. String function instantiate_node(model : Element, type_name : String, instance_name : String):
  65. // Create a node typed by a node from the metamodel
  66. // Basically create a node and type it immediately
  67. String actual_name
  68. actual_name = model_add_node(model, instance_name)
  69. retype(model, instance_name, type_name)
  70. return actual_name
  71. Element function find_attribute_type(model : Element, elem : String, name : String):
  72. Element mm_elem
  73. Element direct_type
  74. direct_type = dict_read_node(model["type_mapping"], model["model"][elem])
  75. mm_elem = find_attribute_definer(model["metamodel"], direct_type, name)
  76. if (element_eq(mm_elem, read_root())):
  77. // Couldn't find element, so is not allowed!
  78. log("Undefined attribute!")
  79. return read_root()
  80. else:
  81. return getName(model["metamodel"], dict_read_edge(mm_elem, name))
  82. Element function get_superclasses(model : Element, elem : Element):
  83. Element result
  84. Integer i
  85. Integer num_edges
  86. Element edge
  87. // Initialize empty set
  88. result = create_node()
  89. i = 0
  90. // Read out all outgoing edges
  91. num_edges = read_nr_out(elem)
  92. log(cast_i2s(num_edges))
  93. while (i < num_edges):
  94. edge = read_out(elem, i)
  95. log(cast_e2s(edge))
  96. if (element_eq(dict_read_node(model["type_mapping"], edge), model["inheritance"])):
  97. create_edge(result, read_edge_dst(edge))
  98. i = i + 1
  99. return result
  100. Element function find_attribute_definer(model : Element, elem : Element, name : String):
  101. if (dict_in(elem, name)):
  102. // Try in the current class definition
  103. return elem
  104. else:
  105. // Not found, so go to all superclasses and try there
  106. Element superclasses
  107. Element current
  108. Element found
  109. superclasses = get_superclasses(model, elem)
  110. while (list_len(superclasses) > 0):
  111. current = set_pop(superclasses)
  112. found = find_attribute_definer(model, current, name)
  113. if (bool_not(element_eq(found, read_root()))):
  114. // Found something!
  115. return current
  116. // Return the de facto error-value
  117. return read_root()
  118. Void function instantiate_attribute(model : Element, element : String, attribute_name : String, value : Element):
  119. // Instantiate an attribute of something that needs to be instantiated
  120. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  121. String attr_type
  122. String attr_name
  123. attr_type = find_attribute_type(model, element, attribute_name)
  124. attr_name = model_add_value(model, "", value)
  125. retype(model, attr_name, getName(model["metamodel"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  126. instantiate_link(model, attr_type, "", element, attr_name)
  127. return
  128. String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String):
  129. // Create a typed link between two nodes
  130. String actual_name
  131. actual_name = model_add_edge(model, name, source, destination)
  132. retype(model, actual_name, type)
  133. return actual_name
  134. Void function instantiate_named(model : Element, type : String, name : String, source : String, destination : String):
  135. // Special helper function to create an edge and name it
  136. String link_name
  137. String name_name
  138. link_name = instantiate_link(model, type, "", source, destination)
  139. //name_name = model_add_value(model, "", name)
  140. //retype(model, name_name, "String")
  141. instantiate_attribute(model, link_name, "name", name)
  142. return
  143. Void function define_inheritance(model : Element, inheritance_name : String):
  144. // Set the inheritance link to the one defined in our own metamodel, given by the specified name
  145. dict_add(model, "inheritance", model["metamodel"]["model"][inheritance_name])
  146. return
  147. Void function model_delete_element(model : Element, name : String):
  148. // Remove the link
  149. // 1) from the type mapping
  150. dict_delete(model["type_mapping"], model["model"][name])
  151. // 2) from the model
  152. delete_element(model["model"][name])
  153. return
  154. Element function read_attribute(model : Element, element : String, attribute : String):
  155. // Read out the value of an attribute
  156. Element attr_type
  157. Element attr_links
  158. Element attr_link
  159. attr_type = find_attribute_type(model, element, attribute)
  160. attr_links = allOutgoingAssociationInstances(model, model["model"][element], model["metamodel"]["model"][attr_type])
  161. while (list_len(attr_links) > 0):
  162. attr_link = set_pop(attr_links)
  163. return read_edge_dst(attr_link)
  164. log("Could not find attribute!")
  165. return read_root()
  166. Void function unset_attribute(model : Element, element : String, attribute : String):
  167. // Removes an attribute if it exists
  168. Element attr_type
  169. Element attr_links
  170. Element attr_link
  171. attr_type = find_attribute_type(model, element, attribute)
  172. attr_links = allOutgoingAssociationInstances(model, model["model"][element], model["metamodel"]["model"][attr_type])
  173. while (list_len(attr_links) > 0):
  174. attr_link = set_pop(attr_links)
  175. model_delete_element(model, getName(model, attr_link))
  176. return
  177. Void function construct_model():
  178. String command
  179. log("Enter modelling constructs!")
  180. while (True):
  181. command = input()
  182. if (command == "instantiate_bottom"):
  183. output(instantiate_bottom())
  184. elif (command == "add_node"):
  185. model_add_node(input(), input())
  186. elif (command == "add_value"):
  187. model_add_value(input(), input(), input())
  188. elif (command == "add_edge"):
  189. model_add_edge(input(), input(), input(), input())
  190. elif (command == "exit"):
  191. return
  192. elif (command == "retype_model"):
  193. retype_model(input(), input())
  194. elif (command == "retype"):
  195. retype(input(), input(), input())
  196. elif (command == "instantiate_model"):
  197. output(instantiate_model(input()))
  198. elif (command == "instantiate_node"):
  199. instantiate_node(input(), input(), input())
  200. elif (command == "instantiate_named"):
  201. instantiate_named(input(), input(), input(), input(), input())
  202. elif (command == "instantiate_attribute"):
  203. instantiate_attribute(input(), input(), input(), input())
  204. elif (command == "instantiate_link"):
  205. instantiate_link(input(), input(), input(), input(), input())
  206. elif (command == "define_inheritance"):
  207. define_inheritance(input(), input())
  208. else:
  209. log("Modelling error: did not understand command " + command)