Search in sources :

Example 6 with TopRewriter

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

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

the class TupleRewriterTest method testTupleEqualityTopRewriter.

@Test
public void testTupleEqualityTopRewriter() {
    TopRewriter tupleEqualityTopRewriter = new TupleEqualityTopRewriter();
    Assert.assertEquals(parse("A1 = B1 and A2 = B2 and A3 = B3"), tupleEqualityTopRewriter.apply(parse("(A1, A2, A3) = (B1, B2, B3)"), context));
    Assert.assertEquals(parse("(A1_1, A1_2, A1_3) = (B1_1, B1_2, B1_3) and A2 = B2 and A3 = B3"), tupleEqualityTopRewriter.apply(parse("((A1_1, A1_2, A1_3), A2, A3) = ((B1_1, B1_2, B1_3), B2, B3)"), context));
    Assert.assertEquals(parse("A1 != B1 or A2 != B2 or A3 != B3"), tupleEqualityTopRewriter.apply(parse("(A1, A2, A3) != (B1, B2, B3)"), context));
    Assert.assertEquals(parse("(A1_1, A1_2, A1_3) != (B1_1, B1_2, B1_3) or A2 != B2 or A3 != B3"), tupleEqualityTopRewriter.apply(parse("((A1_1, A1_2, A1_3), A2, A3) != ((B1_1, B1_2, B1_3), B2, B3)"), context));
    Expression expr = parse("(A1, A2) = (B1, B2, B3)");
    Assert.assertTrue(expr == tupleEqualityTopRewriter.apply(expr, context));
    expr = parse("A1 = B1");
    Assert.assertTrue(expr == tupleEqualityTopRewriter.apply(expr, context));
    expr = parse("(A1, A2) != (B1, B2, B3)");
    Assert.assertTrue(expr == tupleEqualityTopRewriter.apply(expr, context));
    expr = parse("A1 != B1");
    Assert.assertTrue(expr == tupleEqualityTopRewriter.apply(expr, context));
}
Also used : TupleEqualityTopRewriter(com.sri.ai.grinder.theory.tuple.rewriter.TupleEqualityTopRewriter) Expression(com.sri.ai.expresso.api.Expression) TupleEqualityTopRewriter(com.sri.ai.grinder.theory.tuple.rewriter.TupleEqualityTopRewriter) TupleGetSetTopRewriter(com.sri.ai.grinder.theory.tuple.rewriter.TupleGetSetTopRewriter) TopRewriter(com.sri.ai.grinder.rewriter.api.TopRewriter) Test(org.junit.Test)

Example 8 with TopRewriter

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

the class TupleRewriterTest method testTupleGetSetTopRewriter.

@Test
public void testTupleGetSetTopRewriter() {
    TopRewriter tupleGetSetTopRewriter = new TupleGetSetTopRewriter();
    Assert.assertEquals(parse("if I = 1 then a else if I = 2 then b else c"), tupleGetSetTopRewriter.apply(parse("get((a,b,c),I)"), context));
    Assert.assertEquals(parse("if I = 1 then (e,b,c) else if I = 2 then (a,e,c) else (a,b,e)"), tupleGetSetTopRewriter.apply(parse("set((a,b,c),I,e)"), context));
}
Also used : TupleGetSetTopRewriter(com.sri.ai.grinder.theory.tuple.rewriter.TupleGetSetTopRewriter) TupleEqualityTopRewriter(com.sri.ai.grinder.theory.tuple.rewriter.TupleEqualityTopRewriter) TupleGetSetTopRewriter(com.sri.ai.grinder.theory.tuple.rewriter.TupleGetSetTopRewriter) TopRewriter(com.sri.ai.grinder.rewriter.api.TopRewriter) Test(org.junit.Test)

Aggregations

TopRewriter (com.sri.ai.grinder.rewriter.api.TopRewriter)8 CombiningTopRewriter (com.sri.ai.grinder.rewriter.core.CombiningTopRewriter)4 Rewriter (com.sri.ai.grinder.rewriter.api.Rewriter)3 Test (org.junit.Test)3 Expression (com.sri.ai.expresso.api.Expression)2 Exhaustive (com.sri.ai.grinder.rewriter.core.Exhaustive)2 Recursive (com.sri.ai.grinder.rewriter.core.Recursive)2 TupleEqualityTopRewriter (com.sri.ai.grinder.theory.tuple.rewriter.TupleEqualityTopRewriter)2 TupleGetSetTopRewriter (com.sri.ai.grinder.theory.tuple.rewriter.TupleGetSetTopRewriter)2 ForAllRewriter (com.sri.ai.grinder.library.boole.ForAllRewriter)1 ThereExistsRewriter (com.sri.ai.grinder.library.boole.ThereExistsRewriter)1 CommonSimplifier (com.sri.ai.grinder.library.commonrewriters.CommonSimplifier)1 MaxRewriter (com.sri.ai.grinder.library.number.MaxRewriter)1 ProductRewriter (com.sri.ai.grinder.library.number.ProductRewriter)1 SummationRewriter (com.sri.ai.grinder.library.number.SummationRewriter)1 ProceduralAttachments.getProceduralAttachmentsTopRewriter (com.sri.ai.grinder.library.proceduralattachment.ProceduralAttachments.getProceduralAttachmentsTopRewriter)1 FirstOf (com.sri.ai.grinder.rewriter.core.FirstOf)1 FunctorSwitch (com.sri.ai.grinder.rewriter.core.FunctorSwitch)1 Switch (com.sri.ai.grinder.rewriter.core.Switch)1 SymbolSwitch (com.sri.ai.grinder.rewriter.core.SymbolSwitch)1