typing.alc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. include "primitives.alh"
  2. include "utils.alh"
  3. Element function get_type_mapping(model : Element):
  4. // Deserialize dictionary as model
  5. return model["type_mapping"]!
  6. Void function set_type_mapping(model : Element, type_mapping_model : Element):
  7. // Serialize model to dictionary
  8. dict_overwrite(model, "type_mapping", type_mapping_model)
  9. return!
  10. Element function elements_typed_by(model : Element, type_name : String):
  11. Element result
  12. Element temp_result
  13. String temp
  14. Element m_model
  15. m_model = model["model"]
  16. result = set_create()
  17. temp_result = reverseKeyLookupMultiValue(get_type_mapping_as_dict(model), type_name)
  18. while (set_len(temp_result) > 0):
  19. temp = set_pop(temp_result)
  20. if (dict_in(m_model, temp)):
  21. set_add(result, temp)
  22. return result!
  23. Element function get_type_mapping_as_dict(model : Element):
  24. return model["type_mapping"]["root"]!
  25. String function read_type(model : Element, name : String):
  26. String result
  27. Element tm
  28. if (dict_in(model["model"], name)):
  29. if (dict_in(model["type_mapping"]["root"], name)):
  30. result = model["type_mapping"]["root"][name]
  31. if (dict_in(model["metamodel"]["model"], result)):
  32. return result!
  33. else:
  34. return ""!
  35. else:
  36. return ""!
  37. else:
  38. return ""!
  39. Void function retype(model : Element, element : String, type : String):
  40. // Retype a model, deleting any previous type the element had
  41. Element tm
  42. tm = model["type_mapping"]
  43. remove_type(model, element)
  44. // Create new elements
  45. Element type_link
  46. Element type_elem
  47. Element instance_link
  48. Element instance_elem
  49. type_elem = create_value(type)
  50. instance_elem = create_value(element)
  51. type_link = create_edge(tm["root"], type_elem)
  52. instance_link = create_edge(type_link, instance_elem)
  53. dict_add(tm, cast_id(type_elem), type_elem)
  54. dict_add(tm, cast_id(instance_elem), instance_elem)
  55. dict_add(tm, cast_id(type_link), type_link)
  56. dict_add(tm, cast_id(instance_link), instance_link)
  57. return!
  58. Void function new_type_mapping(model : Element):
  59. dict_overwrite(model, "type_mapping", dict_create())
  60. dict_add(model["type_mapping"], "root", create_node())
  61. return !
  62. Void function remove_type(model : Element, name : String):
  63. Element value
  64. //value = dict_read(model["type_mapping"]["root"], name)
  65. value = dict_read_edge(model["type_mapping"]["root"], name)
  66. if (element_neq(value, read_root())):
  67. // TODO why is this a reverseKeyLookup? This means that it will not be removed, as it resolves to a constant...
  68. //delete_element(reverseKeyLookup(model["type_mapping"], value))
  69. delete_element(value)
  70. return !