object_operations.alc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 findAttribute(source : Element, attr_name : Element, types : Element, inheritance_link : Element):
  52. if (dict_in(source, attr_name)):
  53. return dict_read_edge(source, attr_name)
  54. else:
  55. Integer counter
  56. Integer i
  57. Element edge
  58. counter = read_nr_out(source)
  59. i = 0
  60. while (i < counter):
  61. edge = read_out(source, i)
  62. if (element_eq(dict_read_node(types, edge), inheritance_link)):
  63. return findAttribute(read_edge_dst(edge), attr_name, types, inheritance_link)
  64. i = i + 1
  65. // No return at the moment, as this crashes the MvK
  66. log("ERROR: could not find attribute")
  67. Element function readAttribute(model : Element, source : Element, attr_name : String):
  68. // Read out the edge we are talking about
  69. Element edge
  70. edge = findAttribute(dict_read_node(model["type_mapping"], source), attr_name, model["type_mapping"], model["inheritance"])
  71. return read_edge_dst(set_pop(allOutgoingAssociationInstances(model, source, edge)))
  72. Element function deleteAttribute(model : Element, source : Element, attr_name : Element):
  73. Element edge
  74. edge = findAttribute(dict_read_node(model["type_mapping"], source), attr_name, model["type_mapping"], model["inheritance"])
  75. Element found_elements
  76. found_elements = allOutgoingAssociationInstances(model, source, edge)
  77. Element rm_element
  78. if (list_len(found_elements) > 0):
  79. rm_element = read_edge_dst(set_pop(found_elements))
  80. dict_delete(model["type_mapping"], rm_element)
  81. delete_element(rm_element)
  82. return edge
  83. Element function getAttributeList(model : Element, element : String):
  84. Element result
  85. Element keys
  86. Element type
  87. Element attr_name
  88. String attr_type
  89. result = create_node()
  90. type = dict_read_node(model["type_mapping"], model["model"][element])
  91. keys = dict_keys(type)
  92. // Add our own attributes
  93. while (0 < list_len(keys)):
  94. attr_name = set_pop(keys)
  95. log("Test for " + cast_e2s(attr_name))
  96. if (is_physical_string(attr_name)):
  97. log("Found attribute " + cast_v2s(attr_name))
  98. attr_type = getName(model["metamodel"], 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 : Element):
  104. Element result
  105. result = create_node()
  106. // Get all outgoing "dictionary" links
  107. Element set_own
  108. set_own = dict_keys(element)
  109. // Filter them
  110. Element e
  111. while (0 < read_nr_out(set_own)):
  112. e = set_pop(set_own)
  113. if (is_physical_string(e)):
  114. // This is a primitive type, so add to the list
  115. dict_add(result, e, element[e])
  116. return result
  117. String function getName(m : Element, e : Element):
  118. Element element_keys
  119. Element s
  120. s = m["model"]
  121. element_keys = dict_keys(s)
  122. Element key
  123. while (0 < read_nr_out(element_keys)):
  124. key = set_pop(element_keys)
  125. if (element_eq(dict_read_node(s, key), e)):
  126. return key
  127. return string_join(string_join("(unknown: ", cast_e2s(e)), " )")
  128. String function reverseNameLookup(s : Element, e : Element):
  129. Element element_keys
  130. element_keys = dict_keys(s)
  131. Element key
  132. while (0 < read_nr_out(element_keys)):
  133. key = set_pop(element_keys)
  134. if (element_eq(dict_read_node(s, key), e)):
  135. return key
  136. return string_join(string_join("(unknown: ", cast_e2s(e)), " )")
  137. String function print_dict(dict : Element):
  138. Element keys
  139. Element key
  140. String result
  141. keys = dict_keys(dict)
  142. result = ""
  143. while (0 < list_len(keys)):
  144. key = set_pop(keys)
  145. result = result + cast_v2s(key)
  146. result = result + ": "
  147. result = result + cast_v2s(dict[key])
  148. result = result + "\n"
  149. return result