GraphTraversal
A GraphTraversal defines a strategy for traversing an AbstractGraph.
The two key components of GraphTraversal are a set of states and a set of
VisitFunctions, both of which the user
of this class must define. States are Strings that indicate the state of the
current traversal. VisitFunctions perform arbitrary operations given the
current node the GraphTraversal is visiting.
You can associate visitFunctions with states so that different VisitFunctions
are invoked when the traversal is in different states.
Consider the following example: You want to traverse from head towards tail
the nodes of host "a" until you find a node n that communicates with host
"b". After that, you want to traverse from n to head the nodes of host "b".
There might be two states: "on a" and "on b". The VisitFunctions for the two
states might be as follows:
'on a':
function(graphTraversal, node, state, data) {
if(node.getChildByHost('b') != null) {
graphTraversal.setCurrentNode(node.getChildByHost('b'));
graphTraversal.setState('on b');
}
else {
graphTraversal.setCurrentNode(node.getNext());
}
}
'on b':
function(graphTraversal, node, state, data) {
if(!node.isHead()) {
graphTraversal.setCurrentNode(node.getPrev());
}
}
Alternately, you could chose to only define a defaultVisitFunction and decide
what should be done based on the state variable
Constructor
new GraphTraversal()
Constructs a GraphTraversal
- Source:
Methods
end()
Ends the traversal. After this method is invoked, no more nodes will be
visited.
- Source:
getTrail(node) → {Array.<AbstractNode>}
Gets the "trail" of nodes, starting at the one provided. The trail is list of
nodes that were visited in order to get the the parameter.
Parameters:
- Source:
Returns:
-
Type
-
Array.<AbstractNode>
hasEnded() → {Boolean}
Determines if this traversal has ended
- Source:
Returns:
true is this traversal has ended
-
Type
-
Boolean
reset()
Resets the state of the traversal. Bound visit functions will not be touched.
- Source:
run() → {*}
"Runs" the traversal. The traversal will be continuously stepped though (i.e
with step()) until the traversal has ended. The value returned by the last
call to
step will be returned by this method.
- Source:
Returns:
The value returned by the last call to step, or null if step was
never invoked.
-
Type
-
*
setCurrentData(data)
Sets the current data. The current data is an object that will be passed to
the visit function the next time it's invoked. It can be used to pass
arbitrary data
Parameters:
Name |
Type |
Description |
data |
*
|
|
- Source:
setCurrentNode(node)
Sets the current node - the node that will be visited next.
Parameters:
- Source:
setDefaultVisitFunction(fn)
Sets the default visit function. The default visit function will be invoked
if there is no other visit function bound to the current state, OR if the
current state is null.
Parameters:
Name |
Type |
Description |
fn |
GraphTraversal~visitFunction
|
The visit function to set as default |
- Source:
setState(state)
Sets the current state
Parameters:
Name |
Type |
Description |
state |
String
|
|
- Source:
setVisitFunction(state, fn)
Sets the visit function for a state. The visit function will be invoked when
the GraphTraversal is in the given state. If there was previously a visit
function already defined for the state, it will be replaced.
Parameters:
Name |
Type |
Description |
state |
String
|
The state |
fn |
GraphTraversal~visitFunction
|
The visit function |
- Source:
step() → {*}
Executes a single step of the traversal. The correct
VisitFunction is fetched and is
executed. If there is a VisitFunction associated with the current state (i.e
via a previous call to setVisitFunction) and the current state is
not null, then that VisitFunction is invoked. Otherwise, the default visit
function is invoked. Whatever is returned by the VisitFunction will be
returned by this method
If this traversal has already ended, then this
method does nothing and returns null
- Source:
- See:
-
Returns:
The return value of the VisitFunction if one was executed, or
null otherwise
-
Type
-
*
(protected) stepInner() → {*}
This protected function is what actually executes the step.
GraphTraversal#step performs some validation and then calls this
function. Typically, classes extending this class will override this method
to specify what happens during each "step"
- Source:
- See:
-
Returns:
The return value of the VisitFunction if one was executed, or
null otherwise
-
Type
-
*
Type Definitions
VisitFunction(gt, node, state, data)
A VisitFunction is invoked each time the GraphTraversal encounters a new
node. Which VisitFunction is invoked depends on what state the GraphTraversal
is in.
Parameters:
Name |
Type |
Description |
gt |
GraphTraversal
|
The GraphTraversal object invoking the function |
node |
AbstractNode
|
The current node being visited |
state |
String
|
The current state |
data |
Object
|
The current data object. |
- Source:
- See:
-