Search in sources :

Example 6 with Solution

use of com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution 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 7 with Solution

use of com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution 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 8 with Solution

use of com.sri.ai.grinder.sgdpllt.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());
}
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)

Example 9 with Solution

use of com.sri.ai.grinder.sgdpllt.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")));
}
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) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Aggregations

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