modelling.alc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. include "typing.alh"
  8. include "utils.alh"
  9. include "core_algorithm.alh"
  10. Element global_models = ?
  11. String function instantiated_name(element : Element, original : String):
  12. if (original == ""):
  13. return "__" + cast_id(element)!
  14. else:
  15. return original!
  16. Element function instantiate_bottom():
  17. // Just create a new node that serves as the basis for everything
  18. // We don't know anything about the model yet, so just create an empty one
  19. Element new_model
  20. // The actual root node of the model
  21. new_model = dict_create()
  22. // Add an empty model and empty type mapping
  23. dict_add_fast(new_model, "model", dict_create())
  24. // Return the created model
  25. return new_model!
  26. String function model_add_node(model : Element, name : String):
  27. // Adds a new node to the specified model with the desired name
  28. // This is a bottom operation, as it doesn't take any type
  29. Element new_node
  30. String actual_name
  31. new_node = create_node()
  32. actual_name = instantiated_name(new_node, name)
  33. dict_add_fast(model["model"], actual_name, new_node)
  34. return actual_name!
  35. String function model_add_value(model : Element, name : String, value : Element):
  36. // Similar to model_add_node, but add a value as well
  37. String actual_name
  38. actual_name = instantiated_name(value, name)
  39. dict_add_fast(model["model"], actual_name, value)
  40. return actual_name!
  41. String function model_add_edge(model : Element, name : String, source : String, destination : String):
  42. // Add an edge between the source and destination nodes
  43. // Nodes are specified using their string representation previously defined
  44. Element new_edge
  45. String actual_name
  46. if (bool_not(dict_in(model["model"], source))):
  47. log("In link " + name)
  48. log("ERROR: source of link unknown: " + source)
  49. log("Destination: " + destination)
  50. return ""!
  51. if (bool_not(dict_in(model["model"], destination))):
  52. log("In link " + name)
  53. log("ERROR: destination of link unknown: " + destination)
  54. log("Source: " + source)
  55. return ""!
  56. new_edge = create_edge(model["model"][source], model["model"][destination])
  57. actual_name = instantiated_name(new_edge, name)
  58. dict_add_fast(model["model"], actual_name, new_edge)
  59. return actual_name!
  60. Void function retype_model(model : Element, metamodel : Element):
  61. // Remove the type mapping and add a new one for the specified metamodel
  62. new_type_mapping(model)
  63. dict_add_fast(model, "metamodel", metamodel)
  64. return!
  65. Element function instantiate_model(metamodel : Element):
  66. // Instantiate a model
  67. // Basically create an untyped model and retype it
  68. Element model
  69. model = instantiate_bottom()
  70. retype_model(model, metamodel)
  71. return model!
  72. String function reuse_element(model : Element, type_name : String, instance_name : String, element : Element):
  73. String actual_name
  74. if (bool_not(dict_in(model["metamodel"]["model"], type_name))):
  75. log("ERROR: (instantiate_node) no such type in metamodel: " + type_name)
  76. log(" for " + instance_name)
  77. return ""!
  78. actual_name = instantiated_name(element, instance_name)
  79. dict_add(model["model"], actual_name, element)
  80. retype(model, actual_name, type_name)
  81. return actual_name!
  82. String function instantiate_node(model : Element, type_name : String, instance_name : String):
  83. String actual_name
  84. if (bool_not(dict_in(model["metamodel"]["model"], type_name))):
  85. log("ERROR: (instantiate_node) no such type in metamodel: " + type_name)
  86. log(" for " + instance_name)
  87. return ""!
  88. Element value
  89. value = create_node()
  90. actual_name = instantiated_name(value, instance_name)
  91. dict_add_fast(model["model"], actual_name, value)
  92. retype(model, actual_name, type_name)
  93. return actual_name!
  94. String function instantiate_value(model : Element, type_name : String, instance_name : String, value : Element):
  95. String actual_name
  96. if (bool_not(dict_in(model["metamodel"]["model"], type_name))):
  97. log("ERROR: (instantiate_value) no such type in metamodel: " + type_name)
  98. log(" for " + instance_name)
  99. return ""!
  100. actual_name = instantiated_name(value, instance_name)
  101. dict_add_fast(model["model"], actual_name, value)
  102. retype(model, actual_name, type_name)
  103. return actual_name!
  104. String function find_attribute_type(model : Element, elem : String, name : String):
  105. String mm_elem
  106. String direct_type
  107. String result
  108. direct_type = read_type(model, elem)
  109. if (direct_type == ""):
  110. return ""!
  111. mm_elem = find_attribute_definer(model["metamodel"], direct_type, name)
  112. if (mm_elem == ""):
  113. // Couldn't find element, so is not allowed!
  114. return ""!
  115. else:
  116. result = reverseKeyLookup(model["metamodel"]["model"], dict_read_edge(model["metamodel"]["model"][mm_elem], name))
  117. return result!
  118. Element function get_subclasses(model : Element, name : String):
  119. Element result
  120. Integer i
  121. Integer j
  122. Integer num_edges
  123. Element edge
  124. String elem
  125. Element nodes
  126. nodes = set_create()
  127. set_add(nodes, name)
  128. // Initialize empty set
  129. result = set_create()
  130. while (set_len(nodes) > 0):
  131. elem = set_pop(nodes)
  132. if (bool_not(set_in(result, elem))):
  133. set_add(result, elem)
  134. // Read out all incoming edges
  135. num_edges = read_nr_in(model["model"][elem])
  136. j = 0
  137. while (j < num_edges):
  138. edge = read_in(model["model"][elem], j)
  139. if (dict_in(model["model"], reverseKeyLookup(model["model"], edge))):
  140. if (read_type(model, reverseKeyLookup(model["model"], edge)) == "Inheritance"):
  141. set_add(nodes, reverseKeyLookup(model["model"], read_edge_src(edge)))
  142. j = j + 1
  143. return result!
  144. Element function get_superclasses(model : Element, name : String):
  145. Element result
  146. Integer j
  147. Integer num_edges
  148. Element edge
  149. String elem
  150. Element nodes
  151. String edge_name
  152. nodes = set_create()
  153. set_add(nodes, name)
  154. // Initialize empty set
  155. result = set_create()
  156. while (set_len(nodes) > 0):
  157. elem = set_pop(nodes)
  158. if (bool_not(set_in(result, elem))):
  159. set_add(result, elem)
  160. // Read out all outgoing edges
  161. num_edges = read_nr_out(model["model"][elem])
  162. j = 0
  163. while (j < num_edges):
  164. edge = read_out(model["model"][elem], j)
  165. edge_name = reverseKeyLookup(model["model"], edge)
  166. if (edge_name != ""):
  167. if (read_type(model, edge_name) == "Inheritance"):
  168. set_add(nodes, reverseKeyLookup(model["model"], read_edge_dst(edge)))
  169. j = j + 1
  170. return result!
  171. String function find_attribute_definer(model : Element, elem_name : String, name : String):
  172. Element superclasses
  173. String current
  174. Integer nr_out
  175. Element out
  176. String name_attr
  177. String elem
  178. superclasses = get_superclasses(model, elem_name)
  179. while (set_len(superclasses) > 0):
  180. current = set_pop(superclasses)
  181. // TODO is it possible to use allOutgoingAssociationInstances here?
  182. nr_out = read_nr_out(model["model"][current])
  183. while (nr_out > 0):
  184. nr_out = nr_out - 1
  185. out = read_out(model["model"][current], nr_out)
  186. elem = reverseKeyLookup(model["model"], out)
  187. if (elem != ""):
  188. name_attr = read_attribute(model, elem, "name")
  189. if (name_attr == name):
  190. return current!
  191. return ""!
  192. Void function instantiate_attribute(model : Element, element : String, attribute_name : String, value : Element):
  193. // Instantiate an attribute of something that needs to be instantiated
  194. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  195. String attr_type
  196. String attr_name
  197. if (element_neq(read_attribute(model, element, attribute_name), read_root())):
  198. unset_attribute(model, element, attribute_name)
  199. attr_type = find_attribute_type(model, element, attribute_name)
  200. if (attr_type == ""):
  201. return!
  202. if (has_value(value)):
  203. value = create_value(value)
  204. attr_name = model_add_value(model, (element + ".") + attribute_name, value)
  205. retype(model, attr_name, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  206. instantiate_link(model, attr_type, "", element, attr_name)
  207. return!
  208. Void function instantiate_attribute_ref(model : Element, element : String, attribute_name : String, ref : String):
  209. // Instantiate an attribute of something that needs to be instantiated
  210. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  211. String attr_type
  212. String attr_name
  213. attr_type = find_attribute_type(model, element, attribute_name)
  214. if (attr_type == ""):
  215. log("Could not find attribute " + cast_value(attribute_name))
  216. return!
  217. instantiate_link(model, attr_type, "", element, ref)
  218. return!
  219. Void function instantiate_attribute_code(model : Element, element : String, attribute_name : String, code : Element):
  220. // First create a new model for the AL part
  221. String location
  222. location = "code/" + cast_id(code)
  223. export_node(location, code)
  224. // Now link it with a complex attribute
  225. instantiate_attribute(model, element, attribute_name, location)
  226. return!
  227. Void function instantiate_existing_attribute(model : Element, element : String, attribute_name : String, attr_ref : String):
  228. // Instantiate an attribute of something that needs to be instantiated
  229. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  230. String attr_type
  231. String attr_name
  232. attr_type = find_attribute_type(model, element, attribute_name)
  233. if (attr_type == ""):
  234. log("Could not find attribute " + cast_value(attribute_name))
  235. return!
  236. // Make a copy of the value, as it is likely that this value is reused later on
  237. retype(model, attr_ref, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  238. instantiate_link(model, attr_type, "", element, attr_ref)
  239. return!
  240. String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String):
  241. // Create a typed link between two nodes
  242. if (type == ""):
  243. // Have to find the type ourselves, as it isn't defined
  244. Element out
  245. Element in
  246. Element options
  247. options = allowedAssociationsBetween(model, source, destination)
  248. if (set_len(options) == 1):
  249. type = set_pop(options)
  250. elif (set_len(options) == 0):
  251. log("ERROR: cannot find possible link between entries")
  252. log(" for " + source)
  253. log(" to " + destination)
  254. return ""!
  255. else:
  256. log("ERROR: too many possible links between entries")
  257. log(" options: " + set_to_string(options))
  258. return ""!
  259. if (bool_not(dict_in(model["model"], source))):
  260. log("ERROR: source of link undefined: " + source)
  261. return ""!
  262. if (bool_not(dict_in(model["model"], destination))):
  263. log("ERROR: destination of link undefined: " + destination)
  264. return ""!
  265. if (bool_not(dict_in(model["metamodel"]["model"], type))):
  266. log("ERROR: (instantiate_link) no such type in metamodel: " + type)
  267. log(" for " + name)
  268. return ""!
  269. return instantiate_typed_link(model, type, name, source, destination)!
  270. String function instantiate_typed_link(model : Element, type : String, name : String, source : String, destination : String):
  271. Element v
  272. String actual_name
  273. v = create_edge(model["model"][source], model["model"][destination])
  274. actual_name = instantiated_name(v, name)
  275. dict_add_fast(model["model"], actual_name, v)
  276. retype(model, actual_name, type)
  277. return actual_name!
  278. Void function model_delete_element(model : Element, name : String):
  279. // Delete all attributes
  280. //Element attrs
  281. //String attr
  282. //attrs = dict_keys(getAttributeList(model, name))
  283. //while (set_len(attrs) > 0):
  284. // attr = set_pop(attrs)
  285. // log("Remove attr " + attr)
  286. // attr = reverseKeyLookup(model["model"], read_attribute(model, name, attr))
  287. // remove_type(model, attr)
  288. // delete_element(model["model"][attr])
  289. // Delete element itself
  290. remove_type(model, name)
  291. delete_element(model["model"][name])
  292. return!
  293. String function model_undefine_attribute(model : Element, elem : String, attr_name : String):
  294. String attr_edge
  295. attr_edge = reverseKeyLookup(model["model"], dict_read_edge(model["model"][elem], attr_name))
  296. unset_attribute(model, attr_edge, "name")
  297. unset_attribute(model, attr_edge, "optional")
  298. model_delete_element(model, attr_edge)
  299. return attr_edge!
  300. String function model_define_attribute(model : Element, elem : String, name : String, optional : Boolean, type : String):
  301. // Create the necessary links to make it an attribute
  302. String edge_name
  303. edge_name = (elem + "_") + name
  304. while (dict_in(model["model"], edge_name)):
  305. // Already exists, so make random name
  306. edge_name = edge_name + cast_id(model["model"][elem])
  307. log("Name clash detected for attribute: try new name: " + edge_name)
  308. edge_name = instantiate_link(model, "AttributeLink", edge_name, elem, type)
  309. instantiate_attribute(model, edge_name, "name", name)
  310. instantiate_attribute(model, edge_name, "optional", optional)
  311. return edge_name!
  312. String function model_define_attribute_ID(model : Element, elem : String, name : String, optional : Boolean, type : String, ID : String):
  313. // Create the necessary links to make it an attribute
  314. String edge_name
  315. edge_name = ID
  316. while (dict_in(model["model"], edge_name)):
  317. // Already exists, so make random name
  318. edge_name = edge_name + cast_id(model["model"][elem])
  319. log("Name clash detected for attribute: try new name: " + edge_name)
  320. edge_name = instantiate_link(model, "AttributeLink", edge_name, elem, type)
  321. instantiate_attribute(model, edge_name, "name", name)
  322. instantiate_attribute(model, edge_name, "optional", optional)
  323. return edge_name!
  324. Element function read_attribute(model : Element, element : String, attribute : String):
  325. if (dict_in(model["model"], element)):
  326. Integer i
  327. Integer count
  328. Element edge
  329. Element edge_type
  330. Element elem
  331. String name
  332. Element model_dict
  333. Element mm_dict
  334. model_dict = model["model"]
  335. mm_dict = model["metamodel"]["model"]
  336. elem = model_dict[element]
  337. count = read_nr_out(elem)
  338. i = 0
  339. while (i < count):
  340. edge = read_out(elem, i)
  341. name = reverseKeyLookup(model_dict, edge)
  342. if (name != ""):
  343. edge_type = mm_dict[read_type(model, name)]
  344. if (element_eq(edge_type, dict_read_edge(read_edge_src(edge_type), attribute))):
  345. return read_edge_dst(edge)!
  346. i = i + 1
  347. else:
  348. log("Element does not exist: " + element)
  349. // Not found: either element doesn't exist, or we couldn't find it
  350. return read_root()!
  351. Void function unset_attribute(model : Element, element : String, attribute : String):
  352. // Removes an attribute if it exists
  353. String attr_type
  354. Element attr_links
  355. String attr_link
  356. String attr_value
  357. attr_type = find_attribute_type(model, element, attribute)
  358. attr_links = allOutgoingAssociationInstances(model, element, attr_type)
  359. while (set_len(attr_links) > 0):
  360. attr_link = set_pop(attr_links)
  361. attr_value = readAssociationDestination(model, attr_link)
  362. remove_type(model, attr_link)
  363. if (dict_in(model["model"], attr_value)):
  364. remove_type(model, attr_value)
  365. dict_delete(model["model"], attr_value)
  366. delete_element(model["model"][attr_link])
  367. return!
  368. Element function get_func_AL_model(al_model : Element):
  369. Element initial_function
  370. // Find the initial function
  371. initial_function = allInstances(al_model, "Initial")
  372. if (set_len(initial_function) == 0):
  373. log("Could not find function to execute in this model!")
  374. return read_root()!
  375. elif (set_len(initial_function) > 1):
  376. log("Too many functions to execute in this model!")
  377. return read_root()!
  378. else:
  379. return al_model["model"][set_pop(allAssociationDestinations(al_model, set_pop(initial_function), "initial_funcdef"))]!
  380. Element function trim_AL_constructors(list : Element):
  381. Integer length
  382. Element lst
  383. Integer i
  384. length = list_pop_final(list)
  385. i = 0
  386. lst = list_create()
  387. while (i < length):
  388. list_append(lst, list_pop_final(list))
  389. i = i + 1
  390. return lst!
  391. Element function add_text(elem : Element, code : String):
  392. dict_add(elem, "__text", code)
  393. return elem!
  394. Element function construct_model_list(model : Element, list : Element):
  395. String command
  396. list = list_reverse(list)
  397. while (list_len(list) > 0):
  398. command = list_pop_final(list)
  399. if (command == "add_node"):
  400. model_add_node(model, list_pop_final(list))
  401. elif (command == "add_value"):
  402. model_add_value(model, list_pop_final(list), list_pop_final(list))
  403. elif (command == "add_edge"):
  404. model_add_edge(model, list_pop_final(list), list_pop_final(list), list_pop_final(list))
  405. elif (command == "instantiate_node"):
  406. instantiate_node(model, list_pop_final(list), list_pop_final(list))
  407. elif (command == "model_define_attribute"):
  408. model_define_attribute(model, list_pop_final(list), list_pop_final(list), list_pop_final(list), list_pop_final(list))
  409. elif (command == "instantiate_attribute"):
  410. instantiate_attribute(model, list_pop_final(list), list_pop_final(list), list_pop_final(list))
  411. elif (command == "instantiate_attribute_ref"):
  412. instantiate_attribute_ref(model, list_pop_final(list), list_pop_final(list), list_pop_final(list))
  413. elif (command == "instantiate_attribute_code"):
  414. instantiate_attribute_code(model, list_pop_final(list), list_pop_final(list), add_text(construct_function_list(trim_AL_constructors(list)), list_pop_final(list)))
  415. elif (command == "instantiate_link"):
  416. instantiate_link(model, list_pop_final(list), list_pop_final(list), list_pop_final(list), list_pop_final(list))
  417. else:
  418. log("Modelling error: did not understand command " + command)
  419. return model!