Commit a559e78e authored by Sarah Grebing's avatar Sarah Grebing
Browse files

interim

parent e6361f70
package edu.kit.iti.formal.psdbg.interpreter.funchdl;
import edu.kit.iti.formal.psdbg.interpreter.Interpreter;
import edu.kit.iti.formal.psdbg.interpreter.data.KeyData;
import edu.kit.iti.formal.psdbg.interpreter.data.VariableAssignment;
import edu.kit.iti.formal.psdbg.parser.ast.CallStatement;
public class RigidRuleCommandHandler implements CommandHandler<KeyData> {
@Override
public boolean handles(CallStatement call) throws IllegalArgumentException {
return true;
}
@Override
public void evaluate(Interpreter<KeyData> interpreter, CallStatement call, VariableAssignment params) {
}
}
package edu.kit.iti.formal.psdbg.interpreter; package edu.kit.iti.formal.psdbg.interpreter;
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.graphs.PTreeNode; import edu.kit.iti.formal.psdbg.interpreter.graphs.PTreeNode;
import lombok.Getter; import lombok.Getter;
/**
* Event that is fired if a new state was added to a PTreeNode in the Stategraph
*/
public class StateAddedEvent { public class StateAddedEvent {
@Getter @Getter
PTreeNode changedNode; PTreeNode changedNode;
...@@ -12,9 +16,13 @@ public class StateAddedEvent { ...@@ -12,9 +16,13 @@ public class StateAddedEvent {
@Getter @Getter
State addedState; State addedState;
public StateAddedEvent(PTreeNode addedNode, State addedState) { @Getter
InterpreterExtendedState addedExtState;
public StateAddedEvent(PTreeNode addedNode, State addedState, InterpreterExtendedState addedExtState) {
this.changedNode = addedNode; this.changedNode = addedNode;
this.addedState = addedState; this.addedState = addedState;
this.addedExtState = addedExtState;
} }
public String toString() { public String toString() {
......
...@@ -91,7 +91,9 @@ public class InterpreterExtendedState<T> { ...@@ -91,7 +91,9 @@ public class InterpreterExtendedState<T> {
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder("\n%%%%%%%%%%%%%%%%%%%%%\nExtended State "); StringBuilder sb = new StringBuilder();
//sb.append("\n%%%%%%%%%%%%%%%%%%%%%\n");
sb.append("Extended State ");
if (getStmt() != null) { if (getStmt() != null) {
sb.append(getStmt().getNodeName() + ": \n"); sb.append(getStmt().getNodeName() + ": \n");
} }
...@@ -107,7 +109,14 @@ public class InterpreterExtendedState<T> { ...@@ -107,7 +109,14 @@ public class InterpreterExtendedState<T> {
} else { } else {
sb.append("After is empty"); sb.append("After is empty");
} }
sb.append("\n%%%%%%%%%%%%%%%%%%%%\n"); if (getMappingOfCaseToStates().containsKey(stmt)) {
sb.append("Case Stmt with");
getMappingOfCaseToStates().get(stmt).forEach(tGoalNode -> {
sb.append(tGoalNode.getData());
});
}
//sb.append("\n%%%%%%%%%%%%%%%%%%%%\n");
return sb.toString(); return sb.toString();
// this.stateBeforeStmt.toString()+"\n"+this.stateAfterStmt.toString(); // this.stateBeforeStmt.toString()+"\n"+this.stateAfterStmt.toString();
} }
......
...@@ -4,7 +4,7 @@ import edu.kit.iti.formal.psdbg.interpreter.data.InterpreterExtendedState; ...@@ -4,7 +4,7 @@ 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 java.util.LinkedList; import java.util.Stack;
/** /**
* Inner class representing nodes in the stategraph graph * Inner class representing nodes in the stategraph graph
...@@ -27,7 +27,15 @@ public class PTreeNode<T> { ...@@ -27,7 +27,15 @@ public class PTreeNode<T> {
/** /**
* Call context * Call context
*/ */
private LinkedList<ASTNode> context = new LinkedList<>(); private Stack<ASTNode> context = new Stack<>();
public Stack<ASTNode> getContext() {
return context;
}
public void setContext(Stack<ASTNode> context) {
this.context = context;
}
private boolean root; private boolean root;
...@@ -51,19 +59,11 @@ public class PTreeNode<T> { ...@@ -51,19 +59,11 @@ public class PTreeNode<T> {
this.scriptstmt = scriptstmt; this.scriptstmt = scriptstmt;
} }
public LinkedList<ASTNode> getContext() { public InterpreterExtendedState<T> getExtendedState() {
return context;
}
public void setContext(LinkedList<ASTNode> context) {
this.context = context;
}
InterpreterExtendedState<T> getExtendedState() {
return extendedState; return extendedState;
} }
void setExtendedState(InterpreterExtendedState<T> extendedState) { public void setExtendedState(InterpreterExtendedState<T> extendedState) {
this.extendedState = extendedState; this.extendedState = extendedState;
} }
...@@ -75,10 +75,15 @@ public class PTreeNode<T> { ...@@ -75,10 +75,15 @@ public class PTreeNode<T> {
} else { } else {
sb.append("Root Node"); sb.append("Root Node");
} }
if (hasState()) { /* if (hasState()) {
sb.append("\nState " + state.getGoals() + "\n"); sb.append("\nState " + state.getGoals() + "\n");
} else { } else {
sb.append("No State yet"); sb.append("No State yet");
}*/
if (extendedState != null) {
sb.append(extendedStateToString());
} else {
sb.append("No extended State yet");
} }
sb.append("}"); sb.append("}");
return sb.toString(); return sb.toString();
......
...@@ -18,6 +18,10 @@ import lombok.Getter; ...@@ -18,6 +18,10 @@ import lombok.Getter;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*; import java.util.*;
/** /**
...@@ -87,35 +91,7 @@ public class StateGraphWrapper<T> { ...@@ -87,35 +91,7 @@ public class StateGraphWrapper<T> {
} }
/** private static int counter = 0;
* Create the root Node for the state graph
* @param node
*/
public void createRootNode(ASTNode node) {
LOGGER.info("Creating Root for State graph with statement {}@{}", node.getNodeName(), node.getStartPosition());
PTreeNode<T> newStateNode = new PTreeNode<>(node);
State<T> currentInterpreterStateCopy = currentInterpreter.getCurrentState().copy();
//copy current state before executing statement
newStateNode.setState(currentInterpreterStateCopy);
//create extended State
InterpreterExtendedState<T> extState = new InterpreterExtendedState<>();
extState.setStmt(node);
extState.setStateBeforeStmt(currentInterpreterStateCopy);
newStateNode.setExtendedState(extState);
//add node to state graph
boolean res = stateGraph.addNode(newStateNode);
if (!res) {
throw new StateGraphException("Could not create new state for ASTNode " + node + " and add it to the stategraph");
} else {
this.root.set(newStateNode);
//to keep track of added nodes
addedNodes.put(node, newStateNode);
// fireNodeAdded(new NodeAddedEvent(null, newStateNode));
lastNode = newStateNode;
}
}
public MutableValueGraph<PTreeNode<T>, EdgeTypes> getStateGraph() { public MutableValueGraph<PTreeNode<T>, EdgeTypes> getStateGraph() {
return stateGraph; return stateGraph;
...@@ -207,6 +183,41 @@ public class StateGraphWrapper<T> { ...@@ -207,6 +183,41 @@ public class StateGraphWrapper<T> {
return createNewNode(node, false); return createNewNode(node, false);
} }
private static Path graph = Paths.get("/tmp/test.dot");
/**
* Create the root Node for the state graph
*
* @param node
*/
public void createRootNode(ASTNode node) {
LOGGER.info("Creating Root for State graph with statement {}@{}", node.getNodeName(), node.getStartPosition());
PTreeNode<T> newStateNode = new PTreeNode<>(node);
newStateNode.setContext(new Stack<>());
newStateNode.getContext().push(node);
State<T> currentInterpreterStateCopy = currentInterpreter.getCurrentState().copy();
//copy current state before executing statement
newStateNode.setState(currentInterpreterStateCopy);
//create extended State
InterpreterExtendedState<T> extState = new InterpreterExtendedState<>();
extState.setStmt(node);
extState.setStateBeforeStmt(currentInterpreterStateCopy);
newStateNode.setExtendedState(extState);
//add node to state graph
boolean res = stateGraph.addNode(newStateNode);
if (!res) {
throw new StateGraphException("Could not create new state for ASTNode " + node + " and add it to the stategraph");
} else {
this.root.set(newStateNode);
//to keep track of added nodes
addedNodes.put(node, newStateNode);
// fireNodeAdded(new NodeAddedEvent(null, newStateNode));
lastNode = newStateNode;
}
}
/** /**
* Create a new node for the state graph and add edges to already existing nodes * Create a new node for the state graph and add edges to already existing nodes
* *
...@@ -216,26 +227,49 @@ public class StateGraphWrapper<T> { ...@@ -216,26 +227,49 @@ public class StateGraphWrapper<T> {
private Void createNewNode(ASTNode node, boolean isCasesStmt) { private Void createNewNode(ASTNode node, boolean isCasesStmt) {
LOGGER.info("Creating Node for State graph with statement {}@{}", node.getNodeName(), node.getStartPosition()); LOGGER.info("Creating Node for State graph with statement {}@{}", node.getNodeName(), node.getStartPosition());
//save old pointer of last node //save old pointer of last node
State<T> lastState = lastNode.getState(); //State<T> lastState = lastNode.getState();
State<T> lastState;
// if(lastNode.getExtendedState().getStateAfterStmt() != null) {
// lastState = lastNode.getExtendedState().getStateAfterStmt().copy();
// }else{
lastState = currentInterpreter.getCurrentState().copy();
// }
//create a new ProofTreeNode in graph with ast node as parameter //create a new ProofTreeNode in graph with ast node as parameter
PTreeNode<T> newStateNode = new PTreeNode<>(node); PTreeNode<T> newStateNode = new PTreeNode<>(node);
newStateNode.setContext(lastNode.getContext());
//get the state before executing ast node to save it to the extended state //get the state before executing ast node to save it to the extended state
//State<T> currentState = currentInterpreter.getCurrentState().copy(); //State<T> currentState = currentInterpreter.getCurrentState().copy();
InterpreterExtendedState<T> extState; InterpreterExtendedState<T> extState;
if (isCasesStmt) { if (isCasesStmt) {
newStateNode.getContext().push(node);
extState = new InterpreterExtendedState<>(lastNode.getExtendedState().copy()); extState = new InterpreterExtendedState<>(lastNode.getExtendedState().copy());
extState.setStmt(node); extState.setStmt(node);
extState.setStateBeforeStmt(lastState); extState.setStateBeforeStmt(lastState.copy());
newStateNode.setState(lastState); Map<CaseStatement, List<GoalNode<T>>> mappingOfCaseToStates = extState.getMappingOfCaseToStates();
//TODO: implement special handling for cases stmts CasesStatement cs = (CasesStatement) node;
//extState = null; //initialize the cases in the mapping map
cs.getCases().forEach(caseStatement -> {
mappingOfCaseToStates.put(caseStatement, Collections.emptyList());
});
extState.setMappingOfCaseToStates(mappingOfCaseToStates);
//TODO default case
newStateNode.setState(lastState.copy());
} else { } else {
//set pointer to parent extended state //set pointer to parent extended state
extState = new InterpreterExtendedState<>(lastNode.getExtendedState().copy()); extState = new InterpreterExtendedState<>(lastNode.getExtendedState().copy());
extState.setStmt(node); extState.setStmt(node);
extState.setStateBeforeStmt(lastState); //check whether case statement
if (extState.getMappingOfCaseToStates().containsKey(node)) {
extState.getMappingOfCaseToStates().get(node).add(lastState.getSelectedGoalNode().deepCopy());
extState.setStateBeforeStmt(new State<T>(extState.getMappingOfCaseToStates().get(node), lastState.getSelectedGoalNode().deepCopy()));
} else {
extState.setStateBeforeStmt(lastState);
}
} }
newStateNode.setExtendedState(extState); newStateNode.setExtendedState(extState);
...@@ -250,31 +284,15 @@ public class StateGraphWrapper<T> { ...@@ -250,31 +284,15 @@ public class StateGraphWrapper<T> {
} }
private void fireNodeAdded(NodeAddedEvent nodeAddedEvent) { private void fireNodeAdded(NodeAddedEvent nodeAddedEvent) {
counter++;
System.out.println("XXXXXXXXXX counter = " + counter);
changeListeners.forEach(list -> Platform.runLater(() -> { changeListeners.forEach(list -> Platform.runLater(() -> {
list.graphChanged(nodeAddedEvent); list.graphChanged(nodeAddedEvent);
LOGGER.debug("New StateGraphChange " + this.asdot()); //TODO
LOGGER.info("New StateGraphChange \n%%%%%%" + this.asdot() + "\n%%%%%%%");
})); }));
} }
private Void addState(ASTNode node) {
LOGGER.info("Adding a new state for statement {}@{}", node.getNodeName(), node.getStartPosition());
//get node from addedNodes Map
PTreeNode<T> newStateNode = addedNodes.get(node);
//copy Current Interpreter state
State<T> currentState = currentInterpreter.getCurrentState().copy();
//set the state
if (node != this.root.get().getScriptstmt()) {
newStateNode.setState(currentState);
newStateNode.getExtendedState().setStateAfterStmt(currentState);
} else {
// newStateNode.setState(currentState);
newStateNode.getExtendedState().setStateAfterStmt(currentState);
}
fireStateAdded(new StateAddedEvent(newStateNode, currentState));
// LOGGER.info("\n%%%%%%%%%%%%%%%%%%\n Extended state for {} updated with {} \n%%%%%%%%%%%%%%%%%%\n", node.getStartPosition(), newStateNode.getExtendedState().toString());
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);
...@@ -337,6 +355,7 @@ public class StateGraphWrapper<T> { ...@@ -337,6 +355,7 @@ public class StateGraphWrapper<T> {
.append(n.getScriptstmt().getNodeName()) .append(n.getScriptstmt().getNodeName())
.append("@") .append("@")
.append(n.getScriptstmt().getStartPosition().getLineNumber()) .append(n.getScriptstmt().getStartPosition().getLineNumber())
.append(n.extendedStateToString())
.append("\"]\n"); .append("\"]\n");
}); });
...@@ -350,9 +369,47 @@ public class StateGraphWrapper<T> { ...@@ -350,9 +369,47 @@ public class StateGraphWrapper<T> {
}); });
sb.append("}"); sb.append("}");
//TODO: debugging
try (java.io.BufferedWriter writer =
java.nio.file.Files.newBufferedWriter(graph, StandardCharsets.UTF_8)
) {
writer.write(sb.toString());
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
//TODO: end debugging
return sb.toString(); return sb.toString();
} }
private Void addState(ASTNode node) {
LOGGER.info("Adding a new state for statement {}@{}", node.getNodeName(), node.getStartPosition());
//get node from addedNodes Map
PTreeNode<T> newStateNode = addedNodes.get(node);
//copy Current Interpreter state
State<T> currentState = currentInterpreter.getCurrentState().copy();
//set the state
if (node != this.root.get().getScriptstmt()) {
newStateNode.setState(currentState);
newStateNode.getExtendedState().setStateAfterStmt(currentState);
if (newStateNode.getContext().peek().equals(node)) {
newStateNode.getContext().pop();
}
} else {
// newStateNode.setState(currentState);
newStateNode.getExtendedState().setStateAfterStmt(currentState);
if (newStateNode.getContext().peek().equals(node)) {
newStateNode.getContext().pop();
}
}
fireStateAdded(new StateAddedEvent(newStateNode, currentState, newStateNode.getExtendedState()));
LOGGER.debug("Extended state for {} updated with {} \n", node.getStartPosition(), newStateNode.getExtendedState().toString());
return null;
}
private class EntryListener extends DefaultASTVisitor<Void> { private class EntryListener extends DefaultASTVisitor<Void> {
@Override @Override
public Void visit(ProofScript proofScript) { public Void visit(ProofScript proofScript) {
...@@ -431,11 +488,11 @@ public class StateGraphWrapper<T> { ...@@ -431,11 +488,11 @@ public class StateGraphWrapper<T> {
return addState(assignment); return addState(assignment);
} }
@Override /* @Override
public Void visit(CasesStatement casesStatement) { public Void visit(CasesStatement casesStatement) {
return addState(casesStatement); return addState(casesStatement);
} }
*/
/*@Override /*@Override
public Void visit(CaseStatement caseStatement) { public Void visit(CaseStatement caseStatement) {
return addState(caseStatement); return addState(caseStatement);
...@@ -472,10 +529,10 @@ public class StateGraphWrapper<T> { ...@@ -472,10 +529,10 @@ public class StateGraphWrapper<T> {
return addState(tryCase); return addState(tryCase);
} }
@Override /* @Override
public Void visit(SimpleCaseStatement simpleCaseStatement) { public Void visit(SimpleCaseStatement simpleCaseStatement) {
return addState(simpleCaseStatement); return addState(simpleCaseStatement);
} }*/
@Override @Override
public Void visit(ClosesCase closesCase) { public Void visit(ClosesCase closesCase) {
......
...@@ -10,6 +10,7 @@ import edu.kit.iti.formal.psdbg.interpreter.KeyInterpreter; ...@@ -10,6 +10,7 @@ import edu.kit.iti.formal.psdbg.interpreter.KeyInterpreter;
import edu.kit.iti.formal.psdbg.interpreter.NodeAddedEvent; import edu.kit.iti.formal.psdbg.interpreter.NodeAddedEvent;
import edu.kit.iti.formal.psdbg.interpreter.StateAddedEvent; import edu.kit.iti.formal.psdbg.interpreter.StateAddedEvent;
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.KeyData; import edu.kit.iti.formal.psdbg.interpreter.data.KeyData;
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.graphs.*; import edu.kit.iti.formal.psdbg.interpreter.graphs.*;
...@@ -118,12 +119,9 @@ public class ProofTreeController { ...@@ -118,12 +119,9 @@ public class ProofTreeController {
@Override @Override
public void graphChanged(StateAddedEvent stateAddedEvent) { public void graphChanged(StateAddedEvent stateAddedEvent) {
PTreeNode changedNode = stateAddedEvent.getChangedNode(); PTreeNode changedNode = stateAddedEvent.getChangedNode();
// if(changedNode.getScriptstmt().equals(statePointer.getScriptstmt())){
LOGGER.info("Graph changed by adding a state to PTreeNode: {} and the statepointer points to {}", stateAddedEvent, statePointer); LOGGER.info("Graph changed by adding a state to PTreeNode: {} and the statepointer points to {}", stateAddedEvent, statePointer);
nextComputedNode.set(changedNode); nextComputedNode.set(changedNode);
Events.fire(new Events.NewNodeExecuted(changedNode.getScriptstmt())); Events.fire(new Events.NewNodeExecuted(changedNode.getScriptstmt()));
// }
} }
}; };
...@@ -145,33 +143,9 @@ public class ProofTreeController { ...@@ -145,33 +143,9 @@ public class ProofTreeController {
* and bind properties * and bind properties
*/ */
public ProofTreeController() { public ProofTreeController() {
/* blocker.currentStateProperty().addListener((observable, oldValue, newValue) -> blocker.currentStateProperty().addListener((observable, oldValue, newValue) -> {
{ LOGGER.info("The state in the Puppetmaster changed to " + newValue.toString());
Platform.runLater(() -> {
if (newValue != null) {
setNewState(newValue);
}
});
});*/
//get state from blocker, who communicates with interpreter
//this.currentSelectedGoal.bindBidirectional(blocker.currentSelectedGoalProperty());
/* blocker.currentSelectedGoalProperty().addListener((observable, oldValue, newValue) -> {
Platform.runLater(() -> {
if (newValue != null) {
this.setCurrentSelectedGoal(newValue);
}
});
}); });
blocker.currentGoalsProperty().addListener((observable, oldValue, newValue) -> {
Platform.runLater(() -> {
if (newValue != null) {
this.setCurrentGoals(newValue);
}