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")));
}
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);
}
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);
}
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;
}
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;
}
Aggregations