Commit 22c5c2ac authored by Sarah Grebing's avatar Sarah Grebing
Browse files

Big Bugfix

parent 6019bbae
Pipeline #14592 failed with stage
in 2 minutes and 52 seconds
...@@ -100,9 +100,10 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -100,9 +100,10 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
*/ */
@Override @Override
public Object visit(ProofScript proofScript) { public Object visit(ProofScript proofScript) {
enterScope(proofScript);
//add vars //add vars
visit(proofScript.getSignature()); visit(proofScript.getSignature());
enterScope(proofScript);
proofScript.getBody().accept(this); proofScript.getBody().accept(this);
exitScope(proofScript); exitScope(proofScript);
return null; return null;
...@@ -519,6 +520,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -519,6 +520,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
@Override @Override
public Object visit(CallStatement call) { public Object visit(CallStatement call) {
enterScope(call); enterScope(call);
// System.out.println(stateStack.peek().hashCode());
//neuer VarScope //neuer VarScope
//enter new variable scope //enter new variable scope
VariableAssignment params = evaluateParameters(call.getParameters()); VariableAssignment params = evaluateParameters(call.getParameters());
...@@ -535,6 +537,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -535,6 +537,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
//pushState(newErrorState); //pushState(newErrorState);
} }
g.exitScope(); g.exitScope();
// System.out.println(stateStack.peek().hashCode());
exitScope(call); exitScope(call);
return null; return null;
} }
...@@ -614,11 +617,11 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -614,11 +617,11 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
@Override @Override
public Object visit(Signature signature) { public Object visit(Signature signature) {
exitScope(signature); // enterScope(signature);
GoalNode<T> node = getSelectedNode(); GoalNode<T> node = getSelectedNode();
node.enterScope(); node.enterScope();
signature.forEach(node::declareVariable); signature.forEach(node::declareVariable);
enterScope(signature); // exitScope(signature);
return null; return null;
} }
......
...@@ -112,4 +112,6 @@ public class GoalNode<T> { ...@@ -112,4 +112,6 @@ public class GoalNode<T> {
assignments = assignments.push(va); assignments = assignments.push(va);
return assignments; return assignments;
} }
} }
package edu.kit.iti.formal.psdbg.interpreter.data; package edu.kit.iti.formal.psdbg.interpreter.data;
import edu.kit.iti.formal.psdbg.parser.ast.ASTNode;
import edu.kit.iti.formal.psdbg.parser.ast.CaseStatement; import edu.kit.iti.formal.psdbg.parser.ast.CaseStatement;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import java.util.*; import java.util.Collections;
import java.util.stream.Collectors; import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RequiredArgsConstructor
@Data
public class InterpreterExtendedState<T> {
public class InterpreterExtendedState<T> extends State<T> { @Getter
@Setter
private Map<CaseStatement, List<GoalNode<T>>> mappingOfCaseToStates = new HashMap<>(); private Map<CaseStatement, List<GoalNode<T>>> mappingOfCaseToStates = new HashMap<>();
public InterpreterExtendedState(Collection<GoalNode<T>> goals, GoalNode selected) { /**
super(goals, selected); * State before the statement;
} */
@Getter
@Setter
private State<T> stateBeforeStmt;
/**
* State after execution of statement
*/
@Getter
@Setter
private State<T> stateAfterStmt;
/**
* Statement
*/
@Getter
@Setter
private ASTNode stmt;
/**
* Null if root
*/
@Getter
@Setter
private InterpreterExtendedState<T> predecessor;
public InterpreterExtendedState(Collection<GoalNode<T>> goals, GoalNode selected, Map<CaseStatement, List<GoalNode<T>>> casesMapping) {
super(goals, selected);
this.mappingOfCaseToStates = casesMapping;
}
public Map<CaseStatement, List<GoalNode<T>>> getMappingOfCaseToStates() { public InterpreterExtendedState(InterpreterExtendedState<T> pred) {
return mappingOfCaseToStates; if (pred == null) {
this.predecessor = null;
} else {
this.predecessor = pred;
}
} }
public void setMappingOfCaseToStates(Map<CaseStatement, List<GoalNode<T>>> mappingOfCaseToStates) { public InterpreterExtendedState copy() {
this.mappingOfCaseToStates = mappingOfCaseToStates; InterpreterExtendedState ext = new InterpreterExtendedState();
if (this.predecessor != null) {
ext.setPredecessor(this.predecessor.copy());
} else {
ext.predecessor = null;
}
ext.setStmt(stmt.copy());
if (stateAfterStmt != null) {
ext.setStateAfterStmt(this.stateAfterStmt.copy());
} else {
ext.setStateAfterStmt(null);
}
if (stateBeforeStmt != null) {
ext.setStateBeforeStmt(this.stateBeforeStmt.copy());
} else {
ext.setStateBeforeStmt(null);
}
return ext;
} }
/*
public List<GoalNode<T>> getClosedNodes() { public List<GoalNode<T>> getClosedNodes() {
return super.getGoals().stream().filter(nodes -> nodes.isClosed()).collect(Collectors.toList()); return super.getGoals().stream().filter(nodes -> nodes.isClosed()).collect(Collectors.toList());
} }
public List<GoalNode<T>> getopenNodes() { public List<GoalNode<T>> getOpenNodes() {
return super.getGoals().stream().filter(nodes -> !nodes.isClosed()).collect(Collectors.toList()); return super.getGoals().stream().filter(nodes -> !nodes.isClosed()).collect(Collectors.toList());
} }*/
public List<GoalNode<T>> getActiveGoalsForCase(CaseStatement caseStmt) { public List<GoalNode<T>> getActiveGoalsForCase(CaseStatement caseStmt) {
return mappingOfCaseToStates.getOrDefault(caseStmt, Collections.emptyList()); return mappingOfCaseToStates.getOrDefault(caseStmt, Collections.emptyList());
} }
@Override
public String toString() {
StringBuilder sb = new StringBuilder("\n%%%%%%%%%%%%%%%%%%%%%\nExtended State ");
if (getStmt() != null) {
sb.append(getStmt().getNodeName() + ": \n");
}
sb.append("State before Statement: \n");
if (stateBeforeStmt != null) {
stateBeforeStmt.getGoals().stream().map(tGoalNode -> tGoalNode.getData()).forEach(sb::append);
} else {
sb.append("Before is Empty");
}
sb.append("\nAfter Statement:\n");
if (stateAfterStmt != null) {
stateAfterStmt.getGoals().stream().map(tGoalNode -> tGoalNode.getData()).forEach(sb::append);
} else {
sb.append("After is empty");
}
sb.append("\n%%%%%%%%%%%%%%%%%%%%\n");
return sb.toString();
// this.stateBeforeStmt.toString()+"\n"+this.stateAfterStmt.toString();
}
//getuserSelected/ //getuserSelected/
//verfuegbar im case //verfuegbar im case
//map<case, listgoal> //map<case, listgoal>
......
...@@ -10,4 +10,5 @@ public interface GraphChangedListener { ...@@ -10,4 +10,5 @@ public interface GraphChangedListener {
abstract void graphChanged(NodeAddedEvent nodeAddedEvent); abstract void graphChanged(NodeAddedEvent nodeAddedEvent);
} }
...@@ -3,7 +3,6 @@ package edu.kit.iti.formal.psdbg.interpreter.graphs; ...@@ -3,7 +3,6 @@ package edu.kit.iti.formal.psdbg.interpreter.graphs;
import edu.kit.iti.formal.psdbg.interpreter.data.InterpreterExtendedState; import edu.kit.iti.formal.psdbg.interpreter.data.InterpreterExtendedState;
import edu.kit.iti.formal.psdbg.interpreter.data.State; import edu.kit.iti.formal.psdbg.interpreter.data.State;
import edu.kit.iti.formal.psdbg.parser.ast.ASTNode; import edu.kit.iti.formal.psdbg.parser.ast.ASTNode;
import edu.kit.iti.formal.psdbg.parser.ast.CaseStatement;
import java.util.LinkedList; import java.util.LinkedList;
...@@ -66,11 +65,8 @@ public class PTreeNode<T> { ...@@ -66,11 +65,8 @@ public class PTreeNode<T> {
return extendedState; return extendedState;
} }
public void setExtendedState(State<T> state) { public void setExtendedState(InterpreterExtendedState<T> extendedState) {
this.extendedState = new InterpreterExtendedState<>(state.getGoals(), state.getSelectedGoalNode()); this.extendedState = extendedState;
if (getScriptstmt() instanceof CaseStatement) {
}
} }
public String toString() { public String toString() {
...@@ -95,6 +91,7 @@ public class PTreeNode<T> { ...@@ -95,6 +91,7 @@ public class PTreeNode<T> {
} }
public String extendedStateToString() {
return this.extendedState.toString();
}
} }
...@@ -6,6 +6,7 @@ import com.google.common.graph.ValueGraphBuilder; ...@@ -6,6 +6,7 @@ import com.google.common.graph.ValueGraphBuilder;
import edu.kit.iti.formal.psdbg.interpreter.Interpreter; import edu.kit.iti.formal.psdbg.interpreter.Interpreter;
import edu.kit.iti.formal.psdbg.interpreter.NodeAddedEvent; import edu.kit.iti.formal.psdbg.interpreter.NodeAddedEvent;
import edu.kit.iti.formal.psdbg.interpreter.data.GoalNode; import edu.kit.iti.formal.psdbg.interpreter.data.GoalNode;
import edu.kit.iti.formal.psdbg.interpreter.data.InterpreterExtendedState;
import edu.kit.iti.formal.psdbg.interpreter.data.State; import edu.kit.iti.formal.psdbg.interpreter.data.State;
import edu.kit.iti.formal.psdbg.interpreter.exceptions.StateGraphException; import edu.kit.iti.formal.psdbg.interpreter.exceptions.StateGraphException;
import edu.kit.iti.formal.psdbg.parser.DefaultASTVisitor; import edu.kit.iti.formal.psdbg.parser.DefaultASTVisitor;
...@@ -13,16 +14,18 @@ import edu.kit.iti.formal.psdbg.parser.ast.*; ...@@ -13,16 +14,18 @@ import edu.kit.iti.formal.psdbg.parser.ast.*;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleObjectProperty;
import lombok.Getter; import lombok.Getter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.*; import java.util.*;
/** /**
* State graph that is computed on the fly while stepping through script * State graph that is computed on the fly while stepping through script
* A Node in the graph is a PTreeNode {@link PTreeNode} * A Node in the graph is a PTreeNode {@link PTreeNode}
* Edges are computed on the fly while * Edges are computed on the fly while
*/ */
public class StateGraphWrapper<T> { public class StateGraphWrapper<T> {
private static final Logger LOGGER = LogManager.getLogger(StateGraphWrapper.class);
/** /**
* Listeners getting informed when new node added to state graph * Listeners getting informed when new node added to state graph
...@@ -53,7 +56,7 @@ public class StateGraphWrapper<T> { ...@@ -53,7 +56,7 @@ public class StateGraphWrapper<T> {
private PTreeNode<T> lastNode; private PTreeNode<T> lastNode;
/** /**
* Mapping ASTNode to PTreeNode * Mapping ASTNode to PTreeNode for lookup
*/ */
private HashMap<ASTNode, PTreeNode> addedNodes = new LinkedHashMap<>(); private HashMap<ASTNode, PTreeNode> addedNodes = new LinkedHashMap<>();
...@@ -75,7 +78,7 @@ public class StateGraphWrapper<T> { ...@@ -75,7 +78,7 @@ public class StateGraphWrapper<T> {
this.mainScript = mainScript; this.mainScript = mainScript;
createRootNode(this.mainScript); // createRootNode(this.mainScript);
} }
...@@ -91,15 +94,29 @@ public class StateGraphWrapper<T> { ...@@ -91,15 +94,29 @@ public class StateGraphWrapper<T> {
public void createRootNode(ASTNode node) { public void createRootNode(ASTNode node) {
LOGGER.info("Creating Root for State graph");
PTreeNode newStateNode = new PTreeNode(node); PTreeNode newStateNode = new PTreeNode(node);
newStateNode.setState(currentInterpreter.getCurrentState().copy()); State<T> currentInterpreterStateCopy = currentInterpreter.getCurrentState().copy();
//copy current state before executing statement
newStateNode.setState(currentInterpreterStateCopy);
//create extended State
InterpreterExtendedState extState = new InterpreterExtendedState();
extState.setStmt(node);
extState.setStateBeforeStmt(currentInterpreterStateCopy);
newStateNode.setExtendedState(extState);
//add node to state graph
boolean res = stateGraph.addNode(newStateNode); boolean res = stateGraph.addNode(newStateNode);
if (!res) { if (!res) {
throw new StateGraphException("Could not create new state for ASTNode " + node + " and add it to the stategraph"); throw new StateGraphException("Could not create new state for ASTNode " + node + " and add it to the stategraph");
} else { } else {
this.root.set(newStateNode); this.root.set(newStateNode);
//to keep track of added nodes
addedNodes.put(node, newStateNode);
// fireNodeAdded(new NodeAddedEvent(null, newStateNode));
lastNode = newStateNode; lastNode = newStateNode;
} }
} }
public void setUpGraph() { public void setUpGraph() {
...@@ -112,19 +129,18 @@ public class StateGraphWrapper<T> { ...@@ -112,19 +129,18 @@ public class StateGraphWrapper<T> {
//careful TODO look for right edges //careful TODO look for right edges
//TODO handle endpoint of graph //TODO handle endpoint of graph
public PTreeNode getStepOver(PTreeNode statePointer) { public PTreeNode getStepOver(PTreeNode statePointer) {
/*Set<PTreeNode> pTreeNodesNeigbours = stateGraph.adjacentNodes(statePointer);
if(pTreeNodesNeigbours.isEmpty()){
return null;
}else{
pTreeNodesNeigbours.forEach(e-> System.out.println(e.getScriptstmt().getNodeName()+e.getScriptstmt().getStartPosition()));
}*/
if (statePointer == null) { if (statePointer == null) {
return this.rootProperty().get(); return this.rootProperty().get();
} }
//look for successors in the graph
Set<PTreeNode> successors = this.stateGraph.successors(statePointer); Set<PTreeNode> successors = this.stateGraph.successors(statePointer);
//if there are no successors they have to be computed therefore return null, to trigger the proof tree controller
if (successors.isEmpty()) { if (successors.isEmpty()) {
return null; return null;
} else { } else {
//if there are successors we want those which are connetced with State-Flow
Object[] sucs = successors.toArray(); Object[] sucs = successors.toArray();
getNodeWithEdgeType(statePointer, EdgeTypes.STATE_FLOW); getNodeWithEdgeType(statePointer, EdgeTypes.STATE_FLOW);
return (PTreeNode) sucs[0]; return (PTreeNode) sucs[0];
...@@ -136,11 +152,10 @@ public class StateGraphWrapper<T> { ...@@ -136,11 +152,10 @@ public class StateGraphWrapper<T> {
public PTreeNode getStepBack(PTreeNode statePointer) { public PTreeNode getStepBack(PTreeNode statePointer) {
Set<PTreeNode> pred = this.stateGraph.predecessors(statePointer); Set<PTreeNode> pred = this.stateGraph.predecessors(statePointer);
//if pred is empty we have reached the root
if (pred.isEmpty()) { if (pred.isEmpty()) {
return null; return statePointer;
} else { } else {
Object[] sucs = pred.toArray(); Object[] sucs = pred.toArray();
return (PTreeNode) sucs[0]; return (PTreeNode) sucs[0];
...@@ -179,71 +194,96 @@ public class StateGraphWrapper<T> { ...@@ -179,71 +194,96 @@ public class StateGraphWrapper<T> {
* @return * @return
*/ */
private Void createNewNode(ASTNode node) { private Void createNewNode(ASTNode node) {
//save old pointer of last node
State<T> lastState = lastNode.getState(); State<T> lastState = lastNode.getState();
//create a new ProofTreeNode in graph with ast node as parameter
PTreeNode newStateNode; PTreeNode newStateNode;
newStateNode = new PTreeNode(node); newStateNode = new PTreeNode(node);
//get the state before executing ast node to save it to the extended state
//State<KeyData> currentState = currentInterpreter.getCurrentState().copy(); //State<T> currentState = currentInterpreter.getCurrentState().copy();
//System.out.println("Equals of states " + currentState.equals(lastState)); InterpreterExtendedState<T> extState;
//newStateNode.setState(currentState); //set pointer to parent extended state
if (lastNode.getExtendedState() != null) {
extState = new InterpreterExtendedState<T>(lastNode.getExtendedState().copy());
} else {
extState = new InterpreterExtendedState<T>();
}
extState.setStmt(node);
// extState.setStateBeforeStmt(lastNode.getExtendedState().getStateAfterStmt());
extState.setStateBeforeStmt(lastState);
newStateNode.setExtendedState(extState);
stateGraph.addNode(newStateNode); stateGraph.addNode(newStateNode);
stateGraph.putEdgeValue(lastNode, newStateNode, EdgeTypes.STATE_FLOW); stateGraph.putEdgeValue(lastNode, newStateNode, EdgeTypes.STATE_FLOW);
//inform listeners about a new node in the graph
fireNodeAdded(new NodeAddedEvent(lastNode, newStateNode)); fireNodeAdded(new NodeAddedEvent(lastNode, newStateNode));
addedNodes.put(node, newStateNode); addedNodes.put(node, newStateNode);
lastNode = newStateNode; lastNode = newStateNode;
return null;
}
private void fireNodeAdded(NodeAddedEvent nodeAddedEvent) {
changeListeners.forEach(list -> {
Platform.runLater(() -> {
list.graphChanged(nodeAddedEvent);
// System.out.println("New StateGraphChange " + this.asdot());
});
});
}
/**
* If visting a cases statement special care has to be taken for the extended state
*
* @param casesStatement
* @return
*/
private Void createNewCasesNode(CasesStatement casesStatement) {
State<T> lastState = lastNode.getState();
PTreeNode newStateNode;
newStateNode = new PTreeNode(casesStatement);
State<T> currentState = currentInterpreter.getCurrentState().copy();
//merge des alten ext fehlt
InterpreterExtendedState extState = new InterpreterExtendedState(lastNode.getExtendedState().copy());
extState.setStateBeforeStmt(lastState);
extState.setStmt(casesStatement);
newStateNode.setExtendedState(extState);
stateGraph.addNode(newStateNode);
stateGraph.putEdgeValue(lastNode, newStateNode, EdgeTypes.STATE_FLOW);
fireNodeAdded(new NodeAddedEvent(lastNode, newStateNode));
addedNodes.put(casesStatement, newStateNode);
lastNode = newStateNode;
return null; return null;
} }
public Void addState(ASTNode node) { public Void addState(ASTNode node) {
//get node from addedNodes Map
PTreeNode newStateNode = addedNodes.get(node); PTreeNode newStateNode = addedNodes.get(node);
//copy Current Interpreter state
State<T> currentState = currentInterpreter.getCurrentState().copy(); State<T> currentState = currentInterpreter.getCurrentState().copy();
newStateNode.setState(currentState); //set the state
newStateNode.setExtendedState(currentState); if (node != this.root.get().getScriptstmt()) {
newStateNode.setState(currentState);
newStateNode.getExtendedState().setStateAfterStmt(currentState);
} else {
// newStateNode.setState(currentState);
newStateNode.getExtendedState().setStateAfterStmt(currentState);
}
System.out.println("\n%%%%%%%%%%%%%%%%%%\nExtended State for " + node.getStartPosition() + "\n" + newStateNode.getExtendedState().toString() + "\n%%%%%%%%%%%%%%%%%%\n");
return null; return null;
} }
public void install(Interpreter<T> interpreter) { public void install(Interpreter<T> interpreter) {
if (currentInterpreter != null) deinstall(interpreter); if (currentInterpreter != null) deinstall(interpreter);
interpreter.getEntryListeners().add(entryListener); interpreter.getEntryListeners().add(entryListener);
interpreter.getEntryListeners().add(exitListener); interpreter.getExitListeners().add(exitListener);
currentInterpreter = interpreter; currentInterpreter = interpreter;