Search in sources :

Example 1 with AssignmentsIterator

use of com.sri.ai.grinder.helper.AssignmentsIterator in project aic-expresso by aic-sri-international.

the class AssignmentsIteratorTest method test2.

@Test
public void test2() {
    Registry registry = new DefaultRegistry();
    Type myType = new Categorical("People", 2, arrayList(makeSymbol("oscar"), makeSymbol("mary")));
    Symbol x = makeSymbol("X");
    Symbol y = makeSymbol("Y");
    String expected = "{X=oscar, Y=oscar}\n" + "{X=mary, Y=oscar}\n" + "{X=oscar, Y=mary}\n" + "{X=mary, Y=mary}";
    Symbol myTypeExpression = makeSymbol(myType.getName());
    registry = registry.add(myType);
    registry = registry.registerAdditionalSymbolsAndTypes(map(x, myTypeExpression, y, myTypeExpression));
    AssignmentsIterator assignmentsIterator = new AssignmentsIterator(list(x, y), registry);
    String actual = join("\n", assignmentsIterator);
    // System.out.println(actual);	
    assertEquals(expected, actual);
}
Also used : Type(com.sri.ai.expresso.api.Type) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Symbol(com.sri.ai.expresso.api.Symbol) AssignmentsIterator(com.sri.ai.grinder.helper.AssignmentsIterator) DefaultRegistry(com.sri.ai.grinder.sgdpllt.core.DefaultRegistry) Registry(com.sri.ai.grinder.api.Registry) DefaultRegistry(com.sri.ai.grinder.sgdpllt.core.DefaultRegistry) Categorical(com.sri.ai.expresso.type.Categorical) Test(org.junit.Test)

Example 2 with AssignmentsIterator

use of com.sri.ai.grinder.helper.AssignmentsIterator in project aic-expresso by aic-sri-international.

the class AssignmentsIteratorTest method test3.

@Test
public void test3() {
    Registry registry = new DefaultRegistry();
    Type peopleType = new Categorical("People", 4, arrayList(makeSymbol("oscar"), makeSymbol("mary")));
    Type petsType = new Categorical("Pets", 3, arrayList(makeSymbol("fido"), makeSymbol("purrs")));
    Symbol x = makeSymbol("X");
    Symbol y = makeSymbol("Y");
    String expected = "{X=oscar, Y=fido}\n" + "{X=mary, Y=fido}\n" + "{X=people3, Y=fido}\n" + "{X=people4, Y=fido}\n" + "{X=oscar, Y=purrs}\n" + "{X=mary, Y=purrs}\n" + "{X=people3, Y=purrs}\n" + "{X=people4, Y=purrs}\n" + "{X=oscar, Y=pets3}\n" + "{X=mary, Y=pets3}\n" + "{X=people3, Y=pets3}\n" + "{X=people4, Y=pets3}";
    Symbol myPeopleTypeExpression = makeSymbol(peopleType.getName());
    Symbol myPetsTypeExpression = makeSymbol(petsType.getName());
    registry = registry.add(peopleType);
    registry = registry.add(petsType);
    registry = registry.registerAdditionalSymbolsAndTypes(map(x, myPeopleTypeExpression, y, myPetsTypeExpression));
    AssignmentsIterator assignmentsIterator = new AssignmentsIterator(list(x, y), registry);
    String actual = join("\n", assignmentsIterator);
    // System.out.println(actual);	
    assertEquals(expected, actual);
}
Also used : Type(com.sri.ai.expresso.api.Type) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Symbol(com.sri.ai.expresso.api.Symbol) AssignmentsIterator(com.sri.ai.grinder.helper.AssignmentsIterator) DefaultRegistry(com.sri.ai.grinder.sgdpllt.core.DefaultRegistry) Registry(com.sri.ai.grinder.api.Registry) DefaultRegistry(com.sri.ai.grinder.sgdpllt.core.DefaultRegistry) Categorical(com.sri.ai.expresso.type.Categorical) Test(org.junit.Test)

Example 3 with AssignmentsIterator

use of com.sri.ai.grinder.helper.AssignmentsIterator in project aic-expresso by aic-sri-international.

the class FunctionType method iterator.

