use of com.sri.ai.expresso.api.IntensionalSet in project aic-expresso by aic-sri-international.
the class IntersectionIntensionalSetsSimplifier method simplify.
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (expression.hasFunctor(FunctorConstants.INTERSECTION)) {
List<Expression> intensionalMultiSetArgs = new ArrayList<>();
List<Expression> nonIntensionalMultiSetArgs = new ArrayList<>();
for (Expression arg : expression.getArguments()) {
if (Sets.isIntensionalMultiSet(arg)) {
intensionalMultiSetArgs.add(arg);
} else {
nonIntensionalMultiSetArgs.add(arg);
}
}
if (intensionalMultiSetArgs.size() > 1) {
boolean resultIsEmptySet = false;
IntensionalSet intersectedMultiSet = (IntensionalSet) intensionalMultiSetArgs.get(0);
for (int i = 1; i < intensionalMultiSetArgs.size() && !resultIsEmptySet; i++) {
IntensionalSet otherMultiSet = standardizeApartIntensionalSets((IntensionalSet) intensionalMultiSetArgs.get(i), intersectedMultiSet, context);
// {{ (on I1) H1 : C1 }} intersection {{ (on I2) H2 : C2 }}
// ---->
// {{ (on I1) H1 : C1 and evaluate(there exists I2 : C2 and H2 = H1) }}
IndexExpressionsSet i1 = intersectedMultiSet.getIndexExpressions();
IndexExpressionsSet i2 = otherMultiSet.getIndexExpressions();
Expression h1 = intersectedMultiSet.getHead();
Expression h2 = otherMultiSet.getHead();
Expression c1 = intersectedMultiSet.getCondition();
Expression c2 = otherMultiSet.getCondition();
Expression thereExists = ThereExists.make(i2, And.make(c2, Equality.make(h2, h1)));
Context i1ExtendedContext = context.extendWith(i1);
Expression thereExistsEvaluated = context.getTheory().evaluate(thereExists, i1ExtendedContext);
if (thereExistsEvaluated.equals(false)) {
// They don't intersect, which means you have an empty
// set in the intersection, which means the whole thing
// results in the empty set.
resultIsEmptySet = true;
} else if (!thereExistsEvaluated.equals(true)) {
// If we have a condition, other than false and true
// we will want to extend the current result by the condition
Expression extendedCondition = And.make(c1, thereExistsEvaluated);
intersectedMultiSet = (IntensionalSet) IntensionalSet.intensionalMultiSet(i1, h1, extendedCondition);
// Ensure we don't have a false condition.
Expression simplifiedIntersectedMultiSet = context.getTheory().evaluate(intersectedMultiSet, context);
if (Sets.isEmptySet(simplifiedIntersectedMultiSet)) {
resultIsEmptySet = true;
}
}
}
if (resultIsEmptySet) {
result = Sets.EMPTY_SET;
} else if (nonIntensionalMultiSetArgs.size() > 0) {
List<Expression> intersectedArgs = new ArrayList<>();
intersectedArgs.add(intersectedMultiSet);
intersectedArgs.addAll(nonIntensionalMultiSetArgs);
result = Sets.makeIntersection(intersectedArgs.toArray(new Expression[intersectedArgs.size()]));
} else {
result = intersectedMultiSet;
}
}
}
return result;
}
Aggregations