Search in sources :

Example 51 with Type

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

the class GrinderUtil method getTypeCardinality.

/**
 * Returns the cardinality of the type of a given variable in the given registry,
 * looking for <code>| Type |</code>, for <code>Type</code> the type of the variable,
 * in the registry global objects.
 * If the size cannot be determined, returns -1.
 * If the size is infinite, returns -2.
 * @param symbol a variable
 * @param registry the registry
 * @return the cardinality of the type of the variable according to the registry or -1 if it cannot be determined.
 */
public static long getTypeCardinality(Expression symbol, Registry registry) {
    long result = -1;
    Expression variableType = registry.getTypeExpressionOfRegisteredSymbol(symbol);
    if (variableType != null) {
        Expression typeCardinality = Expressions.apply(FunctorConstants.CARDINALITY, variableType);
        Expression typeCardinalityValue = (Expression) registry.getGlobalObject(typeCardinality);
        if (typeCardinalityValue != null) {
            result = typeCardinalityValue.intValueExact();
        }
    }
    // If that didn't work, we try find the Type object:
    if (result == -1) {
        variableType = registry.getTypeExpressionOfRegisteredSymbol(symbol);
        if (variableType != null) {
            Type type = registry.getTypeFromTypeExpression(variableType);
            if (type != null) {
                Expression sizeExpression = type.cardinality();
                if (sizeExpression.equals(apply(CARDINALITY, type.getName()))) {
                    result = -1;
                } else if (sizeExpression.equals(Expressions.INFINITY)) {
                    result = -2;
                } else {
                    result = sizeExpression.intValue();
                }
            }
        }
    }
    return result;
}
Also used : IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) TupleType(com.sri.ai.expresso.type.TupleType) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) FunctionType(com.sri.ai.expresso.type.FunctionType) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression)

Example 52 with Type

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

the class TupleValuedFreeVariablesSimplifier method extendContextWithComponentVariables.

