Search in sources :

Example 21 with ExpressionLiteralSplitterStepSolver

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

the class Recursive method makeStepSolver.

@Override
public ExpressionLiteralSplitterStepSolver makeStepSolver(Expression expression) {
    ExpressionLiteralSplitterStepSolver result;
    Object syntacticFormType = expression.getSyntacticFormType();
    if (syntacticFormTypesToRecurse.contains(syntacticFormType)) {
        result = new RecursiveStepSolver(baseRewriter, expression);
    } else {
        // if expression is a Symbol, simply applying the base rewriter is equivalent to "recursively" applying it, since there are no sub-expressions to recurse into.
        // TODO: For all other types of expressions, this is potentially incorrect because there may be sub-expressions but we do not recurse into them.
        // The reason for that is that Expression does not support the generic providing of context-altering constructs such as indices and conditions in quantified expressions.
        result = baseRewriter.makeStepSolver(expression);
    }
    return result;
}
Also used : ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)

Example 22 with ExpressionLiteralSplitterStepSolver

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

the class Switch method makeStepSolver.

@Override
public ExpressionLiteralSplitterStepSolver makeStepSolver(Expression expression) {
    ExpressionLiteralSplitterStepSolver result;
    T key = keyMaker.apply(expression);
    Rewriter baseRewriter = fromKeyToRewriter.get(key);
    if (baseRewriter != null) {
        result = baseRewriter.makeStepSolver(expression);
    } else {
        result = new ConstantExpressionStepSolver(expression);
    }
    return result;
}
Also used : ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) TopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter) ConstantExpressionStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver)

Example 23 with ExpressionLiteralSplitterStepSolver

use of com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver 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 24 with ExpressionLiteralSplitterStepSolver

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

the class LinearRealArithmeticTheory method getSingleVariableConstraintQuantifierEliminatorStepSolver.

@Override
public ExpressionLiteralSplitterStepSolver getSingleVariableConstraintQuantifierEliminatorStepSolver(AssociativeCommutativeGroup group, SingleVariableConstraint constraint, Expression body, Context context) {
    ExpressionStepSolver formulaSplitterStepSolver;
    if (group instanceof Sum) {
        formulaSplitterStepSolver = new SummationOnLinearRealArithmeticAndPolynomialStepSolver(constraint, body);
    } else {
        formulaSplitterStepSolver = new QuantifierEliminationOnBodyInWhichIndexOnlyOccursInsideLiteralsStepSolver(group, constraint, body);
    }
    ExpressionLiteralSplitterStepSolver result = new ExpressionStepSolverToLiteralSplitterStepSolverAdapter(formulaSplitterStepSolver);
    return result;
}
Also used : ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) ExpressionStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionStepSolver) QuantifierEliminationOnBodyInWhichIndexOnlyOccursInsideLiteralsStepSolver(com.sri.ai.grinder.sgdpllt.core.solver.QuantifierEliminationOnBodyInWhichIndexOnlyOccursInsideLiteralsStepSolver) Sum(com.sri.ai.grinder.sgdpllt.group.Sum) ExpressionStepSolverToLiteralSplitterStepSolverAdapter(com.sri.ai.grinder.sgdpllt.core.solver.ExpressionStepSolverToLiteralSplitterStepSolverAdapter)

Example 25 with ExpressionLiteralSplitterStepSolver

use of com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver 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

ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)26 Expression (com.sri.ai.expresso.api.Expression)14 Context (com.sri.ai.grinder.sgdpllt.api.Context)5 ContextSplitting (com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)5 Rewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter)5 ExpressionStepSolver (com.sri.ai.grinder.sgdpllt.api.ExpressionStepSolver)4 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)4 ExpressionStepSolverToLiteralSplitterStepSolverAdapter (com.sri.ai.grinder.sgdpllt.core.solver.ExpressionStepSolverToLiteralSplitterStepSolverAdapter)4 QuantifierEliminationOnBodyInWhichIndexOnlyOccursInsideLiteralsStepSolver (com.sri.ai.grinder.sgdpllt.core.solver.QuantifierEliminationOnBodyInWhichIndexOnlyOccursInsideLiteralsStepSolver)4 Test (org.junit.Test)4 Step (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Step)3 StepSolver (com.sri.ai.grinder.sgdpllt.api.StepSolver)3 AbstractTheory (com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheory)3 TopRewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter)3 ConstantExpressionStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver)3 Expressions (com.sri.ai.expresso.helper.Expressions)2 Solution (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution)2 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)2 Sum (com.sri.ai.grinder.sgdpllt.group.Sum)2 LiteralRewriter (com.sri.ai.grinder.sgdpllt.library.boole.LiteralRewriter)2