object_operations.alc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. include "primitives.alh"
  2. include "conformance_scd.alh"
  3. include "constructors.alh"
  4. Element function allInstances(model : Element, type : Element):
  5. Element type_mapping
  6. Element result
  7. type_mapping = model["type_mapping"]
  8. result = create_node()
  9. Integer counter
  10. counter = 0
  11. Integer length
  12. length = read_nr_out(type_mapping)
  13. Element edge
  14. while (counter < length):
  15. edge = read_out(type_mapping, counter)
  16. if (element_eq(read_edge_dst(edge), type)):
  17. // Found an element of the specified type
  18. set_add(result, read_edge_dst(read_out(edge, 0)))
  19. counter = counter + 1
  20. return result
  21. Element function allOutgoingAssociationInstances(model : Element, source : Element, assoc : Element):
  22. // Read out all outgoing edges of the model and select those that are typed by the specified association
  23. // TODO for some reason this crashes if allInstances is used!
  24. Integer length
  25. length = read_nr_out(source)
  26. Integer counter
  27. counter = 0
  28. Element result
  29. result = create_node()
  30. Element edge
  31. while (counter < length):
  32. edge = read_out(source, counter)
  33. if (element_eq(dict_read_node(model["type_mapping"], edge), assoc)):
  34. set_add(result, edge)
  35. counter = counter + 1
  36. return result
  37. Element function allIncomingAssociationInstances(model : Element, source : Element, assoc : Element):
  38. // Read out all outgoing edges of the model and select those that are typed by the specified association
  39. Element result
  40. result = create_node()
  41. Element allinsts
  42. allinsts = allInstances(model, assoc)
  43. Element understudy
  44. while (0 < read_nr_out(allinsts)):
  45. understudy = set_pop(allinsts)
  46. if (element_eq(read_edge_dst(understudy), source)):
  47. set_add(result, understudy)
  48. return result
  49. Element function readElementByName(model : Element, name : String):
  50. return model["model"][name]
  51. Element function get_superclasses(model : Element, elem : Element):
  52. Element result
  53. Integer i
  54. Integer num_edges
  55. Element edge
  56. // Initialize empty set
  57. result = create_node()
  58. i = 0
  59. // Read out all outgoing edges
  60. num_edges = read_nr_out(elem)
  61. //log(cast_i2s(num_edges))
  62. while (i < num_edges):
  63. //log("Search SC")
  64. edge = read_out(elem, i)
  65. //log(cast_e2s(edge))
  66. if (element_eq(dict_read_node(model["type_mapping"], edge), model["inheritance"])):
  67. create_edge(result, read_edge_dst(edge))
  68. i = i + 1
  69. return result
  70. Element function getAttributeList(model : Element, element : String):
  71. Element result
  72. Element keys
  73. Element type
  74. Element attr_name
  75. String attr_type
  76. result = create_node()
  77. type = dict_read_node(model["type_mapping"], model["model"][element])
  78. keys = dict_keys(type)
  79. // Add our own attributes
  80. while (0 < list_len(keys)):
  81. attr_name = set_pop(keys)
  82. if (is_physical_string(attr_name)):
  83. attr_type = getName(model["metamodel"], type[attr_name])
  84. dict_add(result, attr_name, attr_type)
  85. Element superclasses
  86. superclasses = get_superclasses(model["metamodel"], type)
  87. while (0 < list_len(superclasses)):
  88. type = set_pop(superclasses)
  89. keys = dict_keys(type)
  90. while (0 < list_len(keys)):
  91. attr_name = set_pop(keys)
  92. if (is_physical_string(attr_name)):
  93. attr_type = getName(model["metamodel"], 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 : Element):
  99. Element result
  100. result = create_node()
  101. // Get all outgoing "dictionary" links
  102. Element set_own
  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, getName(model, element[e]))
  110. return result
  111. String function getName(m : Element, e : Element):
  112. Element element_keys
  113. Element s
  114. s = m["model"]
  115. element_keys = dict_keys(s)
  116. Element key
  117. while (0 < read_nr_out(element_keys)):
  118. key = set_pop(element_keys)
  119. if (element_eq(dict_read_node(s, key), e)):
  120. return key
  121. return string_join(string_join("(unknown: ", cast_e2s(e)), " )")
  122. String function reverseNameLookup(s : Element, e : Element):
  123. Element element_keys
  124. element_keys = dict_keys(s)
  125. Element key
  126. while (0 < read_nr_out(element_keys)):
  127. key = set_pop(element_keys)
  128. if (element_eq(dict_read_node(s, key), e)):
  129. return key
  130. return string_join(string_join("(unknown: ", cast_e2s(e)), " )")
  131. String function print_dict(dict : Element):
  132. Element keys
  133. Element key
  134. String result
  135. keys = dict_keys(dict)
  136. result = ""
  137. while (0 < list_len(keys)):
  138. key = set_pop(keys)
  139. result = result + cast_v2s(key)
  140. result = result + ": "
  141. result = result + cast_v2s(dict[key])
  142. result = result + "\n"
  143. return result