to_graphviz.alc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. Boolean function main(model : Element):
  5. Element rules
  6. String rule
  7. rules = allInstances(model, "Rules/Rule")
  8. while (set_len(rules) > 0):
  9. rule = set_pop(rules)
  10. print_rule(model, rule)
  11. return True!
  12. String function print_rule(model : Element, rule : String):
  13. String text
  14. String type
  15. text = ""
  16. // Preamble
  17. text = text + "digraph {\n"
  18. text = text + "\tnode [shape=\"point\"]\n"
  19. // First go through the list of all entries in the rule
  20. Element nodes
  21. Element edges
  22. nodes = set_create()
  23. edges = set_create()
  24. Element all
  25. String el
  26. all = allAssociationDestinations(model, rule, "Rules/contains")
  27. while (set_len(all) > 0):
  28. el = set_pop(all)
  29. if (is_nominal_instance(model, el, "Rules/Edge")):
  30. // Is an edge
  31. set_add(edges, el)
  32. else:
  33. // Only a node
  34. set_add(nodes, el)
  35. // Print out all the nodes in the rule
  36. String node
  37. while (set_len(nodes) > 0):
  38. node = set_pop(nodes)
  39. text = text + "\t" + node + "["
  40. // Try to add value
  41. if (element_neq(read_attribute(model, node, "value"), read_root())):
  42. // Has a value, so add a label and make it oval
  43. text = text + "shape=\"oval\", label=\"" + cast_string(read_attribute(model, node, "value")) + "\", "
  44. // Add style
  45. type = read_type(model, node)
  46. if (type == "Rules/Match"):
  47. text = text + "color=\"black\""
  48. elif (type == "Rules/Root"):
  49. text = text + "color=\"black\""
  50. elif (type == "Rules/Create"):
  51. text = text + "color=\"green\""
  52. elif (type == "Rules/Delete"):
  53. text = text + "color=\"blue\""
  54. elif (type == "Rules/NAC"):
  55. text = text + "color=\"red\""
  56. text = text + "]\n"
  57. // Print out all the edges in the rule
  58. String edge
  59. String style
  60. while (set_len(edges) > 0):
  61. edge = set_pop(edges)
  62. type = read_type(model, node)
  63. if (type == "Rules/MatchEdge"):
  64. style = "colour=\"black\""
  65. elif (type == "Rules/CreateEdge"):
  66. style = "colour=\"green\", penwidth=4"
  67. elif (type == "Rules/DeleteEdge"):
  68. style = "colour=\"blue\", style=\"dashed\""
  69. elif (type == "Rules/NACEdge"):
  70. style = "colour=\"red\", style=\"dotted\""
  71. // Try to add value
  72. if (element_neq(read_attribute(model, edge, "value"), read_root())):
  73. // Has a value, so add a label
  74. text = text + "\t" + readAssociationSource(model, edge) + " -> " + readAssociationDestination(model, edge) + "["
  75. text = text + style + ", label=\"" + cast_string(read_attribute(model, node, "value")) + "\"]\n"
  76. elif (set_len(allOutgoingAssociationInstances(model, edge, "")) > 0):
  77. // Has no value, and we have outgoing edges ourself, so we should have a node handle
  78. text = text + "\t" + edge + "[width=0]\n"
  79. text = text + "\t" + readAssociationSource(model, edge) + " -> " + edge + "[" + style + ", arrowhead=\"none\"]"
  80. text = text + "\t" + edge + " -> " + readAssociationDestination(model, edge) + "[" + style + "]\n"
  81. else:
  82. // Has no value, meaning that we are referring to an intermediate edge...
  83. text = text + "\t" + readAssociationSource(model, edge) + " -> " + readAssociationDestination(model, edge) + "[" + style + "]\n"
  84. text = text + "]\n"
  85. // Close everything
  86. text = text + "}"
  87. log("Generated text: " + text)
  88. return text!