use of com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker 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.sgdpllt.rewriter.api.RewriterFromStepMaker 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.sgdpllt.rewriter.api.RewriterFromStepMaker in project aic-expresso by aic-sri-international.
the class RecursiveTest method testSimpleRecursiveRewriter.
@Test
public void testSimpleRecursiveRewriter() {
RewriterFromStepMaker rewriter = (Expression e, Context c) -> {
if (Expressions.isNumber(e)) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() + 1));
}
return new Solution(e);
};
Expression initial;
Expression expected;
initial = parse("7");
expected = parse("8");
runTest(rewriter, initial, expected, map());
initial = parse("f(9,g(8,7,6))");
expected = parse("f(10,g(9,8,7))");
runTest(rewriter, initial, expected, map());
initial = parse("(6)(9,g(8,7,6))");
expected = parse("(7)(10,g(9,8,7))");
runTest(rewriter, initial, expected, map());
}
use of com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker in project aic-expresso by aic-sri-international.
the class ExhaustiveTest method testSimpleExhaustiveRewriter.
@Test
public void testSimpleExhaustiveRewriter() {
RewriterFromStepMaker rewriter = (Expression e, Context c) -> {
if (Expressions.isNumber(e) && e.intValue() < 10) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() + 1));
}
return new Solution(e);
};
Expression initial = parse("1");
Expression expected = parse("10");
runTest(rewriter, initial, expected, map());
}
use of com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker 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());
}
Aggregations