modelling.alc 19 KB

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