typing.alc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. include "primitives.alh"
  2. include "utils.alh"
  3. Element function get_type_mapping(model : Element):
  4. // Deserialize dictionary as model
  5. Element tm_model
  6. tm_model = dict_create()
  7. Element m
  8. Element mm
  9. Element edge
  10. Element keys
  11. String key
  12. keys = dict_keys(model["type_mapping"])
  13. while (set_len(keys) > 0):
  14. key = set_pop(keys)
  15. m = model["model"][key]
  16. mm = model["metamodel"]["model"][model["type_mapping"][key]]
  17. edge = create_edge(m, mm)
  18. dict_add_fast(tm_model, cast_id(m), m)
  19. if (bool_not(dict_in(tm_model, cast_id(mm)))):
  20. dict_add_fast(tm_model, cast_id(mm), mm)
  21. dict_add_fast(tm_model, cast_id(edge), edge)
  22. return tm_model!
  23. Void function set_type_mapping(model : Element, type_mapping_model : Element):
  24. // Serialize model to dictionary
  25. Element type_mapping
  26. Element rev_model
  27. Element rev_metamodel
  28. Element keys
  29. String key
  30. type_mapping = dict_create()
  31. keys = dict_keys(type_mapping_model)
  32. rev_model = make_reverse_dictionary(model["model"])
  33. rev_metamodel = make_reverse_dictionary(model["metamodel"]["model"])
  34. while (set_len(keys) > 0):
  35. key = set_pop(keys)
  36. if (is_edge(type_mapping_model[key])):
  37. if (bool_not(bool_or(dict_in(rev_model, cast_id(type_mapping_model[key])), dict_in(rev_metamodel, cast_id(type_mapping_model[key]))))):
  38. // Element is in neither model or metamodel
  39. // Must be a typing link!
  40. // So add it
  41. dict_add_fast(type_mapping, rev_model[cast_id(read_edge_src(type_mapping_model[key]))], rev_metamodel[cast_id(read_edge_dst(type_mapping_model[key]))])
  42. dict_overwrite(model, "type_mapping", type_mapping)
  43. return!
  44. Element function elements_typed_by(model : Element, type_name : String):
  45. Element result
  46. result = reverseKeyLookupMulti(get_type_mapping_as_dict(model), type_name)
  47. return result!
  48. Element function get_type_mapping_as_dict(model : Element):
  49. return model["type_mapping"]!
  50. Element function get_elements_typed_by(model : Element, type : String):
  51. return reverseKeyLookupMulti(model["type_mapping"], type)!
  52. String function read_type(model : Element, name : String):
  53. String result
  54. Element tm
  55. if (dict_in(model["model"], name)):
  56. if (dict_in(model["type_mapping"], name)):
  57. result = model["type_mapping"][name]
  58. if (dict_in(model["metamodel"]["model"], result)):
  59. return result!
  60. else:
  61. return ""!
  62. else:
  63. return ""!
  64. else:
  65. return ""!
  66. Void function retype(model : Element, element : String, type : String):
  67. // Retype a model, deleting any previous type the element had
  68. // The type string is evaluated in the metamodel previously specified
  69. dict_overwrite(model["type_mapping"], element, type)
  70. return!
  71. Void function new_type_mapping(model : Element):
  72. dict_overwrite(model, "type_mapping", dict_create())
  73. return !
  74. Void function remove_type(model : Element, name : String):
  75. if (dict_in(model["type_mapping"], name)):
  76. dict_delete(model["type_mapping"], name)
  77. String elem
  78. elem = cast_id(model["model"][name])
  79. if (dict_in(model["type_mapping"], elem)):
  80. dict_delete(model["type_mapping"], elem)
  81. return !