Search in sources :

Example 1 with DefaultFormulaVisitor

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));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) ArrayList(java.util.ArrayList) TraversalProcess(org.sosy_lab.java_smt.api.visitors.TraversalProcess) DefaultFormulaVisitor(org.sosy_lab.java_smt.api.visitors.DefaultFormulaVisitor) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) RegexFormula(org.sosy_lab.java_smt.api.RegexFormula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) StringFormula(org.sosy_lab.java_smt.api.StringFormula) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) Formula(org.sosy_lab.java_smt.api.Formula) FunctionDeclaration(org.sosy_lab.java_smt.api.FunctionDeclaration) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) Test(org.junit.Test)

Example 2 with DefaultFormulaVisitor

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");
}
Also used : DefaultFormulaVisitor(org.sosy_lab.java_smt.api.visitors.DefaultFormulaVisitor) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) RegexFormula(org.sosy_lab.java_smt.api.RegexFormula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) StringFormula(org.sosy_lab.java_smt.api.StringFormula) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) Formula(org.sosy_lab.java_smt.api.Formula) FunctionDeclaration(org.sosy_lab.java_smt.api.FunctionDeclaration) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) Collection(java.util.Collection) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) Test(org.junit.Test)

Example 3 with DefaultFormulaVisitor

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");
}
Also used : DefaultFormulaVisitor(org.sosy_lab.java_smt.api.visitors.DefaultFormulaVisitor) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) RegexFormula(org.sosy_lab.java_smt.api.RegexFormula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) StringFormula(org.sosy_lab.java_smt.api.StringFormula) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) Formula(org.sosy_lab.java_smt.api.Formula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) TraversalProcess(org.sosy_lab.java_smt.api.visitors.TraversalProcess) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 BitvectorFormula (org.sosy_lab.java_smt.api.BitvectorFormula)3 BooleanFormula (org.sosy_lab.java_smt.api.BooleanFormula)3 FloatingPointFormula (org.sosy_lab.java_smt.api.FloatingPointFormula)3 Formula (org.sosy_lab.java_smt.api.Formula)3 IntegerFormula (org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula)3 RegexFormula (org.sosy_lab.java_smt.api.RegexFormula)3 StringFormula (org.sosy_lab.java_smt.api.StringFormula)3 DefaultFormulaVisitor (org.sosy_lab.java_smt.api.visitors.DefaultFormulaVisitor)3 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 FunctionDeclaration (org.sosy_lab.java_smt.api.FunctionDeclaration)2 TraversalProcess (org.sosy_lab.java_smt.api.visitors.TraversalProcess)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1