modelling.alc 19 KB

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