use of com.sri.ai.expresso.helper.Expressions 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.helper.Expressions in project aic-expresso by aic-sri-international.
the class Context method extendWith.
/**
* Extends context with index expressions, taking into account that new contextual variables may collide with existing ones.
* In this case, it renames the incoming variables to unique identifiers and replaces them in the types of remaining
* index expressions. It also renames the variables in a given expressions supposed to be in their scope (for example,
* the head and condition of an intensionally defined set).
* Returns the new context and the index expressions and expression in scope after the renaming.
* @param indexExpressions
* @param expressionInScope
* @return the new context and the index expressions and expression in scope after the renaming
*/
default default Triple<Context, ExtensionalIndexExpressionsSet, Expression> extendWith(ExtensionalIndexExpressionsSet indexExpressions, Expression expressionInScope) {
Triple<Context, ExtensionalIndexExpressionsSet, Expression> result;
if (thereExists(getIndices(indexExpressions), index -> this.containsSymbol(index))) {
// OPTIMIZATION: only kick in this entire procedure when extending with symbol in the context (previous ones could have been dealt with normally).
// the objects to be returned in the triple:
Context newContext = this;
ArrayList<Expression> newIndexExpressionsList = new ArrayList<>(indexExpressions.getList());
Expression newExpressionInScope = expressionInScope;
// Collects all existing symbols to be able to create unique symbols
Set<Expression> alreadyDefined = Util.set();
alreadyDefined.addAll(this.getSymbols());
alreadyDefined.addAll(Expressions.freeSymbols(new DefaultTuple(newIndexExpressionsList), this));
alreadyDefined.addAll(Expressions.freeSymbols(expressionInScope, this));
Predicate<Expression> isAlreadyDefined = e -> alreadyDefined.contains(e);
for (int i = 0; i != newIndexExpressionsList.size(); i++) {
Expression indexExpression = newIndexExpressionsList.get(i);
Symbol index = (Symbol) indexExpression.get(0);
Expression type = indexExpression.get(1);
PairOf<Expression> newIndexAndNewExpressionInScope = Expressions.standardizeApart(index, isAlreadyDefined, newExpressionInScope);
Expression newIndex = newIndexAndNewExpressionInScope.first;
newExpressionInScope = newIndexAndNewExpressionInScope.second;
// type should not contain the index
Expression newIndexExpression = apply(IN, newIndex, type);
newIndexExpressionsList.set(i, newIndexExpression);
alreadyDefined.add(newIndex);
for (int j = i + 1; j != newIndexExpressionsList.size(); j++) {
Expression anotherIndexExpression = newIndexExpressionsList.get(j);
Expression anotherIndex = anotherIndexExpression.get(0);
Expression anotherType = anotherIndexExpression.get(1);
Expression newAnotherType = anotherType.replaceSymbol(index, newIndex, this);
// anotherIndex is a symbols and does not contain index
Expression newAnotherIndexExpression = apply(IN, anotherIndex, newAnotherType);
newIndexExpressionsList.set(j, newAnotherIndexExpression);
}
}
ExtensionalIndexExpressionsSet newIndexExpressions = new ExtensionalIndexExpressionsSet(newIndexExpressionsList);
newContext = newContext.extendWith(newIndexExpressions);
result = triple(newContext, newIndexExpressions, newExpressionInScope);
} else {
// no collision; usual extension and the expressions do not change.
result = triple(extendWith(indexExpressions), indexExpressions, expressionInScope);
}
return result;
}
use of com.sri.ai.expresso.helper.Expressions in project aic-expresso by aic-sri-international.
the class Context method extendWith.
/**
* Extends context with index expressions, taking into account that new contextual variables may collide with existing ones.
* In this case, it renames the incoming variables to unique identifiers and replaces them in the types of remaining
* index expressions. It also renames these indices if they occur in a given expression --
* this is useful because the client code (invoking this method)
* often knows that these renamed indices may occur in a known set of expressions that need to be updated accordingly.
* Returns the new context, the index expressions and expression in scope after the renaming.
* @param indexExpressions
* @param expressionInScope
* @return the new context and the index expressions and expression in scope after the renaming
*/
default Triple<Context, ExtensionalIndexExpressionsSet, Expression> extendWith(ExtensionalIndexExpressionsSet indexExpressions, Expression expressionInScope) {
Triple<Context, ExtensionalIndexExpressionsSet, Expression> result;
if (thereExists(getIndices(indexExpressions), index -> this.containsSymbol(index))) {
// OPTIMIZATION: only kick in this entire procedure when extending with symbol in the context (previous ones could have been dealt with normally).
// the objects to be returned in the triple:
Context newContext = this;
ArrayList<Expression> newIndexExpressionsList = new ArrayList<>(indexExpressions.getList());
Expression newExpressionInScope = expressionInScope;
// Collects all existing symbols to be able to create unique symbols
Set<Expression> alreadyDefined = Util.set();
alreadyDefined.addAll(this.getSymbols());
alreadyDefined.addAll(Expressions.freeSymbols(new DefaultTuple(newIndexExpressionsList), this));
alreadyDefined.addAll(Expressions.freeSymbols(expressionInScope, this));
Predicate<Expression> isAlreadyDefined = e -> alreadyDefined.contains(e);
for (int i = 0; i != newIndexExpressionsList.size(); i++) {
Expression indexExpression = newIndexExpressionsList.get(i);
Symbol index = (Symbol) indexExpression.get(0);
Expression type = indexExpression.get(1);
PairOf<Expression> newIndexAndNewExpressionInScope = Expressions.standardizeApart(index, isAlreadyDefined, newExpressionInScope);
Expression newIndex = newIndexAndNewExpressionInScope.first;
newExpressionInScope = newIndexAndNewExpressionInScope.second;
// type should not contain the index
Expression newIndexExpression = apply(IN, newIndex, type);
newIndexExpressionsList.set(i, newIndexExpression);
alreadyDefined.add(newIndex);
for (int j = i + 1; j != newIndexExpressionsList.size(); j++) {
Expression anotherIndexExpression = newIndexExpressionsList.get(j);
Expression anotherIndex = anotherIndexExpression.get(0);
Expression anotherType = anotherIndexExpression.get(1);
Expression newAnotherType = anotherType.replaceSymbol(index, newIndex, this);
// anotherIndex is a symbols and does not contain index
Expression newAnotherIndexExpression = apply(IN, anotherIndex, newAnotherType);
newIndexExpressionsList.set(j, newAnotherIndexExpression);
}
}
ExtensionalIndexExpressionsSet newIndexExpressions = new ExtensionalIndexExpressionsSet(newIndexExpressionsList);
newContext = newContext.extendWith(newIndexExpressions);
result = triple(newContext, newIndexExpressions, newExpressionInScope);
} else {
// no collision; usual extension and the expressions do not change.
result = triple(extendWith(indexExpressions), indexExpressions, expressionInScope);
}
return result;
}
use of com.sri.ai.expresso.helper.Expressions 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.helper.Expressions in project aic-expresso by aic-sri-international.
the class NumberOfDistinctExpressionsStepSolverTest 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");
context = context.conjoin(parse(contextString), context);
ArrayList<Expression> list = mapIntoArrayList(elementsStrings, Expressions::parse);
NumberOfDistinctExpressionsStepSolver stepSolver = new NumberOfDistinctExpressionsStepSolver(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 can be 3 or 4, depending on whether Y = a, or Y = b
step = stepSolverIfXEqualsC.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("Y = a"), step.getSplitter());
ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsCAndYEqualsA = step.getStepSolverForWhenSplitterIsTrue();
ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsCAndYIsDifferentFromA = step.getStepSolverForWhenSplitterIsFalse();
// if X = c and Y = a, the number of distinct values is 3 (a, b, c)
step = stepSolverIfXEqualsCAndYEqualsA.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("3"), step.getValue());
// if X = c and Y != a, the number of distinct values is 3 or 4, depending on Y = c
step = stepSolverIfXEqualsCAndYIsDifferentFromA.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("Y = c"), step.getSplitter());
ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsCAndYIsDifferentFromAAndYEqualsC = step.getStepSolverForWhenSplitterIsTrue();
ExpressionLiteralSplitterStepSolver stepSolverIfXEqualsCAndYIsDifferentFromAAndYIsDifferentFromC = step.getStepSolverForWhenSplitterIsFalse();
// if X = c and Y != a and Y = c, the number of distinct values is 3
step = stepSolverIfXEqualsCAndYIsDifferentFromAAndYEqualsC.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("3"), step.getValue());
// if X = c and Y != a and Y != c, the number of distinct values is 4
step = stepSolverIfXEqualsCAndYIsDifferentFromAAndYIsDifferentFromC.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("4"), step.getValue());
// if X = c and Y = a, the number of distinct values is 3 (a, b, c)
step = stepSolverIfXEqualsCAndYEqualsA.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("3"), step.getValue());
// using again just to make sure it produces the same result
step = stepSolverIfXEqualsCAndYEqualsA.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("3"), step.getValue());
// if X != c, the number of distinct value 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());
// if X != c, the number of distinct values can be 4 or 5, depending on whether Y = a, or Y = b
step = stepSolverIfXIsDifferentFromC.step(context);
assertEquals(true, step.itDepends());
assertEquals(parse("Y = a"), step.getSplitter());
ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYEqualsA = step.getStepSolverForWhenSplitterIsTrue();
ExpressionLiteralSplitterStepSolver stepSolverIfXIsDifferentFromCAndYIsDifferentFromA = step.getStepSolverForWhenSplitterIsFalse();
step = stepSolverIfXIsDifferentFromCAndYEqualsA.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("4"), 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 there are 4 distinct values
step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsEqualToC.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("4"), step.getValue());
// if Y != c, then Y is also unique and the number of distinct values is 5
step = stepSolverIfXIsDifferentFromCAndYIsDifferentFromAAndYIsDifferentFromC.step(context);
assertEquals(false, step.itDepends());
assertEquals(parse("5"), step.getValue());
}
Aggregations