Class AbstractTreeVisitor<N extends ITreeNode,​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.AutoCloseable
    This 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:

    1. N root: the root node of the tree to be traversed.
    2. PartialPath patPattern: the pattern of path that the path of target element matches
    3. 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:

    1. The node of the tree must implement ITreeNode interface and the generic N should be defined as the node class.
    2. The result type R should be defined.
    3. Implement the abstract methods, and for the concrete requirements, please refer to the javadoc of specific method.
    • 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:
        close in interface java.lang.AutoCloseable
      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<N extends ITreeNode>
      • next

        public R next()
        Specified by:
        next in interface java.util.Iterator<N extends ITreeNode>
      • 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)
      • 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.