Commit 6a5eb734 authored by Sarah Grebing's avatar Sarah Grebing

Simplification of Join

parent 889b19e1
package edu.kit.formal.commands;
import edu.kit.formal.interpreter.State;
import java.util.ArrayList;
import java.util.List;
/**
* AbstractHandler for Chain of Respionsibility
*/
public abstract class CommandHandler {
protected List<CommandHandler> nextList = new ArrayList<>();
public void addNextCommandHandler(CommandHandler cmdHandler) {
this.nextList.add(cmdHandler);
}
public void handle(String commandName) {
for (int i = 0; i < nextList.size(); i++) {
CommandHandler cmdHandler = nextList.get(i);
cmdHandler.handle(commandName);
}
}
protected abstract State processCommand();
}
package edu.kit.formal.commands;
import edu.kit.formal.interpreter.State;
import edu.kit.formal.proofscriptparser.ast.ProofScript;
import java.util.HashMap;
/**
* Created by sarah on 5/17/17.
*/
public class LocalScriptCommandHandler extends CommandHandler {
HashMap<String, ProofScript> localScripts;
String requestedCommand;
public LocalScriptCommandHandler() {
localScripts = new HashMap<>();
}
@Override
public void handle(String commandName) {
if (localScripts.containsKey(commandName)) {
requestedCommand = commandName;
processCommand();
} else {
super.handle(commandName);
}
}
@Override
protected State processCommand() {
ProofScript command = localScripts.get(requestedCommand);
return null;
//execute
}
}
......@@ -3,7 +3,6 @@ package edu.kit.formal.interpreter;
import edu.kit.formal.proofscriptparser.ast.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
......@@ -150,12 +149,13 @@ public class VariableAssignment {
* Method joins two variable assignments without checking their compatibility
*
* @param assignment
* @return
* @return a new Variable Assignment
*/
public VariableAssignment joinWithoutCheck(VariableAssignment assignment) {
this.getValues().putAll(assignment.getValues());
this.getTypes().putAll(assignment.getTypes());
return this;
VariableAssignment va = new VariableAssignment(null);
va.getValues().putAll(assignment.getValues());
va.getTypes().putAll(assignment.getTypes());
return va;
}
/**
......@@ -166,15 +166,9 @@ public class VariableAssignment {
public VariableAssignment joinWithCheck(VariableAssignment assignment) throws RuntimeException {
Set<String> namesV2 = assignment.getValues().keySet();
Set<String> nonConflicting = new HashSet<>();
Set<String> conflictingCand = new HashSet<>();
//subtract V2 from V1 and add the nonconflicting varNames into the nonconflicting set
nonConflicting = this.getValues().keySet().stream().filter(item -> !namesV2.contains(item)).collect(Collectors.toSet());
//subtract V1 from V2 and add the nonconflicting varNames into the nonconflicting set
nonConflicting.addAll(namesV2.stream().filter(item -> !this.getValues().keySet().contains(item)).collect(Collectors.toSet()));
//create intersection
conflictingCand = this.getValues().keySet().stream().filter(item -> namesV2.contains(item)).collect(Collectors.toSet());
Set<String> conflictingCand = this.getValues().keySet().stream().filter(item -> namesV2.contains(item)).collect(Collectors.toSet());
if (!conflictingCand.isEmpty()) {
for (String s : conflictingCand) {
if (!this.lookupVarValue(s).equals(assignment.lookupVarValue(s))) {
......@@ -186,6 +180,14 @@ public class VariableAssignment {
return this.joinWithoutCheck(assignment);
}
public boolean isEmpty() {
if (this.getValues().isEmpty() && this.parent != null) {
return this.getParent().isEmpty();
} else {
return this.getValues().isEmpty();
}
}
public VariableAssignment push(VariableAssignment va) {
VariableAssignment nva = push();
nva.types.putAll(va.types);
......
package edu.kit.formal.interpreter;
import edu.kit.formal.proofscriptparser.ast.Type;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Created by sarah on 5/23/17.
*/
public class VariableAssignmentTest {
VariableAssignment va1;
VariableAssignment va2;
VariableAssignment va3;
@Before
public void setup() {
va1 = new VariableAssignment(null);
va1.addVarDecl("a", Type.INT);
va1.addVarDecl("b", Type.INT);
va1.addVarDecl("c", Type.INT);
va1.setVarValue("a", Value.from(1));
va1.setVarValue("b", Value.from(2));
va1.setVarValue("c", Value.from(3));
va2 = new VariableAssignment(null);
va2.addVarDecl("d", Type.INT);
va2.addVarDecl("e", Type.INT);
va2.addVarDecl("f", Type.INT);
va2.setVarValue("d", Value.from(1));
va2.setVarValue("e", Value.from(2));
va2.setVarValue("f", Value.from(3));
va3 = new VariableAssignment(null);
va3.addVarDecl("a", Type.INT);
va3.addVarDecl("b", Type.INT);
va3.addVarDecl("c", Type.INT);
va3.setVarValue("a", Value.from(1));
va3.setVarValue("b", Value.from(3));
va3.setVarValue("c", Value.from(3));
}
@Test
public void testjoinWithOutCheck1() {
va1.joinWithoutCheck(va2);
Assert.assertEquals(6, va1.getValues().size());
}
@Test
public void testjoinWithOutCheck2() {
va1.joinWithoutCheck(va3);
Assert.assertEquals(3, va1.getValues().size());
}
@Test
public void testjoinWithCheck1() {
VariableAssignment ret = va1.joinWithCheck(va2);
Assert.assertEquals(6, ret.getValues().size());
}
@Test
public void testjoinWithCheck2() {
VariableAssignment ret = va1.joinWithCheck(va3);
Assert.assertEquals(0, ret.getValues().size());
}
}
script test(i:int, b: bool){
j:int;
foreach{
j := 0;
}
cases{
case j = 0:{
split 2;
}
}
cases{
case match '.*a':{
print_state;
}
}
}
\ 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