typing.alc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. Float start
  10. start = time()
  11. Element dict
  12. Element keys
  13. String key
  14. dict = dict_create()
  15. keys = dict_keys(model["type_mapping"])
  16. Element rev_model
  17. Element rev_metamodel
  18. rev_model = make_reverse_dictionary(model["model"])
  19. rev_metamodel = make_reverse_dictionary(model["metamodel"]["model"])
  20. while (set_len(keys) > 0):
  21. key = set_pop(keys)
  22. if (bool_not(bool_or(dict_in_node(rev_model, model["type_mapping"][key]), dict_in_node(rev_metamodel, model["type_mapping"][key])))):
  23. // Element is in neither model or metamodel
  24. // Must be a typing link!
  25. // So add it
  26. 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]))])
  27. log("get_type_mapping_as_dict : 0.0 : " + cast_v2s(time() - start))
  28. return dict!
  29. String function read_type(model : Element, name : String):
  30. Float start
  31. start = time()
  32. Element m_element
  33. Element link
  34. Integer nr
  35. Element mm_element
  36. Element result
  37. if (dict_in(model["model"], name)):
  38. m_element = model["model"][name]
  39. nr = read_nr_out(m_element) - 1
  40. while (nr >= 0):
  41. link = read_out(m_element, nr)
  42. if (dict_in(model["type_mapping"], cast_id2s(link))):
  43. // Link is in our mapping, so we probably found it...
  44. // But ONLY if it is NOT in the model itself
  45. if (reverseKeyLookup(model["model"], link) == ""):
  46. // It is not, so we actually got it!
  47. // Now follow the edge to the type
  48. mm_element = read_edge_dst(link)
  49. result = reverseKeyLookup(model["metamodel"]["model"], mm_element)
  50. log("read_type : 0.0 : " + cast_v2s(time() - start))
  51. return result!
  52. nr = nr - 1
  53. // Nothing found, so it must not be typed at all
  54. log("read_type : 0.0 : " + cast_v2s(time() - start))
  55. return ""!
  56. Void function retype(model : Element, element : String, type : String):
  57. // Remove previous type
  58. Float start
  59. start = time()
  60. remove_type(model, element)
  61. // Add element of the model
  62. dict_add_fast(model["type_mapping"], cast_id2s(model["model"][element]), model["model"][element])
  63. // Add element of metamodel if not yet there
  64. Element type_entry
  65. type_entry = model["metamodel"]["model"][type]
  66. if (bool_not(dict_in(model["type_mapping"], cast_id2s(type_entry)))):
  67. dict_add_fast(model["type_mapping"], cast_id2s(type_entry), type_entry)
  68. // Draw a link between them: the typing link
  69. Element new_edge
  70. new_edge = create_edge(model["model"][element], type_entry)
  71. dict_add_fast(model["type_mapping"], cast_id2s(new_edge), new_edge)
  72. log("retype : 0.0 : " + cast_v2s(time() - start))
  73. return!
  74. Void function new_type_mapping(model : Element):
  75. if (dict_in(model, "type_mapping")):
  76. dict_delete(model, "type_mapping")
  77. dict_add_fast(model, "type_mapping", dict_create())
  78. return !
  79. Void function remove_type(model : Element, name : String):
  80. Float start
  81. start = time()
  82. String elem
  83. elem = cast_id2s(model["model"][name])
  84. if (dict_in(model["type_mapping"], elem)):
  85. dict_delete(model["type_mapping"], elem)
  86. log("remove_type : 0.0 : " + cast_v2s(time() - start))
  87. return !
  88. Element function elements_typed_by(model : Element, type_name : String):
  89. Float start
  90. Element result
  91. start = time()
  92. result = reverseKeyLookupMulti(get_type_mapping_as_dict(model), type_name)
  93. log("elements_typed_by : 0.0 : " + cast_v2s(time() - start))
  94. return result!