Search in sources :

Example 1 with SyntaxLeaf

use of com.sri.ai.expresso.api.SyntaxLeaf 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 2 with SyntaxLeaf

use of com.sri.ai.expresso.api.SyntaxLeaf in project aic-expresso by aic-sri-international.

the class DefaultCompoundSyntaxTree method toStringWithoutCaching.

@Override
public String toStringWithoutCaching() {
    String rootTreeString = getRootTree().toString();
    if (!(getRootTree() instanceof SyntaxLeaf)) {
        rootTreeString = "(" + rootTreeString + ")";
    }
    Iterator stringOfSubTrees = new FunctionIterator<SyntaxTree, String>(new ToStringWithoutCaching(), getImmediateSubTrees());
    return rootTreeString + "(" + Util.join(", ", stringOfSubTrees) + ")";
}
Also used : SyntaxLeaf(com.sri.ai.expresso.api.SyntaxLeaf) FunctionIterator(com.sri.ai.util.collect.FunctionIterator) Iterator(java.util.Iterator) NestedIterator(com.sri.ai.util.collect.NestedIterator) FunctionIterator(com.sri.ai.util.collect.FunctionIterator)

Example 3 with SyntaxLeaf

use of com.sri.ai.expresso.api.SyntaxLeaf in project aic-expresso by aic-sri-international.

the class DefaultSyntaxLeaf method compareTo.

@Override
public /**
	 * Compares this Symbol to other syntax trees, placing it before {@link CompoundSyntaxTree}s and comparing
	 * it to other Symbols by comparing their values if they are in the same type (as returned by {@link #getValueType()},
	 * or their types if they are of different types.
	 */
int compareTo(Object another) {
    if (this == another) {
        return 0;
    }
    if (another instanceof CompoundSyntaxTree) {
        // Symbols come first
        return -1;
    }
    SyntaxLeaf anotherSymbol = null;
    if (another instanceof SyntaxLeaf) {
        anotherSymbol = (SyntaxLeaf) another;
    } else {
        anotherSymbol = createSyntaxLeaf(another, false);
        // Test again, as may have had self returned from the symbol table.
        if (this == anotherSymbol) {
            return 0;
        }
    }
    try {
        @SuppressWarnings("unchecked") Comparable<Object> value = (Comparable<Object>) getValue();
        @SuppressWarnings("unchecked") Comparable<Object> anotherValue = (Comparable<Object>) anotherSymbol.getValue();
        int result;
        String valueType = getValueType();
        String anotherValueType = anotherSymbol.getValueType();
        if (valueType.equals(anotherValueType)) {
            result = value.compareTo(anotherValue);
        } else {
            result = valueType.compareTo(anotherValueType);
        }
        return result;
    } catch (ClassCastException e) {
        throw new Error("Using DefaultSymbol.compareTo method with non-comparable values.", e);
    }
}
Also used : SyntaxLeaf(com.sri.ai.expresso.api.SyntaxLeaf) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree)

Example 4 with SyntaxLeaf

use of com.sri.ai.expresso.api.SyntaxLeaf in project aic-expresso by aic-sri-international.

the class Expressions method makeFromSyntaxTree.

/**
	 * Returns an expression represented by a given syntax tree.
	 * Scheduled to be removed once expressions are no longer based on syntax trees.
	 */
@Deprecated
public static Expression makeFromSyntaxTree(SyntaxTree syntaxTree) {
    if (syntaxTree instanceof CompoundSyntaxTree) {
        Expression result = makeExpressionOnSyntaxTreeWithLabelAndSubTrees(syntaxTree.getLabel(), syntaxTree.getImmediateSubTrees().toArray());
        //			Expression result = new ExpressionOnCompoundSyntaxTree(syntaxTree);
        return result;
    }
    if (syntaxTree instanceof SyntaxLeaf) {
        SyntaxLeaf syntaxLeaf = (SyntaxLeaf) syntaxTree;
        Expression result;
        if (syntaxLeaf.isStringLiteral()) {
            result = Expressions.makeStringLiteral((String) syntaxLeaf.getValue());
        } else {
            result = Expressions.makeSymbol(syntaxLeaf.getValue());
        }
        //			Expression result = new ExpressionOnSyntaxLeaf(syntaxTree);
        return result;
    }
    throw new Error("Syntax tree " + syntaxTree + " should be either a CompoundSyntaxTree or a Symbol");
}
Also used : SyntaxLeaf(com.sri.ai.expresso.api.SyntaxLeaf) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree)

Aggregations

SyntaxLeaf (com.sri.ai.expresso.api.SyntaxLeaf)4 CompoundSyntaxTree (com.sri.ai.expresso.api.CompoundSyntaxTree)2 Expression (com.sri.ai.expresso.api.Expression)2 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)1 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)1 FunctionIterator (com.sri.ai.util.collect.FunctionIterator)1 NestedIterator (com.sri.ai.util.collect.NestedIterator)1 Rational (com.sri.ai.util.math.Rational)1 Iterator (java.util.Iterator)1