use of com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.ItDependsOn 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.api.ExpressionLiteralSplitterStepSolver.ItDependsOn in project aic-expresso by aic-sri-international.
the class ExpressionConditionedOnLiteralSolutionStep method stepDependingOnLiteral.
/**
* Produces a solver step based on value of literal, with corresponding given solutions
* (if literal is not defined by context, a {@link ItDependsOn} step is returned).
* @param literal
* @param solutionIfTrue
* @param solutionIfFalse
* @param context
* @return
*/
public static Step stepDependingOnLiteral(Expression literal, Expression solutionIfTrue, Expression solutionIfFalse, Context context) {
Step result;
LiteralStepSolver literalStepSolver = new LiteralStepSolver(literal);
StepSolver.Step<Boolean> step = literalStepSolver.step(context);
if (step.itDepends()) {
result = new ItDependsOn(step.getSplitter(), step.getContextSplittingWhenSplitterIsLiteral(), constantExpressionStepSolver(solutionIfTrue), constantExpressionStepSolver(solutionIfFalse));
} else {
result = new Solution(step.getValue() ? solutionIfTrue : solutionIfFalse);
}
return result;
}
Aggregations