to_graphviz.alc 4.0 KB

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