private static Context extendContextWithComponentVariables(Context context, Map<Expression, TupleType> freeVariablesOfTupleType, Map<Expression, List<Pair<Expression, Integer>>> freeVariableComponentsMap) {
    Map<String, String> mapFromSymbolNameToTypeName = new LinkedHashMap<>();
    Set<Type> componentTypes = new LinkedHashSet<>();
    for (Map.Entry<Expression, TupleType> freeVariableOfTupleType : freeVariablesOfTupleType.entrySet()) {
        Expression freeVariable = freeVariableOfTupleType.getKey();
        TupleType freeVariableTupleType = freeVariableOfTupleType.getValue();
        componentTypes.addAll(freeVariableTupleType.getElementTypes());
        List<Pair<Expression, Integer>> components = freeVariableComponentsMap.get(freeVariable);
        for (Pair<Expression, Integer> freeVariableComponent : components) {
            Expression freeVariableComponentVar = freeVariableComponent.first;
            Type freeVariableComponentType = freeVariableTupleType.getElementTypes().get(freeVariableComponent.second - 1);
            mapFromSymbolNameToTypeName.put(freeVariableComponentVar.toString(), freeVariableComponentType.getName());
        }
    }
    Context result = (Context) GrinderUtil.extendRegistryWith(mapFromSymbolNameToTypeName, componentTypes, context);
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Context(com.sri.ai.grinder.api.Context) LinkedHashMap(java.util.LinkedHashMap) Type(com.sri.ai.expresso.api.Type) TupleType(com.sri.ai.expresso.type.TupleType) Expression(com.sri.ai.expresso.api.Expression) TupleType(com.sri.ai.expresso.type.TupleType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(com.sri.ai.util.base.Pair)

Example 53 with Type

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

the class ExpressionStepSolverToLiteralSplitterStepSolverAdapterTest method extendTestingVariables.

// 
// 
private void extendTestingVariables(String variableToBaseTypeOn, TheoryTestingSupport theoryTestingSupport, String... newVariables) {
    Map<String, Type> variablesAndTypes = new LinkedHashMap<>(theoryTestingSupport.getVariableNamesAndTypesForTesting());
    Type type = variablesAndTypes.get(variableToBaseTypeOn);
    if (type == null) {
        throw new IllegalArgumentException("variableToBaseTypeOn=" + variableToBaseTypeOn + ", is not already part of the theory.");
    }
    for (String newVariable : newVariables) {
        variablesAndTypes.put(newVariable, type);
    }
    theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
}
Also used : Type(com.sri.ai.expresso.api.Type) LinkedHashMap(java.util.LinkedHashMap)

Example 54 with Type

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

the class AssignmentsIteratorTest method test2.

@Test
public void test2() {
    Registry registry = new DefaultRegistry();
    Type myType = new Categorical("People", 2, arrayList(makeSymbol("oscar"), makeSymbol("mary")));
    Symbol x = makeSymbol("X");
    Symbol y = makeSymbol("Y");
    String expected = "{X=oscar, Y=oscar}\n" + "{X=mary, Y=oscar}\n" + "{X=oscar, Y=mary}\n" + "{X=mary, Y=mary}";
    Symbol myTypeExpression = makeSymbol(myType.getName());
    registry = registry.makeNewContextWithAddedType(myType);
    registry = registry.makeCloneWithAdditionalRegisteredSymbolsAndTypes(map(x, myTypeExpression, y, myTypeExpression));
    AssignmentMapsIterator assignmentsIterator = new AssignmentMapsIterator(list(x, y), registry);
    String actual = join("\n", assignmentsIterator);
    // System.out.println(actual);
    assertEquals(expected, actual);
}
Also used : AssignmentMapsIterator(com.sri.ai.grinder.helper.AssignmentMapsIterator) Type(com.sri.ai.expresso.api.Type) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Symbol(com.sri.ai.expresso.api.Symbol) DefaultRegistry(com.sri.ai.grinder.core.DefaultRegistry) Registry(com.sri.ai.grinder.api.Registry) DefaultRegistry(com.sri.ai.grinder.core.DefaultRegistry) Categorical(com.sri.ai.expresso.type.Categorical) Test(org.junit.Test)

Example 55 with Type

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

the class AssignmentsIteratorTest method test3.

@Test
public void test3() {
    Registry registry = new DefaultRegistry();
    Type peopleType = new Categorical("People", 4, arrayList(makeSymbol("oscar"), makeSymbol("mary")));
    Type petsType = new Categorical("Pets", 3, arrayList(makeSymbol("fido"), makeSymbol("purrs")));
    Symbol x = makeSymbol("X");
    Symbol y = makeSymbol("Y");
    String expected = "{X=oscar, Y=fido}\n" + "{X=mary, Y=fido}\n" + "{X=people3, Y=fido}\n" + "{X=people4, Y=fido}\n" + "{X=oscar, Y=purrs}\n" + "{X=mary, Y=purrs}\n" + "{X=people3, Y=purrs}\n" + "{X=people4, Y=purrs}\n" + "{X=oscar, Y=pets3}\n" + "{X=mary, Y=pets3}\n" + "{X=people3, Y=pets3}\n" + "{X=people4, Y=pets3}";
    Symbol myPeopleTypeExpression = makeSymbol(peopleType.getName());
    Symbol myPetsTypeExpression = makeSymbol(petsType.getName());
    registry = registry.makeNewContextWithAddedType(peopleType);
    registry = registry.makeNewContextWithAddedType(petsType);
    registry = registry.makeCloneWithAdditionalRegisteredSymbolsAndTypes(map(x, myPeopleTypeExpression, y, myPetsTypeExpression));
    AssignmentMapsIterator assignmentsIterator = new AssignmentMapsIterator(list(x, y), registry);
    String actual = join("\n", assignmentsIterator);
    // System.out.println(actual);
    assertEquals(expected, actual);
}
Also used : AssignmentMapsIterator(com.sri.ai.grinder.helper.AssignmentMapsIterator) Type(com.sri.ai.expresso.api.Type) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Symbol(com.sri.ai.expresso.api.Symbol) DefaultRegistry(com.sri.ai.grinder.core.DefaultRegistry) Registry(com.sri.ai.grinder.api.Registry) DefaultRegistry(com.sri.ai.grinder.core.DefaultRegistry) Categorical(com.sri.ai.expresso.type.Categorical) Test(org.junit.Test)

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