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;
}
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;
}
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);
}
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);
}
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);
}
Aggregations