Search in sources :

Example 16 with Rewriter

use of com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter 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

Rewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter)16 TopRewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter)10 Expression (com.sri.ai.expresso.api.Expression)9 Context (com.sri.ai.grinder.sgdpllt.api.Context)9 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)8 Recursive (com.sri.ai.grinder.sgdpllt.rewriter.core.Recursive)8 FirstOf (com.sri.ai.grinder.sgdpllt.rewriter.core.FirstOf)7 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)6 Exhaustive (com.sri.ai.grinder.sgdpllt.rewriter.core.Exhaustive)6 Test (org.junit.Test)6 DefaultTopRewriter (com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter)5 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)5 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)5 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)5 Solution (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution)4 ContextSplitting (com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)3 LiteralRewriter (com.sri.ai.grinder.sgdpllt.library.boole.LiteralRewriter)3 Switch (com.sri.ai.grinder.sgdpllt.rewriter.core.Switch)3 ForAllRewriter (com.sri.ai.grinder.sgdpllt.library.boole.ForAllRewriter)2 ThereExistsRewriter (com.sri.ai.grinder.sgdpllt.library.boole.ThereExistsRewriter)2