Search in sources :

Example 46 with Context

use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.

the class RecursiveTest method testConditionalRecursiveRewriter.

@Test
public void testConditionalRecursiveRewriter() {
    Expression xIs0 = parse("XIs0");
    RewriterFromStepMaker rewriter = (Expression e, Context c) -> {
        if (Expressions.isNumber(e)) {
            return new Solution(DefaultSymbol.createSymbol(e.intValue() + 1));
        } else if (e.equals(parse("X"))) {
            ContextSplitting splitting = new ContextSplitting(xIs0, c);
            switch(splitting.getResult()) {
                case LITERAL_IS_TRUE:
                    return new Solution(ZERO);
                case LITERAL_IS_FALSE:
                    return new Solution(ONE);
                case LITERAL_IS_UNDEFINED:
                    return new ItDependsOn(xIs0, splitting, new ConstantExpressionStepSolver(ZERO), new ConstantExpressionStepSolver(ONE));
                default:
                    throw new Error("Unpredicted case.");
            }
        }
        return new Solution(e);
    };
    Expression initial;
    Expression expected;
    initial = parse("X");
    expected = parse("if " + xIs0 + " then 0 else 1");
    runTest(rewriter, initial, expected, map(xIs0, parse("Boolean")));
    initial = parse("f(9,g(X,7,6))");
    expected = parse("if " + xIs0 + " then f(10,g(0,8,7)) else f(10,g(1,8,7))");
    runTest(rewriter, initial, expected, map(xIs0, parse("Boolean")));
    initial = parse("X(9,g(h(1,2,X),X,7,6))");
    expected = parse("if " + xIs0 + " then 0(10,g(h(2,3,0),0,8,7)) else 1(10,g(h(2,3,1),1,8,7))");
    runTest(rewriter, initial, expected, map(xIs0, parse("Boolean")));
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) RewriterFromStepMaker(com.sri.ai.grinder.rewriter.api.RewriterFromStepMaker) ItDependsOn(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.ItDependsOn) Solution(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution) ContextSplitting(com.sri.ai.grinder.core.constraint.ContextSplitting) ConstantExpressionStepSolver(com.sri.ai.grinder.theory.base.ConstantExpressionStepSolver) Test(org.junit.Test)

Example 47 with Context

use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.

the class RecursiveTest method testSimpleRecursiveRewriter.

