Search in sources :

Example 1 with Step

use of com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Step 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.sgdpllt.api.Context) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) Expressions(com.sri.ai.expresso.helper.Expressions) Step(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Step) NumberOfDistinctExpressionsIsLessThanStepSolver(com.sri.ai.grinder.sgdpllt.theory.equality.NumberOfDistinctExpressionsIsLessThanStepSolver) Test(org.junit.Test)

Example 2 with Step

use of com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Step in project aic-expresso by aic-sri-international.

the class ExpressionConditionedOnLiteralSolutionStep method stepDependingOnLiteral.

/**
	 * Produces a solver step based on value of literal, with corresponding given solutions
	 * (if literal is not defined by context, a {@link ItDependsOn} step is returned).
	 * @param literal
	 * @param solutionIfTrue
	 * @param solutionIfFalse
	 * @param context
	 * @return
	 */
public static Step stepDependingOnLiteral(Expression literal, Expression solutionIfTrue, Expression solutionIfFalse, Context context) {
    Step result;
    LiteralStepSolver literalStepSolver = new LiteralStepSolver(literal);
    StepSolver.Step<Boolean> step = literalStepSolver.step(context);
    if (step.itDepends()) {
        result = new ItDependsOn(step.getSplitter(), step.getContextSplittingWhenSplitterIsLiteral(), constantExpressionStepSolver(solutionIfTrue), constantExpressionStepSolver(solutionIfFalse));
    } else {
        result = new Solution(step.getValue() ? solutionIfTrue : solutionIfFalse);
    }
    return result;
}
Also used : ItDependsOn(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.ItDependsOn) Step(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Step) StepSolver(com.sri.ai.grinder.sgdpllt.api.StepSolver) ConstantExpressionStepSolver.constantExpressionStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver.constantExpressionStepSolver) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution)

Example 3 with Step

use of com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Step in project aic-expresso by aic-sri-international.

the class Rewriter method step.

default default Step step(Expression expression, Context context) {
    ExpressionLiteralSplitterStepSolver stepSolver = makeStepSolver(expression);
    Step step = stepSolver.step(context);
    return step;
}
Also used : ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) Step(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Step)

Example 4 with Step

use of com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Step in project aic-expresso by aic-sri-international.

the class NumberOfDistinctExpressionsStepSolverTest 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");
    context = context.conjoin(parse(contextString), context);
    ArrayList<Expression> list = mapIntoArrayList(elementsStrings, Expressions::parse);
    NumberOfDistinctExpressionsStepSolver stepSolver = new NumberOfDistinctExpressionsStepSolver(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 can be 3 or 4, depending on whether Y = a, or Y = b
    step = stepSolverIfXEqualsC.step(context);
    assertEquals(true, step.itDepends());
    assertEquals(parse("Y = a"), step.getSplitter());
    ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsCAndYEqualsA = step.getStepSolverForWhenSplitterIsTrue();
    ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsCAndYIsDifferentFromA = step.getStepSolverForWhenSplitterIsFalse();
    // if X = c and Y = a, the number of distinct values is 3 (a, b, c)
    step = stepSolverIfXEqualsCAndYEqualsA.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(parse("3"), step.getValue());
    // if X = c and Y != a, the number of distinct values is 3 or 4, depending on Y = c
    step = stepSolverIfXEqualsCAndYIsDifferentFromA.step(context);
    assertEquals(true, step.itDepends());
    assertEquals(parse("Y = c"), step.getSplitter());
    ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsCAndYIsDifferentFromAAndYEqualsC = step.getStepSolverForWhenSplitterIsTrue();
    ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsCAndYIsDifferentFromAAndYIsDifferentFromC = step.getStepSolverForWhenSplitterIsFalse();
    // if X = c and Y != a and Y = c, the number of distinct values is 3
    step = stepSolverIfXEqualsCAndYIsDifferentFromAAndYEqualsC.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(parse("3"), step.getValue());
    // if X = c and Y != a and Y != c, the number of distinct values is 4
    step = stepSolverIfXEqualsCAndYIsDifferentFromAAndYIsDifferentFromC.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(parse("4"), step.getValue());
    // if X = c and Y = a, the number of distinct values is 3 (a, b, c)
    step = stepSolverIfXEqualsCAndYEqualsA.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(parse("3"), step.getValue());
    // using again just to make sure it produces the same result
    step = stepSolverIfXEqualsCAndYEqualsA.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(parse("3"), step.getValue());
    // if X != c, the number of distinct value 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());
    // if X != c, the number of distinct values can be 4 or 5, depending on whether Y = a, or Y = b
    step = stepSolverIfXIsDifferentFromC.step(context);
    assertEquals(true, step.itDepends());
    assertEquals(parse("Y = a"), step.getSplitter());
    ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYEqualsA = step.getStepSolverForWhenSplitterIsTrue();
    ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYIsDifferentFromA = step.getStepSolverForWhenSplitterIsFalse();
    step = stepSolverIfXIsDifferentFromCAndYEqualsA.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(parse("4"), 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 there are 4 distinct values
    step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsEqualToC.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(parse("4"), step.getValue());
    // if Y != c, then Y is also unique and the number of distinct values is 5
    step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsDifferentFromC.step(context);
    assertEquals(false, step.itDepends());
    assertEquals(parse("5"), step.getValue());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) Expressions(com.sri.ai.expresso.helper.Expressions) Step(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Step) NumberOfDistinctExpressionsStepSolver(com.sri.ai.grinder.sgdpllt.theory.equality.NumberOfDistinctExpressionsStepSolver) Test(org.junit.Test)

Aggregations

Step (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Step)4 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)3 Expression (com.sri.ai.expresso.api.Expression)2 Expressions (com.sri.ai.expresso.helper.Expressions)2 Context (com.sri.ai.grinder.sgdpllt.api.Context)2 TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)2 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)2 Test (org.junit.Test)2 ItDependsOn (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.ItDependsOn)1 Solution (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution)1 StepSolver (com.sri.ai.grinder.sgdpllt.api.StepSolver)1 ConstantExpressionStepSolver.constantExpressionStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver.constantExpressionStepSolver)1 NumberOfDistinctExpressionsIsLessThanStepSolver (com.sri.ai.grinder.sgdpllt.theory.equality.NumberOfDistinctExpressionsIsLessThanStepSolver)1 NumberOfDistinctExpressionsStepSolver (com.sri.ai.grinder.sgdpllt.theory.equality.NumberOfDistinctExpressionsStepSolver)1