use of com.sri.ai.expresso.type.FunctionType in project aic-expresso by aic-sri-international.
the class UnificationStepSolverTest method advancedDifferenceArithmeticTest.
@Ignore("TODO - context implementation currently does not support these more advanced/indirect comparisons")
@Test
public void advancedDifferenceArithmeticTest() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(seededRandom, new DifferenceArithmeticTheory(true, true));
// NOTE: passing explicit FunctionTypes will prevent the general variables' argument types being randomly changed.
theoryTestingSupport.setVariableNamesAndTypesForTesting(map("I", TESTING_INTEGER_INTERVAL_TYPE, "J", TESTING_INTEGER_INTERVAL_TYPE, "K", TESTING_INTEGER_INTERVAL_TYPE, "unary_dar/1", new FunctionType(TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE), "binary_dar/2", new FunctionType(TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE, TESTING_INTEGER_INTERVAL_TYPE)));
Context rootContext = theoryTestingSupport.makeContextWithTestingInformation();
UnificationStepSolver unificationStepSolver = new UnificationStepSolver(parse("binary_dar(I, unary_dar(I))"), parse("binary_dar(unary_dar(J), J)"));
Context localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 0 and J = 1 and unary_dar(J) = 0 and unary_dar(I) = 1"), rootContext);
StepSolver.Step<Boolean> step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 1 and J = 1 and unary_dar(J) = 0 and unary_dar(I) = 1"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(false, step.getValue());
localTestContext = rootContext.conjoinWithConjunctiveClause(parse("I = 0 and J = 1 and unary_dar(1) = 0 and unary_dar(0) = 1"), rootContext);
step = unificationStepSolver.step(localTestContext);
Assert.assertEquals(false, step.itDepends());
Assert.assertEquals(true, step.getValue());
}
use of com.sri.ai.expresso.type.FunctionType in project aic-expresso by aic-sri-international.
the class CompoundTheoryTestingSupport method setVariableNamesAndTypesForTesting.
/**
* This is overridden so that given variables and types for testing are distributed to their
* respective theories according to {@link #isSuitableFor(Expression, Type)}.
*/
@Override
public void setVariableNamesAndTypesForTesting(Map<String, Type> variableNamesAndTypesForTesting) {
// First ensure the compound set of variables names and type information is setup correctly
super.setVariableNamesAndTypesForTesting(variableNamesAndTypesForTesting);
// Then update the sub-theories so that they share appropriate subsets of this information
Map<Theory, Map<String, Type>> mapForSubTheory = map();
for (Theory subTheory : getTheory().getSubTheories()) {
mapForSubTheory.put(subTheory, map());
}
for (Map.Entry<String, Type> variableNameAndType : getVariableNamesAndTypesForTesting().entrySet()) {
String variableName = variableNameAndType.getKey();
Expression variable = Expressions.parse(variableName);
Type type = variableNameAndType.getValue();
for (Theory subTheory : getTheory().getSubTheories()) {
if (subTheory.isSuitableFor(variable, type) || (type instanceof FunctionType && subTheory.isSuitableFor(variable, ((FunctionType) type).getCodomain()))) {
mapForSubTheory.get(subTheory).put(variableName, type);
}
}
}
for (Map.Entry<Theory, TheoryTestingSupport> entry : getTheoryToTestingSupport().entrySet()) {
Map<String, Type> forThisSubTheory = mapForSubTheory.get(entry.getKey());
entry.getValue().setVariableNamesAndTypesForTesting(forThisSubTheory);
}
}
use of com.sri.ai.expresso.type.FunctionType in project aic-expresso by aic-sri-international.
the class TheoryTestingSupport method getTestingVariableType.
/**
* Get the type associated with the given testing variable.
*
* @param variable
* the testing variable whose type is to be returned.
* @return the type of the given testing variable.
*/
default default Type getTestingVariableType(String variable) {
String variableName = getVariableName(variable);
Type result = getVariableNamesAndTypesForTesting().get(variableName);
// We need to check if the variable is a function application
// because if it is we need to use the codomain of its function type
// as the type of the testing variable.
Expression variableExpresison = parse(variable);
Expression functor = variableExpresison.getFunctor();
if (functor != null) {
result = ((FunctionType) result).getCodomain();
}
return result;
}
use of com.sri.ai.expresso.type.FunctionType in project aic-expresso by aic-sri-international.
the class GrinderUtilTest method testIsCategoricalTypeSubtypeOf.
@Test
public void testIsCategoricalTypeSubtypeOf() {
Categorical categoricalType = new Categorical("TestCategorical1", 10, parse("a"), parse("b"), parse("c"));
Assert.assertFalse(isTypeSubtypeOf(categoricalType, BOOLEAN_TYPE));
Assert.assertFalse(isTypeSubtypeOf(categoricalType, INTEGER_TYPE));
Assert.assertFalse(isTypeSubtypeOf(categoricalType, REAL_TYPE));
Assert.assertFalse(isTypeSubtypeOf(categoricalType, new IntegerInterval("Integer")));
Assert.assertFalse(isTypeSubtypeOf(categoricalType, new IntegerInterval("0..4")));
Assert.assertFalse(isTypeSubtypeOf(categoricalType, new RealInterval("Real")));
Assert.assertFalse(isTypeSubtypeOf(categoricalType, new RealInterval("[-0.5;4.5]")));
Assert.assertFalse(isTypeSubtypeOf(categoricalType, new FunctionType(categoricalType)));
Assert.assertFalse(isTypeSubtypeOf(categoricalType, new FunctionType(categoricalType, categoricalType)));
// NOTE: Categorical types equality is based on name.
Assert.assertTrue(isTypeSubtypeOf(categoricalType, new Categorical("TestCategorical1", 10, parse("a"), parse("b"), parse("c"))));
Assert.assertTrue(isTypeSubtypeOf(categoricalType, new Categorical("TestCategorical1", 5, parse("x"), parse("y"), parse("z"))));
Assert.assertFalse(isTypeSubtypeOf(categoricalType, new Categorical("TestCategorical2", 10, parse("a"), parse("b"), parse("c"))));
}
use of com.sri.ai.expresso.type.FunctionType in project aic-expresso by aic-sri-international.
the class GrinderUtilTest method testIsIntegerIntervalSubtypeOf.
@Test
public void testIsIntegerIntervalSubtypeOf() {
IntegerInterval intInterval = new IntegerInterval("Integer");
Assert.assertFalse(isTypeSubtypeOf(intInterval, BOOLEAN_TYPE));
Assert.assertTrue(isTypeSubtypeOf(intInterval, INTEGER_TYPE));
Assert.assertTrue(isTypeSubtypeOf(intInterval, REAL_TYPE));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new IntegerInterval("Integer")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..4")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("Real")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[-0.5;4.5]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new FunctionType(intInterval)));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new FunctionType(intInterval, intInterval)));
intInterval = new IntegerInterval("0..4");
Assert.assertTrue(isTypeSubtypeOf(intInterval, INTEGER_TYPE));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new IntegerInterval("0..4")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("1..4")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..3")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, REAL_TYPE));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("Real")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("[0;4]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]0;4]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4[")));
intInterval = new IntegerInterval("-infinity..4");
Assert.assertTrue(isTypeSubtypeOf(intInterval, INTEGER_TYPE));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..4")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("1..4")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..3")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new IntegerInterval("-infinity..5")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, REAL_TYPE));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("Real")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]0;4]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4[")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("[-infinity;4]")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("]-infinity;4]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[-infinity;4[")));
intInterval = new IntegerInterval("0..infinity");
Assert.assertTrue(isTypeSubtypeOf(intInterval, INTEGER_TYPE));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..4")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("1..4")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..3")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new IntegerInterval("-1..infinity")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, REAL_TYPE));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("Real")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]0;4]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4[")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("[0;infinity]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]0;infinity]")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("[0;infinity[")));
intInterval = new IntegerInterval("-infinity..infinity");
Assert.assertTrue(isTypeSubtypeOf(intInterval, INTEGER_TYPE));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..4")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("1..4")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new IntegerInterval("0..3")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, REAL_TYPE));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("Real")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]0;4]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;4[")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[-infinity;4]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]-infinity;4]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[-infinity;4[")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;infinity]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("]0;infinity]")));
Assert.assertFalse(isTypeSubtypeOf(intInterval, new RealInterval("[0;infinity[")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("[-infinity;infinity]")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("]-infinity;infinity]")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("[-infinity;infinity[")));
Assert.assertTrue(isTypeSubtypeOf(intInterval, new RealInterval("]-infinity;infinity[")));
}
Aggregations