Search in sources :

Example 6 with ContextSplitting

use of com.sri.ai.grinder.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 : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) RewriterFromStepMaker(com.sri.ai.grinder.rewriter.api.RewriterFromStepMaker) ItDependsOn(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.ItDependsOn) Solution(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution) ContextSplitting(com.sri.ai.grinder.core.constraint.ContextSplitting) ConstantExpressionStepSolver(com.sri.ai.grinder.theory.base.ConstantExpressionStepSolver) Test(org.junit.Test)

Example 7 with ContextSplitting

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

the class IfThenElseStepSolver method stepWhenConditionHasNotBeenFullyEvaluatedYet.

private Step stepWhenConditionHasNotBeenFullyEvaluatedYet(Step conditionStep) {
    Expression splitter = conditionStep.getSplitter();
    ContextSplitting splitting = conditionStep.getContextSplittingWhenSplitterIsLiteral();
    IfThenElseStepSolver sequelStepSolverIfSplitterIsTrue = makeSequelStepSolver(conditionStep.getStepSolverForWhenSplitterIs(true), splitting.getContextAndLiteral());
    IfThenElseStepSolver sequelStepSolverIfSplitterIsFalse = makeSequelStepSolver(conditionStep.getStepSolverForWhenSplitterIs(false), splitting.getContextAndLiteralNegation());
    Step result = new ItDependsOn(splitter, splitting, sequelStepSolverIfSplitterIsTrue, sequelStepSolverIfSplitterIsFalse);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ContextSplitting(com.sri.ai.grinder.core.constraint.ContextSplitting)

Example 8 with ContextSplitting

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

Example 9 with ContextSplitting

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

the class ContextSplittingTester method splitContextAndRecordTime.

private ContextSplitting splitContextAndRecordTime(Expression literalToSplitOn, Context contextToSplit) {
    long startTime = 0;
    long endTime = 0;
    startTime = System.currentTimeMillis();
    ContextSplitting splitter = new ContextSplitting(literalToSplitOn, contextToSplit);
    endTime = System.currentTimeMillis();
    contextSplittingResults.timeToSplitContext.add(endTime - startTime);
    return splitter;
}
Also used : ContextSplitting(com.sri.ai.grinder.core.constraint.ContextSplitting)

Example 10 with ContextSplitting

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

the class AbstractDecisionOnAllOrderedPairsOfExpressionsStepSolver method step.

@Override
public Step step(Context context) {
    if (expressions.size() < 2) {
        return makeSolutionStepWhenThereAreNoPairs();
    }
    if (hasPair()) {
        Expression unsimplifiedLiteral = makeLiteral();
        Expression literal = context.getTheory().simplify(unsimplifiedLiteral, context);
        // this null is never used, just making compiler happy
        AbstractDecisionOnAllOrderedPairsOfExpressionsStepSolver stepSolverForWhenLiteralIsTrue = null;
        // this null is never used, just making compiler happy
        AbstractDecisionOnAllOrderedPairsOfExpressionsStepSolver stepSolverForWhenLiteralIsFalse = null;
        ContextSplitting split = new ContextSplitting(literal, context);
        if (split.getResult().equals(ContextSplitting.Result.CONSTRAINT_IS_CONTRADICTORY)) {
            return null;
        }
        boolean literalIsTrue = split.getResult() == ContextSplitting.Result.LITERAL_IS_TRUE;
        boolean literalIsFalse = !literalIsTrue && split.getResult() == ContextSplitting.Result.LITERAL_IS_FALSE;
        boolean undefined = !literalIsTrue && !literalIsFalse;
        boolean needSubStepSolverForWhenLiteralIsTrue = literalIsTrue || undefined;
        boolean needSubStepSolverForWhenLiteralIsFalse = literalIsFalse || undefined;
        if (needSubStepSolverForWhenLiteralIsTrue) {
            stepSolverForWhenLiteralIsTrue = makeSubStepSolverForWhenLiteralIsTrue();
        }
        if (needSubStepSolverForWhenLiteralIsFalse) {
            stepSolverForWhenLiteralIsFalse = makeSubStepSolverForWhenLiteralIsFalse();
        }
        if (literalIsTrue) {
            return stepSolverForWhenLiteralIsTrue.step(split.getConstraintAndLiteral());
        } else if (literalIsFalse) {
            return stepSolverForWhenLiteralIsFalse.step(split.getConstraintAndLiteralNegation());
        } else {
            return new ItDependsOn(literal, split, stepSolverForWhenLiteralIsTrue, stepSolverForWhenLiteralIsFalse);
        }
    }
    // went over all pairs
    Solution result = makeSolutionStepAfterGoingOverAllPairs();
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ContextSplitting(com.sri.ai.grinder.core.constraint.ContextSplitting)

Aggregations

ContextSplitting (com.sri.ai.grinder.core.constraint.ContextSplitting)20 Expression (com.sri.ai.expresso.api.Expression)16 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver)6 Context (com.sri.ai.grinder.api.Context)5 TrueContext (com.sri.ai.grinder.core.TrueContext)4 Solution (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution)3 Test (org.junit.Test)3 Rewriter (com.sri.ai.grinder.rewriter.api.Rewriter)2 Constraint (com.sri.ai.grinder.api.Constraint)1 ItDependsOn (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.ItDependsOn)1 ExpressionStepSolver (com.sri.ai.grinder.api.ExpressionStepSolver)1 SingleQuantifierEliminationProblem (com.sri.ai.grinder.api.SingleQuantifierEliminationProblem)1 StepSolver (com.sri.ai.grinder.api.StepSolver)1 Step (com.sri.ai.grinder.api.StepSolver.Step)1 AssignmentMapsIterator (com.sri.ai.grinder.helper.AssignmentMapsIterator)1 BruteForceCommonInterpreter (com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter)1 RewriterFromStepMaker (com.sri.ai.grinder.rewriter.api.RewriterFromStepMaker)1 ConstantExpressionStepSolver (com.sri.ai.grinder.theory.base.ConstantExpressionStepSolver)1 ArrayList (java.util.ArrayList)1