getting path for a saved intermediate node in tinkerpop

Viewed 34

I have a graph which contains a hierarchal structure where the lowest nodes of the hierarchy contain many relations from it.

You can create the graph structure with the following:

n1 = g.addV('level1').property('name', 'n1').next()
n2 = g.addV('level2').property('name', 'n2').next()
n3 = g.addV('level2').property('name', 'n3').next()
n4 = g.addV('level3').property('val', 'n4').next()
n5 = g.addV('level3').property('val', 'n5').next()

g.addE('contains').from(n1).to(n2).next()
g.addE('contains').from(n1).to(n3).next()
g.addE('contains').from(n2).to(n4).next()
g.addE('contains').from(n3).to(n5).next()

n6 = g.addV('intermediate').property('name', 'n6').next()
n7 = g.addV('final').property('name', 'n7').next()
g.addE('partof').from(n6).to(n4).next()
g.addE('isa').from(n6).to(n7).next()

n8 = g.addV('intermediate').property('name', 'n8').next()
n9 = g.addV('final').property('name', 'n9').next()
g.addE('partof').from(n8).to(n4).next()
g.addE('isa').from(n8).to(n9).next()

n10 = g.addV('intermediate').property('name', 'n10').next()
n11 = g.addV('final').property('name', 'n11').next()
g.addE('partof').from(n10).to(n5).next()
g.addE('isa').from(n10).to(n11).next()

n12 = g.addV('intermediate').property('name', 'n12').next()
n13 = g.addV('final').property('name', 'n13').next()
g.addE('partof').from(n12).to(n5).next()
g.addE('isa').from(n12).to(n13).next()

I next used a query to try to get the path to the "level3" node and then continued on to get a list of "final" nodes and project that so that I get back a table of "taxonomy", which contains the path to level3 and the name of the "final" node.

g.V().hasLabel('level1').until(hasLabel('level3')).repeat(out('contains')).as('a').
    in('partof').hasLabel('intermediate').out('isa').hasLabel('final').
    project('taxonomy', 'enzyme').
        by(select('a').path().by(__.label())).
        by(values('name'))
==>[taxonomy:[level1,level2,level3,intermediate,final,level3],enzyme:n7]
==>[taxonomy:[level1,level2,level3,intermediate,final,level3],enzyme:n9]
==>[taxonomy:[level1,level2,level3,intermediate,final,level3],enzyme:n13]
==>[taxonomy:[level1,level2,level3,intermediate,final,level3],enzyme:n11]

In the query above I save the "level3" node as 'a' and then in the project step I select it and get the path. However the path seems to contain extra elements, it includes the "intermediate" and "final" node types before going back to level3 again. What I expected:

==>[taxonomy:[level1,level2,level3],enzyme:n7]
==>[taxonomy:[level1,level2,level3],enzyme:n9]
==>[taxonomy:[level1,level2,level3],enzyme:n13]
==>[taxonomy:[level1,level2,level3],enzyme:n11]

Is this the expected behaviour of the path step? Is There a way that I can reformulate this query to get my expected result?

1 Answers

You can use a from step or a to step to modify where a path starts from.

gremlin>  

g.V().hasLabel('level1').until(hasLabel('level3')).repeat(out('contains')).as('a').
......1>     in('partof').hasLabel('intermediate').out('isa').hasLabel('final').
......2>     project('taxonomy', 'enzyme').
......3>         by(path().from('a').by(__.label())).
......4>         by(values('name'))  

==>[taxonomy:[level3,intermediate,final],enzyme:n7]
==>[taxonomy:[level3,intermediate,final],enzyme:n9]
==>[taxonomy:[level3,intermediate,final],enzyme:n13]
==>[taxonomy:[level3,intermediate,final],enzyme:n11]

Here is the query with from replaced by to.

gremlin>  g.V().hasLabel('level1').until(hasLabel('level3')).repeat(out('contains')).as('a').
......1>     in('partof').hasLabel('intermediate').out('isa').hasLabel('final').
......2>     project('taxonomy', 'enzyme').
......3>         by(path().to('a').by(__.label())).
......4>         by(values('name'))  
==>[taxonomy:[level1,level2,level3],enzyme:n7]
==>[taxonomy:[level1,level2,level3],enzyme:n9]
==>[taxonomy:[level1,level2,level3],enzyme:n13]
==>[taxonomy:[level1,level2,level3],enzyme:n11]
gremlin> 
Related