modelling.alc 19 KB

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