Search in sources :

Example 1 with FirstOf

use of com.sri.ai.grinder.rewriter.core.FirstOf 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.mergeSwitchesWithTheSameKeyMakerIntoASingleOne(list(switch1, switch2, switch3, switch4, switch5));
    assertEquals(expected, merged);
}
Also used : Switch(com.sri.ai.grinder.rewriter.core.Switch) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) TopRewriter(com.sri.ai.grinder.rewriter.api.TopRewriter) FirstOf(com.sri.ai.grinder.rewriter.core.FirstOf) Test(org.junit.Test)

Example 2 with FirstOf

use of com.sri.ai.grinder.rewriter.core.FirstOf 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 3 with FirstOf

use of com.sri.ai.grinder.rewriter.core.FirstOf in project aic-expresso by aic-sri-international.

the class FirstOfTest method runTest.

private void runTest(List<Rewriter> rewriters, 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.makeCloneWithAdditionalRegisteredSymbolsAndTypes(symbolsAndTypes);
    Rewriter firstOf = new FirstOf(rewriters);
    Expression solution = firstOf.apply(initial, context);
    System.out.println("Solution: " + solution);
    assertEquals(expected, solution);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) FirstOf(com.sri.ai.grinder.rewriter.core.FirstOf) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.core.TrueContext)

Example 4 with FirstOf

use of com.sri.ai.grinder.rewriter.core.FirstOf in project aic-expresso by aic-sri-international.

the class SymbolicShell method evaluate.

private static void evaluate(Expression input, ConsoleIterator consoleIterator, Theory theory, Context context) {
    Rewriter prettyRewriter = new Exhaustive(new Recursive(new FirstOf(theory.getTopRewriter(), new PrettySimplifier())));
    Expression result = theory.evaluate(input, context);
    result = prettyRewriter.apply(result, context);
    consoleIterator.getOutputWriter().println(result + "\n");
}
Also used : PrettySimplifier(com.sri.ai.grinder.library.pretty.PrettySimplifier) Expression(com.sri.ai.expresso.api.Expression) Exhaustive(com.sri.ai.grinder.rewriter.core.Exhaustive) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) FirstOf(com.sri.ai.grinder.rewriter.core.FirstOf) Recursive(com.sri.ai.grinder.rewriter.core.Recursive)

Aggregations

Rewriter (com.sri.ai.grinder.rewriter.api.Rewriter)4 FirstOf (com.sri.ai.grinder.rewriter.core.FirstOf)4 Expression (com.sri.ai.expresso.api.Expression)2 TopRewriter (com.sri.ai.grinder.rewriter.api.TopRewriter)2 CombiningTopRewriter (com.sri.ai.grinder.rewriter.core.CombiningTopRewriter)2 Switch (com.sri.ai.grinder.rewriter.core.Switch)2 Test (org.junit.Test)2 Context (com.sri.ai.grinder.api.Context)1 TrueContext (com.sri.ai.grinder.core.TrueContext)1 PrettySimplifier (com.sri.ai.grinder.library.pretty.PrettySimplifier)1 Exhaustive (com.sri.ai.grinder.rewriter.core.Exhaustive)1 Recursive (com.sri.ai.grinder.rewriter.core.Recursive)1 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)1 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)1 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)1