Search in sources :

Example 1 with AdvancedDOTExporter

use of com.jopdesign.common.graphutils.AdvancedDOTExporter in project jop by jop-devel.

the class Template method exportDOT.

public void exportDOT(File dbgFile) throws IOException {
    DirectedGraph<Location, DefaultEdge> locGraph = new DefaultDirectedGraph<Location, DefaultEdge>(DefaultEdge.class);
    for (Location l : this.locations.values()) locGraph.addVertex(l);
    Map<DefaultEdge, String> edgeMap = new HashMap<DefaultEdge, String>();
    for (Transition t : this.transitions) {
        DefaultEdge e = locGraph.addEdge(t.getSource(), t.getTarget());
        edgeMap.put(e, t.getAttrs().toString());
    }
    FileWriter fw = new FileWriter(dbgFile);
    AdvancedDOTExporter.DOTNodeLabeller<Location> nodeLabeller = new AdvancedDOTExporter.DefaultNodeLabeller<Location>() {

        public int getID(Location node) {
            return node.getId();
        }

        public String getLabel(Location node) {
            return node.getName();
        }
    };
    AdvancedDOTExporter.DOTLabeller<DefaultEdge> edgeLabeller = new AdvancedDOTExporter.MapLabeller<DefaultEdge>(edgeMap);
    AdvancedDOTExporter<Location, DefaultEdge> dotExport = new AdvancedDOTExporter<Location, DefaultEdge>(nodeLabeller, edgeLabeller);
    dotExport.exportDOT(fw, locGraph);
    fw.close();
}
Also used : DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) DefaultEdge(org.jgrapht.graph.DefaultEdge) AdvancedDOTExporter(com.jopdesign.common.graphutils.AdvancedDOTExporter)

Example 2 with AdvancedDOTExporter

use of com.jopdesign.common.graphutils.AdvancedDOTExporter in project jop by jop-devel.

the class Segment method exportDOT.

/**
 * Export to DOT file
 * @param dotFile
 * @throws IOException
 */
public void exportDOT(File dotFile) throws IOException {
    FileWriter dotWriter = new FileWriter(dotFile);
    AdvancedDOTExporter.DOTNodeLabeller<SuperGraphNode> nodeLabeller = new AdvancedDOTExporter.DefaultNodeLabeller<SuperGraphNode>() {

        @Override
        public String getLabel(SuperGraphNode node) {
            StringBuilder sb = new StringBuilder();
            /* for entry nodes: method + call string */
            if (node.getCFGNode().getId() == 0) {
                sb.append(node.getContextCFG().getCfg().getMethodInfo().getFQMethodName() + "\n");
                int i = 1;
                for (InvokeSite is : node.getContextCFG().getCallString()) {
                    sb.append(" #" + i + " " + is.getInvoker().getFQMethodName() + " / " + is.getInstructionHandle().getPosition() + "\n");
                    i += 1;
                }
            } else /* for other nodes: basic block export */
            {
                sb.append(node.getCFGNode().toString());
            }
            return sb.toString();
        }
    };
    DOTLabeller<SuperGraphEdge> edgeLabeller = new AdvancedDOTExporter.DefaultDOTLabeller<SuperGraphEdge>() {

        @Override
        public String getLabel(SuperGraphEdge edge) {
            return "";
        }

        @Override
        public boolean setAttributes(SuperGraphEdge edge, Map<String, String> ht) {
            super.setAttributes(edge, ht);
            if (edge instanceof SuperReturnEdge) {
                ht.put("style", "dotted");
                ht.put("arrowhead", "empty");
            } else if (edge instanceof SuperInvokeEdge) {
                ht.put("style", "dotted");
            }
            return true;
        }
    };
    AdvancedDOTExporter<SuperGraphNode, SuperGraphEdge> de = new AdvancedDOTExporter<SuperGraphNode, SuperGraphEdge>(nodeLabeller, edgeLabeller);
    de.exportDOTDiGraph(dotWriter, this.getNodes(), this.getEdges(), new AdvancedDOTExporter.GraphAdapter<SuperGraphNode, SuperGraphEdge>() {

        @Override
        public SuperGraphNode getEdgeSource(SuperGraphEdge e) {
            return e.getSource();
        }

        @Override
        public SuperGraphNode getEdgeTarget(SuperGraphEdge e) {
            return e.getTarget();
        }
    });
    dotWriter.close();
}
Also used : FileWriter(java.io.FileWriter) SuperReturnEdge(com.jopdesign.common.code.SuperGraph.SuperReturnEdge) SuperInvokeEdge(com.jopdesign.common.code.SuperGraph.SuperInvokeEdge) AdvancedDOTExporter(com.jopdesign.common.graphutils.AdvancedDOTExporter) SuperGraphEdge(com.jopdesign.common.code.SuperGraph.SuperGraphEdge) SuperGraphNode(com.jopdesign.common.code.SuperGraph.SuperGraphNode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with AdvancedDOTExporter

use of com.jopdesign.common.graphutils.AdvancedDOTExporter in project jop by jop-devel.

the class SuperGraph method exportDOT.

public void exportDOT(File dotFile) throws IOException {
    FileWriter dotWriter = new FileWriter(dotFile);
    AdvancedDOTExporter.DOTNodeLabeller<ContextCFG> nodeLabeller = new AdvancedDOTExporter.DefaultNodeLabeller<ContextCFG>() {

        @Override
        public String getLabel(ContextCFG node) {
            StringBuilder sb = new StringBuilder();
            sb.append(node.getCfg().getMethodInfo().getFQMethodName() + "\n");
            int i = 1;
            for (InvokeSite is : node.getContext().getCallString()) {
                sb.append(" #" + i + " " + is.getInvoker().getFQMethodName() + " / " + is.getInstructionHandle().getPosition() + "\n");
                i += 1;
            }
            return sb.toString();
        }
    };
    DOTLabeller<SuperEdge> edgeLabeller = new AdvancedDOTExporter.DefaultDOTLabeller<SuperEdge>() {

        @Override
        public String getLabel(SuperEdge edge) {
            return "";
        }

        @Override
        public boolean setAttributes(SuperEdge edge, Map<String, String> ht) {
            super.setAttributes(edge, ht);
            if (edge instanceof SuperReturnEdge) {
                ht.put("style", "dotted");
                ht.put("arrowhead", "empty");
            }
            return true;
        }
    };
    AdvancedDOTExporter<ContextCFG, SuperEdge> de = new AdvancedDOTExporter<ContextCFG, SuperEdge>(nodeLabeller, edgeLabeller);
    de.exportDOT(dotWriter, this.superGraph);
    dotWriter.close();
}
Also used : FileWriter(java.io.FileWriter) AdvancedDOTExporter(com.jopdesign.common.graphutils.AdvancedDOTExporter) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

AdvancedDOTExporter (com.jopdesign.common.graphutils.AdvancedDOTExporter)3 FileWriter (java.io.FileWriter)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SuperGraphEdge (com.jopdesign.common.code.SuperGraph.SuperGraphEdge)1 SuperGraphNode (com.jopdesign.common.code.SuperGraph.SuperGraphNode)1 SuperInvokeEdge (com.jopdesign.common.code.SuperGraph.SuperInvokeEdge)1 SuperReturnEdge (com.jopdesign.common.code.SuperGraph.SuperReturnEdge)1 LinkedHashMap (java.util.LinkedHashMap)1 DefaultDirectedGraph (org.jgrapht.graph.DefaultDirectedGraph)1 DefaultEdge (org.jgrapht.graph.DefaultEdge)1