Skip to content

Tree Pattern

okram edited this page Mar 14, 2012 · 11 revisions

The path that a traverser takes through a graph can be see as a tree where the root of the tree is the source of the traversal and the branches are the various paths emanating from that root that is respective of the provided path description. Gremlin provides the ability to collect this traversal tree information with the tree-step.

Creating a Tree Data Structure

The example to follow makes use of the toy TinkerPop graph diagrammed here.

gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]

The tree-step makes use of the path information of the step to construct an internal embedded map. This map is the tree representation of the traversal. If closures are provided, then the closures are evaluated in a round-robin fashion against the objects of the path.

gremlin> g.v(1).out.out.tree.cap
==>{v[1]={v[4]={v[3]={}, v[5]={}}}}
gremlin> g.v(1).out.out.tree{it.name}.cap
==>{marko={josh={lop={}, ripple={}}}}

The tree data structure can be walked using Groovy map notation. Note that the leaves of the tree are those maps for which the key yields an empty map.

gremlin> tree = g.v(1).out.out.tree{it.name}.cap.next()
==>marko={josh={lop={}, ripple={}}}
gremlin> tree.marko.josh
==>lop={}
==>ripple={}

Looping and Range Filters

It is possible to make use of the tree-step during a loop. For the following examples, the Grateful Dead graph will be used.

gremlin> g = new TinkerGraph()
==>tinkergraph[vertices:0 edges:0]
gremlin> g.loadGraphML('data/graph-example-2.xml')
==>null

As can be seen, it is possible to use loop to construct the tree. Moreover, by using a range filter (e.g. [0..100]) it is possible to limit the amount of paths to store in the tree data structure.

gremlin> g.v(89).out.loop(1){it.loops < 3}[0..100].tree{it.name}.cap.next()
==>DARK STAR={EYES OF THE WORLD={CHINA DOLL={}, DANCIN IN THE STREETS={}, STELLA BLUE={}, LOST SAILOR={}, PLAYING IN THE BAND={}, UNBROKEN CHAIN={}, BLACK PETER={}, BIG RIVER={}, BOX OF RAIN={}, THE MUSIC NEVER STOPPED={}, THROWING STONES={}, LOOKS LIKE RAIN={}, WHY DONT WE DO IT IN THE ROAD={}, LET ME SING YOUR BLUES AWAY={}, ESTIMATED PROPHET={}, MORNING DEW={}, HES GONE={}, CORINA={}, WOMEN ARE SMARTER={}, WALKING BLUES={}, ONE MORE SATURDAY NIGHT={}, WHARF RAT={}, WEATHER REPORT SUITE={}, GOING DOWN THE ROAD FEELING BAD={}, DONT NEED LOVE={}, TRUCKING={}, SAMSON AND DELILAH={}, DRUMS={}, PROMISED LAND={}, FIRE ON THE MOUNTAIN={}, I WILL TAKE YOU HOME={}, Garcia={}, LET IT GROW={}, PICASSO MOON={}, THE OTHER ONE={}, SUGAR MAGNOLIA={}, SAMBA IN THE RAIN={}, FOOLISH HEART={}, ME AND MY UNCLE={}, Hunter={}, SHIP OF FOOLS={}, I NEED A MIRACLE={}, NOT FADE AWAY={}, SAINT OF CIRCUMSTANCE={}, LONG WAY TO GO HOME={}, ETERNITY={}, IT MUST HAVE BEEN THE ROSES={}, MAYBE YOU KNOW HOW I FEEL={}, GOOD TIME BLUES={}, DARK STAR={}}, TRUCKING={FRANKLINS TOWER={}, GOOD LOVING={}, BERTHA={}, NEW MINGLEWOOD BLUES={}, GIMME SOME LOVIN={}, STELLA BLUE={}, ROLLIN AND TUMBLIN={}, SUGAREE={}, EYES OF THE WORLD={}, JACK STRAW={}, PLAYING IN THE BAND={}, SPOONFUL={}, NEW SPEEDWAY BOOGIE={}, SMOKESTACK LIGHTNING={}, CRAZY FINGERS={}, THROWING STONES={}, DEAL={}, AROUND AND AROUND={}, HEAVEN HELP THE FOOL={}, SPANISH JAM={}, MORNING DEW={}, HES GONE={}, IKO IKO={}, ONE MORE SATURDAY NIGHT={}, I JUST WANNA MAKE LOVE TO YOU={}, TOUCH OF GREY={}, WEATHER REPORT SUITE={}, GOING DOWN THE ROAD FEELING BAD={}, TERRAPIN STATION={}, NOBODYS FAULT BUT MINE={}, ALABAMA GETAWAY={}, COMES A TIME={}, SCARLET BEGONIAS={}, DRUMS={}, LAZY RIVER={}, THAT WOULD BE SOMETHING={}, GOOD MORNING LITTLE SCHOOL GIRL={}, LET IT GROW={}, BIRD SONG={}, SHAKEDOWN STREET={}, SUGAR MAGNOLIA={}, THE OTHER ONE={}, SAMBA IN THE RAIN={}, CHINA CAT SUNFLOWER={}, I NEED A MIRACLE={}, RAMBLE ON ROSE={}, WANG DANG DOODLE={}, NOT FADE AWAY={}, OTHER ONE JAM={}, ROW JIMMY={}, THE ELEVEN={}}}