Search in sources :

Example 1 with Simplifier

use of com.sri.ai.grinder.rewriter.api.Simplifier 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 2 with Simplifier

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

the class TupleRewriterTest method testTupleGetSimplification.

@Test
public void testTupleGetSimplification() {
    Simplifier tupleGetSimplifier = new TupleGetSimplifier();
    Assert.assertEquals(parse("a"), tupleGetSimplifier.apply(parse("get((a,b,c),1)"), context));
    Assert.assertEquals(parse("b"), tupleGetSimplifier.apply(parse("get((a,b,c),2)"), context));
    Assert.assertEquals(parse("c"), tupleGetSimplifier.apply(parse("get((a,b,c),3)"), context));
    Assert.assertEquals(parse("a"), tupleGetSimplifier.apply(parse("get(tuple(a),I)"), context));
    Assert.assertEquals(parse("if I = 1 then a else b"), tupleGetSimplifier.apply(parse("get((a,b),I)"), context));
    Assert.assertEquals(parse("if I = 1 then a else if I = 2 then b else c"), tupleGetSimplifier.apply(parse("get((a,b,c),I)"), context));
    Expression expr = parse("get(I, (a,b,c))");
    Assert.assertTrue(expr == tupleGetSimplifier.apply(expr, context));
    expr = parse("get(N, 1)");
    Assert.assertTrue(expr == tupleGetSimplifier.apply(expr, context));
}
Also used : Expression(com.sri.ai.expresso.api.Expression) TupleGetSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleGetSimplifier) TupleQuantifierSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleQuantifierSimplifier) TupleEqualitySimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleEqualitySimplifier) TupleSetSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleSetSimplifier) TupleDisequalitySimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleDisequalitySimplifier) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) TupleGetSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleGetSimplifier) Test(org.junit.Test)

Example 3 with Simplifier

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

the class TupleRewriterTest method testTupleEqualitySimplification.

@Test
public void testTupleEqualitySimplification() {
    Simplifier tupleEqualitySimplifier = new TupleEqualitySimplifier();
    Assert.assertEquals(parse("A1 = B1 and A2 = B2 and A3 = B3"), tupleEqualitySimplifier.apply(parse("(A1, A2, A3) = (B1, B2, B3)"), context));
    Expression expr = parse("(A1, A2) = (B1, B2, B3)");
    Assert.assertTrue(expr == tupleEqualitySimplifier.apply(expr, context));
    expr = parse("A1 = B1");
    Assert.assertTrue(expr == tupleEqualitySimplifier.apply(expr, context));
}
Also used : TupleEqualitySimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleEqualitySimplifier) Expression(com.sri.ai.expresso.api.Expression) TupleGetSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleGetSimplifier) TupleQuantifierSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleQuantifierSimplifier) TupleEqualitySimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleEqualitySimplifier) TupleSetSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleSetSimplifier) TupleDisequalitySimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleDisequalitySimplifier) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Test(org.junit.Test)

Example 4 with Simplifier

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

the class LinearRealArithmeticTheoryTest method testSummation.

@Test
public void testSummation() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new LinearRealArithmeticTheory(true, true));
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Simplifier simplifier = (e, c) -> theoryTestingSupport.getTheory().simplify(e, c);
    Expression variable;
    String constraintString;
    String bodyString;
    Expression expected;
    variable = parse("X");
    constraintString = "true";
    bodyString = "1";
    expected = parse("4");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < 3 and X != 2";
    bodyString = "1";
    expected = parse("3");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < Y and X != 2";
    bodyString = "1";
    expected = parse("if Y > 0 then Y else 0");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < 3 and X != 2";
    bodyString = "Y";
    expected = parse("3*Y");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < 3 and X != 2";
    bodyString = "X";
    expected = parse("4.5");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < 3 and X != 2 and X = 2";
    bodyString = "Y";
    expected = parse("0");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < Y and X != 2";
    bodyString = "X";
    expected = parse("if Y > 0 then 0.5*Y^2 else 0");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < Y and X != 2";
    bodyString = "Y";
    expected = parse("if Y > 0 then Y^2 else 0");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
    variable = parse("X");
    constraintString = "X < Y and X != 2";
    bodyString = "X + Y";
    expected = parse("if Y > 0 then 1.5*Y^2 else 0");
    runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
