Search in sources :

Example 26 with FunctionType

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());
}
Also used : Context(com.sri.ai.grinder.api.Context) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) FunctionType(com.sri.ai.expresso.type.FunctionType) StepSolver(com.sri.ai.grinder.api.StepSolver) UnificationStepSolver(com.sri.ai.grinder.theory.base.UnificationStepSolver) UnificationStepSolver(com.sri.ai.grinder.theory.base.UnificationStepSolver) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 27 with FunctionType

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);
    }
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Expression(com.sri.ai.expresso.api.Expression) AbstractTheoryTestingSupport(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) FunctionType(com.sri.ai.expresso.type.FunctionType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 28 with FunctionType

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;
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Expression(com.sri.ai.expresso.api.Expression)

Example 29 with FunctionType

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"))));
}
Also used : IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) FunctionType(com.sri.ai.expresso.type.FunctionType) Categorical(com.sri.ai.expresso.type.Categorical) RealInterval(com.sri.ai.expresso.type.RealInterval) Test(org.junit.Test)

Example 30 with FunctionType

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[")));
}
Also used : IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) FunctionType(com.sri.ai.expresso.type.FunctionType) RealInterval(com.sri.ai.expresso.type.RealInterval) Test(org.junit.Test)

Aggregations

FunctionType (com.sri.ai.expresso.type.FunctionType)57 Test (org.junit.Test)28 Expression (com.sri.ai.expresso.api.Expression)23 Type (com.sri.ai.expresso.api.Type)22 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)17 Context (com.sri.ai.grinder.api.Context)15 Context (com.sri.ai.grinder.sgdpllt.api.Context)14 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)13 ArrayList (java.util.ArrayList)13 RealInterval (com.sri.ai.expresso.type.RealInterval)12 TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)11 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)11 TupleType (com.sri.ai.expresso.type.TupleType)10 StepSolver (com.sri.ai.grinder.api.StepSolver)10 StepSolver (com.sri.ai.grinder.sgdpllt.api.StepSolver)10 UnificationStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver)10 UnificationStepSolver (com.sri.ai.grinder.theory.base.UnificationStepSolver)10 Ignore (org.junit.Ignore)10 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)9 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)9