Search in sources :

Example 16 with SyntaxTree

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

the class ExtensionalIndexExpressionsSet method getSubSyntaxTree.

@Override
public SyntaxTree getSubSyntaxTree() {
    List<SyntaxTree> indexExpressionsSyntaxTrees = mapIntoArrayList(getList(), Expression::getSyntaxTree);
    SyntaxTree result = SyntaxTrees.makeKleeneListIfNeeded(indexExpressionsSyntaxTrees);
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) Expression(com.sri.ai.expresso.api.Expression)

Example 17 with SyntaxTree

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

the class SyntaxTrees method parse.

/**
 * Parse a string into a syntax tree by parsing it as an expression using {@link AntlrGrinderParserWrapper}
 * returning its syntax tree.
 */
public static SyntaxTree parse(String string) {
    Expression expression = parser.parse(string);
    SyntaxTree result = expression.getSyntaxTree();
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree) DefaultCompoundSyntaxTree(com.sri.ai.expresso.core.DefaultCompoundSyntaxTree) Expression(com.sri.ai.expresso.api.Expression)

Example 18 with SyntaxTree

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

the class SyntaxTrees method replaceAtPath.

/**
 * Given an syntax tree, a path, a position i in the path, and a sub-syntax tree,
 * returns the syntax tree with its path-i-sub-syntax tree replaced by the given sub-syntax tree.
 * The path is a list of indices indicating a path in the syntax tree.
 * The path-i-sub-syntax tree is the syntax tree obtained by following the path from the position i on.
 * If there are no indices to be followed (i is equal to the path's length), the sub-syntax tree is returned.
 * The method assumes the path describes an existing path-i-sub-syntax tree.
 */
private static SyntaxTree replaceAtPath(SyntaxTree syntaxTree, List<Integer> path, int i, SyntaxTree subTree) {
    if (i != path.size()) {
        int index = path.get(i);
        SyntaxTree subTreeAtI = replaceAtPath(syntaxTree.getSubTree(index), path, i + 1, subTree);
        // does need to be sub tree
        SyntaxTree result = syntaxTree.setImmediateSubTree(index, subTreeAtI);
        return result;
    }
    return subTree;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree) DefaultCompoundSyntaxTree(com.sri.ai.expresso.core.DefaultCompoundSyntaxTree)

Example 19 with SyntaxTree

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

the class DefaultCompoundSyntaxTree method compareTo.

@Override
public /**
 * Compares this CompoundSyntaxTree to other syntax trees, placing it after {@link Symbol}s and comparing
 * it lexicographically to other CompoundSyntaxTree, using both the label and sub trees.
 */
int compareTo(Object anotherObject) {
    int result;
    if (anotherObject instanceof CompoundSyntaxTree) {
        CompoundSyntaxTree normalizedAnotherCompoundSyntaxTree = (CompoundSyntaxTree) anotherObject;
        Iterator<SyntaxTree> subTreesIncludingLabel = getImmediateSubTreesIncludingRootOneIterator();
        Iterator<SyntaxTree> anotherSubTreesIncludingLabel = normalizedAnotherCompoundSyntaxTree.getImmediateSubTreesIncludingRootOneIterator();
        result = lexicographicComparison.compare(subTreesIncludingLabel, anotherSubTreesIncludingLabel);
    } else {
        // Symbols "come first" in the default order
        result = 1;
    }
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree)

Example 20 with SyntaxTree

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

the class DefaultCompoundSyntaxTree method hashCode.

@Override
public int hashCode() {
    if (hashCode == -1) {
        SyntaxTree rootTree = getRootTree();
        int rootHashCode = rootTree.hashCode();
        List<SyntaxTree> immediateSubTrees = getImmediateSubTrees();
        int subTreesHashCode = immediateSubTrees.hashCode();
        hashCode = rootHashCode + subTreesHashCode;
    }
    return hashCode;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree)

Aggregations

SyntaxTree (com.sri.ai.expresso.api.SyntaxTree)23 CompoundSyntaxTree (com.sri.ai.expresso.api.CompoundSyntaxTree)9 Expression (com.sri.ai.expresso.api.Expression)9 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)4 DefaultCompoundSyntaxTree (com.sri.ai.expresso.core.DefaultCompoundSyntaxTree)3 LinkedList (java.util.LinkedList)3 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)2 SyntaxTrees.makeCompoundSyntaxTree (com.sri.ai.expresso.helper.SyntaxTrees.makeCompoundSyntaxTree)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)1 Util.mapIntoList (com.sri.ai.util.Util.mapIntoList)1 Test (org.junit.Test)1