Search in sources :

Example 16 with ExpressionLiteralSplitterStepSolver

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

Example 17 with ExpressionLiteralSplitterStepSolver

use of com.sri.ai.grinder.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.theory.differencearithmetic.ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)

Example 18 with ExpressionLiteralSplitterStepSolver

use of com.sri.ai.grinder.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 = fromKeyValueToRewriter.get(key);
    if (baseRewriter != null) {
        result = baseRewriter.makeStepSolver(expression);
    } else {
        result = new ConstantExpressionStepSolver(expression);
    }
    return result;
}
Also used : ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) TopRewriter(com.sri.ai.grinder.rewriter.api.TopRewriter) ConstantExpressionStepSolver(com.sri.ai.grinder.theory.base.ConstantExpressionStepSolver)

Example 19 with ExpressionLiteralSplitterStepSolver

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

the class UnificationStepSolver method step.

@Override
public StepSolver.Step<Boolean> step(Context context) {
    StepSolver.Step<Boolean> result = null;
    if (precomputedResult != null) {
        result = precomputedResult;
    } else {
        List<Integer> stepUnknownSolutionIndexesForUnificationEqualities = new ArrayList<>(unknownSolutionIndexesForUnificationEqualities);
        List<Integer> stepFoundSolutions = new ArrayList<>();
        for (Integer unknownSolutionIndex : stepUnknownSolutionIndexesForUnificationEqualities) {
            Expression equality = unificationEqualitiesToTest.get(unknownSolutionIndex);
            ExpressionLiteralSplitterStepSolver evaluatorStepSolver = context.getTheory().makeEvaluatorStepSolver(equality);
            Expression equalityResult = evaluatorStepSolver.solve(context);
            if (equalityResult.equals(TRUE)) {
                stepFoundSolutions.add(unknownSolutionIndex);
            } else if (equalityResult.equals(FALSE)) {
                // Can't unify
                result = new StepSolver.Solution<>(Boolean.FALSE);
                break;
            } else {
            // Solution to unification equality still unknown
            }
        }
        if (result == null) {
            stepUnknownSolutionIndexesForUnificationEqualities.removeAll(stepFoundSolutions);
            if (stepUnknownSolutionIndexesForUnificationEqualities.size() == 0) {
                // No more unknown solutions and this means all of them were true if we got to here
                result = new StepSolver.Solution<>(Boolean.TRUE);
            } else {
                // We still have unknown equality unifications, so will split on the first unknown
                // of these equalities
                Integer firstUnknownUnificationEqualityIndex = stepUnknownSolutionIndexesForUnificationEqualities.get(0);
                Expression unknownUnificationEqualityToSplitOn = unificationEqualitiesToTest.get(firstUnknownUnificationEqualityIndex);
                StepSolver<Boolean> ifTrue;
                if (stepUnknownSolutionIndexesForUnificationEqualities.size() == 1) {
                    // If there is only 1 unknown unification equality remaining, then on the true branch
                    // we know the unification will result in true, so just return that known up front.
                    ifTrue = new ConstantStepSolver<>(Boolean.TRUE);
                } else {
                    ifTrue = this.clone();
                    ((UnificationStepSolver) ifTrue).unknownSolutionIndexesForUnificationEqualities = new ArrayList<>(stepUnknownSolutionIndexesForUnificationEqualities);
                }
                StepSolver<Boolean> ifFalse = new ConstantStepSolver<>(Boolean.FALSE);
                ContextSplitting contextSplitting = null;
                // information for the literal.
                if (context.getTheory().isLiteralOrBooleanConstant(unknownUnificationEqualityToSplitOn, context)) {
                    contextSplitting = new ContextSplitting(unknownUnificationEqualityToSplitOn, context);
                }
                result = new StepSolver.ItDependsOn<>(unknownUnificationEqualityToSplitOn, contextSplitting, ifTrue, ifFalse);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) StepSolver(com.sri.ai.grinder.api.StepSolver) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) ContextSplitting(com.sri.ai.grinder.core.constraint.ContextSplitting)

Example 20 with ExpressionLiteralSplitterStepSolver

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

the class FallbackTheory method getSingleQuantifierEliminatorStepSolver.

@Override
public ExpressionLiteralSplitterStepSolver getSingleQuantifierEliminatorStepSolver(SingleQuantifierEliminationProblem problem, Context context) {
    ExpressionLiteralSplitterStepSolver baseQuantifierEliminatorStepSolver = getBase().getSingleQuantifierEliminatorStepSolver(problem, context);
    ExpressionLiteralSplitterStepSolver result = makeFallbackQuantifierEliminationStepSolver(problem, baseQuantifierEliminatorStepSolver);
    return result;
}
Also used : ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver)

Aggregations

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