use of com.randomnoun.build.javaToGraphviz.astToDag.LexicalScope 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());
}
Aggregations