Salt
3.3.6-SNAPSHOT
A powerful, tagset-independent and theory-neutral meta model and API for storing, manipulating, and representing nearly all types of linguistic data .
|
When working with Salt, it is often necessary
For all of these purposes, we offer the generic possibility to traverse a graph object (e.g. a SCorpusGraph or a SDocumentGraph object).
A traversal can be done in two directions: top-down and bottom-up. When using the top-down method, the traversal will follow the relation direction (e.g. a -> b>: node a will be visited first, followed by node b). When using the bottom-up method, the traversal will follow the inverse of the relation direction (e.g. a -> b: node b will be visited before node a).
Additionally, you can choose the order in which nodes will be traversed. For this, we provide two modes: depth-first and breadth-first. In depth-first mode, the sub-graph of node b will be traversed, before its siblings will be visited. Imagine the following tree structure:
In this case, node d will be visited after a,b,c have been visited. In breadth-first mode, the order of the traversal is the other way around, i.e. the nodes a,b,d will be visited before node c.
To define the behavior of a traversal, we provide these types, which are combinations of the direction and the order of a traversal:
The traversal mechanism uses a callback, therefore you need a class implementing the interface GraphTraverseHandler. This interface declares the following three methods, which need to be implemented:
When the traversal reaches a new node, the method checkConstraint(...) is called. It checks whether the following nodes and their sub-graphs should be processed any further. When this method returns true, the method nodeReached(...) is called next. Before a node is left, the method nodeLeft(...) is called. Note that the order of the method invocations depends on the traversal type used.
The following example shows the order of calls for the sample graph given in the following figure.
Here, we list the callbacks in correct order in case of a depth-first traversal. We assume that the called object returns true for the method checkConstraint(...) on all nodes except node span1. In the case of node span1, the checkConstraint(...) method returns false. Therefore, no nodeReached(...) and nodeLeft(...) method will be called for this node. Note, that the list of parameters of the functions presented here is bigger than shown, e.g. the traversing policy and the relation via which the node has been reached is given, too.
To start the traversal, use the following method, available in any object derived from SGraph (e.g. SCorpusGraph or SDocumentGraph):
or
Additionally, you can set a flag to protect the traversal engine from running in cycles. Per default, this flag is set to false and your traverseHandler has to deal with cyclic graphs itself.
To traverse our sample, you can use the following snippet:
To learn how to persist and load a Salt model, please read the article Persist and Load.