use of com.sri.ai.expresso.type.RealInterval in project aic-expresso by aic-sri-international.
the class GrinderUtil method fromTypeExpressionToItsIntrinsicMeaning.
/**
* A method mapping type expressions to their intrinsic {@link Type} objects,
* where "intrinsic" means there is only one possible {@link Type} object
* for them in the registry of grinder
* (therefore, it cannot be used for, say, categorical types defined
* by the user and registered in the registry by name only).
* Current recognized type expressions are
* <code>Boolean</code>, <code>Integer</code>, and function applications
* of the type <code>model..n</code>.
* If there is no such meaning, the method returns <code>null</code>.
* @param typeExpression
* @param registry TODO
* @return
*/
public static Type fromTypeExpressionToItsIntrinsicMeaning(Expression typeExpression, Registry registry) throws Error {
Type type;
if (typeExpression.equals("Boolean")) {
type = BOOLEAN_TYPE;
} else if (typeExpression.equals("Integer")) {
type = INTEGER_TYPE;
} else if (typeExpression.equals("Real")) {
type = REAL_TYPE;
} else if (typeExpression.hasFunctor(INTEGER_INTERVAL) && typeExpression.numberOfArguments() == 2) {
type = new IntegerInterval(typeExpression.get(0), typeExpression.get(1));
} else if ((typeExpression.hasFunctor(FunctorConstants.REAL_INTERVAL_CLOSED_CLOSED) || typeExpression.hasFunctor(FunctorConstants.REAL_INTERVAL_OPEN_CLOSED) || typeExpression.hasFunctor(FunctorConstants.REAL_INTERVAL_CLOSED_OPEN) || typeExpression.hasFunctor(FunctorConstants.REAL_INTERVAL_OPEN_OPEN)) && typeExpression.numberOfArguments() == 2) {
type = new RealInterval(typeExpression.toString());
} else if (FunctionType.isFunctionType(typeExpression)) {
Function<Expression, Type> getType = e -> registry.getTypeFromTypeExpression(e);
Type codomain = getType.apply(FunctionType.getCodomain(typeExpression));
List<Expression> argumentTypeExpressions = FunctionType.getArgumentList(typeExpression);
ArrayList<Type> argumentTypes = mapIntoArrayList(argumentTypeExpressions, getType);
Type[] argumentTypesArray = new Type[argumentTypes.size()];
type = new FunctionType(codomain, argumentTypes.toArray(argumentTypesArray));
} else if (TupleType.isTupleType(typeExpression)) {
List<Type> elementTypes = typeExpression.getArguments().stream().map(elementTypeExpression -> registry.getTypeFromTypeExpression(elementTypeExpression)).collect(Collectors.toList());
type = new TupleType(elementTypes);
} else {
type = null;
}
return type;
}
use of com.sri.ai.expresso.type.RealInterval in project aic-expresso by aic-sri-international.
the class GrinderUtil method isTypeSubtypeOf.
/**
* Test if a type is a subtype of another type.
*
* @param type the type to test if it is a subtype.
* @param ofType type to be tested if a subtype of.
*
* @return true if 'type' is a subtype of 'ofType', false otherwise.
*/
public static boolean isTypeSubtypeOf(Type type, Type ofType) {
boolean result = false;
if (type.equals(ofType)) {
result = true;
} else {
if (type instanceof FunctionType && ofType instanceof FunctionType) {
FunctionType typeFunctionType = (FunctionType) type;
FunctionType ofTypeFunctionType = (FunctionType) ofType;
if (typeFunctionType.getArity() == ofTypeFunctionType.getArity()) {
result = isTypeSubtypeOf(typeFunctionType.getCodomain(), ofTypeFunctionType.getCodomain()) && IntStream.range(0, typeFunctionType.getArity()).allMatch(idx -> isTypeSubtypeOf(ofTypeFunctionType.getArgumentTypes().get(idx), typeFunctionType.getArgumentTypes().get(idx)));
}
} else if (type instanceof TupleType && ofType instanceof TupleType) {
TupleType typeTupleType = (TupleType) type;
TupleType ofTypeTupleType = (TupleType) ofType;
if (typeTupleType.getArity() == ofTypeTupleType.getArity()) {
result = IntStream.range(0, typeTupleType.getArity()).allMatch(idx -> isTypeSubtypeOf(typeTupleType.getElementTypes().get(idx), ofTypeTupleType.getElementTypes().get(idx)));
}
} else if (type instanceof IntegerInterval) {
IntegerInterval typeIntegerInterval = (IntegerInterval) type;
if (ofType instanceof IntegerInterval) {
IntegerInterval ofTypeIntegerInterval = (IntegerInterval) ofType;
result = ofTypeIntegerInterval.isSuperset(typeIntegerInterval.getNonStrictLowerBound(), typeIntegerInterval.getNonStrictUpperBound());
} else if (ofType instanceof RealInterval) {
RealInterval ofTypeRealInterval = (RealInterval) ofType;
result = ofTypeRealInterval.isSuperset(typeIntegerInterval.getNonStrictLowerBound(), typeIntegerInterval.getNonStrictUpperBound());
} else if (ofType instanceof IntegerExpressoType || ofType instanceof RealExpressoType) {
result = true;
}
} else if (type instanceof IntegerExpressoType) {
if (ofType instanceof IntegerInterval) {
IntegerInterval ofTypeIntegerInterval = (IntegerInterval) ofType;
result = ofTypeIntegerInterval.noLowerBound() && ofTypeIntegerInterval.noUpperBound();
} else if (ofType instanceof RealInterval) {
RealInterval ofTypeRealInterval = (RealInterval) ofType;
result = ofTypeRealInterval.noLowerBound() && ofTypeRealInterval.noUpperBound();
} else if (ofType instanceof RealExpressoType) {
result = true;
}
} else if (type instanceof RealInterval) {
RealInterval typeRealInterval = (RealInterval) type;
if (ofType instanceof RealInterval) {
RealInterval ofTypeRealInterval = (RealInterval) ofType;
result = ofTypeRealInterval.isSuperset(typeRealInterval.getLowerBound(), typeRealInterval.getUpperBound());
} else if (ofType instanceof RealExpressoType) {
result = true;
}
} else if (type instanceof RealExpressoType) {
if (ofType instanceof RealInterval) {
RealInterval ofTypeRealInterval = (RealInterval) ofType;
result = ofTypeRealInterval.noLowerBound() && ofTypeRealInterval.noUpperBound();
} else if (ofType instanceof RealExpressoType) {
result = true;
}
}
}
return result;
}
use of com.sri.ai.expresso.type.RealInterval in project aic-expresso by aic-sri-international.
the class TypeTest method testIsFinite.
@Test
public void testIsFinite() {
//
// Categorical type tests
Assert.assertFalse(new Categorical("UnknownCardCatType", -1).isFinite());
Assert.assertFalse(new Categorical("InfiniteCardCatType", -2).isFinite());
Assert.assertTrue(new Categorical("CardCatType", 0).isFinite());
Assert.assertTrue(new Categorical("CardCatType", 100).isFinite());
//
// Integer type tests
Assert.assertFalse(new IntegerExpressoType().isFinite());
//
// Real type tests
Assert.assertFalse(new RealExpressoType().isFinite());
//
// Integer Interval type tests
Assert.assertFalse(new IntegerInterval("Integer").isFinite());
Assert.assertFalse(new IntegerInterval("integer_Interval(-infinity, inifinity)").isFinite());
Assert.assertFalse(new IntegerInterval("integer_Interval(-10, inifinity)").isFinite());
Assert.assertFalse(new IntegerInterval("integer_Interval(-infinity, 10)").isFinite());
Assert.assertTrue(new IntegerInterval("integer_Interval(-10, 10)").isFinite());
//
// Real Interval type tests
Assert.assertFalse(new RealInterval("Real").isFinite());
Assert.assertFalse(new RealInterval("[-infinity;infinity]").isFinite());
Assert.assertFalse(new RealInterval("[-10;infinity]").isFinite());
Assert.assertFalse(new RealInterval("[-infinity;10]").isFinite());
Assert.assertFalse(new RealInterval("[0;1]").isFinite());
//
// Function Type
Assert.assertFalse(new FunctionType(new IntegerExpressoType()).isFinite());
Assert.assertFalse(new FunctionType(new RealExpressoType()).isFinite());
Assert.assertTrue(new FunctionType(new Categorical("Cat", 10)).isFinite());
Assert.assertTrue(new FunctionType(new IntegerInterval(1, 3)).isFinite());
Assert.assertFalse(new FunctionType(new IntegerInterval("Integer")).isFinite());
Assert.assertFalse(new FunctionType(new RealInterval("Real")).isFinite());
Assert.assertTrue(new FunctionType(new TupleType(new IntegerInterval(1, 3))).isFinite());
Assert.assertFalse(new FunctionType(new TupleType(new RealInterval("Real"))).isFinite());
//
Assert.assertFalse(new FunctionType(new IntegerExpressoType(), new Categorical("Cat", 10)).isFinite());
Assert.assertFalse(new FunctionType(new IntegerExpressoType(), new RealExpressoType()).isFinite());
Assert.assertFalse(new FunctionType(new RealExpressoType(), new IntegerExpressoType()).isFinite());
Assert.assertFalse(new FunctionType(new Categorical("Cat", 10), new IntegerExpressoType()).isFinite());
Assert.assertFalse(new FunctionType(new Categorical("Cat", 10), new RealExpressoType()).isFinite());
Assert.assertFalse(new FunctionType(new IntegerInterval("Integer"), new IntegerExpressoType()).isFinite());
Assert.assertTrue(new FunctionType(new IntegerInterval(1, 2), new IntegerInterval(3, 5)).isFinite());
Assert.assertFalse(new FunctionType(new IntegerInterval("Integer"), new RealExpressoType()).isFinite());
Assert.assertFalse(new FunctionType(new RealInterval("Real")).isFinite());
Assert.assertFalse(new FunctionType(new RealInterval("Real"), new IntegerExpressoType()).isFinite());
//
// Tuple Type
Assert.assertFalse(new TupleType(new IntegerExpressoType()).isFinite());
Assert.assertFalse(new TupleType(new RealExpressoType()).isFinite());
Assert.assertTrue(new TupleType(new Categorical("Cat", 10)).isFinite());
Assert.assertTrue(new TupleType(new IntegerInterval(1, 3)).isFinite());
Assert.assertFalse(new TupleType(new IntegerInterval("Integer")).isFinite());
Assert.assertFalse(new TupleType(new RealInterval("Real")).isFinite());
//
Assert.assertFalse(new TupleType(new IntegerExpressoType(), new Categorical("Cat", 10)).isFinite());
Assert.assertFalse(new TupleType(new IntegerExpressoType(), new RealExpressoType()).isFinite());
Assert.assertFalse(new TupleType(new RealExpressoType(), new IntegerExpressoType()).isFinite());
Assert.assertFalse(new TupleType(new Categorical("Cat", 10), new IntegerExpressoType()).isFinite());
Assert.assertFalse(new TupleType(new Categorical("Cat", 10), new RealExpressoType()).isFinite());
Assert.assertFalse(new TupleType(new IntegerInterval("Integer"), new IntegerExpressoType()).isFinite());
Assert.assertTrue(new TupleType(new IntegerInterval(1, 2), new IntegerInterval(3, 5)).isFinite());
Assert.assertFalse(new TupleType(new IntegerInterval("Integer"), new RealExpressoType()).isFinite());
Assert.assertFalse(new TupleType(new RealInterval("Real")).isFinite());
Assert.assertFalse(new TupleType(new RealInterval("Real"), new IntegerExpressoType()).isFinite());
}
use of com.sri.ai.expresso.type.RealInterval in project aic-expresso by aic-sri-international.
the class TypeTest method testIsDiscrete.
@Test
public void testIsDiscrete() {
//
// Categorical type tests
Assert.assertTrue(new Categorical("UnknownCardCatType", -1).isDiscrete());
Assert.assertTrue(new Categorical("InfiniteCardCatType", -2).isDiscrete());
Assert.assertTrue(new Categorical("CardCatType", 0).isDiscrete());
Assert.assertTrue(new Categorical("CardCatType", 100).isDiscrete());
//
// Integer type tests
Assert.assertTrue(new IntegerExpressoType().isDiscrete());
//
// Real type tests
Assert.assertFalse(new RealExpressoType().isDiscrete());
//
// Integer Interval type tests
Assert.assertTrue(new IntegerInterval("Integer").isDiscrete());
Assert.assertTrue(new IntegerInterval("integer_Interval(-infinity, inifinity)").isDiscrete());
Assert.assertTrue(new IntegerInterval("integer_Interval(-10, inifinity)").isDiscrete());
Assert.assertTrue(new IntegerInterval("integer_Interval(-infinity, 10)").isDiscrete());
Assert.assertTrue(new IntegerInterval("integer_Interval(-10, 10)").isDiscrete());
//
// Real Interval type tests
Assert.assertFalse(new RealInterval("Real").isDiscrete());
Assert.assertFalse(new RealInterval("[-infinity;infinity]").isDiscrete());
Assert.assertFalse(new RealInterval("[-10;infinity]").isDiscrete());
Assert.assertFalse(new RealInterval("[-infinity;10]").isDiscrete());
Assert.assertFalse(new RealInterval("[0;1]").isDiscrete());
//
// Function Type
Assert.assertTrue(new FunctionType(new IntegerExpressoType()).isDiscrete());
Assert.assertFalse(new FunctionType(new RealExpressoType()).isDiscrete());
Assert.assertTrue(new FunctionType(new Categorical("Cat", 10)).isDiscrete());
Assert.assertTrue(new FunctionType(new IntegerInterval("Integer")).isDiscrete());
Assert.assertFalse(new FunctionType(new RealInterval("Real")).isDiscrete());
Assert.assertTrue(new FunctionType(new TupleType(new IntegerExpressoType())).isDiscrete());
Assert.assertFalse(new FunctionType(new TupleType(new RealInterval("Real"))).isDiscrete());
//
Assert.assertTrue(new FunctionType(new IntegerExpressoType(), new Categorical("Cat", 10)).isDiscrete());
Assert.assertFalse(new FunctionType(new IntegerExpressoType(), new RealExpressoType()).isDiscrete());
Assert.assertFalse(new FunctionType(new RealExpressoType(), new IntegerExpressoType()).isDiscrete());
Assert.assertTrue(new FunctionType(new Categorical("Cat", 10), new IntegerExpressoType()).isDiscrete());
Assert.assertFalse(new FunctionType(new Categorical("Cat", 10), new RealExpressoType()).isDiscrete());
Assert.assertTrue(new FunctionType(new IntegerInterval("Integer"), new IntegerExpressoType()).isDiscrete());
Assert.assertFalse(new FunctionType(new IntegerInterval("Integer"), new RealExpressoType()).isDiscrete());
Assert.assertFalse(new FunctionType(new RealInterval("Real")).isDiscrete());
Assert.assertFalse(new FunctionType(new RealInterval("Real"), new IntegerExpressoType()).isDiscrete());
//
// Tuple Type
Assert.assertTrue(new TupleType().isDiscrete());
Assert.assertTrue(new TupleType(new IntegerExpressoType()).isDiscrete());
Assert.assertFalse(new TupleType(new RealExpressoType()).isDiscrete());
Assert.assertTrue(new TupleType(new Categorical("Cat", 10)).isDiscrete());
Assert.assertTrue(new TupleType(new IntegerInterval("Integer")).isDiscrete());
Assert.assertFalse(new TupleType(new RealInterval("Real")).isDiscrete());
//
Assert.assertTrue(new TupleType(new IntegerExpressoType(), new Categorical("Cat", 10)).isDiscrete());
Assert.assertFalse(new TupleType(new IntegerExpressoType(), new RealExpressoType()).isDiscrete());
Assert.assertFalse(new TupleType(new RealExpressoType(), new IntegerExpressoType()).isDiscrete());
Assert.assertTrue(new TupleType(new Categorical("Cat", 10), new IntegerExpressoType()).isDiscrete());
Assert.assertFalse(new TupleType(new Categorical("Cat", 10), new RealExpressoType()).isDiscrete());
Assert.assertTrue(new TupleType(new IntegerInterval("Integer"), new IntegerExpressoType()).isDiscrete());
Assert.assertFalse(new TupleType(new IntegerInterval("Integer"), new RealExpressoType()).isDiscrete());
Assert.assertFalse(new TupleType(new RealInterval("Real")).isDiscrete());
Assert.assertFalse(new TupleType(new RealInterval("Real"), new IntegerExpressoType()).isDiscrete());
}
use of com.sri.ai.expresso.type.RealInterval in project aic-expresso by aic-sri-international.
the class AssignmentsSamplingIteratorTest method newSamplingIterator.
private Iterator<Assignment> newSamplingIterator(String indexString, int sampleSize, String conditionString) {
Expression index = parse(indexString);
Expression condition = parse(conditionString);
// Ensure condition of correct type is created
Type indexType = GrinderUtil.getTypeOfExpression(index, context);
if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
SingleVariableLinearRealArithmeticConstraint svlraConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, context.getTheory());
svlraConstraint = (SingleVariableLinearRealArithmeticConstraint) svlraConstraint.conjoin(condition, context);
condition = svlraConstraint;
} else if (indexType instanceof IntegerExpressoType || indexType instanceof IntegerInterval) {
SingleVariableDifferenceArithmeticConstraint svdaConstraint = new SingleVariableDifferenceArithmeticConstraint(index, true, context.getTheory());
svdaConstraint = (SingleVariableDifferenceArithmeticConstraint) svdaConstraint.conjoin(condition, context);
condition = svdaConstraint;
}
AssignmentsSamplingIterator samplingIterator = new AssignmentsSamplingIterator(Arrays.asList(index), condition, conditionRewriter, random, context);
Iterator<Assignment> result = nIterator(sampleSize, samplingIterator);
return result;
}
Aggregations