use of com.sri.ai.expresso.api.Symbol in project aic-expresso by aic-sri-international.
the class Expressions method standardizeApart.
/**
* Given a symbol, a predicate indicating what symbols are already defined in some situation,
* and an expression possibly containing the symbol,
* prime the symbol until it is unique,
* and return the new symbol and the result of replacing the old symbol by the new in the expression containing it.
* @param symbol
* @param alreadyDefined
* @param containingSymbol
* @return the new symbol and the result of replacing the old symbol by the new in the expression containing it.
*/
public static PairOf<Expression> standardizeApart(Expression symbol, Predicate<Expression> alreadyDefined, Expression containingSymbol) {
Expression newSymbol = symbol;
while (alreadyDefined.apply(newSymbol)) {
newSymbol = primedUntilUnique(newSymbol, s -> !alreadyDefined.apply(s));
}
Expression newExpressionContainingSymbol = containingSymbol.replaceSymbol(symbol, newSymbol, null);
PairOf<Expression> result = pairOf(newSymbol, newExpressionContainingSymbol);
return result;
}
use of com.sri.ai.expresso.api.Symbol in project aic-expresso by aic-sri-international.
the class Expressions method parseTextAndMakeSymbolOrStringLiteral.
public static Symbol parseTextAndMakeSymbolOrStringLiteral(String symbolValue) {
boolean isSingleQuoted = symbolValue.startsWith("'") && symbolValue.endsWith("'");
boolean isDoubleQuoted = symbolValue.startsWith("\"") && symbolValue.endsWith("\"");
if (isSingleQuoted || isDoubleQuoted) {
// When parsing, whether a single quoted symbol or a string literal (i.e. double quoted)
// the value is that within the quotes and not the quotes themselves.
symbolValue = symbolValue.substring(1, symbolValue.length() - 1);
}
Symbol result;
if (isDoubleQuoted) {
result = makeStringLiteral(symbolValue);
} else {
result = makeSymbol(symbolValue);
}
return result;
}
use of com.sri.ai.expresso.api.Symbol 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.makeNewContextWithAddedType(peopleType);
registry = registry.makeNewContextWithAddedType(petsType);
registry = registry.makeCloneWithAdditionalRegisteredSymbolsAndTypes(map(x, myPeopleTypeExpression, y, myPetsTypeExpression));
AssignmentMapsIterator assignmentsIterator = new AssignmentMapsIterator(list(x, y), registry);
String actual = join("\n", assignmentsIterator);
// System.out.println(actual);
assertEquals(expected, actual);
}
use of com.sri.ai.expresso.api.Symbol 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.makeNewContextWithAddedType(myType);
registry = registry.makeCloneWithAdditionalRegisteredSymbolsAndTypes(map(x, myTypeExpression, y, myTypeExpression));
AssignmentMapsIterator assignmentsIterator = new AssignmentMapsIterator(list(x, y), registry);
String actual = join("\n", assignmentsIterator);
// System.out.println(actual);
assertEquals(expected, actual);
}
use of com.sri.ai.expresso.api.Symbol in project aic-expresso by aic-sri-international.
the class GrinderUtilTest method setUp.
@Before
public void setUp() {
registry = new DefaultRegistry();
Symbol x = makeSymbol("X");
Symbol y = makeSymbol("Y");
Symbol myPeopleTypeExpression = makeSymbol(peopleType.getName());
Symbol myPetsTypeExpression = makeSymbol(petsType.getName());
registry = registry.makeNewContextWithAddedType(peopleType);
registry = registry.makeNewContextWithAddedType(petsType);
registry = registry.makeCloneWithAdditionalRegisteredSymbolsAndTypes(map(x, myPeopleTypeExpression, y, myPetsTypeExpression));
}
Aggregations