Search in sources :

Example 11 with Rewriter

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

the class SwitchTest method testMerge.

@Test
public void testMerge() {
    Switch<String> switch1 = new Switch<>(stringMaker, Util.map("1", new Label("11"), "2", new Label("21")));
    Switch<String> switch2 = new Switch<>(stringMaker, Util.map("1", new Label("12"), "3", new Label("31")));
    Switch<String> switch3 = new Switch<>(stringMaker, Util.map());
    Switch<String> switch4 = new Switch<>(stringMaker, Util.map("4", new Label("41")));
    Switch<String> switch5 = new Switch<>(stringMaker, Util.map("3", new Label("32")));
    Switch<String> expected = new Switch<>(stringMaker, Util.map("1", new FirstOf(list(new Label("11"), new Label("12"))), "2", new Label("21"), "3", new FirstOf(list(new Label("31"), new Label("32"))), "4", new Label("41")));
    Rewriter merged = Switch.merge(list(switch1, switch2, switch3, switch4, switch5));
    assertEquals(expected, merged);
}
Also used : Switch(com.sri.ai.grinder.sgdpllt.rewriter.core.Switch) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) TopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter) FirstOf(com.sri.ai.grinder.sgdpllt.rewriter.core.FirstOf) Test(org.junit.Test)

Example 12 with Rewriter

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

the class RecursiveTest method runTest.

private void runTest(Rewriter rewriter, Expression initial, Expression expected, Map<Expression, Expression> symbolsAndTypes) {
    CompoundTheory theory = new CompoundTheory(new PropositionalTheory(), new DifferenceArithmeticTheory(false, true));
    Context context = new TrueContext(theory);
    context = context.registerAdditionalSymbolsAndTypes(symbolsAndTypes);
    Rewriter recursive = new Recursive(rewriter);
    Expression solution = recursive.apply(initial, context);
    System.out.println("Solution: " + solution);
    assertEquals(expected, solution);
}
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) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) Recursive(com.sri.ai.grinder.sgdpllt.rewriter.core.Recursive) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext)

Example 13 with Rewriter

use of com.sri.ai.grinder.sgdpllt.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 DefaultTopRewriter(new Switch<String>(stringMaker, Util.map())));
    expected = new Switch<String>(stringMaker, Util.map());
    merged = TopRewriter.merge(initialRewriters);
    assertEquals(expected, merged);
    initialRewriters = list(new Switch<String>(stringMaker, Util.map()), new DefaultTopRewriter(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.merge(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.merge(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 DefaultTopRewriter(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 DefaultTopRewriter(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.merge(initialRewriters);
    assertEquals(expected, merged);
}
Also used : DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) Switch(com.sri.ai.grinder.sgdpllt.rewriter.core.Switch) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) TopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter) DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) TopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter) FirstOf(com.sri.ai.grinder.sgdpllt.rewriter.core.FirstOf) Test(org.junit.Test)

Example 14 with Rewriter

use of com.sri.ai.grinder.sgdpllt.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 : ContextSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) Expression(com.sri.ai.expresso.api.Expression) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) Recursive(com.sri.ai.grinder.sgdpllt.rewriter.core.Recursive) Util.map(com.sri.ai.util.Util.map) Symbol(com.sri.ai.expresso.api.Symbol) Switch(com.sri.ai.grinder.sgdpllt.rewriter.core.Switch) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) DefaultSymbol(com.sri.ai.expresso.core.DefaultSymbol) DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) TopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter) RewriterFromStepMaker(com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker) Function(com.google.common.base.Function) Util.list(com.sri.ai.util.Util.list) Test(org.junit.Test) Context(com.sri.ai.grinder.sgdpllt.api.Context) List(java.util.List) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) ConstantExpressionStepSolver(com.sri.ai.grinder.sgdpllt.theory.base.ConstantExpressionStepSolver) FirstOf(com.sri.ai.grinder.sgdpllt.rewriter.core.FirstOf) Util(com.sri.ai.util.Util) Assert.assertEquals(org.junit.Assert.assertEquals) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver) Switch(com.sri.ai.grinder.sgdpllt.rewriter.core.Switch) Expression(com.sri.ai.expresso.api.Expression) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) TopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter) Test(org.junit.Test)

Example 15 with Rewriter

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

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