object_operations.alc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_nominal_subtype(model, target, reverseKeyLookup(model_dict, read_edge_dst(elem)))):
  32. set_add(result, type)
  33. return result
  34. Element function selectPossibleOutgoing(model : Element, source : String, limit_set : Element):
  35. // Find all possible outgoing link types for the source model
  36. // Should also include those specified on the superclass(es)
  37. String type
  38. Element model_dict
  39. Element elem
  40. Element result
  41. Element source_element
  42. result = create_node()
  43. model_dict = model["model"]
  44. while (0 < list_len(limit_set)):
  45. type = set_pop(limit_set)
  46. elem = model_dict[type]
  47. if (is_nominal_subtype(model, source, reverseKeyLookup(model_dict, read_edge_src(elem)))):
  48. set_add(result, type)
  49. return result
  50. Element function allOutgoingAssociationInstances(model : Element, source_name : String, assoc_name : String):
  51. // Read out all outgoing edges of the model and select those that are typed by the specified association
  52. Element assocs
  53. String assoc
  54. Element result
  55. Element source
  56. assocs = allInstances(model, assoc_name)
  57. source = model["model"][source_name]
  58. result = create_node()
  59. while (0 < list_len(assocs)):
  60. assoc = set_pop(assocs)
  61. if (element_eq(source, read_edge_src(model["model"][assoc]))):
  62. set_add(result, assoc)
  63. return result
  64. Element function allIncomingAssociationInstances(model : Element, target_name : String, assoc_name : String):
  65. // Read out all outgoing edges of the model and select those that are typed by the specified association
  66. Element assocs
  67. String assoc
  68. Element result
  69. Element target
  70. assocs = allInstances(model, assoc_name)
  71. target = model["model"][target_name]
  72. result = create_node()
  73. while (0 < list_len(assocs)):
  74. assoc = set_pop(assocs)
  75. if (element_eq(target, read_edge_dst(model["model"][assoc]))):
  76. set_add(result, assoc)
  77. return result
  78. Element function getAttributeList(model : Element, element : String):
  79. Element result
  80. Element keys
  81. Element type
  82. Element attr_name
  83. String attr_type
  84. result = create_node()
  85. type = dict_read_node(model["type_mapping"], model["model"][element])
  86. keys = dict_keys(type)
  87. // Add our own attributes
  88. while (0 < list_len(keys)):
  89. attr_name = set_pop(keys)
  90. if (is_physical_string(attr_name)):
  91. attr_type = reverseKeyLookup(model["metamodel"]["model"], type[attr_name])
  92. dict_add(result, attr_name, attr_type)
  93. // Go on to the metalevel
  94. // TODO
  95. return result
  96. Element function getInstantiatableAttributes(model : Element, element : String):
  97. Element result
  98. result = create_node()
  99. // Get all outgoing "dictionary" links
  100. Element set_own
  101. Element elem
  102. elem = model["model"][element]
  103. set_own = dict_keys(element)
  104. // Filter them
  105. Element e
  106. while (0 < read_nr_out(set_own)):
  107. e = set_pop(set_own)
  108. if (is_physical_string(e)):
  109. dict_add(result, e, reverseKeyLookup(model["model"], element[e]))
  110. return result
  111. String function reverseKeyLookup(dict : Element, element : Element):
  112. Element elements
  113. String name
  114. elements = dict_keys(dict)
  115. while (0 < list_len(elements)):
  116. name = set_pop(elements)
  117. if (element_eq(dict[name], element)):
  118. return name
  119. return string_join(string_join("(unknown: ", cast_e2s(element)), " )")
  120. String function print_dict(dict : Element):
  121. Element keys
  122. Element key
  123. String result
  124. keys = dict_keys(dict)
  125. result = ""
  126. while (0 < list_len(keys)):
  127. key = set_pop(keys)
  128. result = result + cast_v2s(key)
  129. result = result + ": "
  130. result = result + cast_v2s(dict[key])
  131. result = result + "\n"
  132. return result
  133. String function readAssociationSource(model : Element, name : String):
  134. return reverseKeyLookup(model["model"], read_edge_src(model["model"][name]))
  135. String function readAssociationDestination(model : Element, name : String):
  136. return reverseKeyLookup(model["model"], read_edge_dst(model["model"][name]))
  137. String function followAssociation(model : Element, element_name : String, association_name : String):
  138. Element assocs
  139. String assoc
  140. Element result
  141. assocs = allOutgoingAssociationInstances(model, element_name, association_name)
  142. result = create_node()
  143. while (0 < list_len(assocs)):
  144. set_add(result, readAssociationDestination(model, set_pop(assocs)))
  145. return result