@Override
public Iterator<Expression> iterator() {
    if (!(getCodomain().isDiscrete() && getArgumentTypes().stream().allMatch(Type::isFinite))) {
        throw new Error("Only function types with finite argument types and a discrete codomain can be enumerated.");
    }
    if (cachedIterateRegistry == null) {
        // Pre-compute
        cachedIterateRegistry = new DefaultRegistry();
        int numCodomainValues = argumentTypes.stream().map(Type::cardinality).map(Expression::rationalValue).reduce(Rational.ONE, Rational::multiply).intValue();
        cachedIterateRegistry = cachedIterateRegistry.add(getCodomain());
        Expression codomainTypeExpression = parse(getCodomain().getName());
        codomainVariables = new ArrayList<>(numCodomainValues);
        Map<Expression, Expression> symbolsAndTypes = new LinkedHashMap<>();
        for (int i = 0; i < numCodomainValues; i++) {
            Expression coDomainVariableI = makeSymbol("C" + (i + 1));
            codomainVariables.add(coDomainVariableI);
            symbolsAndTypes.put(coDomainVariableI, codomainTypeExpression);
        }
        List<Expression> argVariables = new ArrayList<>();
        for (int i = 0; i < getArgumentTypes().size(); i++) {
            cachedIterateRegistry = cachedIterateRegistry.add(getArgumentTypes().get(i));
            argVariables.add(makeSymbol("A" + (i + 1)));
            symbolsAndTypes.put(argVariables.get(i), parse(getArgumentTypes().get(i).getName()));
        }
        cachedIterateRegistry = cachedIterateRegistry.setSymbolsAndTypes(symbolsAndTypes);
        StringJoiner lambdaApplicationPrefix = new StringJoiner(", ", "(lambda ", " : ");
        for (Expression argVar : argVariables) {
            lambdaApplicationPrefix.add(argVar + " in " + symbolsAndTypes.get(argVar));
        }
        AssignmentsIterator assignmentsIterator = new AssignmentsIterator(argVariables, cachedIterateRegistry);
        StringJoiner lambdaApplicationBody = new StringJoiner(" else ", "", ")");
        AtomicInteger counter = new AtomicInteger(0);
        assignmentsIterator.forEachRemaining(assignment -> {
            if (counter.incrementAndGet() != numCodomainValues) {
                StringJoiner condition = new StringJoiner(" and ", "if ", " then C" + counter);
                for (int i = 0; i < argVariables.size(); i++) {
                    Expression argVariable = argVariables.get(i);
                    condition.add(argVariable + " = " + assignment.get(argVariable));
                }
                lambdaApplicationBody.add(condition.toString());
            } else {
                lambdaApplicationBody.add("C" + numCodomainValues);
            }
        });
        genericLambda = parse(lambdaApplicationPrefix.toString() + lambdaApplicationBody.toString());
    }
    return FunctionIterator.functionIterator(new AssignmentsIterator(codomainVariables, cachedIterateRegistry), assignment -> {
        Expression lambda = genericLambda;
        for (int i = 0; i < codomainVariables.size(); i++) {
            Expression codomainVariable = codomainVariables.get(i);
            lambda = lambda.replaceFirstOccurrence(codomainVariable, assignment.get(codomainVariable), cachedIterateRegistry);
        }
        return lambda;
    });
}
Also used : Expression(com.sri.ai.expresso.api.Expression) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssignmentsIterator(com.sri.ai.grinder.helper.AssignmentsIterator) ArrayList(java.util.ArrayList) DefaultRegistry(com.sri.ai.grinder.sgdpllt.core.DefaultRegistry) StringJoiner(java.util.StringJoiner) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with AssignmentsIterator

use of com.sri.ai.grinder.helper.AssignmentsIterator in project aic-expresso by aic-sri-international.

the class TupleType method iterator.

