modelling.alc 18 KB

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