Search in sources :

Example 36 with Type

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

the class DefaultRegistry method makeNewContextWithAddedType.

@Override
public DefaultRegistry makeNewContextWithAddedType(Type type) {
    DefaultRegistry result = clone();
    String name = type.getName();
    Expression typeExpression = parse(name);
    LinkedHashMap<Expression, Type> additionalTypeMap = map(typeExpression, type);
    result.fromTypeExpressionToType = new StackedHashMap<>(additionalTypeMap, fromTypeExpressionToType);
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression)

Example 37 with Type

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

the class TrueContext method getType.

@Override
public Type getType(String name) {
    Expression typeExpression = parse(name);
    Type result = getTypeFromTypeExpression(typeExpression);
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression)

Example 38 with Type

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

the class AbstractTheoryTestingSupport method extendWithTestingInformation.

@Override
public Context extendWithTestingInformation(Context context) {
    // we only need to provide the variables types, and not the known constant types, because the latter will be extracted from the already registered types.
    Map<String, String> mapFromSymbolNamesToTypeNames = new LinkedHashMap<String, String>();
    for (Map.Entry<String, Type> symbolAndType : getVariableNamesAndTypesForTesting().entrySet()) {
        mapFromSymbolNamesToTypeNames.put(symbolAndType.getKey(), symbolAndType.getValue().toString());
    }
    for (Map.Entry<String, Type> symbolAndType : getExtendedVariableNamesAndTypesForTesting().entrySet()) {
        mapFromSymbolNamesToTypeNames.put(symbolAndType.getKey(), symbolAndType.getValue().toString());
    }
    Context result = (Context) GrinderUtil.extendRegistryWith(mapFromSymbolNamesToTypeNames, getTypesForTesting(), context);
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) Type(com.sri.ai.expresso.api.Type) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 39 with Type

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

the class AbstractTheoryTestingSupport method getTypesForTesting.

@Override
public Collection<Type> getTypesForTesting() {
    if (cachedTypesForTesting == null) {
        Set<Type> topTypesForTesting = new LinkedHashSet<>(getVariableNamesAndTypesForTesting().values());
        topTypesForTesting.addAll(getTheory().getNativeTypes());
        // Ensure we include embedded types (e.g. in a function or tuple type) as well
        Set<Type> embeddedTypes = new LinkedHashSet<>();
        for (Type topType : topTypesForTesting) {
            embeddedTypes.addAll(topType.getEmbeddedTypes());
        }
        topTypesForTesting.addAll(embeddedTypes);
        cachedTypesForTesting = Collections.unmodifiableCollection(topTypesForTesting);
    }
    return cachedTypesForTesting;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Type(com.sri.ai.expresso.api.Type)

Example 40 with Type

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

the class AssignmentsSamplingIterator method getTypeToSampleFrom.

public static Type getTypeToSampleFrom(Expression variable, Expression condition, Context context) {
    Type result = GrinderUtil.getTypeOfExpression(variable, context);
    if (result instanceof FunctionType) {
        FunctionType functionType = (FunctionType) result;
        result = new LazySampledFunctionType(functionType.getCodomain(), functionType.getArgumentTypes().toArray(new Type[functionType.getArity()]));
    } else {
        if (condition.equals(false)) {
            result = null;
        } else if (condition.equals(true)) {
        // we leave as is.
        } else if (result instanceof RealExpressoType || result instanceof RealInterval) {
            LinearRealArithmeticTheory theory = new LinearRealArithmeticTheory(true, true);
            SingleVariableLinearRealArithmeticConstraint constraint = (SingleVariableLinearRealArithmeticConstraint) theory.makeSingleVariableConstraint(variable, context);
            constraint = (SingleVariableLinearRealArithmeticConstraint) constraint.conjoin(condition, context);
            IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver solver = new IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver(constraint);
            Expression realInterval = solver.solve(context);
            if (Sets.isEmptySet(realInterval)) {
                // used to indicate an empty set.
                result = null;
            } else if (ExtensionalSets.isExtensionalSet(realInterval) && ExtensionalSets.isSingleton(realInterval)) {
                String singletonValue = realInterval.get(0).toString();
                result = new RealInterval("[" + singletonValue + ";" + singletonValue + "]");
            } else {
                result = new RealInterval(realInterval.toString());
            }
        } else if (result instanceof IntegerExpressoType || result instanceof IntegerInterval) {
            DifferenceArithmeticTheory theory = new DifferenceArithmeticTheory(true, true);
            SingleVariableDifferenceArithmeticConstraint constraint = (SingleVariableDifferenceArithmeticConstraint) theory.makeSingleVariableConstraint(variable, context);
            constraint = (SingleVariableDifferenceArithmeticConstraint) constraint.conjoin(condition, context);
            ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver solver = new ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver(constraint);
            // NOTE: the exceptions set returned here is implicit in the condition so no need to use it here.
            RangeAndExceptionsSet rangeAndExceptionsSet = (RangeAndExceptionsSet) solver.solve(context);
            if (rangeAndExceptionsSet.isEmpty()) {
                // used to indicate an empty set.
                result = null;
            } else if (rangeAndExceptionsSet.isSingleton()) {
                result = new IntegerInterval(rangeAndExceptionsSet.getSingleValue().intValueExact(), rangeAndExceptionsSet.getSingleValue().intValueExact());
            } else {
                result = new IntegerInterval(rangeAndExceptionsSet.getStrictLowerBound().intValueExact() + 1, rangeAndExceptionsSet.getNonStrictUpperBound().intValueExact());
            }
        }
    }
    if (result != null && !result.isSampleUniquelyNamedConstantSupported()) {
        throw new IllegalArgumentException("Unable to sample " + variable + " from " + result);
    }
    return result;
}
Also used : RangeAndExceptionsSet(com.sri.ai.grinder.theory.differencearithmetic.RangeAndExceptionsSet) FunctionType(com.sri.ai.expresso.type.FunctionType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver) RealInterval(com.sri.ai.expresso.type.RealInterval) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) FunctionType(com.sri.ai.expresso.type.FunctionType) ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.differencearithmetic.ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver) Expression(com.sri.ai.expresso.api.Expression) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)

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