Package nzilbb.ag.util
Class LayerTraversal<R>
- java.lang.Object
-
- nzilbb.ag.util.LayerTraversal<R>
-
public class LayerTraversal<R> extends Object
TraversesAnnotation
s in aGraph
using the Layer hierarchy (i.e. Annotations are nodes, parent/child linkes are edges).This base class handles the traversal. The pre-order and post-order operations (the actual work being done) can be implemented by subclassing to implement
pre(Annotation)
andpost(Annotation)
.For example, to print a list of annotations in hierarchy order:
LayerTraversal<StringBuffer> t = new LayerTraversal<StringBuffer>(new StringBuffer(), graph) { protected void pre(Annotation annotation) { result.append(annotation.getLabel() + "\n"); } }; System.out.println(t.getResult().toString());
The main traversal uses only the layer hierarchy defined in the graph. This means that if the graph contains annotations for which no layer is defined, those annotations will not be visited by this traversal. After traversing the layer hierarchy, the annotations that have not been visited are processed by
except(Annotation)
- provide an implementation for this method if these annotations must also be processed.- Author:
- Robert Fromont robert@fromont.net.nz
-
-
Constructor Summary
Constructors Constructor Description LayerTraversal()
Default constructor.LayerTraversal(R result)
Constructor with starting result.LayerTraversal(R result, Annotation annotation)
Constructor with starting result and annotation to immediately traverse.LayerTraversal(R result, Graph graph)
Constructor with starting result and graph to immediately traverse.LayerTraversal(R result, Graph graph, boolean breadthFirst)
Constructor with starting result and graph to immediately traverse.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
getBreadthFirst()
Getter forbreadthFirst
: Whether the traversal is breadth-first (true) or depth-first (false - the default).Graph
getGraph()
Getter forgraph
: The graph to be traversed.R
getResult()
Getter forresult
: The result of the traversal, if required.LayerTraversal<R>
setBreadthFirst(boolean newBreadthFirst)
Setter forbreadthFirst
: Whether the traversal is breadth-first (true) or depth-first (false - the default).LayerTraversal<R>
setGraph(Graph newGraph)
Setter forgraph
: The graph to be traversed.LayerTraversal<R>
setResult(R newResult)
Setter forresult
: The result of the traversal, if required.R
traverseAnnotation(Annotation annotation)
Depth-first recursive method that traverses the layer annotations using the layer hierarchy, callingpre(Annotation)
, then calling itself for all children, then calledpost(Annotation)
, before returning.R
traverseGraph(Graph graph)
Traverses the given graph.
-
-
-
Constructor Detail
-
LayerTraversal
public LayerTraversal()
Default constructor.
-
LayerTraversal
public LayerTraversal(R result)
Constructor with starting result.- Parameters:
result
- The result of the traversal.
-
LayerTraversal
public LayerTraversal(R result, Graph graph)
Constructor with starting result and graph to immediately traverse. Traversal will be depth-first.- Parameters:
result
- The result of the traversal.graph
- The graph to be traversed.
-
LayerTraversal
public LayerTraversal(R result, Annotation annotation)
Constructor with starting result and annotation to immediately traverse. Traversal will be depth-first.- Parameters:
result
- The result of the traversal.annotation
- The annotation to be traversed.
-
LayerTraversal
public LayerTraversal(R result, Graph graph, boolean breadthFirst)
Constructor with starting result and graph to immediately traverse.- Parameters:
result
- The result of the traversal.graph
- The graph to be traversed.breadthFirst
- Whether the traversal is breadth-first (true) or depth-first (false).
-
-
Method Detail
-
getGraph
public Graph getGraph()
Getter forgraph
: The graph to be traversed.- Returns:
- The graph to be traversed.
-
setGraph
public LayerTraversal<R> setGraph(Graph newGraph)
Setter forgraph
: The graph to be traversed.- Parameters:
newGraph
- The graph to be traversed.
-
getResult
public R getResult()
Getter forresult
: The result of the traversal, if required.- Returns:
- The result of the traversal, if required.
-
setResult
public LayerTraversal<R> setResult(R newResult)
Setter forresult
: The result of the traversal, if required.- Parameters:
newResult
- The result of the traversal, if required.
-
getBreadthFirst
public boolean getBreadthFirst()
Getter forbreadthFirst
: Whether the traversal is breadth-first (true) or depth-first (false - the default).- Returns:
- Whether the traversal is breadth-first (true) or depth-first (false - the default).
-
setBreadthFirst
public LayerTraversal<R> setBreadthFirst(boolean newBreadthFirst)
Setter forbreadthFirst
: Whether the traversal is breadth-first (true) or depth-first (false - the default).- Parameters:
newBreadthFirst
- Whether the traversal is breadth-first (true) or depth-first (false - the default).
-
traverseGraph
public R traverseGraph(Graph graph)
Traverses the given graph.- Parameters:
graph
- The graph to traverse.- Returns:
- Some result of the traversal, if required.
-
traverseAnnotation
public R traverseAnnotation(Annotation annotation)
Depth-first recursive method that traverses the layer annotations using the layer hierarchy, callingpre(Annotation)
, then calling itself for all children, then calledpost(Annotation)
, before returning.- Parameters:
annotation
- The annotation to traverse through.- Returns:
- The results - i.e.
getResult()
- See Also:
breadthFirst
-
-