Search in sources :

Example 1 with ExpressionLiteralSplitterStepSolver

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

the class CompoundTheory method getSingleVariableConstraintSatisfiabilityStepSolver.

@Override
public ExpressionLiteralSplitterStepSolver getSingleVariableConstraintSatisfiabilityStepSolver(SingleVariableConstraint constraint, Context context) {
    Theory theory = getTheory(constraint.getVariable(), context);
    ExpressionLiteralSplitterStepSolver result;
    if (theory != null) {
        result = theory.getSingleVariableConstraintSatisfiabilityStepSolver(constraint, context);
    } else {
        result = null;
    }
    return result;
}
Also used : Theory(com.sri.ai.grinder.sgdpllt.api.Theory) AbstractTheory(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheory) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)

Example 2 with ExpressionLiteralSplitterStepSolver

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

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

the class ExhaustiveTest method testSimpleExhaustiveConditionalRewriter.

@Test
public void testSimpleExhaustiveConditionalRewriter() {
    class FunkyStepSolver implements ExpressionLiteralSplitterStepSolver {

        private Expression expression;

        public FunkyStepSolver(Expression expression) {
            this.expression = expression;
        }

        @Override
        public FunkyStepSolver clone() {
            FunkyStepSolver result = null;
            try {
                result = (FunkyStepSolver) super.clone();
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            return result;
        }

        @Override
        public Step step(Context context) {
            if (Expressions.isNumber(expression) && expression.intValue() % 10 != 0) {
                if (expression.intValue() == 5) {
                    Expression literal = parse("JumpAt5");
                    ContextSplitting splitting = new ContextSplitting(literal, context);
                    switch(splitting.getResult()) {
                        case LITERAL_IS_TRUE:
                            return new Solution(parse("11"));
                        case LITERAL_IS_FALSE:
                            return new Solution(parse("6"));
                        case LITERAL_IS_UNDEFINED:
                            return new ItDependsOn(literal, splitting, this, this);
                        default:
                            throw new Error("Unpredicted case");
                    }
                } else {
                    return new Solution(DefaultSymbol.createSymbol(expression.intValue() + 1));
                }
            } else
                return new Solution(expression);
        }
    }
    ;
    Rewriter rewriter = (Expression e) -> new FunkyStepSolver(e);
    Expression initial = parse("1");
    Expression expected = parse("if JumpAt5 then 20 else 10");
    runTest(rewriter, initial, expected, map(parse("JumpAt5"), parse("Boolean")));
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Example 4 with ExpressionLiteralSplitterStepSolver

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

the class ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolverTest method runTest.

private void runTest(Expression variable, String constraintString, Expression expected, Context context) {
    Constraint constraint = new SingleVariableDifferenceArithmeticConstraint(variable, true, context.getTheory());
    constraint = constraint.conjoin(parse(constraintString), context);
    ExpressionLiteralSplitterStepSolver stepSolver = new ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver((SingleVariableDifferenceArithmeticConstraint) constraint);
    Expression actual = stepSolver.solve(context);
    System.out.println("Variable " + variable + "\nhas possible values:\n" + actual + "\nsatisfying constraint:\n" + constraintString + "\n");
    assertEquals(expected, actual);
}
Also used : ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)

Example 5 with ExpressionLiteralSplitterStepSolver

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

the class EvaluationTest method runTest.

private void runTest(String expressionString, Expression expected, Context context) {
    Expression expression = parse(expressionString);
    ExpressionLiteralSplitterStepSolver stepSolver = context.getTheory().makeEvaluatorStepSolver(expression);
    System.out.println("Evaluating " + expression);
    Expression solution = ContextDependentExpressionProblemSolver.staticSolve(stepSolver, context);
    System.out.println(expression + " -----> " + solution + "\n");
    assertEquals(expected, solution);
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)

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