typing.alc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. include "primitives.alh"
  2. include "utils.alh"
  3. Element function get_type_mapping(model : Element):
  4. return model["type_mapping"]!
  5. Void function set_type_mapping(model : Element, type_mapping : Element):
  6. dict_overwrite(model, "type_mapping", type_mapping)
  7. return!
  8. Element function get_type_mapping_as_dict(model : Element):
  9. Element dict
  10. Element keys
  11. String key
  12. dict = dict_create()
  13. keys = dict_keys(model["type_mapping"])
  14. Element rev_model
  15. Element rev_metamodel
  16. rev_model = make_reverse_dictionary(model["model"])
  17. rev_metamodel = make_reverse_dictionary(model["metamodel"]["model"])
  18. while (set_len(keys) > 0):
  19. key = set_pop(keys)
  20. if (bool_not(bool_or(dict_in_node(rev_model, model["type_mapping"][key]), dict_in_node(rev_metamodel, model["type_mapping"][key])))):
  21. // Element is in neither model or metamodel
  22. // Must be a typing link!
  23. // So add it
  24. dict_add_fast(dict, rev_model[cast_id2s(read_edge_src(model["type_mapping"][key]))], rev_metamodel[cast_id2s(read_edge_dst(model["type_mapping"][key]))])
  25. return dict!
  26. String function read_type(model : Element, name : String):
  27. Element m_element
  28. Element link
  29. Integer nr
  30. Element mm_element
  31. if (dict_in(model["model"], name)):
  32. m_element = model["model"][name]
  33. nr = read_nr_out(m_element) - 1
  34. while (nr >= 0):
  35. link = read_out(m_element, nr)
  36. if (dict_in(model["type_mapping"], cast_id2s(link))):
  37. // Link is in our mapping, so we probably found it...
  38. // But ONLY if it is NOT in the model itself
  39. if (reverseKeyLookup(model["model"], link) == ""):
  40. // It is not, so we actually got it!
  41. // Now follow the edge to the type
  42. mm_element = read_edge_dst(link)
  43. return reverseKeyLookup(model["metamodel"]["model"], mm_element)!
  44. nr = nr - 1
  45. // Nothing found, so it must not be typed at all
  46. return ""!
  47. Void function retype(model : Element, element : String, type : String):
  48. // Remove previous type
  49. remove_type(model, element)
  50. // Add element of the model
  51. dict_add_fast(model["type_mapping"], cast_id2s(model["model"][element]), model["model"][element])
  52. // Add element of metamodel if not yet there
  53. Element type_entry
  54. type_entry = model["metamodel"]["model"][type]
  55. if (bool_not(dict_in(model["type_mapping"], cast_id2s(type_entry)))):
  56. dict_add_fast(model["type_mapping"], cast_id2s(type_entry), type_entry)
  57. // Draw a link between them: the typing link
  58. Element new_edge
  59. new_edge = create_edge(model["model"][element], type_entry)
  60. dict_add_fast(model["type_mapping"], cast_id2s(new_edge), new_edge)
  61. return!
  62. Void function new_type_mapping(model : Element):
  63. if (dict_in(model, "type_mapping")):
  64. dict_delete(model, "type_mapping")
  65. dict_add_fast(model, "type_mapping", dict_create())
  66. return !
  67. Void function remove_type(model : Element, name : String):
  68. String elem
  69. elem = cast_id2s(model["model"][name])
  70. if (dict_in(model["type_mapping"], elem)):
  71. dict_delete(model["type_mapping"], elem)
  72. return !