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")));
}
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;
}
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.");
}
}
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;
}
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;
}
Aggregations