render_OD.alc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. continue!
  47. Integer x
  48. Integer y
  49. x = loc_x
  50. y = loc_y
  51. // Check if there is already an associated element
  52. if (dict_in(as_to_cs, class)):
  53. // Yes, but is it still clean?
  54. Element related_groups
  55. group = as_to_cs[class]
  56. if (bool_not(read_attribute(model, group, "dirty"))):
  57. dict_add(groups, class, group)
  58. continue!
  59. else:
  60. group = as_to_cs[class]
  61. to_remove = allAssociationDestinations(model, group, "rendered/contains")
  62. x = create_value(read_attribute(model, group, "x"))
  63. y = create_value(read_attribute(model, group, "y"))
  64. while (set_len(to_remove) > 0):
  65. elem_to_remove = set_pop(to_remove)
  66. if (read_type(model, elem_to_remove) == "rendered/Group"):
  67. set_add(to_remove, elem_to_remove)
  68. else:
  69. model_delete_element(model, elem_to_remove)
  70. model_delete_element(model, group)
  71. dict_delete(as_to_cs, class)
  72. if (dict_in(groups, class)):
  73. // Already rendered this, so skip
  74. continue!
  75. text_loc = 5
  76. group = instantiate_node(model, "rendered/Group", "")
  77. instantiate_attribute(model, group, "x", x)
  78. instantiate_attribute(model, group, "y", y)
  79. instantiate_attribute(model, group, "__asid", list_read(string_split(class, "/"), 1))
  80. instantiate_attribute(model, group, "layer", 0)
  81. dict_add(groups, class, group)
  82. loc_x = loc_x + 250
  83. if (loc_x > 2000):
  84. loc_x = 10
  85. loc_y = loc_y + 300
  86. elem = instantiate_node(model, "rendered/Rectangle", "")
  87. instantiate_attribute(model, elem, "x", 0)
  88. instantiate_attribute(model, elem, "y", 0)
  89. instantiate_attribute(model, elem, "height", 40 + set_len(getAttributes(model, class)) * 20)
  90. instantiate_attribute(model, elem, "width", 200)
  91. instantiate_attribute(model, elem, "lineWidth", 2)
  92. instantiate_attribute(model, elem, "lineColour", "black")
  93. instantiate_attribute(model, elem, "fillColour", "white")
  94. instantiate_attribute(model, elem, "layer", 1)
  95. instantiate_link(model, "rendered/contains", "", group, elem)
  96. elem = instantiate_node(model, "rendered/Text", "")
  97. instantiate_attribute(model, elem, "x", 5)
  98. instantiate_attribute(model, elem, "y", 3)
  99. instantiate_attribute(model, elem, "lineWidth", 1)
  100. instantiate_attribute(model, elem, "lineColour", "black")
  101. instantiate_attribute(model, elem, "text", string_join(cast_value(list_read(string_split(class, "/"), 1)), " : " + cast_value(list_read(string_split(read_type(model, class), "/"), 1))))
  102. instantiate_attribute(model, elem, "layer", 2)
  103. instantiate_link(model, "rendered/contains", "", group, elem)
  104. elem = instantiate_node(model, "rendered/Line", "")
  105. instantiate_attribute(model, elem, "x", 0)
  106. instantiate_attribute(model, elem, "y", 20)
  107. instantiate_attribute(model, elem, "targetX", 200)
  108. instantiate_attribute(model, elem, "targetY", 20)
  109. instantiate_attribute(model, elem, "lineWidth", 1)
  110. instantiate_attribute(model, elem, "lineColour", "black")
  111. instantiate_attribute(model, elem, "arrow", False)
  112. instantiate_attribute(model, elem, "layer", 2)
  113. instantiate_link(model, "rendered/contains", "", group, elem)
  114. attrs = getAttributes(model, class)
  115. attr_keys = dict_keys(attrs)
  116. while (dict_len(attr_keys) > 0):
  117. attr_key = set_pop(attr_keys)
  118. elem = instantiate_node(model, "rendered/Text", "")
  119. instantiate_attribute(model, elem, "x", 5)
  120. instantiate_attribute(model, elem, "y", text_loc + 20)
  121. instantiate_attribute(model, elem, "lineWidth", 1)
  122. instantiate_attribute(model, elem, "lineColour", "black")
  123. instantiate_attribute(model, elem, "text", (attr_key + " = ") + cast_value(attrs[attr_key]))
  124. instantiate_attribute(model, elem, "layer", 2)
  125. instantiate_link(model, "rendered/contains", "", group, elem)
  126. text_loc = text_loc + 15
  127. // Flush all associations
  128. elements = allInstances(model, "rendered/ConnectingLine")
  129. while (set_len(elements) > 0):
  130. class = set_pop(elements)
  131. model_delete_element(model, class)
  132. // Rerender associations
  133. Element to_render
  134. to_render = set_create()
  135. class_types = allInstances(metamodel, "Association")
  136. while (set_len(class_types) > 0):
  137. class_type = set_pop(class_types)
  138. log("Checking type " + class_type)
  139. if (string_startswith(class_type, "abstract/")):
  140. elements = allInstances(model, class_type)
  141. log(" Checking instance " + class)
  142. while (set_len(elements) > 0):
  143. class = set_pop(elements)
  144. if (is_edge(model["model"][class])):
  145. if (bool_not(set_in(to_render, class))):
  146. set_add(to_render, class)
  147. log("Added!")
  148. to_render = set_to_list(to_render)
  149. Element delayed_elements
  150. Integer num_to_render
  151. delayed_elements = list_create()
  152. while (list_len(to_render) > 0):
  153. num_to_render = list_len(to_render)
  154. while (list_len(to_render) > 0):
  155. class = list_pop_final(to_render)
  156. attr_keys = dict_keys(getAttributes(model, class))
  157. if (bool_not(bool_and(dict_in(groups, readAssociationSource(model, class)), dict_in(groups, readAssociationDestination(model, class))))):
  158. list_append(delayed_elements, class)
  159. continue!
  160. elem = instantiate_link(model, "rendered/ConnectingLine", "", groups[readAssociationSource(model, class)], groups[readAssociationDestination(model, class)])
  161. dict_add(groups, class, elem)
  162. if (is_edge(model["model"][readAssociationSource(model, class)])):
  163. instantiate_attribute(model, elem, "offsetSourceX", 0)
  164. instantiate_attribute(model, elem, "offsetSourceY", 0)
  165. else:
  166. instantiate_attribute(model, elem, "offsetSourceX", 100)
  167. instantiate_attribute(model, elem, "offsetSourceY", 30)
  168. if (is_edge(model["model"][readAssociationDestination(model, class)])):
  169. instantiate_attribute(model, elem, "offsetTargetX", 0)
  170. instantiate_attribute(model, elem, "offsetTargetY", 0)
  171. else:
  172. instantiate_attribute(model, elem, "offsetTargetX", 100)
  173. instantiate_attribute(model, elem, "offsetTargetY", 30)
  174. instantiate_attribute(model, elem, "lineWidth", 3)
  175. instantiate_attribute(model, elem, "lineColour", "black")
  176. instantiate_attribute(model, elem, "arrow", True)
  177. instantiate_attribute(model, elem, "__asid", list_read(string_split(class, "/"), 1))
  178. instantiate_attribute(model, elem, "layer", 0)
  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... Giving up!")
  182. return True!
  183. else:
  184. to_render = delayed_elements
  185. delayed_elements = list_create()
  186. return True!