modelling.alc 20 KB

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