Search in sources :

Example 1 with Switch

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;
}
Also used : DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) Function(com.google.common.base.Function) Switch(com.sri.ai.grinder.sgdpllt.rewriter.core.Switch) DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map)

Example 2 with Switch

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;
}
Also used : DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) Switch(com.sri.ai.grinder.sgdpllt.rewriter.core.Switch) DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter)

Example 3 with Switch

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);
}
Also used : Switch(com.sri.ai.grinder.sgdpllt.rewriter.core.Switch) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) TopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter) FirstOf(com.sri.ai.grinder.sgdpllt.rewriter.core.FirstOf) Test(org.junit.Test)

Example 4 with Switch

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());
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Switch(com.sri.ai.grinder.sgdpllt.rewriter.core.Switch) Expression(com.sri.ai.expresso.api.Expression) Symbol(com.sri.ai.expresso.api.Symbol) DefaultSymbol(com.sri.ai.expresso.core.DefaultSymbol) RewriterFromStepMaker(com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker) Solution(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Example 5 with Switch

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);
}
Also used : DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) Switch(com.sri.ai.grinder.sgdpllt.rewriter.core.Switch) Rewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter) DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) TopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter) DefaultTopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter) TopRewriter(com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter) FirstOf(com.sri.ai.grinder.sgdpllt.rewriter.core.FirstOf) Test(org.junit.Test)

Aggregations

Switch (com.sri.ai.grinder.sgdpllt.rewriter.core.Switch)6 DefaultTopRewriter (com.sri.ai.grinder.sgdpllt.rewriter.core.DefaultTopRewriter)5 Test (org.junit.Test)4 Rewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.Rewriter)3 TopRewriter (com.sri.ai.grinder.sgdpllt.rewriter.api.TopRewriter)3 FirstOf (com.sri.ai.grinder.sgdpllt.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.sgdpllt.api.Context)2 Solution (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver.Solution)2 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)2 RewriterFromStepMaker (com.sri.ai.grinder.sgdpllt.rewriter.api.RewriterFromStepMaker)2 List (java.util.List)2 Map (java.util.Map)2 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)1 ExpressionLiteralSplitterStepSolver (com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)1 ContextSplitting (com.sri.ai.grinder.sgdpllt.core.constraint.ContextSplitting)1 Recursive (com.sri.ai.grinder.sgdpllt.rewriter.core.Recursive)1