Search in sources :

Example 26 with Type

use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.

the class SingleVariableDifferenceArithmeticConstraint method getType.

/**
 * Returns the {@link IntegerInterval} type of the constraint's variable.
 * @param context
 * @return
 */
public IntegerInterval getType(Context context) {
    if (cachedType == null) {
        Expression variableTypeExpression = getVariableTypeExpression(context);
        Type type = context.getTypeFromTypeExpression(variableTypeExpression);
        if (type instanceof IntegerExpressoType) {
            cachedType = new IntegerInterval("-infinity..infinity");
        // represents Integer as integer interval for uniformity
        } else {
            cachedType = (IntegerInterval) type;
        }
    }
    return cachedType;
}
Also used : Type(com.sri.ai.expresso.api.Type) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) Expression(com.sri.ai.expresso.api.Expression) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval)

Example 27 with Type

use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.

the class EqualityTheory method argumentIsValid.

private boolean argumentIsValid(Expression argumentOfAtomFunctor, Context context) {
    boolean result;
    if (argumentOfAtomFunctor.getSyntacticFormType().equals("Symbol")) {
        Type eType = GrinderUtil.getTypeOfExpression(argumentOfAtomFunctor, context);
        result = isNonBooleanCategoricalType(eType);
    } else {
        result = false;
    }
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type)

Example 28 with Type

use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.

the class SatisfiabilityOfSingleVariableEqualityConstraintStepSolver method getPropagatedCNFBesidesPropagatedLiterals.

@Override
protected Iterable<Iterable<Expression>> getPropagatedCNFBesidesPropagatedLiterals(Context context) {
    if (!variableIsBoundToUniquelyNamedConstant(context)) {
        // the following logic only holds if the variable is not bound to a uniquely named constants,
        // since that eliminates all disequalities to other uniquely named constants as redundant
        long variableDomainSize = getConstraint().getVariableTypeSize(context);
        if (variableDomainSize >= 0 && getConstraint().numberOfDisequals() >= variableDomainSize) {
            // the following procedure can be very expensive but the condition above will rarely be satisfied
            ArrayList<Expression> variableDisequals = getVariableDisequals(context);
            Set<Expression> uniquelyNamedConstantDisequals = getUniquelyNamedConstantDisequals(context);
            Expression typeExpression = GrinderUtil.getTypeExpressionOfExpression(getConstraint().getVariable(), context);
            Type type = context.getTypeFromTypeExpression(typeExpression);
            ArrayList<Expression> remainingUniquelyNamedConstants = arrayListFrom(new PredicateIterator<>(type.iterator(), c -> !uniquelyNamedConstantDisequals.contains(c)));
            CartesianProductIterator<ArrayList<Expression>> subsetOfVariableDisequalsAndRemainingConstantsPermutationIterator = new CartesianProductIterator<ArrayList<Expression>>(() -> new SubsetsOfKIterator<Expression>(variableDisequals, remainingUniquelyNamedConstants.size()), () -> new PermutationIterator<Expression>(remainingUniquelyNamedConstants));
            FunctionIterator<ArrayList<ArrayList<Expression>>, Iterable<Expression>> clausesIterator = FunctionIterator.make(subsetOfVariableDisequalsAndRemainingConstantsPermutationIterator, (ArrayList<ArrayList<Expression>> subsetAndPermutation) -> clauseNegatingAssignmentOfSubsetOfVariablesToParticularPermutationOfRemainingConstants(subsetAndPermutation));
            Iterable<Iterable<Expression>> clauses = in(clausesIterator);
            return clauses;
        }
    }
    // otherwise, nothing is implied.
    return list();
}
Also used : PredicateIterator(com.sri.ai.util.collect.PredicateIterator) PermutationIterator(com.sri.ai.util.collect.PermutationIterator) NestedIterator(com.sri.ai.util.collect.NestedIterator) PairOf(com.sri.ai.util.base.PairOf) Expression(com.sri.ai.expresso.api.Expression) CartesianProductIterator(com.sri.ai.util.collect.CartesianProductIterator) PairOf.makePairOf(com.sri.ai.util.base.PairOf.makePairOf) ArrayList(java.util.ArrayList) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) SubsetsOfKIterator(com.sri.ai.util.collect.SubsetsOfKIterator) Util.in(com.sri.ai.util.Util.in) Expressions.zipApply(com.sri.ai.expresso.helper.Expressions.zipApply) Equality(com.sri.ai.grinder.library.Equality) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) DISEQUALITY(com.sri.ai.grinder.library.FunctorConstants.DISEQUALITY) Context(com.sri.ai.grinder.api.Context) Util.thereExists(com.sri.ai.util.Util.thereExists) Util.arrayList(com.sri.ai.util.Util.arrayList) FunctionIterator(com.sri.ai.util.collect.FunctionIterator) LinkedHashSet(java.util.LinkedHashSet) Util.arrayListFrom(com.sri.ai.util.Util.arrayListFrom) Function(com.google.common.base.Function) Type(com.sri.ai.expresso.api.Type) AbstractBooleanWithPropagatedLiteralsRequiringPropagatedLiteralsAndCNFToBeSatisfiedStepSolver(com.sri.ai.grinder.core.solver.AbstractBooleanWithPropagatedLiteralsRequiringPropagatedLiteralsAndCNFToBeSatisfiedStepSolver) Iterator(java.util.Iterator) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) Beta(com.google.common.annotations.Beta) Util.toLinkedHashSet(com.sri.ai.util.Util.toLinkedHashSet) List(java.util.List) FunctionIterator.functionIterator(com.sri.ai.util.collect.FunctionIterator.functionIterator) Util(com.sri.ai.util.Util) PairOfElementsInListIterator(com.sri.ai.util.collect.PairOfElementsInListIterator) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) ArrayList(java.util.ArrayList) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) CartesianProductIterator(com.sri.ai.util.collect.CartesianProductIterator)

