Commit 8f5c005d authored by Alexander Weigl's avatar Alexander Weigl

print graph as dot for debugging

visit http://www.webgraphviz.com/ for viz.
parent abcfebda
Pipeline #11461 passed with stage
in 2 minutes and 27 seconds
......@@ -2,7 +2,6 @@ package edu.kit.formal.interpreter;
import com.google.common.graph.MutableValueGraph;
import com.google.common.graph.ValueGraphBuilder;
import edu.kit.formal.interpreter.data.KeyData;
import edu.kit.formal.interpreter.funchdl.CommandLookup;
import edu.kit.formal.proofscriptparser.DefaultASTVisitor;
import edu.kit.formal.proofscriptparser.ast.*;
......@@ -11,15 +10,13 @@ import edu.kit.formal.proofscriptparser.ast.*;
* Visitor to create ProgramFlowGraph
*/
public class ProgramFlowVisitor extends DefaultASTVisitor<Void> {
private final CommandLookup functionLookup;
private PTreeNode lastNode;
private MutableValueGraph<PTreeNode, EdgeTypes> graph = ValueGraphBuilder.directed().build();
private Interpreter<KeyData> inter;
public ProgramFlowVisitor(Interpreter<KeyData> inter) {
this.inter = inter;
private MutableValueGraph<PTreeNode, EdgeTypes> graph = ValueGraphBuilder.directed().allowsSelfLoops(true).build();
public ProgramFlowVisitor(CommandLookup functionLookup) {
this.functionLookup = functionLookup;
}
public MutableValueGraph<PTreeNode, EdgeTypes> getGraph() {
......@@ -58,7 +55,6 @@ public class ProgramFlowVisitor extends DefaultASTVisitor<Void> {
public Void visit(CallStatement call) {
PTreeNode currentNode = new PTreeNode(call);
//fixme handle stepinto
CommandLookup lookup = inter.getFunctionLookup();
graph.addNode(currentNode);
graph.putEdgeValue(lastNode, currentNode, EdgeTypes.STEP_OVER);
......@@ -87,4 +83,32 @@ public class ProgramFlowVisitor extends DefaultASTVisitor<Void> {
return super.visit(casesStatement);
}
public String asdot() {
StringBuilder sb = new StringBuilder();
sb.append("digraph G {\nnode [shape=rect]\n ");
graph.nodes().forEach(n -> {
sb.append(n.hashCode())
.append(" [label=\"")
.append(n.getScriptstmt().getNodeName())
.append("@")
.append(n.getScriptstmt().getStartPosition().getLineNumber())
.append("\"]\n");
});
graph.edges().forEach(edge -> {
sb.append(edge.source().hashCode())
.append(" -> ")
.append(edge.target().hashCode())
.append(" [label=\"")
.append(graph.edgeValue(edge.source(), edge.target()).name())
.append("\"]\n");
});
sb.append("}");
return sb.toString();
}
}
......@@ -26,7 +26,7 @@ public class ProofTreeController {
}
private void buildEmptyGraph(ProofScript mainScript) {
ProgramFlowVisitor visitor = new ProgramFlowVisitor(currentInterpreter);
ProgramFlowVisitor visitor = new ProgramFlowVisitor(currentInterpreter.getFunctionLookup());
mainScript.accept(visitor);
System.out.println(visitor.getGraph());
......
......@@ -88,7 +88,7 @@ public abstract class ASTNode<T extends ParserRuleContext>
* @return a {@link java.lang.String} object.
*/
public String getNodeName() {
return getClass().getName();
return getClass().getSimpleName();
}
/**
......
package edu.kit.formal.interpreter;
import edu.kit.formal.interpreter.funchdl.DefaultLookup;
import edu.kit.formal.proofscriptparser.Facade;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import edu.kit.formal.proofscriptparser.TransformAst;
import edu.kit.formal.proofscriptparser.ast.ProofScript;
import org.antlr.v4.runtime.CharStreams;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
/**
* Created by weigl on 22.06.2017.
*/
public class ProgramFlowVisitorTest {
@Test
public void test() throws IOException {
ScriptLanguageParser a = Facade.getParser(CharStreams.fromStream(getClass().getResourceAsStream("/edu/kit/formal/interpreter/simple1.txt")));
List<ProofScript> scripts = (List<ProofScript>) a.start().accept(new TransformAst());
ProofScript s = scripts.get(0);
ProgramFlowVisitor pfv = new ProgramFlowVisitor(new DefaultLookup());
s.accept(pfv);
System.out.println(pfv.asdot());
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment