modelling.alc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. include "typing.alh"
  8. include "utils.alh"
  9. Element global_models = ?
  10. String function instantiated_name(element : Element, original : String):
  11. if (original == ""):
  12. return "__" + cast_id2s(element)!
  13. else:
  14. return original!
  15. Element function instantiate_bottom():
  16. // Just create a new node that serves as the basis for everything
  17. // We don't know anything about the model yet, so just create an empty one
  18. Element new_model
  19. // The actual root node of the model
  20. new_model = dict_create()
  21. // Add an empty model and empty type mapping
  22. dict_add_fast(new_model, "model", dict_create())
  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_fast(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_fast(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_fast(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. if (dict_in(model, "type_mapping")):
  62. dict_delete(model, "type_mapping")
  63. dict_add_fast(model, "type_mapping", new_type_mapping())
  64. dict_add_fast(model, "metamodel", metamodel)
  65. return!
  66. Element function instantiate_model(metamodel : Element):
  67. // Instantiate a model
  68. // Basically create an untyped model and retype it
  69. Element model
  70. model = instantiate_bottom()
  71. retype_model(model, metamodel)
  72. return model!
  73. String function reuse_element(model : Element, type_name : String, instance_name : String, element : Element):
  74. String actual_name
  75. if (bool_not(dict_in(model["metamodel"]["model"], type_name))):
  76. log("ERROR: (instantiate_node) no such type in metamodel: " + type_name)
  77. log(" for " + instance_name)
  78. return ""!
  79. actual_name = instantiated_name(element, instance_name)
  80. dict_add(model["model"], actual_name, element)
  81. retype(model, actual_name, type_name)
  82. return actual_name!
  83. String function instantiate_node(model : Element, type_name : String, instance_name : String):
  84. String actual_name
  85. if (bool_not(dict_in(model["metamodel"]["model"], type_name))):
  86. log("ERROR: (instantiate_node) no such type in metamodel: " + type_name)
  87. log(" for " + instance_name)
  88. return ""!
  89. Element value
  90. value = create_node()
  91. actual_name = instantiated_name(value, instance_name)
  92. dict_add_fast(model["model"], actual_name, value)
  93. retype(model, actual_name, type_name)
  94. return actual_name!
  95. String function instantiate_value(model : Element, type_name : String, instance_name : String, value : Element):
  96. String actual_name
  97. if (bool_not(dict_in(model["metamodel"]["model"], type_name))):
  98. log("ERROR: (instantiate_value) no such type in metamodel: " + type_name)
  99. log(" for " + instance_name)
  100. return ""!
  101. actual_name = instantiated_name(value, instance_name)
  102. dict_add_fast(model["model"], actual_name, value)
  103. retype(model, actual_name, type_name)
  104. return actual_name!
  105. String function find_attribute_type(model : Element, elem : String, name : String):
  106. String mm_elem
  107. String direct_type
  108. String result
  109. direct_type = read_type(model, elem)
  110. if (direct_type == ""):
  111. return ""!
  112. mm_elem = find_attribute_definer(model["metamodel"], direct_type, name)
  113. if (value_eq(mm_elem, "")):
  114. // Couldn't find element, so is not allowed!
  115. return ""!
  116. else:
  117. result = reverseKeyLookup(model["metamodel"]["model"], dict_read_edge(model["metamodel"]["model"][mm_elem], name))
  118. return result!
  119. Element function get_subclasses(model : Element, name : String):
  120. Element result
  121. Integer i
  122. Integer j
  123. Integer num_edges
  124. Element edge
  125. String elem
  126. Element nodes
  127. nodes = set_create()
  128. set_add(nodes, name)
  129. // Initialize empty set
  130. result = set_create()
  131. while (set_len(nodes) > 0):
  132. elem = set_pop(nodes)
  133. if (bool_not(set_in(result, elem))):
  134. set_add(result, elem)
  135. // Read out all incoming edges
  136. num_edges = read_nr_in(model["model"][elem])
  137. j = 0
  138. while (j < num_edges):
  139. edge = read_in(model["model"][elem], j)
  140. if (dict_in(model["model"], reverseKeyLookup(model["model"], edge))):
  141. if (read_type(model, reverseKeyLookup(model["model"], edge)) == "Inheritance"):
  142. set_add(nodes, reverseKeyLookup(model["model"], read_edge_src(edge)))
  143. j = j + 1
  144. return result!
  145. Element function get_superclasses(model : Element, name : String):
  146. Element result
  147. Integer j
  148. Integer num_edges
  149. Element edge
  150. String elem
  151. Element nodes
  152. nodes = set_create()
  153. set_add(nodes, name)
  154. // Initialize empty set
  155. result = set_create()
  156. while (set_len(nodes) > 0):
  157. elem = set_pop(nodes)
  158. if (bool_not(set_in(result, elem))):
  159. set_add(result, elem)
  160. // Read out all outgoing edges
  161. num_edges = read_nr_out(model["model"][elem])
  162. j = 0
  163. while (j < num_edges):
  164. edge = read_out(model["model"][elem], j)
  165. if (dict_in(model["model"], reverseKeyLookup(model["model"], edge))):
  166. if (read_type(model, reverseKeyLookup(model["model"], edge)) == "Inheritance"):
  167. set_add(nodes, reverseKeyLookup(model["model"], read_edge_dst(edge)))
  168. j = j + 1
  169. return result!
  170. String function find_attribute_definer(model : Element, elem_name : String, name : String):
  171. Element superclasses
  172. String current
  173. Integer nr_out
  174. Element out
  175. String name_attr
  176. superclasses = get_superclasses(model, elem_name)
  177. while (set_len(superclasses) > 0):
  178. current = set_pop(superclasses)
  179. nr_out = read_nr_out(model["model"][current])
  180. while (nr_out > 0):
  181. nr_out = nr_out - 1
  182. out = read_out(model["model"][current], nr_out)
  183. name_attr = read_attribute(model, reverseKeyLookup(model["model"], out), "name")
  184. if (name_attr == name):
  185. return current!
  186. return ""!
  187. Void function instantiate_attribute(model : Element, element : String, attribute_name : String, value : Element):
  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. if (element_neq(read_attribute(model, element, attribute_name), read_root())):
  193. unset_attribute(model, element, attribute_name)
  194. attr_type = find_attribute_type(model, element, attribute_name)
  195. if (attr_type == ""):
  196. log("Could not find attribute " + cast_v2s(attribute_name))
  197. log("For element " + element)
  198. log("Type: " + read_type(model, element))
  199. return!
  200. if (has_value(value)):
  201. value = create_value(value)
  202. attr_name = model_add_value(model, (element + ".") + attribute_name, value)
  203. retype(model, attr_name, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  204. instantiate_link(model, attr_type, "", element, attr_name)
  205. return!
  206. Void function instantiate_attribute_ref(model : Element, element : String, attribute_name : String, ref : String):
  207. // Instantiate an attribute of something that needs to be instantiated
  208. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  209. String attr_type
  210. String attr_name
  211. attr_type = find_attribute_type(model, element, attribute_name)
  212. if (attr_type == ""):
  213. log("Could not find attribute " + cast_v2s(attribute_name))
  214. return!
  215. instantiate_link(model, attr_type, "", element, ref)
  216. return!
  217. Void function add_code_model(model : Element, export_name : String, code : Element):
  218. Element code_model
  219. code_model = instantiate_model(model)
  220. add_AL(code_model, code)
  221. export_node(export_name, code_model)
  222. return !
  223. Void function instantiate_attribute_code(model : Element, element : String, attribute_name : String, code : Element):
  224. // First create a new model for the AL part
  225. String location
  226. location = "code/" + cast_id2s(code)
  227. add_code_model(import_node("models/ActionLanguage"), location, code)
  228. // Now link it with a complex attribute
  229. instantiate_attribute(model, element, attribute_name, location)
  230. return!
  231. Void function instantiate_existing_attribute(model : Element, element : String, attribute_name : String, attr_ref : String):
  232. // Instantiate an attribute of something that needs to be instantiated
  233. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  234. String attr_type
  235. String attr_name
  236. attr_type = find_attribute_type(model, element, attribute_name)
  237. if (attr_type == ""):
  238. log("Could not find attribute " + cast_v2s(attribute_name))
  239. return!
  240. // Make a copy of the value, as it is likely that this value is reused later on
  241. retype(model, attr_ref, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
  242. instantiate_link(model, attr_type, "", element, attr_ref)
  243. return!
  244. String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String):
  245. // Create a typed link between two nodes
  246. String actual_name
  247. if (type == ""):
  248. // Have to find the type ourselves, as it isn't defined
  249. Element out
  250. Element in
  251. Element options
  252. options = allowedAssociationsBetween(model, source, destination)
  253. if (set_len(options) == 1):
  254. type = set_pop(options)
  255. elif (set_len(options) == 0):
  256. log("ERROR: cannot find possible link between entries")
  257. log(" for " + source)
  258. log(" to " + destination)
  259. return ""!
  260. else:
  261. log("ERROR: too many possible links between entries")
  262. log(" options: " + set_to_string(options))
  263. return ""!
  264. if (bool_not(dict_in(model["model"], source))):
  265. log("ERROR: source of link undefined: " + source)
  266. set_pop(create_node())
  267. return ""!
  268. if (bool_not(dict_in(model["model"], destination))):
  269. log("ERROR: destination of link undefined: " + destination)
  270. set_pop(create_node())
  271. return ""!
  272. if (bool_not(dict_in(model["metamodel"]["model"], type))):
  273. log("ERROR: (instantiate_link) no such type in metamodel: " + type)
  274. log(" for " + name)
  275. return ""!
  276. Element v
  277. v = create_edge(model["model"][source], model["model"][destination])
  278. actual_name = instantiated_name(v, name)
  279. dict_add_fast(model["model"], actual_name, v)
  280. retype(model, actual_name, type)
  281. return actual_name!
  282. Void function model_delete_element(model : Element, name : String):
  283. remove_type(model, name)
  284. delete_element(model["model"][name])
  285. return!
  286. String function model_define_attribute(model : Element, elem : String, name : String, optional : Boolean, type : String):
  287. // Create the necessary links to make it an attribute
  288. String edge_name
  289. edge_name = (elem + "_") + name
  290. while (dict_in(model["model"], edge_name)):
  291. // Already exists, so make random name
  292. edge_name = edge_name + cast_id2s(model["model"][elem])
  293. log("Name clash detected for attribute: try new name: " + edge_name)
  294. edge_name = instantiate_link(model, "AttributeLink", edge_name, elem, type)
  295. instantiate_attribute(model, edge_name, "name", name)
  296. instantiate_attribute(model, edge_name, "optional", optional)
  297. return edge_name!
  298. Element function read_attribute(model : Element, element : String, attribute : String):
  299. if (dict_in(model["model"], element)):
  300. Integer i
  301. Integer count
  302. Element edge
  303. Element edge_type
  304. Element elem
  305. Element name
  306. elem = model["model"][element]
  307. count = read_nr_out(elem)
  308. i = 0
  309. while (i < count):
  310. edge = read_out(elem, i)
  311. name = reverseKeyLookup(model["model"], edge)
  312. edge_type = model["metamodel"]["model"][read_type(model, name)]
  313. if (element_eq(edge_type, dict_read_edge(read_edge_src(edge_type), attribute))):
  314. return read_edge_dst(edge)!
  315. i = i + 1
  316. else:
  317. log("Element does not exist: " + element)
  318. // Not found: either element doesn't exist, or we couldn't find it
  319. return read_root()!
  320. Void function unset_attribute(model : Element, element : String, attribute : String):
  321. // Removes an attribute if it exists
  322. String attr_type
  323. Element attr_links
  324. String attr_link
  325. attr_type = find_attribute_type(model, element, attribute)
  326. attr_links = allOutgoingAssociationInstances(model, element, attr_type)
  327. while (set_len(attr_links) > 0):
  328. attr_link = set_pop(attr_links)
  329. remove_type(model, reverseKeyLookup(model["model"], read_edge_dst(model["model"][attr_link])))
  330. remove_type(model, attr_link)
  331. dict_delete(model["model"], reverseKeyLookup(model["model"], read_edge_dst(model["model"][attr_link])))
  332. delete_element(model["model"][attr_link])
  333. return!
  334. Void function add_AL_links(model : Element, list : Element, element : Element, type: String, linkname : String, expected_type : String):
  335. if (bool_not(dict_in(element, linkname))):
  336. return!
  337. Element link
  338. link = dict_read_edge(element, linkname)
  339. // The link
  340. if (linkname == "next"):
  341. type = "Statement"
  342. reuse_element(model, (type + "_") + linkname, "", link)
  343. // The name link
  344. link = read_out(link, 0)
  345. reuse_element(model, "dict_link_name", "", link)
  346. // The name node
  347. String link_name
  348. link = read_edge_dst(link)
  349. link_name = "__" + cast_id2s(link)
  350. if (bool_not(dict_in(model["model"], link_name))):
  351. reuse_element(model, "StringAttr", link_name, link)
  352. // Now add the destination to the worker list
  353. set_add_node(list, create_tuple(element[linkname], expected_type))
  354. return!
  355. String function add_AL(model : Element, element : Element):
  356. Element todo
  357. Element node
  358. Element work_node
  359. Element elem
  360. String type
  361. String elem_name
  362. todo = set_create()
  363. set_add_node(todo, create_tuple(element, "funcdef"))
  364. while (0 < dict_len(todo)):
  365. work_node = set_pop(todo)
  366. elem = list_read(work_node, 0)
  367. type = list_read(work_node, 1)
  368. elem_name = "__" + cast_id2s(elem)
  369. if (bool_not(set_in(model["model"], elem_name))):
  370. // Determine the type if we don't know it
  371. if (type == ""):
  372. if (is_physical_action(elem)):
  373. type = cast_a2s(elem)
  374. else:
  375. type = "Element"
  376. // Add the node itself
  377. reuse_element(model, type, elem_name, elem)
  378. // Now add its edges
  379. if (type == "if"):
  380. add_AL_links(model, todo, elem, type, "cond", "")
  381. add_AL_links(model, todo, elem, type, "then", "")
  382. add_AL_links(model, todo, elem, type, "else", "")
  383. add_AL_links(model, todo, elem, type, "next", "")
  384. elif (type == "while"):
  385. add_AL_links(model, todo, elem, type, "cond", "")
  386. add_AL_links(model, todo, elem, type, "body", "")
  387. add_AL_links(model, todo, elem, type, "next", "")
  388. elif (type == "assign"):
  389. add_AL_links(model, todo, elem, type, "var", "resolve")
  390. add_AL_links(model, todo, elem, type, "value", "")
  391. add_AL_links(model, todo, elem, type, "next", "")
  392. elif (type == "break"):
  393. add_AL_links(model, todo, elem, type, "while", "while")
  394. elif (type == "continue"):
  395. add_AL_links(model, todo, elem, type, "while", "while")
  396. elif (type == "return"):
  397. add_AL_links(model, todo, elem, type, "value", "")
  398. elif (type == "resolve"):
  399. add_AL_links(model, todo, elem, type, "var", "")
  400. elif (type == "access"):
  401. add_AL_links(model, todo, elem, type, "var", "resolve")
  402. elif (type == "constant"):
  403. add_AL_links(model, todo, elem, type, "node", "")
  404. elif (type == "output"):
  405. add_AL_links(model, todo, elem, type, "node", "")
  406. add_AL_links(model, todo, elem, type, "next", "")
  407. elif (type == "global"):
  408. add_AL_links(model, todo, elem, type, "var", "String")
  409. add_AL_links(model, todo, elem, type, "next", "")
  410. elif (type == "param"):
  411. add_AL_links(model, todo, elem, type, "name", "String")
  412. add_AL_links(model, todo, elem, type, "value", "")
  413. add_AL_links(model, todo, elem, type, "next_param", "param")
  414. elif (type == "funcdef"):
  415. add_AL_links(model, todo, elem, type, "body", "")
  416. // TODO this should be added, but is not the same as "param"
  417. //add_AL_links(model, todo, elem, type, "params", "")
  418. add_AL_links(model, todo, elem, type, "next", "")
  419. elif (type == "call"):
  420. add_AL_links(model, todo, elem, type, "func", "")
  421. add_AL_links(model, todo, elem, type, "params", "param")
  422. add_AL_links(model, todo, elem, type, "last_param", "param")
  423. add_AL_links(model, todo, elem, type, "next", "")
  424. // Mark the node as first
  425. String initial
  426. initial = instantiate_node(model, "Initial", "")
  427. instantiate_link(model, "initial_funcdef", "", initial, reverseKeyLookup(model["model"], element))
  428. return reverseKeyLookup(model["model"], element)!
  429. Element function construct_model_raw(metamodel : Element):
  430. String command
  431. Element model
  432. Integer i
  433. Integer count
  434. Element lst
  435. i = 0
  436. count = input()
  437. lst = list_create()
  438. while (i < count):
  439. list_append(lst, input())
  440. i = i + 1
  441. return construct_model_list(instantiate_model(metamodel), lst)!
  442. Element function get_func_AL_model(al_model : Element):
  443. Element initial_function
  444. // Find the initial function
  445. initial_function = allInstances(al_model, "Initial")
  446. if (set_len(initial_function) == 0):
  447. log("Could not find function to execute in this model!")
  448. return read_root()!
  449. elif (set_len(initial_function) > 1):
  450. log("Too many functions to execute in this model!")
  451. return read_root()!
  452. else:
  453. return al_model["model"][set_pop(allAssociationDestinations(al_model, set_pop(initial_function), "initial_funcdef"))]!
  454. Element function trim_AL_constructors(list : Element):
  455. Integer length
  456. Element lst
  457. Integer i
  458. length = list_pop_final(list)
  459. i = 0
  460. lst = list_create()
  461. while (i < length):
  462. list_append(lst, list_pop_final(list))
  463. i = i + 1
  464. return lst!
  465. Element function construct_model_list(model : Element, list : Element):
  466. String command
  467. list = list_reverse(list)
  468. while (list_len(list) > 0):
  469. command = list_pop_final(list)
  470. if (command == "add_node"):
  471. model_add_node(model, list_pop_final(list))
  472. elif (command == "add_value"):
  473. model_add_value(model, list_pop_final(list), list_pop_final(list))
  474. elif (command == "add_edge"):
  475. model_add_edge(model, list_pop_final(list), list_pop_final(list), list_pop_final(list))
  476. elif (command == "instantiate_node"):
  477. instantiate_node(model, list_pop_final(list), list_pop_final(list))
  478. elif (command == "model_define_attribute"):
  479. model_define_attribute(model, list_pop_final(list), list_pop_final(list), list_pop_final(list), list_pop_final(list))
  480. elif (command == "instantiate_attribute"):
  481. instantiate_attribute(model, list_pop_final(list), list_pop_final(list), list_pop_final(list))
  482. elif (command == "instantiate_attribute_ref"):
  483. instantiate_attribute_ref(model, list_pop_final(list), list_pop_final(list), list_pop_final(list))
  484. elif (command == "instantiate_attribute_code"):
  485. instantiate_attribute_code(model, list_pop_final(list), list_pop_final(list), construct_function_list(trim_AL_constructors(list)))
  486. elif (command == "instantiate_link"):
  487. instantiate_link(model, list_pop_final(list), list_pop_final(list), list_pop_final(list), list_pop_final(list))
  488. elif (command == "add_code_model"):
  489. add_code_model(model, list_pop_final(list), construct_function_list(trim_AL_constructors(list)))
  490. else:
  491. log("Modelling error: did not understand command " + command)
  492. return model!