Declarative Traversals

DSE Version: 6.0


Gremlin declarative traversals are graph pattern matching traversals. They specify a graph pattern that should be matched against a graph. In this unit, you will learn about declarative traversals.


Hey everyone, I am Denise Gosnell and this is declarative traversals.

Declarative traversals require you to think differently! Instead of trying to instruct a traverser how to compute a result, we declare how the result should look like and let the traverser find a solution.

Gremlin declarative traversals are graph pattern matching traversals. They specify a graph pattern that should be matched against a graph. If you already know SPARQL, a graph query language for RDF (Resource Description Framework) data, you should be comfortable with graph pattern matching in Gremlin, too.

Let’s visualize this.

The gremlin outside of the graph is thinking of the triangular pattern of those three vertices. That is a pattern we will define with the gremlin language and the actual traversers in the graph are finding that pattern.

As with any programming language, a declarative approach describes an outcome that we want -- a thing that we want to happen. It doesn’t give us the steps on how to get there. In other words, declarative traversals focus on what to compute, rather than how to compute it.

We only need to introduce one new step for declarative traversals. It is the match step. It has three different forms that we can use it in. IN all three cases, we provide patterns we would like it to match.

In the first case, we provide named variables we provide named variables at the beginning and at the end of the traversal.

In the second case, we ony provide a named variable at the beginning of the traversal.

And in the last case, there are no declared variables.

Notice that declarative traversals use the as step modulator to declare variables that have to be bound to some values in a solution. We used the as step previously with projecting traversals to define step labels. In declarative traversals, the terminology is different: we say the as step creates variables instead of step labels.

Let’s take a look at some examples.

A reminder as always – of our KillrVideo movie schema.

Movies are related to people. And those people are those responsible for creating the movie. Users rate movies and know eacher, and movies belong to genres.

Let us start with the example of: directors who appear in their own movies. We have solved it elsewhere, but we are going to solve this same query with a pattern matching traversal.

We are going to provide two different patterns. Each pattern starts with a gremlin anonymous traversal. We are going to specific the pattern after that.  Also, notice that we are using the first of the 3 pattern forms where we have a declared variable at the beginning and the end of the declared pattern.

How we are using those declared variables is one of the most important pieces of this example. Looking at each of the patterns, we are using the same variables “m” and “p”. When we use the same name for a declared variable, that will bind to the same value. So, we are defining two patterns, but they will apply to the same movies and the same people.

So the first pattern is looking for some movie with an outgoing actor edge to some person. This find actors in some movie.

The second pattern looks for an outgoing director edge to a person. But because m and p are the same declared variables in both patterns, they are going to be constrained to be the same movies and the same people. In other words, we are constrained to directors who acted in their movies.

We are going to select the people, remove the duplicates, and randomly sample 3.  And, for any movie buffs out there, we found three expected results: clint eastwood, Charles chaplin and quentin tarantino

Two things to note about the match step before moving to another example.

First, the database engine can evaluate traversal patterns in any order to compute the same result. So if your application requires deterministic results, you will need to apply an order to the result stream.

Secondly, and you may have already noticed, At the end of this traversal, the select and by are used just like in projecting traversals to return desired information.

Now, let’s look for directors who have appeared in their own western movies. We will follow the same process as before, but this time we need to bring in the genre vertices.

We are going to follow the same style of match by using declarative variables at the beginning and end of each pattern, and we are going to again use the same variable names as we did in the previous example.

The first two patterns here repeat the pattern from last time: finding direcors who acted in their own movies. The third pattern here now requires that those movies be western movies. To do that, we look for the pattern “belongsTo” to traverse to the genre vertices and apply a filter to only consider the western genre.

As before, we select the people who match this pattern, remove duplicates, and sample three. This time, alongside Clint Eastwood and Quentin Tarantino, we find John Wayne.

Adding onto this example, let’s add a little bit more complexity. This time, let’s introduce a new pattern with the not step. We are going to look for directors who acted in their own movies, which were comedies this time, and have never participated in any movie that has an average rating of less than 8.0.

We start off the same. We have m which has an outgoing actor edge to p. M also has an outgoing director edge to p. So these are directors who have acted in their own films.

Next, m has a belongs to edge whose name is comedy. So this belongs to the comedy genres.

On the next line, we begin with the not step. So we are going to negate what we are about to traverse. But first, we have to compute the average rating for the movie. This time, we want to consider any movie the actor/director has participated in. So we start from P, go to any movie via all in edges. Next, we want to compute the movie’s average rating. This introduces the need for the local step. We have to use local here so that calculate the average for that movie, instead of the average ratings for all movies. To do this, tell the traversers to use a local scope to forces the evaluation of elements within the current object / traverser. Without local, the traverser would assume global scope and we would end up calculating the average ratings for all movies, instead of just the ratings observed for this single movie.

From this movie, and within the individual traverser’s local scope, we have 4 remaining steps:

1.     We walk out the “Rating” edge,

2.     Next, we access the ratings by calling values (“rating”),

3.     We use the mean step to calculate the average.

4.     Lastly, we only want that traverser to pass if the average rating for the movie is less than 8

Now, remember – that is all inside the not. So, if that  we are going to negate this part of the pattern matching step. So, with this final pattern, we are limiting the results to only those directors who

1.     appeared in their own Comedy movies and

2.     have never participated in a movie that has an average rating of less than 8.0.

Let’s take a look at these newly introduced gremlin steps.

The not step Passes an incoming traverser onto the next step if the internal traversal yields no results.

The and step Passes an incoming traverser to the next step if every internal traversal yields a result.

The or step Passes an incoming traverser to the next step if at least one internal traversal yields a result.

These steps, while frequently used with declarative traversals, are not limited to the match step. They can be used with imperative traversals, as well.

Let’s put these to practice and consider another example.

Now, let’s Find:

·    actors who appeared in 10+ movies

·    and acted in The Matrix or acted in Die Hard

·    and the actor’s name is not Alan Rickman.

Using the match step, we break that down into separate patterns. This example demonstrates the use of and, or, and not.  To do this, let’s break it down into each step.

In the first pattern, we look for all actors who acted in 10 movies.

The next pattern will find those actors from the matrix or die hard, but not alan rickman. To do this, we need to build up a series of or statements.

The first statement we want to evaluate is if an actor has been in the matrix. To do that, we want to traverse from people, going through the actor edge, and only consider movies named the matrix.

We want that statement to be true, OR, we want to find people from the movie Die hard.

In our last example, we want to Find directors who directed 5+ Adventure movies; find their Adventure movies rated 8.0+.

Finally, this example may seem very complex. If so, take your time to study it.

It demonstrates that one can mix declarative and imperative traversals as needed.

Two things to note about this example before we dig in:

  1. Variables in different match-steps are not related because each match-step is stateless.

  2. Mixing different approaches to querying is not always a good idea as readability may be affected. Use carefully.

Now it is your turn to give this a try. Give this next exercise a go and get your hands dirty with declarative traversals.  

No write up.
No Exercises.
No FAQs.
No resources.
Comments are closed.