Search in sources :

Example 1 with ControlFlowEdger

use of com.randomnoun.build.javaToGraphviz.astToDag.ControlFlowEdger in project java-to-graphviz by randomnoun.

the class JavaToGraphviz method writeGraphviz.

/**
 * Generate a single graphviz diagram.
 *
 * @param writer
 * @return true if there are more diagrams, false otherwise
 * @throws IOException
 */
public boolean writeGraphviz(Writer writer) throws IOException {
    DagSubgraph rootGraph = dag.rootGraphs.get(rootGraphIdx);
    PrintWriter pw = new PrintWriter(writer);
    // remove edges from previous runs
    dag.edges.clear();
    // a rootGraph can now contain multiple rootNodes
    // but a rootNode can only be in a single rootGraph
    int c = 0;
    for (DagNode rootNode : dag.rootNodes) {
        c++;
        if (rootGraph.nodes.contains(rootNode)) {
            // logger.info("including rootNode " + c);
            String edgerNamesCsv = rootNode.options.get("edgerNamesCsv");
            if (Text.isBlank(edgerNamesCsv)) {
                edgerNamesCsv = "control-flow";
            }
            boolean enableKeepNodeFilter = "true".equals(rootNode.options.get("enableKeepNodeFilter"));
            List<String> edgerNames;
            try {
                edgerNames = Text.parseCsv(edgerNamesCsv);
            } catch (ParseException e1) {
                throw new IllegalArgumentException("edgerNamesCsv is not valid CSV", e1);
            }
            rootNode.options.get("removeNodes");
            LexicalScope lexicalScope = new LexicalScope();
            for (String edgerName : edgerNames) {
                if (edgerName.equals("control-flow")) {
                    ControlFlowEdger edger = new ControlFlowEdger(dag);
                    edger.addEdges(dag, rootNode, lexicalScope);
                } else if (edgerName.equals("ast")) {
                    AstEdger edger = new AstEdger(dag);
                    edger.addEdges(dag, rootNode, lexicalScope);
                // @TODO data-flow
                } else {
                    throw new IllegalArgumentException("unknown edgerName '" + edgerName + "'");
                }
            }
            // going to assume that edges don't cross rootNodes, which they don't yet either.
            for (DagNode n : dag.nodes) {
                n.inEdges = new ArrayList<>();
                n.outEdges = new ArrayList<>();
            }
            for (DagEdge e : dag.edges) {
                e.n1.outEdges.add(e);
                e.n2.inEdges.add(e);
            }
            if (enableKeepNodeFilter) {
                DagNodeFilter filter = new DagNodeFilter(dag);
                if (rootNode.keepNodeMatcher.matches("startNode")) {
                    rootNode.keepNode = true;
                    filter.setLastKeepNode(rootNode, rootNode);
                } else {
                    rootNode.keepNode = false;
                }
                filter.removeNodes(rootNode);
            }
        }
    }
    // subgraphs are now defined in the Dag from the stylesheet
    DagStyleApplier dsa = new DagStyleApplier(dag, rootGraph);
    Document doc = dsa.createDom();
    if (format.equals("dom1")) {
        pw.println(doc.toString());
    } else {
        dsa.inlineStyles(styleSheet);
    }
    if (format.equals("dom1")) {
    // already generated output
    } else if (format.equals("dom2")) {
        doc = dsa.getDocument();
        pw.println(doc.toString());
    } else {
        pw.println(rootGraph.toGraphviz(0));
    }
    rootGraphIdx++;
    return (rootGraphIdx < dag.rootGraphs.size());
}
Also used : LexicalScope(com.randomnoun.build.javaToGraphviz.astToDag.LexicalScope) DagNodeFilter(com.randomnoun.build.javaToGraphviz.astToDag.DagNodeFilter) DagSubgraph(com.randomnoun.build.javaToGraphviz.dag.DagSubgraph) Document(org.jsoup.nodes.Document) DagNode(com.randomnoun.build.javaToGraphviz.dag.DagNode) AstEdger(com.randomnoun.build.javaToGraphviz.astToDag.AstEdger) ParseException(java.text.ParseException) ControlFlowEdger(com.randomnoun.build.javaToGraphviz.astToDag.ControlFlowEdger) DagStyleApplier(com.randomnoun.build.javaToGraphviz.astToDag.DagStyleApplier) DagEdge(com.randomnoun.build.javaToGraphviz.dag.DagEdge) PrintWriter(java.io.PrintWriter)

Aggregations

AstEdger (com.randomnoun.build.javaToGraphviz.astToDag.AstEdger)1 ControlFlowEdger (com.randomnoun.build.javaToGraphviz.astToDag.ControlFlowEdger)1 DagNodeFilter (com.randomnoun.build.javaToGraphviz.astToDag.DagNodeFilter)1 DagStyleApplier (com.randomnoun.build.javaToGraphviz.astToDag.DagStyleApplier)1 LexicalScope (com.randomnoun.build.javaToGraphviz.astToDag.LexicalScope)1 DagEdge (com.randomnoun.build.javaToGraphviz.dag.DagEdge)1 DagNode (com.randomnoun.build.javaToGraphviz.dag.DagNode)1 DagSubgraph (com.randomnoun.build.javaToGraphviz.dag.DagSubgraph)1 PrintWriter (java.io.PrintWriter)1 ParseException (java.text.ParseException)1 Document (org.jsoup.nodes.Document)1