modelling.alc 13 KB

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