modelling.alc 21 KB

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