object_operations.alc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. log("Found instance of " + type_name)
  17. set_add(result, key)
  18. log("Finished searching for instances")
  19. return result
  20. Element function selectPossibleIncoming(model : Element, target : String, limit_set : Element):
  21. // Find all possible incoming link types for the target model
  22. // Should also include those specified on the superclass(es)
  23. String type
  24. Element metamodel_dict
  25. Element elem
  26. Element result
  27. Element target_element
  28. result = create_node()
  29. metamodel_dict = model["metamodel"]["model"]
  30. target_element = model["model"][target]
  31. while (0 < list_len(limit_set)):
  32. type = set_pop(limit_set)
  33. elem = metamodel_dict[type]
  34. if (is_edge(elem)):
  35. if (is_nominal_instance(model, target, reverseKeyLookup(metamodel_dict, read_edge_dst(elem)))):
  36. set_add(result, type)
  37. return result
  38. Element function selectPossibleOutgoing(model : Element, source : String, limit_set : Element):
  39. // Find all possible outgoing link types for the source model
  40. // Should also include those specified on the superclass(es)
  41. String type
  42. Element metamodel_dict
  43. Element elem
  44. Element result
  45. Element source_element
  46. result = create_node()
  47. metamodel_dict = model["metamodel"]["model"]
  48. source_element = model["model"][source]
  49. while (0 < list_len(limit_set)):
  50. type = set_pop(limit_set)
  51. elem = metamodel_dict[type]
  52. if (is_edge(elem)):
  53. if (is_nominal_instance(model, source, reverseKeyLookup(metamodel_dict, read_edge_src(elem)))):
  54. set_add(result, type)
  55. return result
  56. Element function allOutgoingAssociationInstances(model : Element, source_name : String, assoc_name : String):
  57. // Read out all outgoing edges of the model and select those that are typed by the specified association
  58. // TODO for some reason this crashes if allInstances is used!
  59. Element assocs
  60. String assoc
  61. Element result
  62. Element source
  63. assocs = allInstances(model, assoc_name)
  64. source = model["model"][source_name]
  65. result = create_node()
  66. while (0 < list_len(assocs)):
  67. assoc = set_pop(assocs)
  68. if (element_eq(source, read_edge_src(model["model"][assoc]))):
  69. set_add(result, assoc)
  70. return result
  71. Element function allIncomingAssociationInstances(model : Element, target_name : String, assoc_name : String):
  72. // Read out all outgoing edges of the model and select those that are typed by the specified association
  73. Element assocs
  74. String assoc
  75. Element result
  76. Element target
  77. assocs = allInstances(model, assoc_name)
  78. target = model["model"][target_name]
  79. result = create_node()
  80. while (0 < list_len(assocs)):
  81. assoc = set_pop(assocs)
  82. if (element_eq(target, read_edge_dst(model["model"][assoc]))):
  83. set_add(result, assoc)
  84. return result
  85. Element function getAttributeList(model : Element, element : String):
  86. Element result
  87. Element keys
  88. Element type
  89. Element attr_name
  90. String attr_type
  91. result = create_node()
  92. type = dict_read_node(model["type_mapping"], model["model"][element])
  93. keys = dict_keys(type)
  94. // Add our own attributes
  95. while (0 < list_len(keys)):
  96. attr_name = set_pop(keys)
  97. if (is_physical_string(attr_name)):
  98. attr_type = reverseKeyLookup(model["metamodel"]["model"], type[attr_name])
  99. dict_add(result, attr_name, attr_type)
  100. // Go on to the metalevel
  101. // TODO
  102. return result
  103. Element function getInstantiatableAttributes(model : Element, element : String):
  104. Element result
  105. result = create_node()
  106. // Get all outgoing "dictionary" links
  107. Element set_own
  108. Element elem
  109. elem = model["model"][element]
  110. set_own = dict_keys(element)
  111. // Filter them
  112. Element e
  113. while (0 < read_nr_out(set_own)):
  114. e = set_pop(set_own)
  115. if (is_physical_string(e)):
  116. dict_add(result, e, reverseKeyLookup(model["model"], element[e]))
  117. return result
  118. // TODO Utility functions!
  119. String function reverseKeyLookup(dict : Element, element : Element):
  120. return dict_reverse(dict, element)
  121. // TODO this code makes everything very inefficient; wait to enable this for once the compilation is implemented
  122. Element elements
  123. String name
  124. elements = dict_keys(dict)
  125. while (0 < list_len(elements)):
  126. name = set_pop(elements)
  127. if (element_eq(dict[name], element)):
  128. return name
  129. return string_join(string_join("(unknown: ", cast_e2s(element)), " )")
  130. String function print_dict(dict : Element):
  131. Element keys
  132. Element key
  133. String result
  134. keys = dict_keys(dict)
  135. result = ""
  136. while (0 < list_len(keys)):
  137. key = set_pop(keys)
  138. result = result + cast_v2s(key)
  139. result = result + ": "
  140. result = result + cast_v2s(dict[key])
  141. result = result + "\n"
  142. return result