Search in sources :

Example 41 with Context

use of com.sri.ai.grinder.api.Context 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 42 with Context

use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.

the class LambdaBetaReductionSimplifierTest method testSimpleReduction.

@Test
public void testSimpleReduction() {
    Type peopleType = new Categorical("People", 4, arrayList(makeSymbol("ann"), makeSymbol("bob")));
    Context context = new TrueContext();
    context = context.makeNewContextWithAddedType(peopleType);
    Assert.assertEquals(parse("if X = ann then 0 else if X = bob then 0 else 0"), simplifier.apply(parse("(lambda : if X = ann then 0 else if X = bob then 0 else 0)()"), context));
    Assert.assertEquals(parse("if ann = ann then 0 else if ann = bob then 0 else 0"), simplifier.apply(parse("(lambda X in People : if X = ann then 0 else if X = bob then 0 else 0)(ann)"), context));
    Assert.assertEquals(parse("if ann = ann then 0 else if bob = bob then 0 else 0"), simplifier.apply(parse("(lambda X in People, Y in People : if X = ann then 0 else if Y = bob then 0 else 0)(ann, bob)"), context));
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Type(com.sri.ai.expresso.api.Type) Categorical(com.sri.ai.expresso.type.Categorical) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Example 43 with Context

use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.

the class LambdaBetaReductionSimplifierTest method testNoReduction.

@Test
public void testNoReduction() {
    Type peopleType = new Categorical("People", 4, arrayList(makeSymbol("ann"), makeSymbol("bob"), makeSymbol("tom")));
    Context context = new TrueContext();
    context = context.makeNewContextWithAddedType(peopleType);
    Assert.assertEquals(parse("(lambda X in People : if X = ann then 0 else if X = bob then 0 else 0)()"), simplifier.apply(parse("(lambda X in People : if X = ann then 0 else if X = bob then 0 else 0)()"), context));
    Assert.assertEquals(parse("(lambda X in People, Y in People : if X = ann then 0 else if Y = bob then 0 else 0)(ann)"), simplifier.apply(parse("(lambda X in People, Y in People : if X = ann then 0 else if Y = bob then 0 else 0)(ann)"), context));
    Assert.assertEquals(parse("(lambda X in People, Y in People : if X = ann then 0 else if Y = bob then 0 else 0)(ann, bob, tom)"), simplifier.apply(parse("(lambda X in People, Y in People : if X = ann then 0 else if Y = bob then 0 else 0)(ann, bob, tom)"), context));
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Type(com.sri.ai.expresso.api.Type) Categorical(com.sri.ai.expresso.type.Categorical) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Example 44 with Context

use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.

the class ProceduralAttachmentsTest method test.

@Test
public void test() {
    Context context;
    Expression input;
    Expression expected;
    context = new TrueContext();
    Simplifier replaceBy10 = (e, c) -> makeSymbol(10);
    registerProceduralAttachment(makeSymbol("ten"), replaceBy10, context);
    Simplifier makeHWithFlippedArguments = (e, c) -> apply("h", e.getArguments());
    registerProceduralAttachment(makeSymbol("g"), 2, makeHWithFlippedArguments, context);
    input = parse("ten + 1 + g(1, 2) + g(1)");
    expected = parse("11 + h(1, 2) + g(1)");
    runTest(input, expected, 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) Test(org.junit.Test) Expression(com.sri.ai.expresso.api.Expression) CombiningTopRewriter(com.sri.ai.grinder.rewriter.core.CombiningTopRewriter) ProceduralAttachments.registerProceduralAttachment(com.sri.ai.grinder.library.proceduralattachment.ProceduralAttachments.registerProceduralAttachment) ProceduralAttachments.getProceduralAttachmentsTopRewriter(com.sri.ai.grinder.library.proceduralattachment.ProceduralAttachments.getProceduralAttachmentsTopRewriter) Exhaustive(com.sri.ai.grinder.rewriter.core.Exhaustive) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) TrueContext(com.sri.ai.grinder.core.TrueContext) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) TopRewriter(com.sri.ai.grinder.rewriter.api.TopRewriter) Context(com.sri.ai.grinder.api.Context) Util.println(com.sri.ai.util.Util.println) Recursive(com.sri.ai.grinder.rewriter.core.Recursive) Assert.assertEquals(org.junit.Assert.assertEquals) Expression(com.sri.ai.expresso.api.Expression) CommonSimplifier(com.sri.ai.grinder.library.commonrewriters.CommonSimplifier) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Example 45 with Context

use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.

the class ExhaustiveTest method runTest.

private void runTest(Rewriter rewriter, Expression initial, Expression expected, Map<Expression, Expression> symbolsAndTypes) {
    CompoundTheory theory = new CompoundTheory(new PropositionalTheory(), new DifferenceArithmeticTheory(false, true));
    Context context = new TrueContext(theory);
    context = context.makeCloneWithAdditionalRegisteredSymbolsAndTypes(symbolsAndTypes);
    Rewriter exhaustive = new Exhaustive(rewriter);
    Expression solution = exhaustive.apply(initial, context);
    System.out.println("Solution: " + solution);
    assertEquals(expected, solution);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) Exhaustive(com.sri.ai.grinder.rewriter.core.Exhaustive) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.core.TrueContext)

Aggregations

Context (com.sri.ai.grinder.api.Context)132 Expression (com.sri.ai.expresso.api.Expression)100 Test (org.junit.Test)50 TrueContext (com.sri.ai.grinder.core.TrueContext)40 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)36 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)32 Type (com.sri.ai.expresso.api.Type)31 Theory (com.sri.ai.grinder.api.Theory)24 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)23 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)22 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)21 Set (java.util.Set)20 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)19 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)19 CompleteMultiVariableContext (com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext)16 Map (java.util.Map)16 Beta (com.google.common.annotations.Beta)15 Constraint (com.sri.ai.grinder.api.Constraint)15 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)14