modelling.alc 17 KB

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