Search in sources :

Example 6 with Rational

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);
}
Also used : Rational(com.sri.ai.util.math.Rational)

Example 7 with Rational

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;
}
Also used : SyntaxLeaf(com.sri.ai.expresso.api.SyntaxLeaf) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression)

Example 8 with Rational

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);
}
Also used : Rational(com.sri.ai.util.math.Rational)

Example 9 with Rational

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}}"));
}
Also used : Rational(com.sri.ai.util.math.Rational) Categorical(com.sri.ai.expresso.type.Categorical) Test(org.junit.Test)

Example 10 with Rational

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 }}"));
}
Also used : Rational(com.sri.ai.util.math.Rational) Test(org.junit.Test)

Aggregations

Rational (com.sri.ai.util.math.Rational)72 Expression (com.sri.ai.expresso.api.Expression)33 Test (org.junit.Test)26 Monomial (com.sri.ai.grinder.polynomial.api.Monomial)11 ArrayList (java.util.ArrayList)10 Type (com.sri.ai.expresso.api.Type)9 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)8 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)7 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)6 RealInterval (com.sri.ai.expresso.type.RealInterval)6 List (java.util.List)6 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)5 Symbol (com.sri.ai.expresso.api.Symbol)4 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)4 Categorical (com.sri.ai.expresso.type.Categorical)4 FunctionType (com.sri.ai.expresso.type.FunctionType)4 TupleType (com.sri.ai.expresso.type.TupleType)4 Polynomial (com.sri.ai.grinder.polynomial.api.Polynomial)4 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)3 HashMap (java.util.HashMap)3