Search in sources :

Example 6 with Rewriter

use of com.sri.ai.grinder.rewriter.api.Rewriter in project aic-expresso by aic-sri-international.

the class SwitchTest method testConditionalSwitchRewriter.

@Test
public void testConditionalSwitchRewriter() {
    Rewriter baseRewriterSum = e -> new SumStepSolver(e);
    Rewriter baseRewriterSubtraction = e -> new SubtractionStepSolver(e);
    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 - 3");
    expected = parse("if Inverted then 13 else 7");
    runTest(rewriter, initial, expected, map(inverted, parse("Boolean")));
    initial = parse("10 + 3");
    expected = parse("if Inverted then 7 else 13");
    runTest(rewriter, initial, expected, map(inverted, parse("Boolean")));
}
Also used : PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) Expression(com.sri.ai.expresso.api.Expression) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) Util.map(com.sri.ai.util.Util.map) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) Symbol(com.sri.ai.expresso.api.Symbol) TrueContext(com.sri.ai.grinder.core.TrueContext) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) ConstantExpressionStepSolver(com.sri.ai.grinder.theory.base.ConstantExpressionStepSolver) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) DefaultSymbol(com.sri.ai.expresso.core.DefaultSymbol) TopRewriter(com.sri.ai.grinder.rewriter.api.TopRewriter) Context(com.sri.ai.grinder.api.Context) Solution(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution) Recursive(com.sri.ai.grinder.rewriter.core.Recursive) Function(com.google.common.base.Function) Switch(com.sri.ai.grinder.rewriter.core.Switch) Util.list(com.sri.ai.util.Util.list) Test(org.junit.Test) FirstOf(com.sri.ai.grinder.rewriter.core.FirstOf) List(java.util.List) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) Util(com.sri.ai.util.Util) RewriterFromStepMaker(com.sri.ai.grinder.rewriter.api.RewriterFromStepMaker) Assert.assertEquals(org.junit.Assert.assertEquals) ContextSplitting(com.sri.ai.grinder.core.constraint.ContextSplitting) Switch(com.sri.ai.grinder.rewriter.core.Switch) Expression(com.sri.ai.expresso.api.Expression) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) TopRewriter(com.sri.ai.grinder.rewriter.api.TopRewriter) Test(org.junit.Test)

Example 7 with Rewriter

use of com.sri.ai.grinder.rewriter.api.Rewriter in project aic-expresso by aic-sri-international.

the class SwitchTest method topRewriterTest.

