Search in sources :

Example 1 with TupleType

use of com.sri.ai.expresso.type.TupleType in project aic-expresso by aic-sri-international.

the class TupleRewriterTest method testTupleValuedFreeVariablesSimplifier.

@Test
public void testTupleValuedFreeVariablesSimplifier() {
    Context tupleTheoryContext = new TrueContext(new CompoundTheory(new DifferenceArithmeticTheory(false, false), new TupleTheory()));
    TupleType nTupleType = new TupleType(new IntegerInterval(1, 10), new IntegerInterval(1, 10));
    tupleTheoryContext = (Context) GrinderUtil.extendRegistryWith(map("N", nTupleType.toString()), Arrays.asList(nTupleType), tupleTheoryContext);
    TupleValuedFreeVariablesSimplifier simplifier = new TupleValuedFreeVariablesSimplifier();
    Expression expression = parse("sum( {{ (on X in 1..10) if N = (2, X) then 2 else 3 }} )");
    Expression simplified = simplifier.apply(expression, tupleTheoryContext);
    Assert.assertEquals(parse("if get(N, 1) = 2 then 29 else 30"), simplified);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) TupleType(com.sri.ai.expresso.type.TupleType) TupleValuedFreeVariablesSimplifier(com.sri.ai.grinder.sgdpllt.theory.tuple.rewriter.TupleValuedFreeVariablesSimplifier) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) TupleTheory(com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory) Test(org.junit.Test)

Example 2 with TupleType

use of com.sri.ai.expresso.type.TupleType 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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Context(com.sri.ai.grinder.sgdpllt.api.Context) LinkedHashMap(java.util.LinkedHashMap) Type(com.sri.ai.expresso.api.Type) TupleType(com.sri.ai.expresso.type.TupleType) Expression(com.sri.ai.expresso.api.Expression) TupleType(com.sri.ai.expresso.type.TupleType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(com.sri.ai.util.base.Pair)

Example 3 with TupleType

use of com.sri.ai.expresso.type.TupleType in project aic-expresso by aic-sri-international.

the class AssignmentsSamplingIteratorTest method testSampleOverTuple.

@Test
public void testSampleOverTuple() {
    updateContextWithIndexAndType("T", new TupleType(new IntegerInterval(1, 10), GrinderUtil.BOOLEAN_TYPE));
    Assert.assertEquals("{T=(6, true)}:{T=(8, true)}:{T=(5, true)}", join(":", newSamplingIterator("T", 3, "true")));
}
Also used : IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) TupleType(com.sri.ai.expresso.type.TupleType) Test(org.junit.Test)

Example 4 with TupleType

use of com.sri.ai.expresso.type.TupleType in project aic-expresso by aic-sri-international.

the class TypeTest method testCardinality.

@Test
public void testCardinality() {
    // i.e. 2^3
    Assert.assertEquals(parse("8"), new FunctionType(GrinderUtil.BOOLEAN_TYPE, new IntegerInterval(0, 2)).cardinality());
    // i.e. 3^(4*2)
    Assert.assertEquals(parse("6561"), new FunctionType(new IntegerInterval(1, 3), new IntegerInterval(1, 4), new IntegerInterval(1, 2)).cardinality());
    // i.e. 1 - the empty tuple
    Assert.assertEquals(parse("1"), new TupleType().cardinality());
    // i.e. 2x3x4
    Assert.assertEquals(parse("24"), new TupleType(GrinderUtil.BOOLEAN_TYPE, new IntegerInterval(1, 3), new IntegerInterval(1, 4)).cardinality());
}
Also used : FunctionType(com.sri.ai.expresso.type.FunctionType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) TupleType(com.sri.ai.expresso.type.TupleType) Test(org.junit.Test)

Example 5 with TupleType

use of com.sri.ai.expresso.type.TupleType in project aic-expresso by aic-sri-international.

the class TypeTest method testIterator.

