render_OD.alc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. Boolean function main(model : Element):
  5. Element elements
  6. String class
  7. Element attrs
  8. Element attr_keys
  9. String attr_key
  10. String group
  11. String elem
  12. Integer loc_x
  13. Integer loc_y
  14. Integer text_loc
  15. loc_x = 10
  16. loc_y = 10
  17. Element to_remove
  18. String elem_to_remove
  19. Element groups
  20. Element class_types
  21. Element metamodel
  22. metamodel = model["metamodel"]
  23. String class_type
  24. // Construct our own kind of tracability
  25. Element cs_to_as
  26. Element as_to_cs
  27. String asid
  28. cs_to_as = dict_create()
  29. as_to_cs = dict_create()
  30. groups = allInstances(model, "rendered/Group")
  31. while (set_len(groups) > 0):
  32. group = set_pop(groups)
  33. asid = read_attribute(model, group, "__asid")
  34. dict_add(cs_to_as, group, "abstract/" + asid)
  35. dict_add(as_to_cs, "abstract/" + asid, group)
  36. // Now render everything
  37. groups = dict_create()
  38. class_types = allInstances(metamodel, "Class")
  39. while (set_len(class_types) > 0):
  40. class_type = set_pop(class_types)
  41. if (string_startswith(class_type, "abstract/")):
  42. elements = allInstances(model, class_type)
  43. while (set_len(elements) > 0):
  44. class = set_pop(elements)
  45. if (is_edge(model["model"][class])):
  46. log("Skipping edge " + class)
  47. continue!
  48. log("Drawing " + class)
  49. Integer x
  50. Integer y
  51. x = loc_x
  52. y = loc_y
  53. // Check if there is already an associated element
  54. if (dict_in(as_to_cs, class)):
  55. // Yes, but is it still clean?
  56. Element related_groups
  57. Boolean dirty
  58. dirty = False
  59. related_groups = as_to_cs[class]
  60. while (set_len(related_groups) > 0):
  61. group = set_pop(related_groups)
  62. if (value_eq(read_attribute(model, group, "dirty"), True)):
  63. // No, so mark all as dirty
  64. dirty = True
  65. break!
  66. else:
  67. // Yes, so just ignore this!
  68. continue!
  69. if (bool_not(dirty)):
  70. dict_add(groups, class, group)
  71. continue!
  72. else:
  73. group = as_to_cs[class]
  74. group = set_pop(related_groups)
  75. to_remove = allAssociationDestinations(model, group, "rendered/contains")
  76. x = create_value(read_attribute(model, group, "x"))
  77. y = create_value(read_attribute(model, group, "y"))
  78. while (set_len(to_remove) > 0):
  79. elem_to_remove = set_pop(to_remove)
  80. if (read_type(model, elem_to_remove) == "rendered/Group"):
  81. set_add(to_remove, elem_to_remove)
  82. else:
  83. model_delete_element(model, elem_to_remove)
  84. model_delete_element(model, group)
  85. if (dict_in(groups, class)):
  86. // Already rendered this, so skip
  87. continue!
  88. text_loc = 5
  89. group = instantiate_node(model, "rendered/Group", "")
  90. instantiate_attribute(model, group, "x", x)
  91. instantiate_attribute(model, group, "y", y)
  92. instantiate_attribute(model, group, "__asid", list_read(string_split(class, "/"), 1))
  93. dict_add(groups, class, group)
  94. loc_x = loc_x + 250
  95. if (loc_x > 2000):
  96. loc_x = 10
  97. loc_y = loc_y + 300
  98. elem = instantiate_node(model, "rendered/Rectangle", "")
  99. instantiate_attribute(model, elem, "x", 0)
  100. instantiate_attribute(model, elem, "y", 0)
  101. instantiate_attribute(model, elem, "height", 40 + set_len(getAttributes(model, class)) * 20)
  102. instantiate_attribute(model, elem, "width", 200)
  103. instantiate_attribute(model, elem, "lineWidth", 2)
  104. instantiate_attribute(model, elem, "lineColour", "black")
  105. instantiate_attribute(model, elem, "fillColour", "white")
  106. instantiate_link(model, "rendered/contains", "", group, elem)
  107. elem = instantiate_node(model, "rendered/Text", "")
  108. instantiate_attribute(model, elem, "x", 5)
  109. instantiate_attribute(model, elem, "y", 3)
  110. instantiate_attribute(model, elem, "lineWidth", 1)
  111. instantiate_attribute(model, elem, "lineColour", "black")
  112. instantiate_attribute(model, elem, "text", string_join(cast_v2s(list_read(string_split(class, "/"), 1)), " : " + cast_v2s(list_read(string_split(read_type(model, class), "/"), 1))))
  113. instantiate_link(model, "rendered/contains", "", group, elem)
  114. elem = instantiate_node(model, "rendered/Line", "")
  115. instantiate_attribute(model, elem, "x", 0)
  116. instantiate_attribute(model, elem, "y", 20)
  117. instantiate_attribute(model, elem, "targetX", 200)
  118. instantiate_attribute(model, elem, "targetY", 20)
  119. instantiate_attribute(model, elem, "lineWidth", 1)
  120. instantiate_attribute(model, elem, "lineColour", "black")
  121. instantiate_attribute(model, elem, "arrow", False)
  122. instantiate_link(model, "rendered/contains", "", group, elem)
  123. attrs = getAttributes(model, class)
  124. attr_keys = dict_keys(attrs)
  125. while (dict_len(attr_keys) > 0):
  126. attr_key = set_pop(attr_keys)
  127. elem = instantiate_node(model, "rendered/Text", "")
  128. instantiate_attribute(model, elem, "x", 5)
  129. instantiate_attribute(model, elem, "y", text_loc + 20)
  130. instantiate_attribute(model, elem, "lineWidth", 1)
  131. instantiate_attribute(model, elem, "lineColour", "black")
  132. instantiate_attribute(model, elem, "text", (attr_key + " = ") + cast_v2s(attrs[attr_key]))
  133. instantiate_link(model, "rendered/contains", "", group, elem)
  134. text_loc = text_loc + 15
  135. // Flush all associations
  136. elements = allInstances(model, "rendered/ConnectingLine")
  137. while (set_len(elements) > 0):
  138. class = set_pop(elements)
  139. model_delete_element(model, class)
  140. // Rerender associations
  141. Element to_render
  142. to_render = set_create()
  143. class_types = allInstances(metamodel, "Class")
  144. while (set_len(class_types) > 0):
  145. class_type = set_pop(class_types)
  146. if (string_startswith(class_type, "abstract/")):
  147. elements = allInstances(model, class_type)
  148. while (set_len(elements) > 0):
  149. class = set_pop(elements)
  150. if (is_edge(model["model"][class])):
  151. if (bool_not(set_in(to_render, class))):
  152. set_add(to_render, class)
  153. to_render = set_to_list(to_render)
  154. Element delayed_elements
  155. Integer num_to_render
  156. delayed_elements = list_create()
  157. while (list_len(to_render) > 0):
  158. num_to_render = list_len(to_render)
  159. while (list_len(to_render) > 0):
  160. class = list_pop_final(to_render)
  161. attr_keys = dict_keys(getAttributes(model, class))
  162. log("Association: " + class)
  163. log("Connects " + cast_v2s(readAssociationSource(model, class)))
  164. log(" to " + cast_v2s(readAssociationDestination(model, class)))
  165. if (bool_not(bool_and(dict_in(groups, readAssociationSource(model, class)), dict_in(groups, readAssociationDestination(model, class))))):
  166. log("DELAY")
  167. list_append(delayed_elements, class)
  168. continue!
  169. elem = instantiate_link(model, "rendered/ConnectingLine", "", groups[readAssociationSource(model, class)], groups[readAssociationDestination(model, class)])
  170. dict_add(groups, class, elem)
  171. instantiate_attribute(model, elem, "offsetSourceX", 100)
  172. instantiate_attribute(model, elem, "offsetSourceY", 50)
  173. instantiate_attribute(model, elem, "offsetTargetX", 100)
  174. instantiate_attribute(model, elem, "offsetTargetY", 50)
  175. instantiate_attribute(model, elem, "lineWidth", 4)
  176. instantiate_attribute(model, elem, "lineColour", "black")
  177. instantiate_attribute(model, elem, "arrow", True)
  178. instantiate_attribute(model, elem, "__asid", list_read(string_split(class, "/"), 1))
  179. instantiate_link(model, "rendered/contains", "", group, elem)
  180. if (num_to_render == list_len(delayed_elements)):
  181. log("Could not decrease number of rendered elements anymore...")
  182. return True!
  183. else:
  184. to_render = delayed_elements
  185. delayed_elements = list_create()
  186. return True!