Search in sources :

Example 1 with RewriterFromStepMaker

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

Example 2 with RewriterFromStepMaker

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());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Switch(com.sri.ai.grinder.sgdpllt.rewriter.core.Switch) Expression(com.sri.ai.expresso.api.Expression) Symbol(com.sri.ai.expresso.api.Symbol) DefaultSymbol(com.sri.ai.expresso.core.DefaultSymbol) RewriterFromStepMaker(com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Example 3 with RewriterFromStepMaker

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());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) RewriterFromStepMaker(com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Example 4 with RewriterFromStepMaker

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());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) RewriterFromStepMaker(com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Example 5 with RewriterFromStepMaker

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());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) RewriterFromStepMaker(com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)5 Context (com.sri.ai.grinder.sgdpllt.api.Context)5 Solution (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution)5 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)5 RewriterFromStepMaker (com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker)5 Test (org.junit.Test)5 Symbol (com.sri.ai.expresso.api.Symbol)1 DefaultSymbol (com.sri.ai.expresso.core.DefaultSymbol)1 ItDependsOn (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.ItDependsOn)1 ContextSplitting (com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)1 Rewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter)1 Switch (com.sri.ai.grinder.sgdpllt.rewriter.core.Switch)1 ConstantExpressionStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver)1