Traversal of the Gremlin graph, which uses the previous value of the edge property to filter subsequent edges

When traversing the graph, I only want to consider edges that have a property equal to the property of one of the edges visited in the previous step of the traversal.

I found http://tinkerpop.apache.org/docs/current/recipes/#traversal-induced-values , but it seems to work for only one object, in my case I need to change the value while passing. For example, starting with V1, which has outgoing edges (E1, E2, E3 ...), I want to cross E1 to V2, and then cross any edge from V2, where edge.property (x) == E1.property ( x) and do the same for all edges from V1 (E2, E3, ...)

I can not find documentation that supports the way to do this in Gremlin, is this possible?

+6
source share
1 answer

Ok, first create an example graph:

gremlin> v1 = graph.addVertex('name', 'v1') ==>v[0] gremlin> v2 = graph.addVertex('name', 'v2') ==>v[2] gremlin> v3 = graph.addVertex('name', 'v3') ==>v[4] gremlin> v4 = graph.addVertex('name', 'v4') ==>v[6] gremlin> v1.addEdge('knows', v2, 'property1', 10) ==>e[8][0-knows->2] gremlin> v1.addEdge('knows', v2, 'property1', 20) ==>e[9][0-knows->2] gremlin> v2.addEdge('knows', v3, 'property1', 10) ==>e[10][2-knows->4] gremlin> v2.addEdge('knows', v4, 'property1', 50) ==>e[11][2-knows->6] 

Now we have a simple graph, where two edges go from v1 to v2 with the values ​​of properties 10 and 20. v2 has two outgoing edges, of which one edge has the value of property 10. This is the edge to which the query should go, and the resulting vertex is v3.

One way to do this with match() -step :

 gV(v1).outE().match( __.as('e1').values('property1').as('p1'), __.as('e1').inV().as('v2'), __.as('v2').outE().as('e2'), __.as('e2').values('property1').as('p2'). where('p1', eq('p2'))). select('e2').inV().values('name') ==>v3 

Full example in GremlinBin: http://gremlinbin.com/bin/view/5809c1fc0626b

+5
source

Source: https://habr.com/ru/post/1011530/


All Articles