use of com.sri.ai.grinder.rewriter.core.CombiningTopRewriter in project aic-expresso by aic-sri-international.
the class SamplingProceduralAttachmentSingleQuantifierEliminatorTest method test.
@Test
public void test() {
String intervalString = "0..10";
Context context = new TrueContext(new DifferenceArithmeticTheory(true, true));
MultiQuantifierEliminationProblem problem = new DefaultMultiQuantifierEliminationProblem(new Sum(), list(parse("I")), list(parse(intervalString)), parse("I != 3"), parse("I^2"));
SamplingProceduralAttachmentSingleQuantifierEliminator eliminator = new SamplingProceduralAttachmentSingleQuantifierEliminator(new CommonSimplifier(), new Random());
// SHOULD NOT HAVE TO
context = problem.extend(context);
Expression sampler = eliminator.solve(problem, context);
println(sampler);
Rewriter rewriter = new CombiningTopRewriter(new CommonSimplifier(), ProceduralAttachments.getProceduralAttachmentsTopRewriter(context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
println(rewriter.apply(sampler, context));
}
use of com.sri.ai.grinder.rewriter.core.CombiningTopRewriter in project aic-expresso by aic-sri-international.
the class ProceduralAttachmentsTest method runTest.
private void runTest(Expression input, Expression expected, Context context) {
TopRewriter topRewriter;
Rewriter evaluator;
Expression output;
topRewriter = new CombiningTopRewriter(new CommonSimplifier(), getProceduralAttachmentsTopRewriter(context));
evaluator = new Recursive(new Exhaustive(topRewriter));
output = evaluator.apply(input, context);
println(output);
assertEquals(expected, output);
}
use of com.sri.ai.grinder.rewriter.core.CombiningTopRewriter 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.CombiningTopRewriter 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