modelling.alc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. include "primitives.alh"
  2. include "io.alh"
  3. String function instantiated_name(element : Element, original : String):
  4. if (original == ""):
  5. return "__" + cast_id2s(element)
  6. else:
  7. return original
  8. Element function instantiate_bottom():
  9. // Just create a new node that serves as the basis for everything
  10. // We don't know anything about the model yet, so just create an empty one
  11. Element new_model
  12. // The actual root node of the model
  13. new_model = create_node()
  14. // Add an empty model and empty type mapping
  15. dict_add(new_model, "model", create_node())
  16. dict_add(new_model, "type_mapping", create_node())
  17. // Return the created model
  18. return new_model
  19. String function model_add_node(model : Element, name : String):
  20. // Adds a new node to the specified model with the desired name
  21. // This is a bottom operation, as it doesn't take any type
  22. Element new_node
  23. String actual_name
  24. new_node = create_node()
  25. actual_name = instantiated_name(new_node, name)
  26. dict_add(model["model"], actual_name, new_node)
  27. return actual_name
  28. String function model_add_value(model : Element, name : String, value : Element):
  29. // Similar to model_add_node, but add a value as well
  30. String actual_name
  31. actual_name = instantiated_name(value, name)
  32. dict_add(model["model"], actual_name, value)
  33. return actual_name
  34. String function model_add_edge(model : Element, name : String, source : String, destination : String):
  35. // Add an edge between the source and destination nodes
  36. // Nodes are specified using their string representation previously defined
  37. Element new_edge
  38. String actual_name
  39. new_edge = create_edge(model["model"][source], model["model"][destination])
  40. actual_name = instantiated_name(new_edge, name)
  41. dict_add(model["model"], actual_name, new_edge)
  42. return actual_name
  43. Void function retype_model(model : Element, metamodel : Element):
  44. // Remove the type mapping and add a new one for the specified metamodel
  45. dict_delete(model, "type_mapping")
  46. dict_add(model, "type_mapping", create_node())
  47. dict_add(model, "metamodel", metamodel)
  48. return
  49. Void function retype(model : Element, element : String, type : String):
  50. // Retype a model, deleting any previous type the element had
  51. // The type string is evaluated in the metamodel previously specified
  52. if (dict_in_node(model["type_mapping"], model["model"][element])):
  53. dict_delete(model["type_mapping"], model["model"][element])
  54. dict_add(model["type_mapping"], model["model"][element], model["metamodel"]["model"][type])
  55. return
  56. Element function instantiate_model(metamodel : Element):
  57. // Instantiate a model
  58. // Basically create an untyped model and retype it
  59. Element model
  60. model = instantiate_bottom()
  61. retype_model(model, metamodel)
  62. return model
  63. String function instantiate_node(model : Element, type_name : String, instance_name : String):
  64. // Create a node typed by a node from the metamodel
  65. // Basically create a node and type it immediately
  66. String actual_name
  67. actual_name = model_add_node(model, instance_name)
  68. retype(model, instance_name, type_name)
  69. return actual_name
  70. Element function find_attribute_type(model : Element, elem : String, name : String):
  71. Element mm_elem
  72. Element direct_type
  73. direct_type = dict_read_node(model["type_mapping"], model["model"][elem])
  74. mm_elem = find_attribute_definer(model["metamodel"], direct_type, name)
  75. if (element_eq(mm_elem, read_root())):
  76. // Couldn't find element, so is not allowed!
  77. log("Undefined attribute!")
  78. return read_root()
  79. else:
  80. return getName(model["metamodel"], dict_read_edge(mm_elem, name))
  81. Element function get_superclasses(model : Element, elem : Element):
  82. Element result
  83. Integer i
  84. Integer num_edges
  85. Element edge
  86. // Initialize empty set
  87. result = create_node()
  88. i = 0
  89. // Read out all outgoing edges
  90. num_edges = read_nr_out(elem)
  91. log(cast_i2s(num_edges))
  92. while (i < num_edges):
  93. log("Search SC")
  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. log("Search")
  112. current = set_pop(superclasses)
  113. found = find_attribute_definer(model, current, name)
  114. if (bool_not(element_eq(found, read_root()))):
  115. // Found something!
  116. return current
  117. // Return the de facto error-value
  118. return read_root()
  119. Void function instantiate_attribute(model : Element, element : String, attribute_name : String, value : Element):
  120. // Instantiate an attribute of something that needs to be instantiated
  121. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  122. String attr_type
  123. String attr_name
  124. attr_type = find_attribute_type(model, element, attribute_name)
  125. attr_name = model_add_value(model, "", value)
  126. retype(model, attr_name, getName(model["metamodel"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  127. instantiate_link(model, attr_type, "", element, attr_name)
  128. return
  129. String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String):
  130. // Create a typed link between two nodes
  131. String actual_name
  132. actual_name = model_add_edge(model, name, source, destination)
  133. retype(model, actual_name, type)
  134. return actual_name
  135. Void function instantiate_named(model : Element, type : String, name : String, source : String, destination : String):
  136. // Special helper function to create an edge and name it
  137. String link_name
  138. String name_name
  139. link_name = instantiate_link(model, type, "", source, destination)
  140. //name_name = model_add_value(model, "", name)
  141. //retype(model, name_name, "String")
  142. instantiate_attribute(model, link_name, "name", name)
  143. return
  144. Void function define_inheritance(model : Element, inheritance_name : String):
  145. // Set the inheritance link to the one defined in our own metamodel, given by the specified name
  146. dict_add(model, "inheritance", model["metamodel"]["model"][inheritance_name])
  147. return
  148. Void function construct_model():
  149. String command
  150. log("Enter modelling constructs!")
  151. while (True):
  152. command = input()
  153. if (command == "instantiate_bottom"):
  154. output(instantiate_bottom())
  155. elif (command == "add_node"):
  156. model_add_node(input(), input())
  157. elif (command == "add_value"):
  158. model_add_value(input(), input(), input())
  159. elif (command == "add_edge"):
  160. model_add_edge(input(), input(), input(), input())
  161. elif (command == "exit"):
  162. return
  163. elif (command == "retype_model"):
  164. retype_model(input(), input())
  165. elif (command == "retype"):
  166. retype(input(), input(), input())
  167. elif (command == "instantiate_model"):
  168. output(instantiate_model(input()))
  169. elif (command == "instantiate_node"):
  170. instantiate_node(input(), input(), input())
  171. elif (command == "instantiate_named"):
  172. instantiate_named(input(), input(), input(), input(), input())
  173. elif (command == "instantiate_attribute"):
  174. instantiate_attribute(input(), input(), input(), input())
  175. elif (command == "instantiate_link"):
  176. instantiate_link(input(), input(), input(), input(), input())
  177. elif (command == "define_inheritance"):
  178. define_inheritance(input(), input())
  179. else:
  180. log("Modelling error: did not understand command " + command)