Class TreeUtils
java.lang.Object
org.aksw.commons.collections.trees.TreeUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> intchildIndexOf(TreeOps2<T> ops, T node) static <T> Map<T, com.google.common.collect.Multimap<T, T>> clusterNodesByFirstMultiaryAncestor(Tree<T> tree, com.google.common.collect.Multimap<T, T> mapping) Input: A mapping from cache nodes to candidate query nodes represented as a Multimapinvalid input: '<'T, T>.static <T> longstatic <T> longstatic <A,B> com.google.common.collect.Multimap <A, B> deriveParentMapping(Tree<A> aTree, Tree<B> bTree, com.google.common.collect.Multimap<A, B> childMapping) Given a mapping of child nodes, determine which parents may be mapped to each other.static <T> TfindAncestor(Tree<T> tree, T node, Predicate<T> predicate) Find the first ancestor for which the predicate evaluates to truestatic <T> TfindAncestor(T node, Function<? super T, ? extends T> getParent, Predicate<? super T> predicate) static <T> Tstatic <T> TfindSuccessor(T item, Function<? super T, ? extends T> successor, BiPredicate<? super T, ? super T> predicate) static <T> TfirstMultiaryAncestor(Tree<T> tree, T node) Return a node's first ancestor having an arity > 1 null if there is none.static <T> TgetFirstMultiaryAncestor(Tree<T> tree, T node) static <T> voidgetLeafs(Collection<T> result, Tree<T> tree, T node) static <T> List<T> static <T> Set<T> getParentsOf(Tree<T> tree, Iterable<T> children) Get the set of immediate parents for a given set of childrenstatic <T> Tstatic <X> List<X> getUnaryAncestors(X x, Tree<X> tree, Tree<X> multiaryTree) static <T> com.google.common.collect.Multimap<T, T> groupByParent(Tree<T> tree, Collection<T> nodes, com.google.common.collect.Multimap<T, T> result) static <T> intindexOf(List<T> list, T find, BiPredicate<? super T, ? super T> isEquiv) innerNodesPerLevel(Tree<T> tree) For each level, yield the inner nodes The root node will always be part of the list, even if it does not have childrenstatic <T> Stream<T> inOrderSearch(T node, Function<T, ? extends Iterable<T>> parentToChildren) inOrderSearch(T node, Function<T, ? extends Iterable<T>> parentToChildren, Function<? super T, V> nodeToValue, BiPredicate<T, V> doDescend) In-order-search starting from the given node and descending into the tree.static <T> Stream<T> leafStream(Tree<T> tree) static <T> longnodesPerLevel(Tree<T> tree) Returns the set of nodes in each level of the tree The set containing the root will be the first item in the liststatic <T> voidstatic <T> Map<T, T> Traverse an op structure and create a map from each subOp to its immediate parent NOTE It must be ensured that common sub expressions are different objects, since we are using an identity hash map for mapping children to parentsstatic <T> Set<T> propagateBottomUpLabel(Tree<T> tree, Predicate<T> predicate) Given a predicate, return the minimum set of nodes, for which all nodes in their subtree satisfy the predicate.static <T> Tree<T> remapSubTreesToLeafs(Tree<T> tree, Function<? super T, ? extends T> remapFn) Create a new tree object which has certain nodes remapped with *leaf* nodesstatic <T> Tree<T> removeUnaryNodes(Tree<T> tree) static <T> TremoveUnaryNodes(Tree<T> tree, T node, com.google.common.collect.ListMultimap<T, T> parentToChildren) static <T> Tree<T> static <T> TreplaceNode(Tree<T> tree, T node, T replacement, BiPredicate<? super T, ? super T> isEquiv) static <T> Stream<T> streamAncestors(T path, Function<? super T, ? extends T> getParent) Return all ancestors of the given item, including itself.static <T> Tsubstitute(T node, boolean descendIntoSubst, Function<? super T, ? extends Collection<? extends T>> getChildrenFn, Function<? super T, ? extends T> transformFn, BiFunction<T, List<T>, T> copyFn) static <T> Tsubstitute(T node, boolean descendIntoSubst, TreeOps2<T> ops, Function<? super T, ? extends T> transformFn) static <T> Tree<T>
-
Constructor Details
-
TreeUtils
public TreeUtils()
-
-
Method Details
-
streamAncestors
-
getRoot
-
findSuccessor
public static <T> T findSuccessor(T item, Function<? super T, ? extends T> successor, BiPredicate<? super T, ? super T> predicate) - Parameters:
item-successor-predicate- Receives the current item and its successor- Returns:
-
findAncestor
-
findRoot
-
groupByParent
public static <T> com.google.common.collect.Multimap<T,T> groupByParent(Tree<T> tree, Collection<T> nodes, com.google.common.collect.Multimap<T, T> result) -
propagateBottomUpLabel
Given a predicate, return the minimum set of nodes, for which all nodes in their subtree satisfy the predicate. The algo starts with the set X of leaf nodes satisfying the predicate, and moves upwards. If all children of a parent satisfy the predicate, the children are removed from X and the parent is added instead. Note: Uses IdentityHashSet- Parameters:
tree-predicate-- Returns:
-
subTree
-
nodeCount
-
depth
-
depth
-
childIndexOf
-
replace
-
indexOf
-
replaceNode
public static <T> T replaceNode(Tree<T> tree, T node, T replacement, BiPredicate<? super T, ? super T> isEquiv) -
substitute
-
substitute
public static <T> T substitute(T node, boolean descendIntoSubst, Function<? super T, ? extends Collection<? extends T>> getChildrenFn, Function<? super T, ? extends T> transformFn, BiFunction<T, List<T>, T> copyFn) -
findAncestor
-
inOrderSearch
-
inOrderSearch
public static <T,V> Stream<Map.Entry<T,V>> inOrderSearch(T node, Function<T, ? extends Iterable<T>> parentToChildren, Function<? super T, V> nodeToValue, BiPredicate<T, V> doDescend) In-order-search starting from the given node and descending into the tree. Each node may be mapped to a value. A predicate determines whether to stop descending further into a sub-tree. Useful for extracting patterns from a tree.- Parameters:
node-parentToChildren-nodeToValue-doDescend-- Returns:
-
innerNodesPerLevel
-
nodesPerLevel
-
leafStream
-
getLeafs
-
getLeafs
-
getParentsOf
-
parentMap
Traverse an op structure and create a map from each subOp to its immediate parent NOTE It must be ensured that common sub expressions are different objects, since we are using an identity hash map for mapping children to parents- Parameters:
op-- Returns:
-
parentMap
-
remapSubTreesToLeafs
-
removeUnaryNodes
-
removeUnaryNodes
public static <T> T removeUnaryNodes(Tree<T> tree, T node, com.google.common.collect.ListMultimap<T, T> parentToChildren) -
clusterNodesByFirstMultiaryAncestor
public static <T> Map<T, com.google.common.collect.Multimap<T,T>> clusterNodesByFirstMultiaryAncestor(Tree<T> tree, com.google.common.collect.Multimap<T, T> mapping) Input: A mapping from cache nodes to candidate query nodes represented as a Multimapinvalid input: '<'T, T>. Output: The mapping partitioned by each node's first multiary ancestor. Output could also be: Multimapinvalid input: '<'Op, Op> - fmaToNodesCache For every cacheFma, map to the corresponding queryFmas - and for each of these mappings yield the candidate node mappings of the children Multimapinvalid input: '<'OpCacheFma, Mapinvalid input: '<'OpQueryFma, Multimapinvalid input: '<'OpCache, OpQuery>>> Q: What if cache nodes do not have a fma? A: In this case the fma would be null, which means that there can only be a single cache node which would be grouped with a null fma. In the query, we can then check whether we are pairing a union with another union or null. We always map from cache to query. Mapinvalid input: '<'CacheFma, QueryFma> So the challenge is now again how to represent all the facts and how to perform the permutations / combinations...- Parameters:
tree-cacheTree-cacheToQueryCands-- Returns:
-
getFirstMultiaryAncestor
-
firstMultiaryAncestor
Return a node's first ancestor having an arity > 1 null if there is none.- Parameters:
tree-node-- Returns:
-
getUnaryAncestors
-
deriveParentMapping
public static <A,B> com.google.common.collect.Multimap<A,B> deriveParentMapping(Tree<A> aTree, Tree<B> bTree, com.google.common.collect.Multimap<A, B> childMapping) Given a mapping of child nodes, determine which parents may be mapped to each other. For any nodes mapped in 'childMapping', their parents may be mapped as well.- Parameters:
aTree-bTree-childMapping-- Returns:
-