object_operations.alc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. include "primitives.alh"
  2. include "conformance_scd.alh"
  3. include "constructors.alh"
  4. Element function allInstances(model : Element, type_name : String):
  5. Element type_mapping
  6. Element result
  7. Element type
  8. String key
  9. Element keys
  10. keys = dict_keys(model["model"])
  11. type = model["metamodel"]["model"][type_name]
  12. result = create_node()
  13. while (0 < list_len(keys)):
  14. key = set_pop(keys)
  15. if (is_nominal_instance(model, key, type_name)):
  16. set_add(result, key)
  17. return result
  18. Element function selectPossibleIncoming(model : Element, target : String, limit_set : Element):
  19. // Find all possible incoming link types for the target model
  20. // Should also include those specified on the superclass(es)
  21. String type
  22. Element model_dict
  23. Element elem
  24. Element result
  25. Element target_element
  26. result = create_node()
  27. model_dict = model["model"]
  28. while (0 < list_len(limit_set)):
  29. type = set_pop(limit_set)
  30. elem = model_dict[type]
  31. if (is_edge(elem)):
  32. if (is_nominal_subtype(model, target, reverseKeyLookup(model_dict, read_edge_dst(elem)))):
  33. set_add(result, type)
  34. return result
  35. Element function selectPossibleOutgoing(model : Element, source : String, limit_set : Element):
  36. // Find all possible outgoing link types for the source model
  37. // Should also include those specified on the superclass(es)
  38. String type
  39. Element model_dict
  40. Element elem
  41. Element result
  42. Element source_element
  43. result = create_node()
  44. model_dict = model["model"]
  45. while (0 < list_len(limit_set)):
  46. type = set_pop(limit_set)
  47. elem = model_dict[type]
  48. if (is_edge(elem)):
  49. if (is_nominal_subtype(model, source, reverseKeyLookup(model_dict, read_edge_src(elem)))):
  50. set_add(result, type)
  51. return result
  52. Element function allOutgoingAssociationInstances(model : Element, source_name : String, assoc_name : String):
  53. // Read out all outgoing edges of the model and select those that are typed by the specified association
  54. Element assocs
  55. String assoc
  56. Element result
  57. Element source
  58. assocs = allInstances(model, assoc_name)
  59. source = model["model"][source_name]
  60. result = create_node()
  61. while (0 < list_len(assocs)):
  62. assoc = set_pop(assocs)
  63. if (element_eq(source, read_edge_src(model["model"][assoc]))):
  64. set_add(result, assoc)
  65. return result
  66. Element function allIncomingAssociationInstances(model : Element, target_name : String, assoc_name : String):
  67. // Read out all outgoing edges of the model and select those that are typed by the specified association
  68. Element assocs
  69. String assoc
  70. Element result
  71. Element target
  72. assocs = allInstances(model, assoc_name)
  73. target = model["model"][target_name]
  74. result = create_node()
  75. while (0 < list_len(assocs)):
  76. assoc = set_pop(assocs)
  77. if (element_eq(target, read_edge_dst(model["model"][assoc]))):
  78. set_add(result, assoc)
  79. return result
  80. Element function getAttributeList(model : Element, element : String):
  81. Element result
  82. Element keys
  83. Element type
  84. Element attr_name
  85. String attr_type
  86. result = create_node()
  87. type = dict_read_node(model["type_mapping"], model["model"][element])
  88. keys = dict_keys(type)
  89. // Add our own attributes
  90. while (0 < list_len(keys)):
  91. attr_name = set_pop(keys)
  92. if (is_physical_string(attr_name)):
  93. attr_type = reverseKeyLookup(model["metamodel"]["model"], type[attr_name])
  94. dict_add(result, attr_name, attr_type)
  95. // Go on to the metalevel
  96. // TODO
  97. return result
  98. Element function getInstantiatableAttributes(model : Element, element : String):
  99. Element result
  100. result = create_node()
  101. // Get all outgoing "dictionary" links
  102. Element set_own
  103. Element elem
  104. elem = model["model"][element]
  105. set_own = dict_keys(element)
  106. // Filter them
  107. Element e
  108. while (0 < read_nr_out(set_own)):
  109. e = set_pop(set_own)
  110. if (is_physical_string(e)):
  111. dict_add(result, e, reverseKeyLookup(model["model"], element[e]))
  112. return result
  113. // TODO Utility functions!
  114. String function reverseKeyLookup(dict : Element, element : Element):
  115. Element elements
  116. String name
  117. elements = dict_keys(dict)
  118. while (0 < list_len(elements)):
  119. name = set_pop(elements)
  120. if (element_eq(dict[name], element)):
  121. return name
  122. return string_join(string_join("(unknown: ", cast_e2s(element)), " )")
  123. String function print_dict(dict : Element):
  124. Element keys
  125. Element key
  126. String result
  127. keys = dict_keys(dict)
  128. result = ""
  129. while (0 < list_len(keys)):
  130. key = set_pop(keys)
  131. result = result + cast_v2s(key)
  132. result = result + ": "
  133. result = result + cast_v2s(dict[key])
  134. result = result + "\n"
  135. return result