Skip to content

Backtrack Pattern

okram edited this page · 23 revisions

Many times its desirable to check out a particular path and if some criteria is met at the end of that path, then go back to the element from n-steps ago.

  • “What is the age of my friends who have friends who are older than 30 years old?”
g = TinkerGraphFactory.createTinkerGraph()

The query below says, in plain English: “What is the age of the people that know people that are 30+ years old?” The call to back(4) refers to the elements 4 steps ago that have paths up to the back(4) step (i.e. back to the V step).

gremlin> g.V.outE[[label:'knows']].inV{it.age > 30}.back(4).age
==>29

A more complicated example is provided over the Grateful Dead graph diagrammed in Defining a More Complex Property Graph.

g = new TinkerGraph()
GraphMLReader.inputGraph(g, new FileInputStream('data/graph-example-2.xml'))

The example query below states the following:

  • get the song with id 89 (Dark Star).
  • get all the songs that follow Dark Star in concert.
  • get the singers of those songs.
  • filter to only those songs that are sung by Jerry Garcia.
  • go back 4 steps to yield those songs that follow Dark Star and are sung by Jerry Garcia.
  • get the names of those songs that follow Dark Star and are sung by Jerry Garcia.
gremlin> g.v(89).outE[[label:'followed_by']].inV.outE[[label:'sung_by']].inV[[name:'Garcia']].back(4).name
==>EYES OF THE WORLD
==>SING ME BACK HOME
==>MORNING DEW
==>HES GONE
==>CHINA DOLL
==>WHARF RAT
==>BROKEDOWN PALACE
==>TERRAPIN STATION
==>DEAL
==>ATTICS OF MY LIFE
==>COMES A TIME
==>STELLA BLUE
==>BERTHA

In order to determine how many steps to go back, the GremlinPipeline.toString() can be handy for displaying all the steps in an expression.

gremlin> println g.v(89).outE[[label:'followed_by']].inV.outE[[label:'sung_by']].inV[[name:'Garcia']]     
[VertexEdgePipe<OUT_EDGES>, LabelFilterPipe<NOT_EQUAL,followed_by>, EdgeVertexPipe<IN_VERTEX>, VertexEdgePipe<OUT_EDGES>, LabelFilterPipe<NOT_EQUAL,sung_by>, EdgeVertexPipe<IN_VERTEX>, PropertyFilterPipe<name,NOT_EQUAL,Garcia>]
==>null

No, using the back step, notice how back(4) wraps 4 pipes prior to it. The name of the pipe in Pipes is FutureFilterPipe.

gremlin> println g.v(89).outE[[label:'followed_by']].inV.outE[[label:'sung_by']].inV[[name:'Garcia']].back(4).name
[VertexEdgePipe<OUT_EDGES>, LabelFilterPipe<NOT_EQUAL,followed_by>, EdgeVertexPipe<IN_VERTEX>, FutureFilterPipe[[VertexEdgePipe<OUT_EDGES>, LabelFilterPipe<NOT_EQUAL,sung_by>, EdgeVertexPipe<IN_VERTEX>, PropertyFilterPipe<name,NOT_EQUAL,Garcia>]], PropertyPipe<name>]
==>null
Something went wrong with that request. Please try again.