object_operations.alc 4.9 KB

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