modelling.alc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. include "primitives.alh"
  2. include "io.alh"
  3. include "object_operations.alh"
  4. include "constructors.alh"
  5. include "metamodels.alh"
  6. include "library.alh"
  7. Element global_models = ?
  8. String function instantiated_name(element : Element, original : String):
  9. if (original == ""):
  10. return "__" + cast_id2s(element)!
  11. else:
  12. return original!
  13. Element function instantiate_bottom():
  14. // Just create a new node that serves as the basis for everything
  15. // We don't know anything about the model yet, so just create an empty one
  16. Element new_model
  17. // The actual root node of the model
  18. new_model = create_node()
  19. // Add an empty model and empty type mapping
  20. dict_add(new_model, "model", create_node())
  21. dict_add(new_model, "type_mapping", create_node())
  22. dict_add(new_model, "__kind", "formalism")
  23. // Return the created model
  24. return new_model!
  25. String function model_add_node(model : Element, name : String):
  26. // Adds a new node to the specified model with the desired name
  27. // This is a bottom operation, as it doesn't take any type
  28. Element new_node
  29. String actual_name
  30. new_node = create_node()
  31. actual_name = instantiated_name(new_node, name)
  32. dict_add(model["model"], actual_name, new_node)
  33. return actual_name!
  34. String function model_add_value(model : Element, name : String, value : Element):
  35. // Similar to model_add_node, but add a value as well
  36. String actual_name
  37. actual_name = instantiated_name(value, name)
  38. dict_add(model["model"], actual_name, value)
  39. return actual_name!
  40. String function model_add_edge(model : Element, name : String, source : String, destination : String):
  41. // Add an edge between the source and destination nodes
  42. // Nodes are specified using their string representation previously defined
  43. Element new_edge
  44. String actual_name
  45. if (bool_not(dict_in(model["model"], source))):
  46. log("In link " + name)
  47. log("ERROR: source of link unknown: " + source)
  48. log("Destination: " + destination)
  49. return ""!
  50. if (bool_not(dict_in(model["model"], destination))):
  51. log("In link " + name)
  52. log("ERROR: destination of link unknown: " + destination)
  53. log("Source: " + source)
  54. return ""!
  55. new_edge = create_edge(model["model"][source], model["model"][destination])
  56. actual_name = instantiated_name(new_edge, name)
  57. dict_add(model["model"], actual_name, new_edge)
  58. return actual_name!
  59. Void function retype_model(model : Element, metamodel : Element):
  60. // Remove the type mapping and add a new one for the specified metamodel
  61. dict_delete(model, "type_mapping")
  62. dict_add(model, "type_mapping", create_node())
  63. dict_add(model, "metamodel", metamodel)
  64. return!
  65. Void function retype(model : Element, element : String, type : String):
  66. // Retype a model, deleting any previous type the element had
  67. // The type string is evaluated in the metamodel previously specified
  68. if (dict_in_node(model["type_mapping"], model["model"][element])):
  69. dict_delete_node(model["type_mapping"], model["model"][element])
  70. dict_add(model["type_mapping"], model["model"][element], model["metamodel"]["model"][type])
  71. return!
  72. Element function instantiate_model(metamodel : Element):
  73. // Instantiate a model
  74. // Basically create an untyped model and retype it
  75. Element model
  76. model = instantiate_bottom()
  77. retype_model(model, metamodel)
  78. return model!
  79. String function instantiate_node(model : Element, type_name : String, instance_name : String):
  80. // Create a node typed by a node from the metamodel
  81. // Basically create a node and type it immediately
  82. String actual_name
  83. actual_name = model_add_node(model, instance_name)
  84. retype(model, actual_name, type_name)
  85. return actual_name!
  86. String function instantiate_value(model : Element, type_name : String, instance_name : String, value : Element):
  87. // Create a node typed by a node from the metamodel
  88. // Basically create a node and type it immediately
  89. String actual_name
  90. actual_name = model_add_value(model, instance_name, value)
  91. retype(model, actual_name, type_name)
  92. return actual_name!
  93. String function find_attribute_type(model : Element, elem : String, name : String):
  94. String mm_elem
  95. String direct_type
  96. direct_type = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][elem]))
  97. mm_elem = find_attribute_definer(model["metamodel"], direct_type, name)
  98. if (value_eq(mm_elem, "")):
  99. // Couldn't find element, so is not allowed!
  100. return ""!
  101. else:
  102. return reverseKeyLookup(model["metamodel"]["model"], dict_read_edge(model["metamodel"]["model"][mm_elem], name))!
  103. Element function get_superclasses(model : Element, name : String):
  104. Element result
  105. Integer i
  106. Integer j
  107. Integer num_edges
  108. Element edge
  109. Element elem
  110. Element nodes
  111. Element inheritance
  112. nodes = create_node()
  113. set_add(nodes, model["model"][name])
  114. inheritance = model["metamodel"]["model"]["Inheritance"]
  115. // Initialize empty set
  116. result = create_node()
  117. i = 0
  118. while (list_len(nodes) > 0):
  119. elem = set_pop(nodes)
  120. if (bool_not(set_in(result, reverseKeyLookup(model["model"], elem)))):
  121. create_edge(result, reverseKeyLookup(model["model"], elem))
  122. // Read out all outgoing edges
  123. num_edges = read_nr_out(elem)
  124. j = 0
  125. while (j < num_edges):
  126. edge = read_out(elem, j)
  127. if (element_eq(dict_read_node(model["type_mapping"], edge), inheritance)):
  128. set_add(nodes, read_edge_dst(edge))
  129. j = j + 1
  130. return result!
  131. String function find_attribute_definer(model : Element, elem_name : String, name : String):
  132. Element superclasses
  133. String current
  134. Integer nr_out
  135. Element out
  136. String name_attr
  137. superclasses = get_superclasses(model, elem_name)
  138. while (list_len(superclasses) > 0):
  139. current = set_pop(superclasses)
  140. nr_out = read_nr_out(model["model"][current])
  141. while (nr_out > 0):
  142. nr_out = nr_out - 1
  143. out = read_out(model["model"][current], nr_out)
  144. name_attr = read_attribute(model, reverseKeyLookup(model["model"], out), "name")
  145. if (name_attr == name):
  146. return current!
  147. return ""!
  148. Void function instantiate_attribute(model : Element, element : String, attribute_name : String, value : Element):
  149. // Instantiate an attribute of something that needs to be instantiated
  150. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  151. String attr_type
  152. String attr_name
  153. attr_type = find_attribute_type(model, element, attribute_name)
  154. if (attr_type == ""):
  155. log("Could not find attribute " + cast_v2s(attribute_name))
  156. return!
  157. // Make a copy of the value, as it is likely that this value is reused later on
  158. value = create_value(value)
  159. attr_name = model_add_value(model, (element + ".") + attribute_name, value)
  160. retype(model, attr_name, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  161. instantiate_link(model, attr_type, "", element, attr_name)
  162. return!
  163. Void function instantiate_attribute_ref(model : Element, element : String, attribute_name : String, ref : String):
  164. // Instantiate an attribute of something that needs to be instantiated
  165. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  166. String attr_type
  167. String attr_name
  168. attr_type = find_attribute_type(model, element, attribute_name)
  169. if (attr_type == ""):
  170. log("Could not find attribute " + cast_v2s(attribute_name))
  171. return!
  172. instantiate_link(model, attr_type, "", element, ref)
  173. return!
  174. Void function instantiate_attribute_code(model : Element, element : String, attribute_name : String, code : Element):
  175. String ref
  176. ref = add_AL(model, code)
  177. instantiate_existing_attribute(model, element, attribute_name, ref)
  178. return!
  179. Void function instantiate_existing_attribute(model : Element, element : String, attribute_name : String, attr_ref : String):
  180. // Instantiate an attribute of something that needs to be instantiated
  181. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  182. String attr_type
  183. String attr_name
  184. attr_type = find_attribute_type(model, element, attribute_name)
  185. if (attr_type == ""):
  186. log("Could not find attribute " + cast_v2s(attribute_name))
  187. return!
  188. // Make a copy of the value, as it is likely that this value is reused later on
  189. retype(model, attr_ref, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  190. instantiate_link(model, attr_type, "", element, attr_ref)
  191. return!
  192. String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String):
  193. // Create a typed link between two nodes
  194. String actual_name
  195. actual_name = model_add_edge(model, name, source, destination)
  196. if (type == ""):
  197. // Have to find the type ourselves, as it isn't defined
  198. Element out
  199. Element in
  200. Element options
  201. options = allowedAssociationsBetween(model, source, destination)
  202. if (read_nr_out(options) == 1):
  203. type = set_pop(options)
  204. elif (read_nr_out(options) == 0):
  205. log("ERROR: cannot find possible link between entries")
  206. return ""!
  207. else:
  208. log("ERROR: too many possible links between entries")
  209. return ""!
  210. retype(model, actual_name, type)
  211. return actual_name!
  212. Void function model_delete_element(model : Element, name : String):
  213. // Remove the link
  214. // 1) from the type mapping
  215. dict_delete_node(model["type_mapping"], model["model"][name])
  216. // 2) from the model
  217. delete_element(model["model"][name])
  218. return!
  219. String function model_define_attribute(model : Element, elem : String, name : String, type : String):
  220. // Create the necessary links to make it an attribute
  221. String edge_name
  222. edge_name = instantiate_link(model, "Association", "", elem, type)
  223. instantiate_attribute(model, edge_name, "name", name)
  224. return edge_name!
  225. Element function read_attribute(model : Element, element : String, attribute : String):
  226. Integer i
  227. Integer count
  228. Element edge
  229. Element edge_type
  230. Element elem
  231. Element typing
  232. elem = model["model"][element]
  233. typing = model["type_mapping"]
  234. count = read_nr_out(elem)
  235. i = 0
  236. while (i < count):
  237. edge = read_out(elem, i)
  238. if (dict_in_node(typing, edge)):
  239. edge_type = dict_read_node(typing, edge)
  240. if (element_eq(edge_type, dict_read_edge(read_edge_src(edge_type), attribute))):
  241. return read_edge_dst(edge)!
  242. i = i + 1
  243. return read_root()!
  244. Void function unset_attribute(model : Element, element : String, attribute : String):
  245. // Removes an attribute if it exists
  246. String attr_type
  247. Element attr_links
  248. String attr_link
  249. attr_type = find_attribute_type(model, element, attribute)
  250. attr_links = allOutgoingAssociationInstances(model, element, attr_type)
  251. while (list_len(attr_links) > 0):
  252. attr_link = set_pop(attr_links)
  253. dict_delete_node(model["type_mapping"], read_edge_dst(model["model"][attr_link]))
  254. dict_delete_node(model["type_mapping"], model["model"][attr_link])
  255. dict_delete_node(model["model"], reverseKeyLookup(model["model"], read_edge_dst(model["model"][attr_link])))
  256. delete_element(model["model"][attr_link])
  257. return!
  258. Void function add_AL_links(model : Element, list : Element, element : Element, type: String, linkname : String, expected_type : String):
  259. if (bool_not(dict_in(element, linkname))):
  260. return!
  261. Element link
  262. link = dict_read_edge(element, linkname)
  263. // The link
  264. dict_add(model["model"], "__" + cast_id2s(link), link)
  265. dict_add(model["type_mapping"], link, model["metamodel"]["model"][(type + "_") + linkname])
  266. // The name link
  267. link = read_out(link, 0)
  268. dict_add(model["model"], "__" + cast_id2s(link), link)
  269. dict_add(model["type_mapping"], link, model["metamodel"]["model"]["to_str"])
  270. // The name node
  271. link = read_edge_dst(link)
  272. if (bool_not(set_in_node(model["model"], link))):
  273. dict_add(model["model"], "__" + cast_id2s(link), link)
  274. dict_add(model["type_mapping"], link, model["metamodel"]["model"]["String"])
  275. // Now add the destination to the worker list
  276. Element node
  277. node = create_node()
  278. list_append(node, element[linkname])
  279. list_append(node, expected_type)
  280. set_add(list, node)
  281. return!
  282. String function add_AL(model : Element, element : Element):
  283. Element todo
  284. Element node
  285. Element work_node
  286. Element elem
  287. String type
  288. todo = create_node()
  289. node = create_node()
  290. list_append(node, element)
  291. list_append(node, "funcdef")
  292. set_add(todo, node)
  293. while (0 < dict_len(todo)):
  294. work_node = set_pop(todo)
  295. elem = list_read(work_node, 0)
  296. type = list_read(work_node, 1)
  297. if (bool_not(set_in_node(model["model"], elem))):
  298. // Determine the type if we don't know it
  299. if (type == ""):
  300. if (is_physical_action(elem)):
  301. type = cast_a2s(elem)
  302. else:
  303. type = "Any"
  304. // Add the node itself
  305. dict_add(model["model"], "__" + cast_id2s(elem), elem)
  306. dict_add(model["type_mapping"], elem, model["metamodel"]["model"][type])
  307. // Now add its edges
  308. if (type == "if"):
  309. add_AL_links(model, todo, elem, type, "cond", "")
  310. add_AL_links(model, todo, elem, type, "then", "")
  311. add_AL_links(model, todo, elem, type, "else", "")
  312. add_AL_links(model, todo, elem, type, "next", "")
  313. elif (type == "while"):
  314. add_AL_links(model, todo, elem, type, "cond", "")
  315. add_AL_links(model, todo, elem, type, "body", "")
  316. add_AL_links(model, todo, elem, type, "next", "")
  317. elif (type == "assign"):
  318. add_AL_links(model, todo, elem, type, "var", "")
  319. add_AL_links(model, todo, elem, type, "value", "")
  320. add_AL_links(model, todo, elem, type, "next", "")
  321. elif (type == "break"):
  322. add_AL_links(model, todo, elem, type, "while", "while")
  323. elif (type == "continue"):
  324. add_AL_links(model, todo, elem, type, "while", "while")
  325. elif (type == "return"):
  326. add_AL_links(model, todo, elem, type, "value", "")
  327. elif (type == "resolve"):
  328. add_AL_links(model, todo, elem, type, "var", "")
  329. elif (type == "access"):
  330. add_AL_links(model, todo, elem, type, "var", "")
  331. elif (type == "constant"):
  332. add_AL_links(model, todo, elem, type, "node", "")
  333. elif (type == "output"):
  334. add_AL_links(model, todo, elem, type, "node", "")
  335. add_AL_links(model, todo, elem, type, "next", "")
  336. elif (type == "global"):
  337. add_AL_links(model, todo, elem, type, "var", "String")
  338. add_AL_links(model, todo, elem, type, "next", "")
  339. elif (type == "param"):
  340. add_AL_links(model, todo, elem, type, "name", "String")
  341. add_AL_links(model, todo, elem, type, "value", "")
  342. add_AL_links(model, todo, elem, type, "next_param", "param")
  343. elif (type == "funcdef"):
  344. add_AL_links(model, todo, elem, type, "body", "")
  345. add_AL_links(model, todo, elem, type, "next", "")
  346. elif (type == "call"):
  347. add_AL_links(model, todo, elem, type, "func", "")
  348. add_AL_links(model, todo, elem, type, "params", "param")
  349. add_AL_links(model, todo, elem, type, "last_param", "param")
  350. add_AL_links(model, todo, elem, type, "next", "")
  351. return reverseKeyLookup(model["model"], element)!
  352. Void function add_constraint(model : Element, element : String, constraint : Action):
  353. // Add local constraints to an element
  354. Element attr_type
  355. String link_name
  356. String constraint_name
  357. constraint_name = add_AL(model, constraint)
  358. attr_type = find_attribute_type(model, element, "constraint")
  359. instantiate_link(model, attr_type, "", element, constraint_name)
  360. return!
  361. Void function construct_model():
  362. String command
  363. while (True):
  364. command = input()
  365. if (command == "instantiate_bottom"):
  366. Element m
  367. m = instantiate_bottom()
  368. dict_add(global_models, input(), m)
  369. elif (command == "add_node"):
  370. model_add_node(global_models[input()], input())
  371. elif (command == "add_value"):
  372. model_add_value(global_models[input()], input(), input())
  373. elif (command == "add_edge"):
  374. model_add_edge(global_models[input()], input(), input(), input())
  375. elif (command == "exit"):
  376. return!
  377. elif (command == "retype_model"):
  378. retype_model(global_models[input()], global_models[input()])
  379. elif (command == "retype"):
  380. retype(global_models[input()], input(), input())
  381. elif (command == "instantiate_model"):
  382. Element m
  383. m = instantiate_model(global_models[input()])
  384. dict_add(global_models, input(), m)
  385. elif (command == "instantiate_node"):
  386. instantiate_node(global_models[input()], input(), input())
  387. elif (command == "instantiate_attribute"):
  388. instantiate_attribute(global_models[input()], input(), input(), input())
  389. elif (command == "instantiate_attribute_ref"):
  390. instantiate_attribute_ref(global_models[input()], input(), input(), input())
  391. elif (command == "instantiate_attribute_code"):
  392. instantiate_attribute_code(global_models[input()], input(), input(), construct_function())
  393. elif (command == "instantiate_link"):
  394. instantiate_link(global_models[input()], input(), input(), input(), input())
  395. elif (command == "add_constraint"):
  396. add_constraint(global_models[input()], input(), construct_function())
  397. elif (command == "initialize_SCD"):
  398. initialize_SCD(input())
  399. elif (command == "initialize_bottom"):
  400. initialize_bottom(input())
  401. elif (command == "export_node"):
  402. String local_name
  403. String location
  404. local_name = input()
  405. location = input()
  406. export_node(location, global_models[local_name])
  407. elif (command == "import_node"):
  408. Element m
  409. command = input()
  410. m = import_node(command)
  411. if (element_eq(m, read_root())):
  412. log("Error: import not found for " + command)
  413. else:
  414. dict_add(global_models, input(), m)
  415. else:
  416. log("Modelling error: did not understand command " + command)