Search in sources :

Example 6 with SyntaxTree

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

the class DefaultCompoundSyntaxTree method replaceSubTrees.

@Override
public SyntaxTree replaceSubTrees(Function<SyntaxTree, SyntaxTree> replacementFunction) {
    Object rootTreeReplacement = replacementFunction.apply(getRootTree());
    if (rootTreeReplacement == null) {
        rootTreeReplacement = getRootTree();
    }
    List<SyntaxTree> subTreeListReplacement = subTrees;
    for (int i = 0; i != subTrees.size(); i++) {
        SyntaxTree subTree = subTrees.get(i);
        SyntaxTree subTreeReplacement = null;
        if (subTree != null) {
            subTreeReplacement = replacementFunction.apply(subTree);
        }
        subTreeListReplacement = storeSubTreeReplacement(subTreeListReplacement, i, subTree, subTreeReplacement);
    }
    SyntaxTree result = makeReplacementIfAny(rootTreeReplacement, subTreeListReplacement);
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree)

Example 7 with SyntaxTree

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

the class DefaultCompoundSyntaxTree method setImmediateSubTree.

@Override
public SyntaxTree setImmediateSubTree(int i, Object newIthSubTreeObject) {
    SyntaxTree newRootTree = getRootTree();
    SyntaxTree newIthSubTree = SyntaxTrees.wrap(newIthSubTreeObject);
    List<SyntaxTree> newSubTrees = subTrees;
    // If we use {@link #getSubTrees()}, we get an unmodifiable list object instead of the original arguments list.
    if (i == -1) {
        newRootTree = newIthSubTree;
    } else {
        newSubTrees = storeSubTreeReplacement(newSubTrees, i, getSubTree(i), newIthSubTree);
    }
    SyntaxTree result = makeReplacementIfAny(newRootTree, newSubTrees);
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree)

Example 8 with SyntaxTree

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

the class DefaultCompoundSyntaxTree method replaceSubTrees.

@Override
public SyntaxTree replaceSubTrees(Function<SyntaxTree, SyntaxTree> replacementFunction, boolean onlyTheFirstOne, Predicate<SyntaxTree> prunePredicate, BinaryProcedure<SyntaxTree, SyntaxTree> listener, boolean ignoreTopExpression) {
    if (prunePredicate != null && prunePredicate.apply(this)) {
        return this;
    }
    if (!ignoreTopExpression) {
        SyntaxTree topReplacement = replacementFunction.apply(this);
        if (topReplacement != this && topReplacement != null) {
            if (listener != null) {
                listener.apply(this, topReplacement);
            }
            return topReplacement;
        }
    }
    SyntaxTree rootTreeReplacement = getRootTree().replaceSubTrees(replacementFunction, onlyTheFirstOne, prunePredicate, listener, false);
    List<SyntaxTree> argumentListReplacement = subTrees;
    if (rootTreeReplacement == null) {
        rootTreeReplacement = getRootTree();
    }
    if (!onlyTheFirstOne || rootTreeReplacement == getRootTree()) {
        for (int i = 0; i != subTrees.size(); i++) {
            SyntaxTree argument = subTrees.get(i);
            SyntaxTree argumentReplacement = argument == null ? null : argument.replaceSubTrees(replacementFunction, onlyTheFirstOne, prunePredicate, listener, false);
            argumentListReplacement = storeSubTreeReplacement(argumentListReplacement, i, argument, argumentReplacement);
            if (onlyTheFirstOne && argumentListReplacement != subTrees) {
                break;
            }
        }
    }
    SyntaxTree result = makeReplacementIfAny(rootTreeReplacement, argumentListReplacement);
    if (listener != null) {
        listener.apply(this, result);
    }
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree)

Example 9 with SyntaxTree

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

the class SyntaxTreeBasedSubExpressionAddress method replaceAtPath.

/**
 * Makes a new Expression based on a syntax tree, but with the sub-expression at the end of this {@link SyntaxTreeBasedSubExpressionAddress}'s path (from the ith step on)
 * (over syntax tree) equal to given subExpression (including a guarantee that it will be the same object instance).
 * The path is a list of indices indicating a path in the expression tree.
 * The path-i-sub-expression is the expression 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-expression is returned.
 * The method assumes the path describes an existing path-i-sub-expression.
 */
private Expression replaceAtPath(SyntaxTree syntaxTree, int i, Expression subExpression) {
    // This method is subtle; follow explanations below carefully.
    if (i != getPath().size()) {
        Object rootTreeOrExpression = syntaxTree.getRootTree();
        List<SyntaxTree> subTrees = syntaxTree.getImmediateSubTrees();
        Object[] subTreesOrSubExpressions = Arrays.copyOf(syntaxTree.getImmediateSubTrees().toArray(), subTrees.size());
        int index = getPath().get(i);
        // at this point, (rootTreeOrExpression, subTreesOrSubExpressions) contains only syntax trees, the sub-trees of syntaxTree.
        if (index == -1) {
            // replace the root tree
            rootTreeOrExpression = subExpression;
        } else {
            // replace a sub-tree
            Expression subExpressionAtI = replaceAtPath((SyntaxTree) subTreesOrSubExpressions[index], i + 1, subExpression);
            // by recursion, subExpressionAtI is guaranteed to be based on the sub-tree on index with subExpression instance placed at the end of path.
            subTreesOrSubExpressions[index] = subExpressionAtI;
        }
        // now (rootTreeOrExpression, subTreesOrSubExpressions) contains an Expression, and not only SyntaxTrees.
        Expression result = Expressions.makeExpressionOnSyntaxTreeWithLabelAndSubTrees(rootTreeOrExpression, subTreesOrSubExpressions);
        // This only holds for immediate sub-expressions, but the recursive class to replaceAtPath took care of the deeper ones.
        return result;
    }
    return subExpression;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) Expression(com.sri.ai.expresso.api.Expression)

Example 10 with SyntaxTree

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

the class SyntaxTrees method wrap.

/**
 * The array version of {@link #wrap(Object)}.
 */
public static List<SyntaxTree> wrap(Object[] array) {
    LinkedList<SyntaxTree> result = new LinkedList<SyntaxTree>();
    for (int i = 0; i != array.length; i++) {
        SyntaxTree wrap = wrap(array[i]);
        result.add(wrap);
    }
    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) LinkedList(java.util.LinkedList)

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