Search in sources :

Example 1 with CombiningTopRewriter

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));
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) CommonSimplifier(com.sri.ai.grinder.library.commonrewriters.CommonSimplifier) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) Sum(com.sri.ai.grinder.group.Sum) TrueContext(com.sri.ai.grinder.core.TrueContext) SamplingProceduralAttachmentSingleQuantifierEliminator(com.sri.ai.grinder.interpreter.SamplingProceduralAttachmentSingleQuantifierEliminator) Random(java.util.Random) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) Expression(com.sri.ai.expresso.api.Expression) DefaultMultiQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultMultiQuantifierEliminationProblem) MultiQuantifierEliminationProblem(com.sri.ai.grinder.api.MultiQuantifierEliminationProblem) DefaultMultiQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultMultiQuantifierEliminationProblem) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) Test(org.junit.Test)

Example 2 with CombiningTopRewriter

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);
}
Also used : CommonSimplifier(com.sri.ai.grinder.library.commonrewriters.CommonSimplifier) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) Expression(com.sri.ai.expresso.api.Expression) Exhaustive(com.sri.ai.grinder.rewriter.core.Exhaustive) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) ProceduralAttachments.getProceduralAttachmentsTopRewriter(com.sri.ai.grinder.library.proceduralattachment.ProceduralAttachments.getProceduralAttachmentsTopRewriter) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) TopRewriter(com.sri.ai.grinder.rewriter.api.TopRewriter) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) ProceduralAttachments.getProceduralAttachmentsTopRewriter(com.sri.ai.grinder.library.proceduralattachment.ProceduralAttachments.getProceduralAttachmentsTopRewriter) TopRewriter(com.sri.ai.grinder.rewriter.api.TopRewriter) Recursive(com.sri.ai.grinder.rewriter.core.Recursive)

Example 3 with CombiningTopRewriter

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);
}
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 CombiningTopRewriter

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

Aggregations

CombiningTopRewriter (com.sri.ai.grinder.rewriter.core.CombiningTopRewriter)4 Rewriter (com.sri.ai.grinder.rewriter.api.Rewriter)3 Expression (com.sri.ai.expresso.api.Expression)2 CommonSimplifier (com.sri.ai.grinder.library.commonrewriters.CommonSimplifier)2 TopRewriter (com.sri.ai.grinder.rewriter.api.TopRewriter)2 Switch (com.sri.ai.grinder.rewriter.core.Switch)2 Test (org.junit.Test)2 Context (com.sri.ai.grinder.api.Context)1 MultiQuantifierEliminationProblem (com.sri.ai.grinder.api.MultiQuantifierEliminationProblem)1 TrueContext (com.sri.ai.grinder.core.TrueContext)1 DefaultMultiQuantifierEliminationProblem (com.sri.ai.grinder.core.solver.DefaultMultiQuantifierEliminationProblem)1 Sum (com.sri.ai.grinder.group.Sum)1 SamplingProceduralAttachmentSingleQuantifierEliminator (com.sri.ai.grinder.interpreter.SamplingProceduralAttachmentSingleQuantifierEliminator)1 ProceduralAttachments.getProceduralAttachmentsTopRewriter (com.sri.ai.grinder.library.proceduralattachment.ProceduralAttachments.getProceduralAttachmentsTopRewriter)1 Exhaustive (com.sri.ai.grinder.rewriter.core.Exhaustive)1 FirstOf (com.sri.ai.grinder.rewriter.core.FirstOf)1 Recursive (com.sri.ai.grinder.rewriter.core.Recursive)1 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)1 Random (java.util.Random)1