Преглед изворни кода

Initial version of "to graphviz" exporter

Yentl Van Tendeloo пре 7 година
родитељ
комит
ee140602ef
2 измењених фајлова са 106 додато и 1 уклоњено
  1. 1 1
      kernel/rules/MvK_rules_MM.mvc
  2. 105 0
      kernel/rules/to_graphviz.alc

+ 1 - 1
kernel/rules/MvK_rules_MM.mvc

@@ -20,7 +20,7 @@ Class Root : Node {}
 Association contains (Rule, Node) {}
 
 Association Edge : Node (Node, Node) {
-    name : String
+    name? : String
 }
 
 Association CreateEdge : Edge (Node, Node) {}

+ 105 - 0
kernel/rules/to_graphviz.alc

@@ -0,0 +1,105 @@
+include "primitives.alh"
+include "modelling.alh"
+include "object_operations.alh"
+
+Boolean function main(model : Element):
+    Element rules
+    String rule
+
+    rules = allInstances(model, "Rules/Rule")
+    while (set_len(rules) > 0):
+        rule = set_pop(rules)
+        print_rule(model, rule)
+
+    return True!
+
+String function print_rule(model : Element, rule : String):
+    String text
+    String type
+    text = ""
+
+    // Preamble
+    text = text + "digraph {\n"
+    text = text + "\tnode [shape=\"point\"]\n"
+
+    // First go through the list of all entries in the rule
+    Element nodes
+    Element edges
+    nodes = set_create()
+    edges = set_create()
+
+    Element all
+    String el
+    all = allAssociationDestinations(model, rule, "Rules/contains")
+    while (set_len(all) > 0):
+        el = set_pop(all)
+        if (is_nominal_instance(model, el, "Rules/Edge")):
+            // Is an edge
+            set_add(edges, el)
+        else:
+            // Only a node
+            set_add(nodes, el)
+
+    // Print out all the nodes in the rule
+    String node
+    while (set_len(nodes) > 0):
+        node = set_pop(nodes)
+        text = text + "\t" + node + "["
+
+        // Try to add value
+        if (element_neq(read_attribute(model, node, "value"), read_root())):
+            // Has a value, so add a label and make it oval
+            text = text + "shape=\"oval\", label=\"" + cast_string(read_attribute(model, node, "value")) + "\", "
+
+        // Add style
+        type = read_type(model, node)
+        if (type == "Rules/Match"):
+            text = text + "color=\"black\""
+        elif (type == "Rules/Root"):
+            text = text + "color=\"black\""
+        elif (type == "Rules/Create"):
+            text = text + "color=\"green\""
+        elif (type == "Rules/Delete"):
+            text = text + "color=\"blue\""
+        elif (type == "Rules/NAC"):
+            text = text + "color=\"red\""
+
+        text = text + "]\n"
+
+    // Print out all the edges in the rule
+    String edge
+    String style
+    while (set_len(edges) > 0):
+        edge = set_pop(edges)
+
+        type = read_type(model, node)
+        if (type == "Rules/MatchEdge"):
+            style = "colour=\"black\""
+        elif (type == "Rules/CreateEdge"):
+            style = "colour=\"green\", penwidth=4"
+        elif (type == "Rules/DeleteEdge"):
+            style = "colour=\"blue\", style=\"dashed\""
+        elif (type == "Rules/NACEdge"):
+            style = "colour=\"red\", style=\"dotted\""
+
+        // Try to add value
+        if (element_neq(read_attribute(model, edge, "value"), read_root())):
+            // Has a value, so add a label
+            text = text + "\t" + readAssociationSource(model, edge) + " -> " + readAssociationDestination(model, edge) + "["
+            text = text + style + ", label=\"" + cast_string(read_attribute(model, node, "value")) + "\"]\n"
+        elif (set_len(allOutgoingAssociationInstances(model, edge, "")) > 0):
+            // Has no value, and we have outgoing edges ourself, so we should have a node handle
+            text = text + "\t" + edge + "[width=0]\n"
+            text = text + "\t" + readAssociationSource(model, edge) + " -> " + edge + "[" + style + ", arrowhead=\"none\"]"
+            text = text + "\t" + edge + " -> " + readAssociationDestination(model, edge) + "[" + style + "]\n"
+        else:
+            // Has no value, meaning that we are referring to an intermediate edge...
+            text = text + "\t" + readAssociationSource(model, edge) + " -> " + readAssociationDestination(model, edge) + "[" + style + "]\n"
+
+        text = text + "]\n"
+
+    // Close everything
+    text = text + "}"
+
+    log("Generated text: " + text)
+    return text!