use of org.sosy_lab.java_smt.api.visitors.DefaultFormulaVisitor in project java-smt by sosy-lab.
the class SolverVisitorTest method extractionDeclarations.
@Test
public void extractionDeclarations() {
requireIntegers();
// Create the variables and uf
IntegerFormula a = imgr.makeVariable("a");
IntegerFormula b = imgr.makeVariable("b");
IntegerFormula ab = imgr.add(a, b);
BooleanFormula uf1 = fmgr.declareAndCallUF("testFunc", FormulaType.BooleanType, a, b, ab);
BooleanFormula uf2 = fmgr.declareAndCallUF("testFunc", FormulaType.BooleanType, ab, b, a);
BooleanFormula f = bmgr.and(uf1, uf2);
final Collection<Formula> usedArgs = new LinkedHashSet<>();
final List<FunctionDeclaration<?>> usedDecls = new ArrayList<>();
FormulaVisitor<TraversalProcess> argCollectingVisitor = new DefaultFormulaVisitor<>() {
@Override
public TraversalProcess visitFunction(Formula pF, List<Formula> args, FunctionDeclaration<?> pFunctionDeclaration) {
usedArgs.addAll(args);
usedDecls.add(pFunctionDeclaration);
return visitDefault(pF);
}
@Override
protected TraversalProcess visitDefault(Formula pF) {
return TraversalProcess.CONTINUE;
}
};
mgr.visitRecursively(f, argCollectingVisitor);
// check general stuff about variables, copied from above
assertThat(usedArgs).hasSize(5);
assertThat(usedArgs).containsExactly(uf1, uf2, a, b, ab);
Map<String, Formula> vars = mgr.extractVariables(f);
assertThat(vars).hasSize(2);
assertThat(vars.keySet()).containsExactly("a", "b");
Map<String, Formula> varsUfs = mgr.extractVariablesAndUFs(f);
assertThat(varsUfs).hasSize(3);
assertThat(varsUfs.keySet()).containsExactly("a", "b", "testFunc");
// check correct traversal order of the functions
assertThat(usedDecls).hasSize(4);
assertThat(usedDecls.get(0).getKind()).isEqualTo(FunctionDeclarationKind.AND);
assertThat(usedDecls.get(1).getName()).isEqualTo("testFunc");
assertThat(usedDecls.get(2).getKind()).isEqualTo(FunctionDeclarationKind.ADD);
assertThat(usedDecls.get(3).getName()).isEqualTo("testFunc");
// check UF-equality. This check went wrong in CVC4 and was fixed.
assertThat(usedDecls.get(1)).isEqualTo(usedDecls.get(3));
}
use of org.sosy_lab.java_smt.api.visitors.DefaultFormulaVisitor in project java-smt by sosy-lab.
the class SolverVisitorTest method extractionArguments.
@Test
public void extractionArguments() {
requireIntegers();
// Create the variables and uf
IntegerFormula a = imgr.makeVariable("a");
IntegerFormula b = imgr.makeVariable("b");
IntegerFormula ab = imgr.add(a, b);
BooleanFormula uf = fmgr.declareAndCallUF("testFunc", FormulaType.BooleanType, a, b, ab);
FormulaVisitor<Collection<Formula>> argCollectingVisitor = new DefaultFormulaVisitor<>() {
final Collection<Formula> usedArgs = new LinkedHashSet<>();
@Override
public Collection<Formula> visitFunction(Formula pF, List<Formula> args, FunctionDeclaration<?> pFunctionDeclaration) {
usedArgs.addAll(args);
return usedArgs;
}
@Override
protected Collection<Formula> visitDefault(Formula pF) {
return usedArgs;
}
};
Collection<Formula> usedArgs = mgr.visit(uf, argCollectingVisitor);
assertThat(usedArgs).hasSize(3);
assertThat(usedArgs).containsExactly(a, b, ab);
Map<String, Formula> vars = mgr.extractVariables(uf);
assertThat(vars).hasSize(2);
assertThat(vars.keySet()).containsExactly("a", "b");
Map<String, Formula> varsUfs = mgr.extractVariablesAndUFs(uf);
assertThat(varsUfs).hasSize(3);
assertThat(varsUfs.keySet()).containsExactly("a", "b", "testFunc");
}
use of org.sosy_lab.java_smt.api.visitors.DefaultFormulaVisitor in project java-smt by sosy-lab.
the class SolverVisitorTest method testFormulaVisitor.
/**
* A very basic test for the formula visitor, defines a visitor which gathers all found free
* variables.
*/
@Test
public void testFormulaVisitor() {
IntegerFormula x = imgr.makeVariable("x");
IntegerFormula y = imgr.makeVariable("y");
IntegerFormula z = imgr.makeVariable("z");
BooleanFormula f = bmgr.or(imgr.equal(z, imgr.add(x, y)), imgr.equal(x, imgr.add(z, y)));
final Set<String> usedVariables = new HashSet<>();
FormulaVisitor<TraversalProcess> nameExtractor = new DefaultFormulaVisitor<>() {
@Override
protected TraversalProcess visitDefault(Formula formula) {
return TraversalProcess.CONTINUE;
}
@Override
public TraversalProcess visitFreeVariable(Formula formula, String name) {
usedVariables.add(name);
return TraversalProcess.CONTINUE;
}
};
mgr.visitRecursively(f, nameExtractor);
assertThat(usedVariables).containsExactly("x", "y", "z");
}
Aggregations