Commit 79786fc6 authored by Sarah Grebing's avatar Sarah Grebing

Reverted changes for cases and some bugfixes

parent 79fbb50d
Pipeline #13264 failed with stage
in 10 seconds
...@@ -112,9 +112,9 @@ public class ASTChanger extends DefaultASTVisitor<ASTNode> { ...@@ -112,9 +112,9 @@ public class ASTChanger extends DefaultASTVisitor<ASTNode> {
} }
@Override @Override
public tryCase visit(tryCase tryCase) { public TryCase visit(TryCase TryCase) {
tryCase.getBody().accept(this); TryCase.getBody().accept(this);
return tryCase; return TryCase;
} }
@Override @Override
......
...@@ -161,8 +161,8 @@ public interface ASTTraversal<T> extends Visitor<T> { ...@@ -161,8 +161,8 @@ public interface ASTTraversal<T> extends Visitor<T> {
} }
@Override @Override
default T visit(tryCase tryCase) { default T visit(TryCase TryCase) {
tryCase.getBody().accept(this); TryCase.getBody().accept(this);
return null; return null;
} }
......
...@@ -133,8 +133,8 @@ public class DefaultASTVisitor<T> implements Visitor<T> { ...@@ -133,8 +133,8 @@ public class DefaultASTVisitor<T> implements Visitor<T> {
} }
@Override @Override
public T visit(tryCase tryCase) { public T visit(TryCase TryCase) {
return defaultVisit(tryCase); return defaultVisit(TryCase);
} }
@Override @Override
......
...@@ -319,10 +319,10 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -319,10 +319,10 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
public Object visitCasesList(ScriptLanguageParser.CasesListContext ctx) { public Object visitCasesList(ScriptLanguageParser.CasesListContext ctx) {
if (ctx.TRY() != null) { if (ctx.TRY() != null) {
tryCase tryCase = new tryCase(); TryCase TryCase = new TryCase();
tryCase.setRuleContext(ctx); TryCase.setRuleContext(ctx);
tryCase.setBody((Statements) ctx.body.accept(this)); TryCase.setBody((Statements) ctx.body.accept(this));
return tryCase; return TryCase;
} }
if (ctx.closesScript != null) { if (ctx.closesScript != null) {
ClosesCase closesCase = new ClosesCase(); ClosesCase closesCase = new ClosesCase();
......
...@@ -68,7 +68,7 @@ public interface Visitor<T> { ...@@ -68,7 +68,7 @@ public interface Visitor<T> {
T visit(UnaryExpression e); T visit(UnaryExpression e);
T visit(tryCase tryCase); T visit(TryCase TryCase);
T visit(SimpleCaseStatement simpleCaseStatement); T visit(SimpleCaseStatement simpleCaseStatement);
......
...@@ -9,10 +9,10 @@ import lombok.NoArgsConstructor; ...@@ -9,10 +9,10 @@ import lombok.NoArgsConstructor;
*/ */
@Data @Data
@NoArgsConstructor @NoArgsConstructor
public class tryCase extends CaseStatement { public class TryCase extends CaseStatement {
private boolean isClosedStmt = true; private boolean isClosedStmt = true;
public tryCase(Statements body) { public TryCase(Statements body) {
this.body = body; this.body = body;
} }
...@@ -28,7 +28,7 @@ public class tryCase extends CaseStatement { ...@@ -28,7 +28,7 @@ public class tryCase extends CaseStatement {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public tryCase copy() { public TryCase copy() {
return new tryCase(body.copy()); return new TryCase(body.copy());
} }
} }
...@@ -12,7 +12,7 @@ import edu.kit.iti.formal.psdbg.interpreter.data.State; ...@@ -12,7 +12,7 @@ import edu.kit.iti.formal.psdbg.interpreter.data.State;
import edu.kit.iti.formal.psdbg.interpreter.data.VariableAssignment; import edu.kit.iti.formal.psdbg.interpreter.data.VariableAssignment;
import edu.kit.iti.formal.psdbg.interpreter.funchdl.CommandLookup; import edu.kit.iti.formal.psdbg.interpreter.funchdl.CommandLookup;
import edu.kit.iti.formal.psdbg.parser.ast.ClosesCase; import edu.kit.iti.formal.psdbg.parser.ast.ClosesCase;
import edu.kit.iti.formal.psdbg.parser.ast.tryCase; import edu.kit.iti.formal.psdbg.parser.ast.TryCase;
import edu.kit.iti.formal.psdbg.parser.types.SimpleType; import edu.kit.iti.formal.psdbg.parser.types.SimpleType;
import lombok.Getter; import lombok.Getter;
import org.key_project.util.collection.ImmutableList; import org.key_project.util.collection.ImmutableList;
...@@ -46,24 +46,43 @@ public class KeyInterpreter extends Interpreter<KeyData> { ...@@ -46,24 +46,43 @@ public class KeyInterpreter extends Interpreter<KeyData> {
@Override @Override
public Object visit(ClosesCase closesCase) { public Object visit(ClosesCase closesCase) {
State<KeyData> currentStateToMatch = peekState(); State<KeyData> currentStateToMatch = peekState();
State<KeyData> currentStateToMatchCopy = peekState().copy(); //deepcopy State<KeyData> currentStateToMatchCopy = currentStateToMatch.copy(); //deepcopy
GoalNode<KeyData> selectedGoalNode = currentStateToMatch.getSelectedGoalNode(); GoalNode<KeyData> selectedGoalNode = currentStateToMatch.getSelectedGoalNode();
GoalNode<KeyData> selectedGoalCopy = currentStateToMatch.getSelectedGoalNode().deepCopy(); //deepcopy GoalNode<KeyData> selectedGoalCopy = currentStateToMatch.getSelectedGoalNode().deepCopy(); //deepcopy
enterScope(closesCase); enterScope(closesCase);
// executeBody(closesCase.getClosesScript(), ) //execute closesscript
executeBody(closesCase.getClosesScript(), selectedGoalNode, new VariableAssignment(selectedGoalNode.getAssignments()));
//check whether script closed proof
State<KeyData> stateafterIsClosable = peekState();
List<GoalNode<KeyData>> goals = stateafterIsClosable.getGoals();
boolean allClosed = true;
for (GoalNode<KeyData> goal : goals) {
KeyData data = (KeyData) goal.getData();
if (!data.getNode().isClosed()) {
allClosed = false;
break;
}
}
//prune proof
System.out.println("The closes script " + (allClosed ? "closed the proof.\n" : "did not close the proof.\n") + "Rolling Back proof now.");
Proof currentKeYproof = selectedGoalNode.getData().getProof();
ImmutableList<Goal> subtreeGoals = currentKeYproof.getSubtreeGoals(((KeyData) selectedGoalNode.getData()).getNode());
currentKeYproof.pruneProof(selectedGoalCopy.getData().getNode());
popState();
pushState(currentStateToMatchCopy);
exitScope(closesCase); exitScope(closesCase);
return false; return allClosed;
} }
@Override @Override
public Object visit(tryCase tryCase) { public Object visit(TryCase TryCase) {
State<KeyData> currentStateToMatch = peekState(); State<KeyData> currentStateToMatch = peekState();
State<KeyData> currentStateToMatchCopy = peekState().copy(); //deepcopy State<KeyData> currentStateToMatchCopy = currentStateToMatch.copy(); //deepcopy
GoalNode<KeyData> selectedGoalNode = currentStateToMatch.getSelectedGoalNode(); GoalNode<KeyData> selectedGoalNode = currentStateToMatch.getSelectedGoalNode();
GoalNode<KeyData> selectedGoalCopy = currentStateToMatch.getSelectedGoalNode().deepCopy(); //deepcopy GoalNode<KeyData> selectedGoalCopy = currentStateToMatch.getSelectedGoalNode().deepCopy(); //deepcopy
enterScope(tryCase); enterScope(TryCase);
executeBody(tryCase.getBody(), selectedGoalNode, new VariableAssignment(selectedGoalNode.getAssignments())); executeBody(TryCase.getBody(), selectedGoalNode, new VariableAssignment(selectedGoalNode.getAssignments()));
State<KeyData> stateafterIsClosable = peekState(); State<KeyData> stateafterIsClosable = peekState();
List<GoalNode<KeyData>> goals = stateafterIsClosable.getGoals(); List<GoalNode<KeyData>> goals = stateafterIsClosable.getGoals();
boolean allClosed = true; boolean allClosed = true;
...@@ -83,7 +102,7 @@ public class KeyInterpreter extends Interpreter<KeyData> { ...@@ -83,7 +102,7 @@ public class KeyInterpreter extends Interpreter<KeyData> {
pushState(currentStateToMatchCopy); pushState(currentStateToMatchCopy);
} }
//check if state is closed //check if state is closed
exitScope(tryCase); exitScope(TryCase);
return allClosed; return allClosed;
/* //executeBody and if goal is closed afterwards return true /* //executeBody and if goal is closed afterwards return true
......
...@@ -8,7 +8,6 @@ import edu.kit.iti.formal.psdbg.parser.ast.ProofScript; ...@@ -8,7 +8,6 @@ import edu.kit.iti.formal.psdbg.parser.ast.ProofScript;
import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CharStreams;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
...@@ -35,7 +34,7 @@ public class KeYInterpreterTest { ...@@ -35,7 +34,7 @@ public class KeYInterpreterTest {
return i; return i;
} }
@Ignore
@Test @Test
public void testIsClosable() throws IOException, ProblemLoaderException { public void testIsClosable() throws IOException, ProblemLoaderException {
facade.loadKeyFileSync(new File("src/test/resources/edu/kit/iti/formal/psdbg/interpreter/contraposition/contraposition.key")); facade.loadKeyFileSync(new File("src/test/resources/edu/kit/iti/formal/psdbg/interpreter/contraposition/contraposition.key"));
......
package edu.kit.iti.formal.psdbg.interpreter; package edu.kit.iti.formal.psdbg.interpreter;
import com.google.common.collect.Sets;
import edu.kit.iti.formal.psdbg.interpreter.assignhook.VariableAssignmentHook; import edu.kit.iti.formal.psdbg.interpreter.assignhook.VariableAssignmentHook;
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.State; import edu.kit.iti.formal.psdbg.interpreter.data.State;
...@@ -13,10 +12,8 @@ import edu.kit.iti.formal.psdbg.parser.Visitor; ...@@ -13,10 +12,8 @@ import edu.kit.iti.formal.psdbg.parser.Visitor;
import edu.kit.iti.formal.psdbg.parser.ast.*; import edu.kit.iti.formal.psdbg.parser.ast.*;
import edu.kit.iti.formal.psdbg.parser.data.Value; import edu.kit.iti.formal.psdbg.parser.data.Value;
import edu.kit.iti.formal.psdbg.parser.types.Type; import edu.kit.iti.formal.psdbg.parser.types.Type;
import javafx.util.Pair;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.util.*; import java.util.*;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -167,38 +164,13 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -167,38 +164,13 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
return null; return null;
} }
@Override
public Object visit(tryCase tryCase) {
enterScope(tryCase);
exitScope(tryCase);
return false;
}
@Override
public Object visit(SimpleCaseStatement simpleCaseStatement) {
Expression matchExpression = simpleCaseStatement.getGuard();
State<T> currentStateToMatch = peekState();
GoalNode<T> selectedGoal = currentStateToMatch.getSelectedGoalNode();
VariableAssignment va = evaluateMatchInGoal(matchExpression, selectedGoal);
if (va != null) {
enterScope(simpleCaseStatement);
executeBody(simpleCaseStatement.getBody(), selectedGoal, va);
exitScope(simpleCaseStatement);
return true;
} else {
return false;
}
}
/** /**
* Old visit method, for deocumentation purposes
* *
* @param casesStatement * @param casesStatement
* @return * @return
*/ */
@Deprecated @Override
public Object visitOld(CasesStatement casesStatement) { public Object visit(CasesStatement casesStatement) {
enterScope(casesStatement); enterScope(casesStatement);
State<T> beforeCases = peekState(); State<T> beforeCases = peekState();
...@@ -212,7 +184,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -212,7 +184,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
//handle cases //handle cases
List<CaseStatement> cases = casesStatement.getCases(); List<CaseStatement> cases = casesStatement.getCases();
Map<GoalNode, Pair<VariableAssignment, Statements>> goalToCaseMapping = matchGoalsToCases(allGoalsBeforeCases, casesStatement); // Map<GoalNode, Pair<VariableAssignment, Statements>> goalToCaseMapping = matchGoalsToCases(allGoalsBeforeCases, casesStatement);
for (GoalNode<T> goal : allGoalsBeforeCases) { for (GoalNode<T> goal : allGoalsBeforeCases) {
newState(goal); //to allow the visit method for the case stmt to retrieve goal newState(goal); //to allow the visit method for the case stmt to retrieve goal
...@@ -266,13 +238,37 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -266,13 +238,37 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
return null; return null;
} }
/* @Override
public Object visit(SimpleCaseStatement simpleCaseStatement) {
Expression matchExpression = simpleCaseStatement.getGuard();
State<T> currentStateToMatch = peekState();
GoalNode<T> selectedGoal = currentStateToMatch.getSelectedGoalNode();
VariableAssignment va = evaluateMatchInGoal(matchExpression, selectedGoal);
if (va != null) {
enterScope(simpleCaseStatement);
executeBody(simpleCaseStatement.getBody(), selectedGoal, va);
exitScope(simpleCaseStatement);
return true;
} else {
return false;
}
}*/
@Override
public Object visit(TryCase TryCase) {
enterScope(TryCase);
exitScope(TryCase);
return false;
}
/** /**
* Computes which goals are handled by the different cases in the order the cases appear in the script * Computes which goals are handled by the different cases in the order the cases appear in the script
* @param allGoalsBeforeCases * @param allGoalsBeforeCases
* @param cases all cases as ordered list * @param cases all cases as ordered list
* @return a mapping of each goal to the matched case body together with variableassignments from the matching process * @return a mapping of each goal to the matched case body together with variableassignments from the matching process
*/ */
private Map<GoalNode, Pair<VariableAssignment, Statements>> matchGoalsToCases(List<GoalNode<T>> allGoalsBeforeCases, CasesStatement cases) { /* private Map<GoalNode, Pair<VariableAssignment, Statements>> matchGoalsToCases(List<GoalNode<T>> allGoalsBeforeCases, CasesStatement cases) {
//list of cases //list of cases
List<CaseStatement> caseStmts = cases.getCases(); List<CaseStatement> caseStmts = cases.getCases();
//remaining goals //remaining goals
...@@ -302,7 +298,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -302,7 +298,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
return returnMap; return returnMap;
} }*/
/** /**
* Match a set of goal nodes against a matchpattern of a case and return the matched goals together with instantiated variables * Match a set of goal nodes against a matchpattern of a case and return the matched goals together with instantiated variables
...@@ -311,7 +307,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -311,7 +307,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
* @param aCase * @param aCase
* @return * @return
*/ */
private Map<GoalNode<T>, VariableAssignment> matchGoal(Set<GoalNode<T>> allGoalsBeforeCases, CaseStatement aCase) { /* private Map<GoalNode<T>, VariableAssignment> matchGoal(Set<GoalNode<T>> allGoalsBeforeCases, CaseStatement aCase) {
HashMap<GoalNode<T>, VariableAssignment> matchedGoals = new HashMap<>(); HashMap<GoalNode<T>, VariableAssignment> matchedGoals = new HashMap<>();
if (!aCase.isClosedStmt()) { if (!aCase.isClosedStmt()) {
...@@ -326,16 +322,41 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -326,16 +322,41 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
return matchedGoals; return matchedGoals;
} else { } else {
//handle try and closes //handle try and closes
throw new NotImplementedException(); if(aCase instanceof ClosesCase){
ClosesCase closesCase = (ClosesCase) aCase;
for (GoalNode<T> goal : allGoalsBeforeCases) {
//put state onto stack
newState(goal);
boolean matched = (Boolean) closesCase.accept(this);
if (matched) {
matchedGoals.put(goal, new VariableAssignment(null));
}
}
return matchedGoals;
}else{
TryCase tryCase = (TryCase) aCase;
for (GoalNode<T> goal : allGoalsBeforeCases) {
//put state onto stack
newState(goal);
boolean matched = (Boolean) tryCase.accept(this);
if (matched) {
matchedGoals.put(goal, new VariableAssignment(null));
}
}
return matchedGoals;
}
} }
} }*/
/** /**
* @param casesStatement * @param casesStatement
* @return * @return
*/ */
@Override /*
public Object visit(CasesStatement casesStatement) { public Object visitOld(CasesStatement casesStatement) {
enterScope(casesStatement); enterScope(casesStatement);
State<T> beforeCases = peekState(); State<T> beforeCases = peekState();
...@@ -352,13 +373,17 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -352,13 +373,17 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
Map<GoalNode, Pair<VariableAssignment, Statements>> goalToCaseMapping = matchGoalsToCases(allGoalsBeforeCases, casesStatement); Map<GoalNode, Pair<VariableAssignment, Statements>> goalToCaseMapping = matchGoalsToCases(allGoalsBeforeCases, casesStatement);
goalToCaseMapping.forEach((goalNode, variableAssignmentStatementsPair) -> { goalToCaseMapping.forEach((goalNode, variableAssignmentStatementsPair) -> {
State<T> createdState = newState(goalNode); if(variableAssignmentStatementsPair.getValue().isEmpty()){
executeBody(variableAssignmentStatementsPair.getValue(), goalNode, variableAssignmentStatementsPair.getKey()); //TODO this is the try case
//stmts.accept(this); }else {
State<T> createdState = newState(goalNode);
State<T> stateAfterCase = popState(); //remove state from stack executeBody(variableAssignmentStatementsPair.getValue(), goalNode, variableAssignmentStatementsPair.getKey());
if (stateAfterCase.getGoals() != null) { //stmts.accept(this);
resultingGoals.addAll(stateAfterCase.getGoals());
State<T> stateAfterCase = popState(); //remove state from stack
if (stateAfterCase.getGoals() != null) {
resultingGoals.addAll(stateAfterCase.getGoals());
}
} }
}); });
...@@ -381,7 +406,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -381,7 +406,7 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
//stateStack.peek().getGoals().removeAll(beforeCases.getGoals()); //stateStack.peek().getGoals().removeAll(beforeCases.getGoals());
exitScope(casesStatement); exitScope(casesStatement);
return null; return null;
} }*/
/** /**
* Evaluate a match in a specific goal * Evaluate a match in a specific goal
......
...@@ -74,13 +74,21 @@ public class State<T> { ...@@ -74,13 +74,21 @@ public class State<T> {
} }
/** /**
* TODO correct this method, atm does nothing helpful! * Deep Copy state
* *
* @return * @return
*/ */
public State<T> copy() { public State<T> copy() {
List<GoalNode<T>> copiedGoals = new ArrayList<>(goals); List<GoalNode<T>> copiedGoals = new ArrayList<>();
GoalNode<T> refToSelGoal = selectedGoalNode; GoalNode<T> refToSelGoal = null;
for (GoalNode<T> goal : goals) {
GoalNode<T> deepcopy = goal.deepCopy();
copiedGoals.add(deepcopy);
if (goal.equals(selectedGoalNode)) {
refToSelGoal = deepcopy;
}
}
return new State<T>(copiedGoals, refToSelGoal); return new State<T>(copiedGoals, refToSelGoal);
} }
......
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