@Test
public void testIterator() {
    FunctionType fType;
    // i.e. 2
    fType = new FunctionType(new IntegerInterval(1, 2));
    checkTypeIteration(fType, "lambda : 1", "lambda : 2");
    // i.e. 2^2
    fType = new FunctionType(new IntegerInterval(1, 2), new Categorical("Car", 2));
    checkTypeIteration(fType, "lambda A1 in Car : if A1 = car1 then 1 else 1", "lambda A1 in Car : if A1 = car1 then 2 else 1", "lambda A1 in Car : if A1 = car1 then 1 else 2", "lambda A1 in Car : if A1 = car1 then 2 else 2");
    // i.e. 2^(2*2)
    fType = new FunctionType(new IntegerInterval(1, 2), new Categorical("Car", 2), new Categorical("Bike", 2));
    checkTypeIteration(fType, "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 1 else if (A1 = car2) and (A2 = bike1) then 1 else if (A1 = car1) and (A2 = bike2) then 1 else 1", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 2 else if (A1 = car2) and (A2 = bike1) then 1 else if (A1 = car1) and (A2 = bike2) then 1 else 1", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 1 else if (A1 = car2) and (A2 = bike1) then 2 else if (A1 = car1) and (A2 = bike2) then 1 else 1", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 2 else if (A1 = car2) and (A2 = bike1) then 2 else if (A1 = car1) and (A2 = bike2) then 1 else 1", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 1 else if (A1 = car2) and (A2 = bike1) then 1 else if (A1 = car1) and (A2 = bike2) then 2 else 1", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 2 else if (A1 = car2) and (A2 = bike1) then 1 else if (A1 = car1) and (A2 = bike2) then 2 else 1", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 1 else if (A1 = car2) and (A2 = bike1) then 2 else if (A1 = car1) and (A2 = bike2) then 2 else 1", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 2 else if (A1 = car2) and (A2 = bike1) then 2 else if (A1 = car1) and (A2 = bike2) then 2 else 1", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 1 else if (A1 = car2) and (A2 = bike1) then 1 else if (A1 = car1) and (A2 = bike2) then 1 else 2", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 2 else if (A1 = car2) and (A2 = bike1) then 1 else if (A1 = car1) and (A2 = bike2) then 1 else 2", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 1 else if (A1 = car2) and (A2 = bike1) then 2 else if (A1 = car1) and (A2 = bike2) then 1 else 2", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 2 else if (A1 = car2) and (A2 = bike1) then 2 else if (A1 = car1) and (A2 = bike2) then 1 else 2", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 1 else if (A1 = car2) and (A2 = bike1) then 1 else if (A1 = car1) and (A2 = bike2) then 2 else 2", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 2 else if (A1 = car2) and (A2 = bike1) then 1 else if (A1 = car1) and (A2 = bike2) then 2 else 2", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 1 else if (A1 = car2) and (A2 = bike1) then 2 else if (A1 = car1) and (A2 = bike2) then 2 else 2", "lambda A1 in Car, A2 in Bike : if (A1 = car1) and (A2 = bike1) then 2 else if (A1 = car2) and (A2 = bike1) then 2 else if (A1 = car1) and (A2 = bike2) then 2 else 2");
    // i.e. 0
    TupleType tType = new TupleType();
    checkTypeIteration(tType, "()");
    // i.e. 3
    tType = new TupleType(new IntegerInterval(1, 3));
    checkTypeIteration(tType, "tuple(1)", "tuple(2)", "tuple(3)");
    // i.e. 2x3
    tType = new TupleType(GrinderUtil.BOOLEAN_TYPE, new IntegerInterval(1, 3));
    checkTypeIteration(tType, "(true, 1)", "(false, 1)", "(true, 2)", "(false, 2)", "(true, 3)", "(false, 3)");
}
Also used : FunctionType(com.sri.ai.expresso.type.FunctionType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) TupleType(com.sri.ai.expresso.type.TupleType) Categorical(com.sri.ai.expresso.type.Categorical) Test(org.junit.Test)

Aggregations

TupleType (com.sri.ai.expresso.type.TupleType)23 Expression (com.sri.ai.expresso.api.Expression)17 Type (com.sri.ai.expresso.api.Type)13 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)11 FunctionType (com.sri.ai.expresso.type.FunctionType)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)8 RealInterval (com.sri.ai.expresso.type.RealInterval)8 List (java.util.List)7 Categorical (com.sri.ai.expresso.type.Categorical)6 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)6 Context (com.sri.ai.grinder.api.Context)6 Pair (com.sri.ai.util.base.Pair)6 LinkedHashMap (java.util.LinkedHashMap)6 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)5 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)5 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)5 Expressions (com.sri.ai.expresso.helper.Expressions)5 Rational (com.sri.ai.util.math.Rational)5