Search in sources :

Example 26 with Context

use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.

the class SGDPLLTUtil method makeContext.

public static Context makeContext(Map<String, String> mapFromSymbolNameToTypeName, Map<String, String> mapFromCategoricalTypeNameToSizeString, Collection<Type> additionalTypes, Predicate<Expression> isUniquelyNamedConstantPredicate, Theory theory) {
    Context result = (Context) GrinderUtil.extendRegistryWith(mapFromSymbolNameToTypeName, additionalTypes, mapFromCategoricalTypeNameToSizeString, isUniquelyNamedConstantPredicate, new TrueContext(theory));
    result = result.setIsUniquelyNamedConstantPredicate(isUniquelyNamedConstantPredicate);
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context)

Example 27 with Context

use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.

the class MaximumExpressionStepSolverTest method test.

@Test
public void test() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new DifferenceArithmeticTheory(true, true));
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    List<String> expressionStrings;
    String order;
    Expression orderMinimum;
    Expression orderMaximum;
    Expression expected;
    expressionStrings = list("I", "J");
    expected = parse("if I < J then J else I");
    order = LESS_THAN;
    orderMinimum = MINUS_INFINITY;
    orderMaximum = INFINITY;
    runTest(expressionStrings, order, orderMinimum, orderMaximum, expected, context);
    expressionStrings = list("I", "J");
    expected = parse("if I > J then J else I");
    order = GREATER_THAN;
    orderMinimum = INFINITY;
    orderMaximum = MINUS_INFINITY;
    runTest(expressionStrings, order, orderMinimum, orderMaximum, expected, context);
    expressionStrings = list("2", "3", "J");
    expected = parse("if 3 < J then J else 3");
    order = LESS_THAN;
    orderMinimum = MINUS_INFINITY;
    orderMaximum = INFINITY;
    runTest(expressionStrings, order, orderMinimum, orderMaximum, expected, context);
    expressionStrings = list("2", "I", "3", "J");
    expected = parse("if 2 < I then if I < J then J else I else if 3 < J then J else 3");
    order = LESS_THAN;
    orderMinimum = MINUS_INFINITY;
    orderMaximum = INFINITY;
    runTest(expressionStrings, order, orderMinimum, orderMaximum, expected, context);
    expressionStrings = list("1", "2");
    expected = parse("2");
    order = LESS_THAN;
    orderMinimum = MINUS_INFINITY;
    orderMaximum = INFINITY;
    runTest(expressionStrings, order, orderMinimum, orderMaximum, expected, context);
    expressionStrings = list("1", "2");
    expected = parse("1");
    order = GREATER_THAN;
    orderMinimum = INFINITY;
    orderMaximum = MINUS_INFINITY;
    runTest(expressionStrings, order, orderMinimum, orderMaximum, expected, context);
    expressionStrings = list("1", "-infinity");
    expected = parse("1");
    order = LESS_THAN;
    orderMinimum = MINUS_INFINITY;
    orderMaximum = INFINITY;
    runTest(expressionStrings, order, orderMinimum, orderMaximum, expected, context);
    expressionStrings = list("1", "infinity");
    expected = parse("infinity");
    order = LESS_THAN;
    orderMinimum = MINUS_INFINITY;
    orderMaximum = INFINITY;
    runTest(expressionStrings, order, orderMinimum, orderMaximum, expected, context);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) Test(org.junit.Test)

Example 28 with Context

use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.

the class SelectExpressionsSatisfyingComparisonStepSolverTest method test.

@Test
public void test() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new DifferenceArithmeticTheory(true, true));
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    List<String> expressionStrings;
    Expression bound;
    Expression expected;
    expressionStrings = list("I", "J");
    bound = parse("J");
    expected = parse("if I < J then list(I) else list()");
    runTest(expressionStrings, bound, expected, context);
    expressionStrings = list("I", "2", "J");
    bound = parse("3");
    expected = parse("if I < 3 then if J < 3 then list(I, 2, J) else list(I, 2) else if J < 3 then list(2, J) else list(2)");
    runTest(expressionStrings, bound, expected, context);
    expressionStrings = list();
    bound = parse("3");
    expected = parse("list()");
    runTest(expressionStrings, bound, expected, context);
    expressionStrings = list("I", "2", "J");
    bound = parse("infinity");
    expected = parse("list(I, 2, J)");
    runTest(expressionStrings, bound, expected, context);
    expressionStrings = list("I", "2", "J");
    bound = parse("-infinity");
    expected = parse("list()");
    runTest(expressionStrings, bound, expected, context);
    expressionStrings = list("I", "2", "infinity");
    bound = parse("3");
    expected = parse("if I < 3 then list(I, 2) else list(2)");
    runTest(expressionStrings, bound, expected, context);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) Test(org.junit.Test)