// variable = parse("X");
// constraintString = "X < Y and X != 2";
// bodyString = "X^2 + Y";
// expected = parse("if Y > 0 then 0.333333333*Y^3 + Y^2 else 0");
// runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) BinaryFunction(com.sri.ai.util.base.BinaryFunction) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) Random(java.util.Random) Expression(com.sri.ai.expresso.api.Expression) SummationOnLinearRealArithmeticAndPolynomialStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.SummationOnLinearRealArithmeticAndPolynomialStepSolver) TrueContext(com.sri.ai.grinder.core.TrueContext) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) ExpressionStepSolver(com.sri.ai.grinder.api.ExpressionStepSolver) Context(com.sri.ai.grinder.api.Context) MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver) Function(com.google.common.base.Function) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) SatisfiabilityOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.SatisfiabilityOfSingleVariableLinearRealArithmeticConstraintStepSolver) Test(org.junit.Test) Beta(com.google.common.annotations.Beta) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) Theory(com.sri.ai.grinder.api.Theory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) Sum(com.sri.ai.grinder.group.Sum) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver) Assert.assertEquals(org.junit.Assert.assertEquals) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Test(org.junit.Test)

Example 5 with Simplifier

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

the class TupleRewriterTest method testTupleSetSimplification.

@Test
public void testTupleSetSimplification() {
    Simplifier tupleSetSimplifier = new TupleSetSimplifier();
    Assert.assertEquals(parse("(e,b,c)"), tupleSetSimplifier.apply(parse("set((a,b,c),1,e)"), context));
    Assert.assertEquals(parse("(a,e,c)"), tupleSetSimplifier.apply(parse("set((a,b,c),2,e)"), context));
    Assert.assertEquals(parse("(a,b,e)"), tupleSetSimplifier.apply(parse("set((a,b,c),3,e)"), context));
    Assert.assertEquals(parse("tuple(e)"), tupleSetSimplifier.apply(parse("set(tuple(a),I,e)"), context));
    Assert.assertEquals(parse("if I = 1 then (e,b) else (a,e)"), tupleSetSimplifier.apply(parse("set((a,b),I,e)"), context));
    Assert.assertEquals(parse("if I = 1 then (e,b,c) else if I = 2 then (a,e,c) else (a,b,e)"), tupleSetSimplifier.apply(parse("set((a,b,c),I,e)"), context));
    Expression expr = parse("set(I, (a,b,c), e)");
    Assert.assertTrue(expr == tupleSetSimplifier.apply(expr, context));
}
Also used : TupleSetSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleSetSimplifier) Expression(com.sri.ai.expresso.api.Expression) TupleGetSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleGetSimplifier) TupleQuantifierSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleQuantifierSimplifier) TupleEqualitySimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleEqualitySimplifier) TupleSetSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleSetSimplifier) TupleDisequalitySimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleDisequalitySimplifier) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Test(org.junit.Test)

Aggregations

Simplifier (com.sri.ai.grinder.rewriter.api.Simplifier)11 Expression (com.sri.ai.expresso.api.Expression)10 Test (org.junit.Test)8 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)5 Context (com.sri.ai.grinder.api.Context)5 TupleDisequalitySimplifier (com.sri.ai.grinder.theory.tuple.rewriter.TupleDisequalitySimplifier)5 TupleEqualitySimplifier (com.sri.ai.grinder.theory.tuple.rewriter.TupleEqualitySimplifier)5 TupleGetSimplifier (com.sri.ai.grinder.theory.tuple.rewriter.TupleGetSimplifier)5 TupleQuantifierSimplifier (com.sri.ai.grinder.theory.tuple.rewriter.TupleQuantifierSimplifier)5 TupleSetSimplifier (com.sri.ai.grinder.theory.tuple.rewriter.TupleSetSimplifier)5 TupleValuedFreeVariablesSimplifier (com.sri.ai.grinder.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier)5 Beta (com.google.common.annotations.Beta)4 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)4 Type (com.sri.ai.expresso.api.Type)3 Constraint (com.sri.ai.grinder.api.Constraint)3 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)3 Theory (com.sri.ai.grinder.api.Theory)3 DefaultSingleQuantifierEliminationProblem (com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem)3 And (com.sri.ai.grinder.library.boole.And)3 BinaryFunction (com.sri.ai.util.base.BinaryFunction)3