Search in sources :

Example 1 with NumberOfDistinctExpressionsStepSolver

use of com.sri.ai.grinder.theory.equality.NumberOfDistinctExpressionsStepSolver 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.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) NumberOfDistinctExpressionsStepSolver(com.sri.ai.grinder.theory.equality.NumberOfDistinctExpressionsStepSolver) Test(org.junit.Test)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)1 Expressions (com.sri.ai.expresso.helper.Expressions)1 Context (com.sri.ai.grinder.api.Context)1 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver)1 Step (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Step)1 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)1 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)1 NumberOfDistinctExpressionsStepSolver (com.sri.ai.grinder.theory.equality.NumberOfDistinctExpressionsStepSolver)1 Test (org.junit.Test)1