@Test
public void testSimpleRecursiveRewriter() {
    RewriterFromStepMaker rewriter = (Expression e, Context c) -> {
        if (Expressions.isNumber(e)) {
            return new Solution(DefaultSymbol.createSymbol(e.intValue() + 1));
        }
        return new Solution(e);
    };
    Expression initial;
    Expression expected;
    initial = parse("7");
    expected = parse("8");
    runTest(rewriter, initial, expected, map());
    initial = parse("f(9,g(8,7,6))");
    expected = parse("f(10,g(9,8,7))");
    runTest(rewriter, initial, expected, map());
    initial = parse("(6)(9,g(8,7,6))");
    expected = parse("(7)(10,g(9,8,7))");
    runTest(rewriter, initial, expected, map());
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) RewriterFromStepMaker(com.sri.ai.grinder.rewriter.api.RewriterFromStepMaker) Solution(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Example 48 with Context

use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.

the class EvaluationTest method testEvaluationOfQuantifiersOverFunctions.

@Test
public void testEvaluationOfQuantifiersOverFunctions() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory(), new BruteForceFunctionTheory()));
    Map<String, Type> variablesAndTypes = new LinkedHashMap<>(theoryTestingSupport.getVariableNamesAndTypesForTesting());
    Type booleanType = variablesAndTypes.get("P");
    variablesAndTypes.put("S", booleanType);
    variablesAndTypes.put("T", booleanType);
    variablesAndTypes.put("U", booleanType);
    theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    String expressionString;
    Expression expected;
    expressionString = "sum( {{ (on f in 0..2 -> Boolean)  if f(0) and f(1) then 2 else 3  :  f(2) }} )";
    // 2+3+3+3
    expected = parse("11");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in 1..2 -> Boolean, g in 1..2 -> Boolean)  if f(1) and g(2) then 2 else 3  :  f(2) }} )";
    expected = parse("22");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in 1..2 -> Boolean, g in 1..2 -> Boolean)  if f(1) and g(2) then 2 else 3 }} )";
    expected = parse("44");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in 1..2 -> Boolean, g in 1..2 -> Boolean)  if f(1) then 2 else 3 }} )";
    expected = parse("40");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in 1..2 -> Boolean)  if f(1) then 2 else 3 }} )";
    expected = parse("10");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in 1..2 -> 1..2)  if f(1) = 1 then 2 else 3 }} )";
    expected = parse("10");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in 1..2 -> 1..2)  f(1) }} )";
    expected = parse("6");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in '->'(1..2))  f() }} )";
    expected = parse("3");
    runTest(expressionString, expected, context);
    expressionString = "sum({{ (on f in '->'(1..2))  f }} )";
    expected = parse("(lambda : 1) + (lambda : 2)");
    runTest(expressionString, expected, context);
    expressionString = "product( {{ (on f in 0..2 -> Boolean)  if f(0) and f(1) then 2 else 3  :  f(2) }} )";
    // 2*3*3*3
    expected = parse("54");
    runTest(expressionString, expected, context);
    expressionString = "max( {{ (on f in 0..2 -> Boolean)  if f(0) and f(1) then 2 else 3  :  f(2) }} )";
    expected = parse("3");
    runTest(expressionString, expected, context);
    expressionString = "| f in 0..2 -> Boolean : f(0) |";
    expected = parse("4");
    runTest(expressionString, expected, context);
    expressionString = "| f in 0..2 -> Boolean : f(0) |";
    expected = parse("4");
    runTest(expressionString, expected, context);
    expressionString = "| f in 0..2 -> Boolean : f(0) |";
    expected = parse("4");
    runTest(expressionString, expected, context);
    expressionString = "| f in x(0..2, 0..2) -> Boolean : f(0, 0) |";
    expected = parse("256");
    runTest(expressionString, expected, context);
    expressionString = "| f in 0..2 x 0..2 -> Boolean : f(0, 0) |";
    expected = parse("256");
    runTest(expressionString, expected, context);
    expressionString = "for all f in 0..2 -> Boolean : f(0)";
    expected = parse("false");
    runTest(expressionString, expected, context);
    expressionString = "for all f in x(0..2) -> Boolean : (f(0) or not f(0)) and P";
    expected = parse("P");
    runTest(expressionString, expected, context);
    expressionString = "there exists f in '->'(x(0..2), Boolean) : f(0)";
    expected = parse("true");
    runTest(expressionString, expected, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) BruteForceFunctionTheory(com.sri.ai.grinder.theory.function.BruteForceFunctionTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 49 with Context

use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.

the class EvaluationTest method testEvaluationOfFunctionApplications.

@Test
public void testEvaluationOfFunctionApplications() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
    Map<String, Type> variablesAndTypes = new LinkedHashMap<>(theoryTestingSupport.getVariableNamesAndTypesForTesting());
    Type booleanType = variablesAndTypes.get("P");
    variablesAndTypes.put("S", booleanType);
    variablesAndTypes.put("T", booleanType);
    variablesAndTypes.put("U", booleanType);
    theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    String expressionString;
    Expression expected;
    expressionString = "0";
    expected = parse("0");
    runTest(expressionString, expected, context);
    expressionString = "I > J";
    expected = parse("I > J");
    runTest(expressionString, expected, context);
    expressionString = "I > J and I < J";
    expected = parse("false");
    runTest(expressionString, expected, context);
    expressionString = "(if I > J then 1 else 2) + (if I <= J then 30 else 40)";
    expected = parse("if I > J then 41 else 32");
    runTest(expressionString, expected, context);
    expressionString = "(if I > J then 1 else 2) + (if I <= J then 3 else 4)";
    expected = parse("5");
    runTest(expressionString, expected, context);
    expressionString = "(if I > J then if P or Q then 1 else 2 else 5) + (if I <= J then 3 else if not Q then 4 else -3)";
    expected = parse("if I > J then if P then if not Q then 5 else -2 else if Q then -2 else 6 else 8");
    runTest(expressionString, expected, context);
    expressionString = "(if I > J then if P or X = a or Y != b then 1 else 2 else 5) + (if I <= J then 3 else if not (X != a or Y = c and Q) then 4 else -3)";
    expected = parse("if I > J then if P then if X != a then -2 else if Y = c then if Q then -2 else 5 else 5 else if X = a then if Y = c then if Q then -2 else 5 else 5 else if Y != b then -2 else -1 else 8");
    runTest(expressionString, expected, context);
    expressionString = "if P and Q and R then 1 else 0";
    expected = parse("if P then if Q then if R then 1 else 0 else 0 else 0");
    runTest(expressionString, expected, context);
    expressionString = "if P and Q and R and S and T and U then 1 else 0";
    expected = parse("if P then if Q then if R then if S then if T then if U then 1 else 0 else 0 else 0 else 0 else 0 else 0");
    runTest(expressionString, expected, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 50 with Context

use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.

the class NumberOfDistinctExpressionsIsLessThanStepSolverTest method test.

@Test
public void test() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new EqualityTheory(true, true));
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    String contextString = "X != Y and X != a and X != b and Y != b";
    List<String> elementsStrings = list("X", "Y", "a", "b", "c");
    int limit = 5;
    context = context.conjoin(parse(contextString), context);
    ArrayList<Expression> list = mapIntoArrayList(elementsStrings, Expressions::parse);
    NumberOfDistinctExpressionsIsLessThanStepSolver stepSolver = new NumberOfDistinctExpressionsIsLessThanStepSolver(limit, list);
    Step step = stepSolver.step(context);
    assertEquals(true, step.itDepends());
    assertEquals(parse("X = c"), step.getSplitter());
    ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsC = step.getStepSolverForWhenSplitterIsTrue();
    ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromC = step.getStepSolverForWhenSplitterIsFalse();
    // if X = c, the number of distinct values is at most 4, so it will never reach the limit
    step = stepSolverIfXEqualsC.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(TRUE, step.getValue());
    // using again just to make sure it produces the same result
    step = stepSolverIfXEqualsC.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(TRUE, step.getValue());
    // if X != c, the number of distinct values will now depend on Y = a
    step = stepSolverIfXIsDifferentFromC.step(context);
    assertEquals(true, step.itDepends());
    assertEquals(parse("Y = a"), step.getSplitter());
    // using again just to make sure it produces the same result
    step = stepSolverIfXIsDifferentFromC.step(context);
    assertEquals(true, step.itDepends());
    assertEquals(parse("Y = a"), step.getSplitter());
    ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYEqualsA = step.getStepSolverForWhenSplitterIsTrue();
    ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYIsDifferentFromA = step.getStepSolverForWhenSplitterIsFalse();
    // ok, moving on, assuming Y = a, limit will not be reached
    step = stepSolverIfXIsDifferentFromCAndYEqualsA.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(TRUE, step.getValue());
    // if however Y != a, limit will depend on Y = c
    step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromA.step(context);
    assertEquals(true, step.itDepends());
    assertEquals(parse("Y = c"), step.getSplitter());
    ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsEqualToC = step.getStepSolverForWhenSplitterIsTrue();
    ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsDifferentFromC = step.getStepSolverForWhenSplitterIsFalse();
    // if Y = c, then limit is not going to be reached
    step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsEqualToC.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(TRUE, step.getValue());
    // if Y != c, then limit is reached
    step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsDifferentFromC.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(FALSE, step.getValue());
}
Also used : Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) Expressions(com.sri.ai.expresso.helper.Expressions) Step(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Step) NumberOfDistinctExpressionsIsLessThanStepSolver(com.sri.ai.grinder.theory.equality.NumberOfDistinctExpressionsIsLessThanStepSolver) Test(org.junit.Test)

Aggregations

Context (com.sri.ai.grinder.api.Context)132 Expression (com.sri.ai.expresso.api.Expression)100 Test (org.junit.Test)50 TrueContext (com.sri.ai.grinder.core.TrueContext)40 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)36 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)32 Type (com.sri.ai.expresso.api.Type)31 Theory (com.sri.ai.grinder.api.Theory)24 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)23 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)22 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)21 Set (java.util.Set)20 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)19 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)19 CompleteMultiVariableContext (com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext)16 Map (java.util.Map)16 Beta (com.google.common.annotations.Beta)15 Constraint (com.sri.ai.grinder.api.Constraint)15 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)14