use of com.sri.ai.grinder.sgdpllt.rewriter.core.Switch in project aic-expresso by aic-sri-international.
the class TopRewriter method makeMergedSwitches.
public static <T> List<Switch<T>> makeMergedSwitches(List<? extends TopRewriter> topRewriters) throws Error {
List<Rewriter> flattenedRewriters = FirstOf.flatten(topRewriters);
Map<Function<Expression, T>, List<Switch<T>>> switchRewritersSeparatedByKeyMaker = map();
for (Rewriter switchRewriterAsRewriter : flattenedRewriters) {
if (!(switchRewriterAsRewriter instanceof Switch)) {
throw new Error(DefaultTopRewriter.class + " merge must be applied only to lists of rewriters that, once flattened with regard to " + FirstOf.class + ", are composed only of " + Switch.class + " rewriters. This is requires because the final product must be a " + Switch.class + " rewriter, and it needs each basic rewriter to be associated with a key value, and only other " + Switch.class + "rewriters provide those.");
} else {
@SuppressWarnings("unchecked") Switch<T> switchRewriter = (Switch<T>) switchRewriterAsRewriter;
Util.addToCollectionValuePossiblyCreatingIt(switchRewritersSeparatedByKeyMaker, switchRewriter.getKeyMaker(), switchRewriter, LinkedList.class);
}
}
List<Switch<T>> mergedRewriters = list();
for (Map.Entry<Function<Expression, T>, List<Switch<T>>> entry : switchRewritersSeparatedByKeyMaker.entrySet()) {
Switch<T> mergedForEntry = Switch.merge(entry.getValue());
mergedRewriters.add(mergedForEntry);
// we don't need to use entry.getKey() because that's the keyMaker, which is also present in the values ({@link Switch} rewriters).
// The entry.getKey()s are only needed to make sure the rewriters being merged share the same keyMaker.
}
return mergedRewriters;
}
use of com.sri.ai.grinder.sgdpllt.rewriter.core.Switch in project aic-expresso by aic-sri-international.
the class TopRewriter method merge.
/**
* 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 DefaultTopRewriter} rewriter with them as base rewriters.
* If this list contains only one {@link Switch<T>} rewriter, returns it.
* @param topRewriters
* @param <T> the type of keys in the switch rewriters.
* @return
*/
public static <T> TopRewriter merge(List<? extends TopRewriter> topRewriters) {
List<Switch<T>> mergedTopRewriters = makeMergedSwitches(topRewriters);
TopRewriter result;
if (mergedTopRewriters.size() == 1) {
result = getFirst(mergedTopRewriters);
} else {
result = new DefaultTopRewriter(mergedTopRewriters, true);
}
return result;
}
use of com.sri.ai.grinder.sgdpllt.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.merge(list(switch1, switch2, switch3, switch4, switch5));
assertEquals(expected, merged);
}
use of com.sri.ai.grinder.sgdpllt.rewriter.core.Switch in project aic-expresso by aic-sri-international.
the class SwitchTest method testSimpleSwitchRewriter.
@Test
public void testSimpleSwitchRewriter() {
RewriterFromStepMaker baseRewriterSum = (Expression e, Context c) -> {
int argument1 = e.get(0).intValue();
int argument2 = e.get(1).intValue();
Symbol resultValue = DefaultSymbol.createSymbol(argument1 + argument2);
return new Solution(resultValue);
};
RewriterFromStepMaker baseRewriterSubtraction = (Expression e, Context c) -> {
int argument1 = e.get(0).intValue();
int argument2 = e.get(1).intValue();
Symbol resultValue = DefaultSymbol.createSymbol(argument1 - argument2);
return new Solution(resultValue);
};
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 - 7");
expected = parse("3");
runTest(rewriter, initial, expected, map());
initial = parse("10 + 3");
expected = parse("13");
runTest(rewriter, initial, expected, map());
}
use of com.sri.ai.grinder.sgdpllt.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 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);
}
Aggregations