Search in sources :

Example 1 with Switch

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

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

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

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

the class TopRewriter method gatherListsOfSwitchesSharingKeyMaker.

public static <T> Map<Function<Expression, T>, List<Rewriter>> gatherListsOfSwitchesSharingKeyMaker(List<Rewriter> switches) throws Error {
    Map<Function<Expression, T>, List<Rewriter>> fromKeyMakerToSwitches = map();
    for (Rewriter switchAsRewriter : switches) {
        @SuppressWarnings("unchecked") Switch<T> switchObject = (Switch<T>) switchAsRewriter;
        Util.addToCollectionValuePossiblyCreatingIt(fromKeyMakerToSwitches, switchObject.getKeyMaker(), switchObject, LinkedList.class);
    }
    return fromKeyMakerToSwitches;
}
Also used : Function(com.google.common.base.Function) Switch(com.sri.ai.grinder.rewriter.core.Switch) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) List(java.util.List) LinkedList(java.util.LinkedList)

Example 5 with Switch

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

the class TopRewriter method makeTopRewriterFromTopRewritersThatAreEitherFirstOfOrSwitches.

/**
 * Takes a list of {@link TopRewriter}s,
 * then determines a list of {@link Switch<T>} rewriters,
 * each of them the merge of all {@link Switch<T>} rewriters with the same key maker
 * (from {@link Switch.merge(List<Rewriter>)}.
 * If this list contains more than one {@link Switch<T>} rewriter,
 * returns a {@link CombiningTopRewriter} rewriter with them as base rewriters.
 * If this list contains only one {@link Switch<T>} rewriter, returns it.
 * @param topRewritersThatAreEitherFirstOfOrSwitches
 * @param <T> the type of keys in the switch rewriters.
 * @return
 */
public static <T> TopRewriter makeTopRewriterFromTopRewritersThatAreEitherFirstOfOrSwitches(List<? extends TopRewriter> topRewritersThatAreEitherFirstOfOrSwitches) {
    List<Switch<T>> mergedSwitches = makeMergedSwitchesFromTopRewritersThatAreEitherFirstOfOrSwitches(topRewritersThatAreEitherFirstOfOrSwitches);
    TopRewriter result = makeTopRewriterFromAlreadyMergedSwitches(mergedSwitches);
    return result;
}
Also used : Switch(com.sri.ai.grinder.rewriter.core.Switch) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter)

Aggregations

Switch (com.sri.ai.grinder.rewriter.core.Switch)6 CombiningTopRewriter (com.sri.ai.grinder.rewriter.core.CombiningTopRewriter)5 Test (org.junit.Test)4 Rewriter (com.sri.ai.grinder.rewriter.api.Rewriter)3 TopRewriter (com.sri.ai.grinder.rewriter.api.TopRewriter)3 FirstOf (com.sri.ai.grinder.rewriter.core.FirstOf)3 Function (com.google.common.base.Function)2 Expression (com.sri.ai.expresso.api.Expression)2 Symbol (com.sri.ai.expresso.api.Symbol)2 DefaultSymbol (com.sri.ai.expresso.core.DefaultSymbol)2 Context (com.sri.ai.grinder.api.Context)2 Solution (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution)2 TrueContext (com.sri.ai.grinder.core.TrueContext)2 RewriterFromStepMaker (com.sri.ai.grinder.rewriter.api.RewriterFromStepMaker)2 List (java.util.List)2 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)1 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver)1 ContextSplitting (com.sri.ai.grinder.core.constraint.ContextSplitting)1 Recursive (com.sri.ai.grinder.rewriter.core.Recursive)1 ConstantExpressionStepSolver (com.sri.ai.grinder.theory.base.ConstantExpressionStepSolver)1