@Override
public Iterator<Expression> iterator() {
    if (!getElementTypes().stream().allMatch(Type::isDiscrete)) {
        throw new Error("Only tuple types with discrete element types can be enumerated.");
    }
    if (cachedIterateRegistry == null) {
        // Pre-compute
        elementVariables = new ArrayList<>();
        cachedIterateRegistry = new DefaultRegistry();
        Map<Expression, Expression> symbolsAndTypes = new LinkedHashMap<>();
        for (int i = 0; i < getArity(); i++) {
            Expression elementVariableI = makeSymbol("E" + (i + 1));
            elementVariables.add(elementVariableI);
            symbolsAndTypes.put(elementVariableI, parse(getElementTypes().get(i).getName()));
            cachedIterateRegistry = cachedIterateRegistry.add(elementTypes.get(i));
        }
        cachedIterateRegistry = cachedIterateRegistry.setSymbolsAndTypes(symbolsAndTypes);
        StringJoiner tupleVariableRepresentation = new StringJoiner(", ", "tuple(", ")");
        for (Expression eleVar : elementVariables) {
            tupleVariableRepresentation.add(eleVar.toString());
        }
        genericTuple = parse(tupleVariableRepresentation.toString());
    }
    return FunctionIterator.functionIterator(new AssignmentsIterator(elementVariables, cachedIterateRegistry), assignment -> {
        Expression tuple = genericTuple;
        for (int i = 0; i < elementVariables.size(); i++) {
            Expression elementVariable = elementVariables.get(i);
            tuple = tuple.replaceFirstOccurrence(elementVariable, assignment.get(elementVariable), cachedIterateRegistry);
        }
        return tuple;
    });
}
Also used : Expression(com.sri.ai.expresso.api.Expression) AssignmentsIterator(com.sri.ai.grinder.helper.AssignmentsIterator) DefaultRegistry(com.sri.ai.grinder.sgdpllt.core.DefaultRegistry) StringJoiner(java.util.StringJoiner) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with AssignmentsIterator

use of com.sri.ai.grinder.helper.AssignmentsIterator in project aic-expresso by aic-sri-international.

the class AssignmentsIteratorTest method test1.

@Test
public void test1() {
    Registry registry = new DefaultRegistry();
    Type myType = new Categorical("People", 4, arrayList(makeSymbol("oscar"), makeSymbol("mary")));
    Symbol x = makeSymbol("X");
    Symbol y = makeSymbol("Y");
    String expected = "{X=oscar, Y=oscar}\n" + "{X=mary, Y=oscar}\n" + "{X=people3, Y=oscar}\n" + "{X=people4, Y=oscar}\n" + "{X=oscar, Y=mary}\n" + "{X=mary, Y=mary}\n" + "{X=people3, Y=mary}\n" + "{X=people4, Y=mary}\n" + "{X=oscar, Y=people3}\n" + "{X=mary, Y=people3}\n" + "{X=people3, Y=people3}\n" + "{X=people4, Y=people3}\n" + "{X=oscar, Y=people4}\n" + "{X=mary, Y=people4}\n" + "{X=people3, Y=people4}\n" + "{X=people4, Y=people4}";
    Symbol myTypeExpression = makeSymbol(myType.getName());
    registry = registry.add(myType);
    registry = registry.registerAdditionalSymbolsAndTypes(map(x, myTypeExpression, y, myTypeExpression));
    AssignmentsIterator assignmentsIterator = new AssignmentsIterator(list(x, y), registry);
    String actual = join("\n", assignmentsIterator);
    // System.out.println(actual);	
    assertEquals(expected, actual);
}
Also used : Type(com.sri.ai.expresso.api.Type) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Symbol(com.sri.ai.expresso.api.Symbol) AssignmentsIterator(com.sri.ai.grinder.helper.AssignmentsIterator) DefaultRegistry(com.sri.ai.grinder.sgdpllt.core.DefaultRegistry) Registry(com.sri.ai.grinder.api.Registry) DefaultRegistry(com.sri.ai.grinder.sgdpllt.core.DefaultRegistry) Categorical(com.sri.ai.expresso.type.Categorical) Test(org.junit.Test)

Aggregations

AssignmentsIterator (com.sri.ai.grinder.helper.AssignmentsIterator)6 DefaultRegistry (com.sri.ai.grinder.sgdpllt.core.DefaultRegistry)5 Expression (com.sri.ai.expresso.api.Expression)3 Symbol (com.sri.ai.expresso.api.Symbol)3 Type (com.sri.ai.expresso.api.Type)3 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)3 Categorical (com.sri.ai.expresso.type.Categorical)3 Registry (com.sri.ai.grinder.api.Registry)3 Test (org.junit.Test)3 LinkedHashMap (java.util.LinkedHashMap)2 StringJoiner (java.util.StringJoiner)2 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)1 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)1 Context (com.sri.ai.grinder.sgdpllt.api.Context)1 SingleVariableConstraint (com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)1 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)1 DefaultMultiVariableConstraint (com.sri.ai.grinder.sgdpllt.core.constraint.DefaultMultiVariableConstraint)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1