Example 29 with Type

use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.

the class BruteForceFunctionTheoryTestingSupport method makeFunctionApplication.

protected Expression makeFunctionApplication(String functorName, FunctionType functionType) {
    // Generate arguments for the application
    List<Expression> args = new ArrayList<>();
    for (Type argType : functionType.getArgumentTypes()) {
        // If constants supported, use at random
        if (argType.isSampleUniquelyNamedConstantSupported() && getRandom().nextBoolean()) {
            args.add(argType.sampleUniquelyNamedConstant(getRandom()));
        } else {
            // Otherwise retrieve a term variable matching that type and use it
            String termVariable = pickTestingVariableAtRandom(getTermVariableNamesAndTypesForTesting(), argType, variableName -> true);
            Type termType = getTermVariableNamesAndTypesForTesting().get(termVariable);
            if (termType instanceof FunctionType) {
                // Allow for nested function applications
                args.add(makeFunctionApplication(termVariable, (FunctionType) termType));
            } else {
                // Otherwise just assign the variable for the term.
                args.add(parse(termVariable));
            }
        }
    }
    Expression result = Expressions.apply(functorName, args.toArray(new Object[args.size()]));
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Expression(com.sri.ai.expresso.api.Expression) FunctionType(com.sri.ai.expresso.type.FunctionType) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList)

Example 30 with Type

use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.

the class LinearRealArithmeticTheory method symbolIsRealTyped.

private static boolean symbolIsRealTyped(Expression variable, Context context) {
    Type variableType = context.getTypeOfRegisteredSymbol(variable);
    boolean variableIsInteger = variableType instanceof RealExpressoType || variableType instanceof RealInterval;
    return variableIsInteger;
}
Also used : Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) RealInterval(com.sri.ai.expresso.type.RealInterval)

Aggregations

Type (com.sri.ai.expresso.api.Type)123 Expression (com.sri.ai.expresso.api.Expression)93 FunctionType (com.sri.ai.expresso.type.FunctionType)28 Test (org.junit.Test)25 Context (com.sri.ai.grinder.api.Context)24 LinkedHashMap (java.util.LinkedHashMap)22 Categorical (com.sri.ai.expresso.type.Categorical)21 Context (com.sri.ai.grinder.sgdpllt.api.Context)20 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)19 TupleType (com.sri.ai.expresso.type.TupleType)19 ArrayList (java.util.ArrayList)19 Map (java.util.Map)19 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)18 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)18 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)15 RealInterval (com.sri.ai.expresso.type.RealInterval)14 Beta (com.google.common.annotations.Beta)13 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)12 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)12 List (java.util.List)12