use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class DefaultSymbol method flushGlobalSymbolTable.
public static void flushGlobalSymbolTable() {
if (AICUtilConfiguration.isRecordCacheStatistics()) {
System.out.println("Global Symbol Table Cache Stats=" + globalSymbolTable.stats());
System.out.println("Global String Literal Table Cache Stats=" + globalStringLiteralTable.stats());
}
// Causes relevant flags to be reset.
useGlobalSymbolTable = ExpressoConfiguration.isUseGlobalSymbolTable();
cacheNumericSymbols = ExpressoConfiguration.isGlobalSymbolTableToCacheNumerics();
if (globalSymbolTable != null) {
globalSymbolTable.invalidateAll();
}
if (globalStringLiteralTable != null) {
globalStringLiteralTable.invalidateAll();
}
globalSymbolTable = newSymbolTable();
globalStringLiteralTable = newSymbolTable();
// Add well known symbols to the table
// The Booleans.
globalSymbolTable.put(true, SYMBOL_TRUE);
globalSymbolTable.put(Boolean.TRUE, SYMBOL_TRUE);
globalSymbolTable.put("true", SYMBOL_TRUE);
globalSymbolTable.put(false, SYMBOL_FALSE);
globalSymbolTable.put(Boolean.FALSE, SYMBOL_FALSE);
globalSymbolTable.put("false", SYMBOL_FALSE);
// Common Numbers
globalSymbolTable.put("0", SYMBOL_0);
globalSymbolTable.put(new Integer(0), SYMBOL_0);
globalSymbolTable.put(new Rational(0), SYMBOL_0);
globalSymbolTable.put("1", SYMBOL_1);
globalSymbolTable.put(new Integer(1), SYMBOL_1);
globalSymbolTable.put(new Rational(1), SYMBOL_1);
globalSymbolTable.put("2", SYMBOL_2);
globalSymbolTable.put(new Integer(2), SYMBOL_2);
globalSymbolTable.put(new Rational(2), SYMBOL_2);
globalSymbolTable.put("3", SYMBOL_3);
globalSymbolTable.put(new Integer(3), SYMBOL_3);
globalSymbolTable.put(new Rational(3), SYMBOL_3);
globalSymbolTable.put("4", SYMBOL_4);
globalSymbolTable.put(new Integer(4), SYMBOL_4);
globalSymbolTable.put(new Rational(4), SYMBOL_4);
globalSymbolTable.put("5", SYMBOL_5);
globalSymbolTable.put(new Integer(5), SYMBOL_5);
globalSymbolTable.put(new Rational(5), SYMBOL_5);
globalSymbolTable.put("6", SYMBOL_6);
globalSymbolTable.put(new Integer(6), SYMBOL_6);
globalSymbolTable.put(new Rational(6), SYMBOL_6);
globalSymbolTable.put("7", SYMBOL_7);
globalSymbolTable.put(new Integer(7), SYMBOL_7);
globalSymbolTable.put(new Rational(7), SYMBOL_7);
globalSymbolTable.put("8", SYMBOL_8);
globalSymbolTable.put(new Integer(8), SYMBOL_8);
globalSymbolTable.put(new Rational(8), SYMBOL_8);
globalSymbolTable.put("9", SYMBOL_9);
globalSymbolTable.put(new Integer(9), SYMBOL_9);
globalSymbolTable.put(new Rational(9), SYMBOL_9);
}
use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class DefaultSyntaxLeaf method createSyntaxLeaf.
public static SyntaxLeaf createSyntaxLeaf(Object inputValue, boolean isStringLiteral) {
Object value = inputValue;
SyntaxLeaf result = null;
// If global symbol table to be used and the symbol's value is not
// an expression - i.e. quoted expressions of the form:
// <X>
// as these need to be instantiated each time in order to be
// parsed correctly.
boolean useGlobalSymbolTable = _useGlobalSymbolTable && !(inputValue instanceof Expression);
if (useGlobalSymbolTable) {
if (isStringLiteral) {
result = _globalStringLiteralTable.getIfPresent(inputValue);
} else {
result = _globalSymbolTable.getIfPresent(inputValue);
}
}
if (result == null) {
if (value instanceof Number && !(value instanceof Rational)) {
value = new Rational(((Number) value).doubleValue());
} else if (value.equals("true")) {
value = Boolean.TRUE;
} else if (value.equals("false")) {
value = Boolean.FALSE;
} else if (value instanceof String) {
value = UNESCAPE_STRING_VALUE.translate((String) value);
if (!isStringLiteral) {
// attempt to implicitly convert to a Rational value if possible.
try {
value = new Rational((String) value);
} catch (NumberFormatException e) {
// ignore
}
}
}
result = new DefaultSyntaxLeaf(value, isStringLiteral);
if (useGlobalSymbolTable && !(!_cacheNumericSymbols && result.getValue() instanceof Number)) {
if (isStringLiteral) {
_globalStringLiteralTable.put(inputValue, result);
} else {
_globalSymbolTable.put(inputValue, result);
}
}
}
return result;
}
use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class DefaultSyntaxLeaf method flushGlobalSymbolTable.
public static void flushGlobalSymbolTable() {
if (AICUtilConfiguration.isRecordCacheStatistics()) {
System.out.println("Global Symbol Table Cache Stats=" + _globalSymbolTable.stats());
}
// Causes relevant flags to be reset.
_useGlobalSymbolTable = ExpressoConfiguration.isUseGlobalSymbolTable();
_cacheNumericSymbols = ExpressoConfiguration.isGlobalSymbolTableToCacheNumerics();
if (_globalSymbolTable != null) {
_globalSymbolTable.invalidateAll();
}
if (_globalStringLiteralTable != null) {
_globalStringLiteralTable.invalidateAll();
}
_globalSymbolTable = newSymbolTable();
_globalStringLiteralTable = newSymbolTable();
// Add well known symbols to the table
// The Booleans.
_globalSymbolTable.put(true, SYMBOL_TRUE);
_globalSymbolTable.put(Boolean.TRUE, SYMBOL_TRUE);
_globalSymbolTable.put("true", SYMBOL_TRUE);
_globalSymbolTable.put(false, SYMBOL_FALSE);
_globalSymbolTable.put(Boolean.FALSE, SYMBOL_FALSE);
_globalSymbolTable.put("false", SYMBOL_FALSE);
// Common Numbers
_globalSymbolTable.put("0", SYMBOL_0);
_globalSymbolTable.put(new Integer(0), SYMBOL_0);
_globalSymbolTable.put(new Rational(0), SYMBOL_0);
_globalSymbolTable.put("1", SYMBOL_1);
_globalSymbolTable.put(new Integer(1), SYMBOL_1);
_globalSymbolTable.put(new Rational(1), SYMBOL_1);
_globalSymbolTable.put("2", SYMBOL_2);
_globalSymbolTable.put(new Integer(2), SYMBOL_2);
_globalSymbolTable.put(new Rational(2), SYMBOL_2);
_globalSymbolTable.put("3", SYMBOL_3);
_globalSymbolTable.put(new Integer(3), SYMBOL_3);
_globalSymbolTable.put(new Rational(3), SYMBOL_3);
_globalSymbolTable.put("4", SYMBOL_4);
_globalSymbolTable.put(new Integer(4), SYMBOL_4);
_globalSymbolTable.put(new Rational(4), SYMBOL_4);
_globalSymbolTable.put("5", SYMBOL_5);
_globalSymbolTable.put(new Integer(5), SYMBOL_5);
_globalSymbolTable.put(new Rational(5), SYMBOL_5);
_globalSymbolTable.put("6", SYMBOL_6);
_globalSymbolTable.put(new Integer(6), SYMBOL_6);
_globalSymbolTable.put(new Rational(6), SYMBOL_6);
_globalSymbolTable.put("7", SYMBOL_7);
_globalSymbolTable.put(new Integer(7), SYMBOL_7);
_globalSymbolTable.put(new Rational(7), SYMBOL_7);
_globalSymbolTable.put("8", SYMBOL_8);
_globalSymbolTable.put(new Integer(8), SYMBOL_8);
_globalSymbolTable.put(new Rational(8), SYMBOL_8);
_globalSymbolTable.put("9", SYMBOL_9);
_globalSymbolTable.put(new Integer(9), SYMBOL_9);
_globalSymbolTable.put(new Rational(9), SYMBOL_9);
}
use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class MeasureTest method testCategoricalTypeDomain.
@Test
public void testCategoricalTypeDomain() {
updateContextWithIndexAndType("N", new Categorical("People", 5, parse("p1"), parse("p2"), parse("p3"), parse("p4"), parse("p5")));
Assert.assertEquals(new Rational(5), measure("{{(on I in People) I : true}}"));
Assert.assertEquals(new Rational(4), measure("{{(on I in People) I : I != p1}}"));
Assert.assertEquals(new Rational(3), measure("{{(on I in People) I : I != p1 and I != p5}}"));
}
use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class MeasureTest method testTupleTypeDomain.
// (element_1, ..., element_n) = measure(element_1) * ... * measure(element_n)
@Test
public void testTupleTypeDomain() {
Assert.assertEquals(new Rational(6), measure("{{ (on T in (0..2 x Boolean)) T : true }}"));
Assert.assertEquals(new Rational(12), measure("{{ (on T in (0..2 x [3;7])) T : true }}"));
}
Aggregations