Search in sources :

Example 46 with IntensionalSet

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;
}
Also used : Context(com.sri.ai.grinder.api.Context) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)46 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)46 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)24 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)15 ArrayList (java.util.ArrayList)12 Type (com.sri.ai.expresso.api.Type)11 Context (com.sri.ai.grinder.api.Context)9 Context (com.sri.ai.grinder.sgdpllt.api.Context)9 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)8 RealInterval (com.sri.ai.expresso.type.RealInterval)8 Rational (com.sri.ai.util.math.Rational)8 TrueContext (com.sri.ai.grinder.core.TrueContext)5 List (java.util.List)5 CountingFormula (com.sri.ai.expresso.api.CountingFormula)4 FunctionType (com.sri.ai.expresso.type.FunctionType)4 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)4 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)4 TupleType (com.sri.ai.expresso.type.TupleType)4 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)3 Beta (com.google.common.annotations.Beta)2