GRAPH TRAVERSER

In a data query, you can use the GRAPH TRAVERSER operator to process recursive operations on each element of the input stream. It is suitable to navigate through a graph tree of data.

To process your graph of data the way you want, you must define a graph of node processors.

A node processor first receives, and then applies treatments to a node, to finally forward the current node children to its successor node processor.

Node processors call each other recursively to traverse the whole graph until the GRAPH TRAVERSER has processed all nodes, or a stop condition has been reached.

This page discusses:

See Also
Expression Language
Defining Data Queries

General Parameters

Parameter Description
Label

Specify the label to display below the operator type in the data query.

Description

Add a description to display as tooltip on hover over the operator icon.

Tip: It is useful to explain what the operator does, especially when you share the data query with other users.
Operator input variable Specify the name to give to the variable that contains the input data. The variable is then available when you define expressions.

Traversal Parameter

The GRAPH TRAVERSER operator performs a DFS (Depth-First Search) graph traversal. It always processes the children of a given node before its siblings.

Parameter Description
Max depth Defines the maximum distance between the graph traverser root and the last graph node level. If this max depth is crossed, the query stops to prevent the JVM from failing. By default, it cannot explore graphs with a depth higher than 512 nodes.

Variables

You can define context variables to store the current state of your graph traversal. Node processors can access and update these variables. To define a variable, specify its static type and initialization value.

There are 3 kinds of variables to answer the different kind of use cases. The differences between these 3 variable types are the visibility, and the number of instances.

Variable type Description
Global There is only one instance of this variable for the whole graph traversal. This instance is visible by everybody. When a node processor updates the value, the new value is available to all node processors.

Common use case: count the number of processed nodes during the whole graph traversal.

Path When a node processor updates a Path variable, it copies its new value for each child. This way, the changes made on a Path variable are only visible to a node child.
Note: When a node processor defines the value of a Path variable using a Context updates action, the rest of the node processing and all the succeeding node processors will read this value until the next value update.

Common use case: For each node, you want to store the path, that is the succession of nodes, from the root that led to this node.

Node The operator creates an instance of this variable anytime it processes a new node. The second time the operator processes a node, it can fetch the value specified for this variable during the first processing.

Common use case: Count the number of times a node has been processed during the graph traversal.

Processors

A processor treats a single type of node forwarded by its predecessor. To specify which processor receives the element from the input stream, define an initial processor in your configuration.

A processor applies the following treatments in this order:

Action Description
Context updates You can define context variables to store the current state of your traversal. Node processors can update and access these variables. For more information, see Variables.
Emit predicate Expression returning a Boolean. This expression decides whether to call the Emit expression for the current node or not.
Emit expression Defines what the GRAPH TRAVERSER operator returns. All emit expressions of all processors must return the same type.
Stop node condition Expression returning a Boolean. This expression decides whether to call a Transition for the current node or not.
Transition Forwards elements to the successor node processor. For more information, see the following section.

Transition

A transition is a function that provides elements to forward to the next processor. There are several types of transitions.

Transition Description
Explicit Nodes Transition Expects an expression providing children to the successor node processor referenced by To. You can use this kind of transition for most use cases.
Search Transition Searches items directly from the index (similar to SEARCH operator). Expects an item class, an index unit, and a predicate. It provides matching items to the successor node processor referenced by To.
Conditional Transition Advanced transition type that targets 2 processors. It determines the processor to apply at runtime using the Switch predicate. If the Switch predicate is:
  • true, it uses the main Transition,
  • false, it uses the Fallback transition

Example

In this example, the graph of data is a social network indexed with the following data model:

class User {
    name: String;
    friends: List<User>;
}

We want to compute all paths that can lead from UserA to UserD, UserA being the root, and UserD the end condition. The social network looks like this:

graph TD
A[UserA] -->|friends with| B[UserB]
A[UserA] -->|friends with| C[UserC]
C[UserC] -->|friends with| D[UserD]
B[UserB] -->|friends with| D[UserD]

The correct output for this use case is [UserA, UserC, UserD] and [UserA, UserB, UserD].

To do this, we must configure the GRAPH TRAVERSER as follows:

graph TD
A[user] -->|user->friends| A[user]

We need:

  • A single node processor called user treating nodes of type: Item<User>
  • A Path variable to collect graph paths from UserA to UserD called path of type: List<Item<User>>.

As the initial processor is user, the configuration of the node processor is:

  • Context update: path = [...path, user]
  • Emit predicate: user == UserD
  • Emit expression: path
  • Explicit node transition: user->friends targeting To: user

In this example, relationships are oriented and UserD has no children, so the graph traversal stops, because there are no more node to process.

In a more realistic model, relationships are not oriented and you can find cycles in the graph. For example, UserD can be friend with UserA. You can handle this kind of use case with:

  • A Node variable: visited: Boolean = false that flags whether a node has been processed or not.
  • If you add the Stop node condition to user, the graph traversal stops on each path if the current node has already been processed. There is no risk of infinite loop on cycles.