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