use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class NumberOfDistinctExpressionsIsLessThanStepSolverTest method test.
@Test
public void test() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new EqualityTheory(true, true));
Context context = theoryTestingSupport.makeContextWithTestingInformation();
String contextString = "X != Y and X != a and X != b and Y != b";
List<String> elementsStrings = list("X", "Y", "a", "b", "c");
int limit = 5;
context = context.conjoin(parse(contextString), context);
ArrayList<Expression> list = mapIntoArrayList(elementsStrings, Expressions::parse);
NumberOfDistinctExpressionsIsLessThanStepSolver stepSolver = new NumberOfDistinctExpressionsIsLessThanStepSolver(limit, list);
Step step = stepSolver.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("X = c"), step.getSplitter());
ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsC = step.getStepSolverForWhenSplitterIsTrue();
ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromC = step.getStepSolverForWhenSplitterIsFalse();
// if X = c, the number of distinct values is at most 4, so it will never reach the limit
step = stepSolverIfXEqualsC.step(context);
assertEquals(false, step.itDepends());
assertEquals(TRUE, step.getValue());
// using again just to make sure it produces the same result
step = stepSolverIfXEqualsC.step(context);
assertEquals(false, step.itDepends());
assertEquals(TRUE, step.getValue());
// if X != c, the number of distinct values will now depend on Y = a
step = stepSolverIfXIsDifferentFromC.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("Y = a"), step.getSplitter());
// using again just to make sure it produces the same result
step = stepSolverIfXIsDifferentFromC.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("Y = a"), step.getSplitter());
ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYEqualsA = step.getStepSolverForWhenSplitterIsTrue();
ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYIsDifferentFromA = step.getStepSolverForWhenSplitterIsFalse();
// ok, moving on, assuming Y = a, limit will not be reached
step = stepSolverIfXIsDifferentFromCAndYEqualsA.step(context);
assertEquals(false, step.itDepends());
assertEquals(TRUE, step.getValue());
// if however Y != a, limit will depend on Y = c
step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromA.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("Y = c"), step.getSplitter());
ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsEqualToC = step.getStepSolverForWhenSplitterIsTrue();
ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsDifferentFromC = step.getStepSolverForWhenSplitterIsFalse();
// if Y = c, then limit is not going to be reached
step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsEqualToC.step(context);
assertEquals(false, step.itDepends());
assertEquals(TRUE, step.getValue());
// if Y != c, then limit is reached
step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsDifferentFromC.step(context);
assertEquals(false, step.itDepends());
assertEquals(FALSE, step.getValue());
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class RecursiveTest method testConditionalRecursiveRewriter.
@Test
public void testConditionalRecursiveRewriter() {
Expression xIs0 = parse("XIs0");
RewriterFromStepMaker rewriter = (Expression e, Context c) -> {
if (Expressions.isNumber(e)) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() + 1));
} else if (e.equals(parse("X"))) {
ContextSplitting splitting = new ContextSplitting(xIs0, c);
switch(splitting.getResult()) {
case LITERAL_IS_TRUE:
return new Solution(ZERO);
case LITERAL_IS_FALSE:
return new Solution(ONE);
case LITERAL_IS_UNDEFINED:
return new ItDependsOn(xIs0, splitting, new ConstantExpressionStepSolver(ZERO), new ConstantExpressionStepSolver(ONE));
default:
throw new Error("Unpredicted case.");
}
}
return new Solution(e);
};
Expression initial;
Expression expected;
initial = parse("X");
expected = parse("if " + xIs0 + " then 0 else 1");
runTest(rewriter, initial, expected, map(xIs0, parse("Boolean")));
initial = parse("f(9,g(X,7,6))");
expected = parse("if " + xIs0 + " then f(10,g(0,8,7)) else f(10,g(1,8,7))");
runTest(rewriter, initial, expected, map(xIs0, parse("Boolean")));
initial = parse("X(9,g(h(1,2,X),X,7,6))");
expected = parse("if " + xIs0 + " then 0(10,g(h(2,3,0),0,8,7)) else 1(10,g(h(2,3,1),1,8,7))");
runTest(rewriter, initial, expected, map(xIs0, parse("Boolean")));
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class SwitchTest 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.registerAdditionalSymbolsAndTypes(symbolsAndTypes);
Rewriter recursive = new Recursive(rewriter);
Expression solution = recursive.apply(initial, context);
System.out.println("Solution: " + solution);
assertEquals(expected, solution);
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class UnionEmptySetSimplifierTest method testSimplifyUniSets.
@Test
public void testSimplifyUniSets() {
Expression union = parse("{} union {(1,2)} union {(1,3)}");
Assert.assertEquals(parse("{(1,2)} union {(1,3)}"), simplifier.apply(union, context));
union = parse("{(1,2)} union {} union {(1,3)}");
Assert.assertEquals(parse("{(1,2)} union {(1,3)}"), simplifier.apply(union, context));
union = parse("{(1,2)} union {(1,3)} union {}");
Assert.assertEquals(parse("{(1,2)} union {(1,3)}"), simplifier.apply(union, context));
union = parse("{} union {(1,2)}");
Assert.assertEquals(parse("{(1,2)}"), simplifier.apply(union, context));
union = parse("{(1,2)} union {}");
Assert.assertEquals(parse("{(1,2)}"), simplifier.apply(union, context));
}
use of com.sri.ai.expresso.api.Expression 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.registerAdditionalSymbolsAndTypes(symbolsAndTypes);
Rewriter exhaustive = new Exhaustive(rewriter);
Expression solution = exhaustive.apply(initial, context);
System.out.println("Solution: " + solution);
assertEquals(expected, solution);
}
Aggregations