modelling.alc 18 KB

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