object_operations.alc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 allPossibleIncoming(model : Element, target : String):
  22. // Find all possible incoming link types for the target model
  23. // Should also include those specified on the superclass(es)
  24. Element all_elems
  25. String type
  26. Element metamodel
  27. Element elem
  28. Element result
  29. result = create_node()
  30. metamodel = model["metamodel"]
  31. all_elems = dict_keys(metamodel["model"])
  32. log("Read all possible incoming edges for " + target)
  33. while (0 < list_len(all_elems)):
  34. type = set_pop(all_elems)
  35. elem = metamodel["model"][type]
  36. if (is_edge(elem)):
  37. if (is_nominal_instance(model, model["model"][target], read_edge_src(elem))):
  38. set_add(result, elem)
  39. log("Found " + reverseKeyLookup(metamodel["model"], elem))
  40. log(" " + type)
  41. return result
  42. Element function allPossibleOutgoing(model : Element, source : String):
  43. // Find all possible outgoing link types for the source model
  44. // Should also include those specified on the superclass(es)
  45. Element all_elems
  46. String type
  47. Element metamodel
  48. Element elem
  49. Element result
  50. result = create_node()
  51. metamodel = model["metamodel"]
  52. all_elems = dict_keys(model["metamodel"]["model"])
  53. while (0 < list_len(all_elems)):
  54. type = set_pop(all_elems)
  55. elem = metamodel["model"][type]
  56. if (is_edge(elem)):
  57. if (is_nominal_instance(model, model["model"][source], read_edge_dst(elem))):
  58. set_add(result, elem)
  59. return result
  60. Element function allOutgoingAssociationInstances(model : Element, source : Element, assoc : Element):
  61. // Read out all outgoing edges of the model and select those that are typed by the specified association
  62. // TODO for some reason this crashes if allInstances is used!
  63. Integer length
  64. length = read_nr_out(source)
  65. Integer counter
  66. counter = 0
  67. Element result
  68. result = create_node()
  69. Element edge
  70. while (counter < length):
  71. edge = read_out(source, counter)
  72. if (element_eq(dict_read_node(model["type_mapping"], edge), assoc)):
  73. set_add(result, edge)
  74. counter = counter + 1
  75. return result
  76. Element function allIncomingAssociationInstances(model : Element, source : Element, assoc : Element):
  77. // Read out all outgoing edges of the model and select those that are typed by the specified association
  78. Element result
  79. result = create_node()
  80. Element allinsts
  81. allinsts = allInstances(model, assoc)
  82. Element understudy
  83. while (0 < read_nr_out(allinsts)):
  84. understudy = set_pop(allinsts)
  85. if (element_eq(read_edge_dst(understudy), source)):
  86. set_add(result, understudy)
  87. return result
  88. Element function readElementByName(model : Element, name : String):
  89. return model["model"][name]
  90. Element function getAttributeList(model : Element, element : String):
  91. Element result
  92. Element keys
  93. Element type
  94. Element attr_name
  95. String attr_type
  96. result = create_node()
  97. type = dict_read_node(model["type_mapping"], model["model"][element])
  98. keys = dict_keys(type)
  99. // Add our own attributes
  100. while (0 < list_len(keys)):
  101. attr_name = set_pop(keys)
  102. log("Test for " + cast_e2s(attr_name))
  103. if (is_physical_string(attr_name)):
  104. log("Found attribute " + cast_v2s(attr_name))
  105. attr_type = getName(model["metamodel"], type[attr_name])
  106. dict_add(result, attr_name, attr_type)
  107. // Go on to the metalevel
  108. // TODO
  109. return result
  110. Element function getInstantiatableAttributes(model : Element, element : Element):
  111. Element result
  112. result = create_node()
  113. // Get all outgoing "dictionary" links
  114. Element set_own
  115. set_own = dict_keys(element)
  116. // Filter them
  117. Element e
  118. while (0 < read_nr_out(set_own)):
  119. e = set_pop(set_own)
  120. if (is_physical_string(e)):
  121. dict_add(result, e, getName(model, element[e]))
  122. return result
  123. String function getName(m : Element, e : Element):
  124. Element element_keys
  125. Element s
  126. s = m["model"]
  127. element_keys = dict_keys(s)
  128. Element key
  129. while (0 < read_nr_out(element_keys)):
  130. key = set_pop(element_keys)
  131. if (element_eq(dict_read_node(s, key), e)):
  132. return key
  133. return string_join(string_join("(unknown: ", cast_e2s(e)), " )")
  134. String function reverseKeyLookup(dict : Element, element : Element):
  135. Element elements
  136. String name
  137. elements = dict_keys(dict)
  138. while (0 < list_len(elements)):
  139. name = set_pop(elements)
  140. if (element_eq(dict[name], element)):
  141. return name
  142. return string_join(string_join("(unknown: ", cast_e2s(element)), " )")
  143. String function print_dict(dict : Element):
  144. Element keys
  145. Element key
  146. String result
  147. keys = dict_keys(dict)
  148. result = ""
  149. while (0 < list_len(keys)):
  150. key = set_pop(keys)
  151. result = result + cast_v2s(key)
  152. result = result + ": "
  153. result = result + cast_v2s(dict[key])
  154. result = result + "\n"
  155. return result