Search in sources :

Example 11 with ContextSplitting

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

the class AbstractLinearStepSolver method step.

@Override
public Step<T> step(Context context) {
    Step<T> result;
    if (current != n) {
        Expression unsimplifiedLiteral = makeLiteral();
        Expression literal = context.getTheory().simplify(unsimplifiedLiteral, context);
        ContextSplitting split = new ContextSplitting(literal, context);
        switch(split.getResult()) {
            case CONSTRAINT_IS_CONTRADICTORY:
                result = null;
                break;
            case LITERAL_IS_TRUE:
                result = makeSubStepSolverWhenLiteralIsTrue().step(context);
                break;
            case LITERAL_IS_FALSE:
                result = makeSubStepSolverWhenLiteralIsFalse().step(context);
                break;
            case LITERAL_IS_UNDEFINED:
                result = new ItDependsOn<T>(literal, split, makeSubStepSolverWhenLiteralIsTrue(), makeSubStepSolverWhenLiteralIsFalse());
                break;
            default:
                throw new Error("Unexpected ConstraintSplitting result.");
        }
    } else {
        result = makeSolutionWhenAllElementsHaveBeenChecked();
    }
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)

Example 12 with ContextSplitting

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

the class FirstOfTest method testSimpleFirstOfConditionalRewriter.

@Test
public void testSimpleFirstOfConditionalRewriter() {
    class JumperAtStepSolver implements ExpressionLiteralSplitterStepSolver {

        private Expression expression;

        private int jumpPoint;

        public JumperAtStepSolver(Expression expression, int jumpPoint) {
            this.expression = expression;
            this.jumpPoint = jumpPoint;
        }

        @Override
        public JumperAtStepSolver clone() {
            JumperAtStepSolver result = null;
            try {
                result = (JumperAtStepSolver) 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() == jumpPoint) {
                    Expression literal = parse("Jump5");
                    ContextSplitting splitting = new ContextSplitting(literal, context);
                    switch(splitting.getResult()) {
                        case LITERAL_IS_TRUE:
                            return new Solution(DefaultSymbol.createSymbol(jumpPoint + 5));
                        case LITERAL_IS_FALSE:
                            return new Solution(DefaultSymbol.createSymbol(jumpPoint + 1));
                        case LITERAL_IS_UNDEFINED:
                            return new ItDependsOn(literal, splitting, this, this);
                        default:
                            throw new Error("Unpredicted case");
                    }
                } else {
                    return new Solution(expression);
                }
            } else
                return new Solution(expression);
        }
    }
    ;
    List<Rewriter> rewriters = Util.<Rewriter>list((Expression e) -> new JumperAtStepSolver(e, 5), (Expression e) -> new JumperAtStepSolver(e, 8));
    Expression initial;
    Expression expected;
    initial = parse("1");
    // no jumps at 1
    expected = parse("1");
    runTest(rewriters, initial, expected, map(parse("Jump5"), parse("Boolean")));
    initial = parse("5");
    expected = parse("if Jump5 then 10 else 6");
    runTest(rewriters, initial, expected, map(parse("Jump5"), parse("Boolean")));
    initial = parse("8");
    expected = parse("if Jump5 then 13 else 9");
    runTest(rewriters, initial, expected, map(parse("Jump5"), 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)

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