Example 29 with Context

use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.

the class BruteForceCommonInterpreterTest method test.

@Test
public void test() {
    LinkedHashMap<Expression, Expression> assignment = map(parse("Two"), Expressions.TWO);
    BruteForceCommonInterpreter interpreter = new BruteForceCommonInterpreter();
    Context context = new TrueContext();
    context = AbstractIterativeMultiIndexQuantifierElimination.extendAssignments(assignment, context);
    String expression;
    String expected;
    expression = "max({{ (on I in 0..3) max( {{ (on J in 0..3) I + J : I != J }} ) }})";
    expected = "5";
    runTest(expression, expected, interpreter, context);
    expression = "3 * (Two + 5 - 3)*(-10)";
    expected = "-120";
    runTest(expression, expected, interpreter, context);
    expression = "3 * (2 + 5 - 3)*(-10)";
    expected = "-120";
    runTest(expression, expected, interpreter, context);
    expression = "sum({{ (on x in 0..2)  x }} )";
    expected = "3";
    runTest(expression, expected, interpreter, context);
    expression = "sum({{ (on f in 0..2 -> Boolean)  if f(0) then 2 else 3 }} )";
    expected = "20";
    runTest(expression, expected, interpreter, context);
    expression = "sum({{ (on f in 0..2 -> Boolean)  if f(0) and f(1) then 2 else 3  :  f(2) }} )";
    expected = "11";
    runTest(expression, expected, interpreter, context);
    expression = "sum({{ (on f in 0..2 -> Boolean)  " + "if f(0) and f(1) then 2 else | f in 0..2 x 0..2 -> Boolean : f(0, 0) |  " + ":  f(2) }} )";
    expected = "770";
    runTest(expression, expected, interpreter, context);
    expression = "sum({{ (on f in '->'(x(1..2), Boolean), g in '->'(x(1..2), Boolean))  if f(1) and g(2) then 2 else 3  :  f(2) }} )";
    expected = "22";
    runTest(expression, expected, interpreter, context);
    expression = "| f in '->'(x(0..2, 0..2), Boolean) : f(0, 0) |";
    expected = "256";
    runTest(expression, expected, interpreter, context);
    expression = "| f in 0..2 x 0..2 -> Boolean : f(0, 0) |";
    expected = "256";
    runTest(expression, expected, interpreter, context);
    expression = "| f in 0..2 x 0..2 -> Boolean, g in 0..2 -> Boolean : f(0, 0) |";
    expected = "2048";
    runTest(expression, expected, interpreter, context);
    expression = "sum( {{ (on p in Boolean) if p then 1 else 2 }} )";
    expected = "3";
    runTest(expression, expected, interpreter, context);
}
Also used : TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) BruteForceCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.BruteForceCommonInterpreter) Expression(com.sri.ai.expresso.api.Expression) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Test(org.junit.Test)

Example 30 with Context

use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.

the class MeasureTest method measure.

private Rational measure(String testIntensionalSetString) {
    Expression testIntensionalSetExpression = parse(testIntensionalSetString);
    Expression properlyConditionedIntensionalSetExpression = testIntensionalSetExpression;
    if (Sets.isIntensionalSet(testIntensionalSetExpression)) {
        IntensionalSet intensionalSet = (IntensionalSet) testIntensionalSetExpression;
        List<Expression> indices = IndexExpressions.getIndices(intensionalSet.getIndexExpressions());
        if (indices.size() == 1) {
            Expression index = indices.get(0);
            Context intensionalSetContext = context.extendWith(intensionalSet.getIndexExpressions());
            Type type = GrinderUtil.getType(index, intensionalSetContext);
            if (type instanceof RealExpressoType || type instanceof RealInterval) {
                SingleVariableLinearRealArithmeticConstraint singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, context.getTheory());
                singleVariableConstraint = (SingleVariableLinearRealArithmeticConstraint) singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
                properlyConditionedIntensionalSetExpression = IntensionalSet.make(Sets.isMultiSet(intensionalSet) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, intensionalSet.getIndexExpressions(), intensionalSet.getHead(), singleVariableConstraint);
            }
        }
    }
    Rational result = Measure.get(properlyConditionedIntensionalSetExpression, context);
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Aggregations

Context (com.sri.ai.grinder.sgdpllt.api.Context)105 Expression (com.sri.ai.expresso.api.Expression)80 Test (org.junit.Test)48 TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)36 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)30 Type (com.sri.ai.expresso.api.Type)28 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)28 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)22 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)20 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)18 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)17 Map (java.util.Map)17 Beta (com.google.common.annotations.Beta)16 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)16 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)14 LinearRealArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory)14 FunctionType (com.sri.ai.expresso.type.FunctionType)13 Util (com.sri.ai.util.Util)13 SingleVariableConstraint (com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)12