modelling.alc 19 KB

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