Class AbstractTreeVisitor<N extends ITreeNode,R>
- java.lang.Object
-
- org.apache.iotdb.commons.schema.tree.AbstractTreeVisitor<N,R>
-
- Type Parameters:
N- The node consisting the tree.R- The result extracted from the tree.
- All Implemented Interfaces:
java.lang.AutoCloseable,java.util.Iterator<R>
public abstract class AbstractTreeVisitor<N extends ITreeNode,R> extends java.lang.Object implements java.util.Iterator<R>, java.lang.AutoCloseableThis class defines a dfs-based algorithm of tree-traversing with path pattern match, and support iterating each element of the result.This class takes three basis parameters as input:
- N root: the root node of the tree to be traversed.
- PartialPath patPattern: the pattern of path that the path of target element matches
- boolean isPrefixMatch: whether the pathPattern is used for matching the prefix; if so, all elements with path starting with the matched prefix will be collected
If any tree wants to integrate and use this class. The following steps must be attained:
- The node of the tree must implement ITreeNode interface and the generic N should be defined as the node class.
- The result type R should be defined.
- Implement the abstract methods, and for the concrete requirements, please refer to the javadoc of specific method.
-
-
Field Summary
Fields Modifier and Type Field Description protected IPatternFApatternFAprotected Nroot
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractTreeVisitor()protectedAbstractTreeVisitor(N root, PartialPath pathPattern, boolean isPrefixMatch)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract booleanacceptFullMatchedNode(N node)Only accepted nodes will be considered for hasNext() and next()protected abstract booleanacceptInternalMatchedNode(N node)Only accepted nodes will be considered for hasNext() and next()voidclose()protected abstract RgenerateResult(N nextMatchedNode)The method used for generating the result based on the matched node.protected NgetAncestorNodeByLevel(int level)protected abstract NgetChild(N parent, java.lang.String childName)protected abstract java.util.Iterator<N>getChildrenIterator(N parent)java.lang.ThrowablegetFailure()protected java.lang.String[]getFullPathFromRootToNode(N node)Get full path from root to node.protected intgetLevelOfNextMatchedNode()Get level from root to NextMatchedNode.protected NgetParentOfNextMatchedNode()protected PartialPathgetParentPartialPath()Get full path of parent of current node.protected PartialPathgetPartialPathFromRootToNode(N node)Get partial path from root to node.protected intgetSizeOfAncestor()booleanhasNext()protected voidinitStack()This method must be invoked before iterationbooleanisSuccess()protected abstract booleanmayTargetNodeType(N node)May node can be accepted if it reaches final state.Rnext()protected voidreleaseNode(N node)protected voidreleaseNodeIterator(java.util.Iterator<N> nodeIterator)voidreset()protected voidsetFailure(java.lang.Throwable e)protected abstract booleanshouldVisitSubtreeOfFullMatchedNode(N node)Full-match means the node matches the last node name of the given path pattern.protected abstract booleanshouldVisitSubtreeOfInternalMatchedNode(N node)Internal-match means the node matches an internal node name of the given path pattern.protected IFAStatetryGetNextState(N node, IFAState sourceState, java.util.Map<java.lang.String,IFATransition> preciseMatchTransitionMap)protected IFAStatetryGetNextState(N node, IFAState sourceState, IFATransition transition)
-
-
-
Field Detail
-
patternFA
protected final IPatternFA patternFA
-
-
Constructor Detail
-
AbstractTreeVisitor
protected AbstractTreeVisitor()
-
AbstractTreeVisitor
protected AbstractTreeVisitor(N root, PartialPath pathPattern, boolean isPrefixMatch)
-
-
Method Detail
-
initStack
protected final void initStack()
This method must be invoked before iteration
-
reset
public void reset()
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
hasNext
public boolean hasNext()
-
getParentPartialPath
protected PartialPath getParentPartialPath()
Get full path of parent of current node. This method should be used in acceptInternalMatchedNode(N), acceptFullMatchedNode(N),shouldVisitSubtreeOfInternalMatchedNode(N) or shouldVisitSubtreeOfFullMatchedNode(N).- Returns:
- full path from traverse start node to the parent of current node
-
getPartialPathFromRootToNode
protected final PartialPath getPartialPathFromRootToNode(N node)
Get partial path from root to node.- Parameters:
node- node must be concluded in ancestorStack or nextMatchedNode- Returns:
- partial path from traverse start node to the specified node
-
getFullPathFromRootToNode
protected final java.lang.String[] getFullPathFromRootToNode(N node)
Get full path from root to node.- Parameters:
node- node must be concluded in ancestorStack or nextMatchedNode- Returns:
- full path from traverse start node to the specified node
-
getAncestorNodeByLevel
protected final N getAncestorNodeByLevel(int level)
-
getParentOfNextMatchedNode
protected final N getParentOfNextMatchedNode()
-
getLevelOfNextMatchedNode
protected final int getLevelOfNextMatchedNode()
Get level from root to NextMatchedNode. Level of root is 0. For example, root.sg.d1.s1, NextMatchedNode is s1, then return 3.- Returns:
- level from root to NextMatchedNode
-
getSizeOfAncestor
protected final int getSizeOfAncestor()
-
setFailure
protected void setFailure(java.lang.Throwable e)
-
getFailure
public java.lang.Throwable getFailure()
-
isSuccess
public boolean isSuccess()
-
getChild
protected abstract N getChild(N parent, java.lang.String childName) throws java.lang.Exception
- Throws:
java.lang.Exception
-
getChildrenIterator
protected abstract java.util.Iterator<N> getChildrenIterator(N parent) throws java.lang.Exception
- Throws:
java.lang.Exception
-
releaseNode
protected void releaseNode(N node)
-
releaseNodeIterator
protected void releaseNodeIterator(java.util.Iterator<N> nodeIterator)
-
shouldVisitSubtreeOfInternalMatchedNode
protected abstract boolean shouldVisitSubtreeOfInternalMatchedNode(N node)
Internal-match means the node matches an internal node name of the given path pattern. root.sg internal match root.sg.**(pattern). This method should be implemented according to concrete tasks.Return whether the subtree of given node should be processed. If return true, the traversing process will keep traversing the subtree. If return false, the traversing process will skip the subtree of given node.
-
shouldVisitSubtreeOfFullMatchedNode
protected abstract boolean shouldVisitSubtreeOfFullMatchedNode(N node)
Full-match means the node matches the last node name of the given path pattern. root.sg.d full match root.sg.**(pattern) This method should be implemented according to concrete tasks.Return whether the subtree of given node should be processed. If return true, the traversing process will keep traversing the subtree. If return false, the traversing process will skip the subtree of given node.
-
acceptInternalMatchedNode
protected abstract boolean acceptInternalMatchedNode(N node)
Only accepted nodes will be considered for hasNext() and next()
-
acceptFullMatchedNode
protected abstract boolean acceptFullMatchedNode(N node)
Only accepted nodes will be considered for hasNext() and next()
-
generateResult
protected abstract R generateResult(N nextMatchedNode)
The method used for generating the result based on the matched node.
-
tryGetNextState
protected IFAState tryGetNextState(N node, IFAState sourceState, java.util.Map<java.lang.String,IFATransition> preciseMatchTransitionMap)
-
tryGetNextState
protected IFAState tryGetNextState(N node, IFAState sourceState, IFATransition transition)
-
mayTargetNodeType
protected abstract boolean mayTargetNodeType(N node)
May node can be accepted if it reaches final state. Its implementation should not depend on the context.- Parameters:
node- node to be checked- Returns:
- false is if node must not be accepted. Otherwise, return true.
-
-