modelling.alc 13 KB

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