use of com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution 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")));
}
use of com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution in project aic-expresso by aic-sri-international.
the class SwitchTest method testSimpleSwitchRewriter.
@Test
public void testSimpleSwitchRewriter() {
RewriterFromStepMaker baseRewriterSum = (Expression e, Context c) -> {
int argument1 = e.get(0).intValue();
int argument2 = e.get(1).intValue();
Symbol resultValue = DefaultSymbol.createSymbol(argument1 + argument2);
return new Solution(resultValue);
};
RewriterFromStepMaker baseRewriterSubtraction = (Expression e, Context c) -> {
int argument1 = e.get(0).intValue();
int argument2 = e.get(1).intValue();
Symbol resultValue = DefaultSymbol.createSymbol(argument1 - argument2);
return new Solution(resultValue);
};
Switch rewriter = new Switch<String>(e -> e.getFunctor() != null ? e.getFunctor().toString() : "", map("+", baseRewriterSum, "-", baseRewriterSubtraction));
Expression initial;
Expression expected;
initial = parse("7");
expected = parse("7");
runTest(rewriter, initial, expected, map());
initial = parse("10 - 7");
expected = parse("3");
runTest(rewriter, initial, expected, map());
initial = parse("10 + 3");
expected = parse("13");
runTest(rewriter, initial, expected, map());
}
use of com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution in project aic-expresso by aic-sri-international.
the class FirstOfTest method testSimpleFirstOfRewriter.
@Test
public void testSimpleFirstOfRewriter() {
List<RewriterFromStepMaker> rewriters = Util.<RewriterFromStepMaker>list((Expression e, Context c) -> {
if (Expressions.isNumber(e) && e.intValue() == 10) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() - 1));
}
return new Solution(e);
}, (Expression e, Context c) -> {
if (Expressions.isNumber(e) && e.intValue() == 9) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() - 1));
}
return new Solution(e);
}, (Expression e, Context c) -> {
if (Expressions.isNumber(e) && e.intValue() == 8) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() - 1));
}
return new Solution(e);
}, (Expression e, Context c) -> {
if (Expressions.isNumber(e) && e.intValue() == 7) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() - 1));
}
return new Solution(e);
}, (Expression e, Context c) -> {
if (Expressions.isNumber(e) && e.intValue() == 6) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() - 1));
}
return new Solution(e);
});
Expression initial = parse("8");
Expression expected = parse("7");
runTest(new LinkedList<Rewriter>(rewriters), initial, expected, map());
initial = parse("7");
expected = parse("6");
runTest(new LinkedList<Rewriter>(rewriters), initial, expected, map());
}
use of com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution 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")));
}
Aggregations