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);
}
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);
}
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;
});
}
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;
});
}
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);
}
Aggregations