Search in sources :

Example 1 with ContextSplitting

use of com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting in project aic-expresso by aic-sri-international.

the class RecursiveTest method testConditionalRecursiveRewriter.

@Test
public void testConditionalRecursiveRewriter() {
    Expression xIs0 = parse("XIs0");
    RewriterFromStepMaker rewriter = (Expression e, Context c) -> {
        if (Expressions.isNumber(e)) {
            return new Solution(DefaultSymbol.createSymbol(e.intValue() + 1));
        } else if (e.equals(parse("X"))) {
            ContextSplitting splitting = new ContextSplitting(xIs0, c);
            switch(splitting.getResult()) {
                case LITERAL_IS_TRUE:
                    return new Solution(ZERO);
                case LITERAL_IS_FALSE:
                    return new Solution(ONE);
                case LITERAL_IS_UNDEFINED:
                    return new ItDependsOn(xIs0, splitting, new ConstantExpressionStepSolver(ZERO), new ConstantExpressionStepSolver(ONE));
                default:
                    throw new Error("Unpredicted case.");
            }
        }
        return new Solution(e);
    };
    Expression initial;
    Expression expected;
    initial = parse("X");
    expected = parse("if " + xIs0 + " then 0 else 1");
    runTest(rewriter, initial, expected, map(xIs0, parse("Boolean")));
    initial = parse("f(9,g(X,7,6))");
    expected = parse("if " + xIs0 + " then f(10,g(0,8,7)) else f(10,g(1,8,7))");
    runTest(rewriter, initial, expected, map(xIs0, parse("Boolean")));
    initial = parse("X(9,g(h(1,2,X),X,7,6))");
    expected = parse("if " + xIs0 + " then 0(10,g(h(2,3,0),0,8,7)) else 1(10,g(h(2,3,1),1,8,7))");
    runTest(rewriter, initial, expected, map(xIs0, 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) RewriterFromStepMaker(com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker) ItDependsOn(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.ItDependsOn) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting) ConstantExpressionStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver) Test(org.junit.Test)

Example 2 with ContextSplitting

use of com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting 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 3 with ContextSplitting

use of com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting in project aic-expresso by aic-sri-international.

the class AbstractQuantifierEliminationStepSolver method step.

@Override
public Step step(Context context) {
    Step result;
    Context contextForBody = getContextForBody(context);
    if (contextForBody.isContradiction()) {
        result = new Solution(group.additiveIdentityElement());
    } else {
        ExpressionLiteralSplitterStepSolver bodyStepSolver = getInitialBodyStepSolver(context.getTheory());
        ExpressionLiteralSplitterStepSolver.Step bodyStep = bodyStepSolver.step(contextForBody);
        // Check (**) in this file to see where this happens
        if (!bodyStep.itDepends()) {
            ExpressionLiteralSplitterStepSolver evaluatorStepSolver = getTheory().makeEvaluatorStepSolver(bodyStep.getValue());
            bodyStep = evaluatorStepSolver.step(context);
        }
        if (bodyStep.itDepends()) {
            // "intercept" literals containing the index and split the quantifier based on it
            if (isSubExpressionOf(getIndex(), bodyStep.getSplitterLiteral())) {
                Expression literalOnIndex = bodyStep.getSplitterLiteral();
                result = resultIfLiteralContainsIndex(literalOnIndex, bodyStep, contextForBody, context);
            } else {
                // not on index, just pass the expression on which we depend on, but with appropriate sub-step solvers (this, for now)
                AbstractQuantifierEliminationStepSolver ifTrue = clone();
                AbstractQuantifierEliminationStepSolver ifFalse = clone();
                ifTrue.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIsTrue();
                ifFalse.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIsFalse();
                ifTrue.initialContextForBody = bodyStep.getContextSplittingWhenSplitterIsLiteral().getContextAndLiteral();
                ifFalse.initialContextForBody = bodyStep.getContextSplittingWhenSplitterIsLiteral().getContextAndLiteralNegation();
                // to compute the result's constraint splitting,
                // we cannot directly re-use bodyStep.getConstraintSplitting() because it was not obtained from
                // the context it is returning to,
                // but from the context conjoined with the index constraint.
                // In order to provide two contexts to work with the sequel step solvers,
                // we calculate the splittings here.
                // TODO: In the future, we expect it possible to efficiently extract the contextForBody component relative
                // to the original context only, excluding the index.
                ContextSplitting split = new ContextSplitting(bodyStep.getSplitterLiteral(), context);
                result = new ItDependsOn(bodyStep.getSplitterLiteral(), split, ifTrue, ifFalse);
            }
        } else {
            // body is already literal free
            result = eliminateQuantifierForLiteralFreeBodyAndSingleVariableConstraint(indexConstraint, bodyStep.getValue(), context);
        }
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)

Example 4 with ContextSplitting

use of com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting in project aic-expresso by aic-sri-international.

the class ContextDependentExpressionProblemSolver method solve.

/**
	 * Returns the solution for a problem using a step solver.
	 * @param stepSolver
	 * @param context
	 * @return
	 */
public Expression solve(ExpressionLiteralSplitterStepSolver stepSolver, Context context) {
    if (interrupted) {
        throw new Error("Solver interrupted.");
    }
    Expression result;
    ExpressionLiteralSplitterStepSolver.Step step = stepSolver.step(context);
    if (step.itDepends()) {
        Expression splitter = step.getSplitter();
        ContextSplitting split = (ContextSplitting) step.getContextSplittingWhenSplitterIsLiteral();
        myAssert(() -> split.isUndefined(), () -> "Undefined " + ContextSplitting.class + " result value: " + split.getResult());
        Expression subSolution1 = solve(step.getStepSolverForWhenSplitterIsTrue(), split.getConstraintAndLiteral());
        Expression subSolution2 = solve(step.getStepSolverForWhenSplitterIsFalse(), split.getConstraintAndLiteralNegation());
        result = IfThenElse.make(splitter, subSolution1, subSolution2, true);
    } else {
        result = step.getValue();
    }
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)

Example 5 with ContextSplitting

use of com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting in project aic-expresso by aic-sri-international.

the class LiteralExpressionStepSolver method step.

@Override
public StepSolver.Step<Expression> step(Context context) {
    ContextSplitting split = new ContextSplitting(literal, context);
    switch(split.getResult()) {
        case CONSTRAINT_IS_CONTRADICTORY:
            return null;
        case LITERAL_IS_TRUE:
            return new Solution<>(TRUE);
        case LITERAL_IS_FALSE:
            return new Solution<>(FALSE);
        case LITERAL_IS_UNDEFINED:
            StepSolver<Expression> ifTrue = constantStepSolver(TRUE);
            StepSolver<Expression> ifFalse = constantStepSolver(FALSE);
            return new ItDependsOn<Expression>(literal, split, ifTrue, ifFalse);
        default:
            throw new Error("Unrecognized splitting result.");
    }
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)

Aggregations

ContextSplitting (com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)12 Expression (com.sri.ai.expresso.api.Expression)11 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)5 Context (com.sri.ai.grinder.sgdpllt.api.Context)4 Solution (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution)3 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)3 Test (org.junit.Test)3 Rewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter)2 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)1 ItDependsOn (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.ItDependsOn)1 StepSolver (com.sri.ai.grinder.sgdpllt.api.StepSolver)1 RewriterFromStepMaker (com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker)1 ConstantExpressionStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver)1 ArrayList (java.util.ArrayList)1