@Test
public void topRewriterTest() {
    List<TopRewriter> initialRewriters;
    Rewriter expected;
    Rewriter merged;
    initialRewriters = list(new Switch<String>(stringMaker, Util.map()), new CombiningTopRewriter(new Switch<String>(stringMaker, Util.map())));
    expected = new Switch<String>(stringMaker, Util.map());
    merged = TopRewriter.makeTopRewriterFromTopRewritersThatAreEitherFirstOfOrSwitches(initialRewriters);
    assertEquals(expected, merged);
    initialRewriters = list(new Switch<String>(stringMaker, Util.map()), new CombiningTopRewriter(new Switch<String>(stringMaker, Util.map()), new Switch<Object>(syntacticFormTypeMaker, Util.map())), new Switch<Object>(syntacticFormTypeMaker, Util.map()));
    expected = new FirstOf(new Switch<String>(stringMaker, Util.map()), new Switch<Object>(syntacticFormTypeMaker, Util.map()));
    merged = TopRewriter.makeTopRewriterFromTopRewritersThatAreEitherFirstOfOrSwitches(initialRewriters);
    assertEquals(expected, merged);
    initialRewriters = list(new Switch<String>(stringMaker, Util.map("1", new FirstOf(new Label("11"), new Label("12")), "2", new Label("21"), "3", new Label("31"), "4", new Label("41"))));
    expected = new Switch<String>(stringMaker, Util.map("1", new FirstOf(new Label("11"), new Label("12")), "2", new Label("21"), "3", new Label("31"), "4", new Label("41")));
    merged = TopRewriter.makeTopRewriterFromTopRewritersThatAreEitherFirstOfOrSwitches(initialRewriters);
    assertEquals(expected, merged);
    initialRewriters = list(new Switch<String>(stringMaker, Util.map("1", new FirstOf(new Label("11"), new Label("12")), "2", new Label("21"), "3", new FirstOf(new Label("31")), "4", new Label("41"))), new CombiningTopRewriter(new Switch<String>(stringMaker, Util.map("1", new FirstOf(new Label("12"), /* again */
    new Label("13")), "2", new FirstOf(new Label("22"), new Label("23")), "3", new Label("31"), /* again */
    "4", new Label("42"))), new Switch<Object>(syntacticFormTypeMaker, Util.map("Symbol", new FirstOf(new Label("S1"), new Label("S2")), "Function application", new FirstOf(new Label("F1"), new Label("F2")), "Lambda expression", new Label("L1")))), new Switch<Object>(syntacticFormTypeMaker, Util.map("Symbol", new FirstOf(new Label("S2"), new Label("S3")), "Function application", new Label("F2"), "Lambda expression", new FirstOf(new Label("L2"), new Label("L3")))));
    expected = new CombiningTopRewriter(new Switch<String>(stringMaker, Util.map("1", new FirstOf(new Label("11"), new Label("12"), new Label("13")), "2", new FirstOf(new Label("21"), new Label("22"), new Label("23")), "3", new Label("31"), "4", new FirstOf(list(new Label("41"), new Label("42"))))), new Switch<Object>(syntacticFormTypeMaker, Util.map("Symbol", new FirstOf(new Label("S1"), new Label("S2"), new Label("S3")), "Function application", new FirstOf(new Label("F1"), new Label("F2")), "Lambda expression", new FirstOf(new Label("L1"), new Label("L2"), new Label("L3")))));
    merged = TopRewriter.makeTopRewriterFromTopRewritersThatAreEitherFirstOfOrSwitches(initialRewriters);
    assertEquals(expected, merged);
}
Also used : Switch(com.sri.ai.grinder.rewriter.core.Switch) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) TopRewriter(com.sri.ai.grinder.rewriter.api.TopRewriter) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) TopRewriter(com.sri.ai.grinder.rewriter.api.TopRewriter) FirstOf(com.sri.ai.grinder.rewriter.core.FirstOf) Test(org.junit.Test)

Example 8 with Rewriter

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

Example 9 with Rewriter

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

Example 10 with Rewriter

use of com.sri.ai.grinder.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 : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) ContextSplitting(com.sri.ai.grinder.core.constraint.ContextSplitting) Solution(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Aggregations

Rewriter (com.sri.ai.grinder.rewriter.api.Rewriter)15 Expression (com.sri.ai.expresso.api.Expression)11 Context (com.sri.ai.grinder.api.Context)9 TrueContext (com.sri.ai.grinder.core.TrueContext)9 TopRewriter (com.sri.ai.grinder.rewriter.api.TopRewriter)7 Test (org.junit.Test)7 CombiningTopRewriter (com.sri.ai.grinder.rewriter.core.CombiningTopRewriter)6 Recursive (com.sri.ai.grinder.rewriter.core.Recursive)6 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)6 FirstOf (com.sri.ai.grinder.rewriter.core.FirstOf)5 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)5 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)5 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver)4 Solution (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution)4 Exhaustive (com.sri.ai.grinder.rewriter.core.Exhaustive)4 ContextSplitting (com.sri.ai.grinder.core.constraint.ContextSplitting)3 Switch (com.sri.ai.grinder.rewriter.core.Switch)3 CommonSimplifier (com.sri.ai.grinder.library.commonrewriters.CommonSimplifier)2 RewriterFromStepMaker (com.sri.ai.grinder.rewriter.api.RewriterFromStepMaker)2 ConstantExpressionStepSolver (com.sri.ai.grinder.theory.base.ConstantExpressionStepSolver)2