gremlintinkerpopgremlinpython

direct edge traversal omitting paths


I am running into curious problem.

enter image description here

In graph above,I can't run repeat/until due to memory overload issue and vertices names changing at various level. I am trying direct edge traversal through each step.

However, I see that the query is only showing paths with max traversal depth.

Graph:

g.addV(‘color-group’).property(id,1).property(single, ‘Color’, ‘primary’).next()
    g.addV(‘primary-color’).property(id,2).property(single, ‘Color’, ‘red’).next()
    g.addV(‘primary-color’).property(id,3).property(single, ‘Color’, ‘blue’).next()
    g.addV(‘primary-color’).property(id,4).property(single, ‘Color’, ‘yellow’).next()
    g.addV(‘secondary-color’).property(id,5).property(single, ‘Color’, ‘red-10’).next()
    
    g.V(1).addE('links').to(g.V(2)).next()
    g.V(1).addE('links').to(g.V(3)).next()
    g.V(1).addE('links').to(g.V(4)).next()
    g.V(4).addE('links').to(g.V(5)).next()
    

Query:

>>> g.V().hasLabel('color-group').has('Color', 'primary').as_('color_group').outE().inV().outE().inV().path().next()

Result:

 path[v[1], e[40][1-links->4], v[4], e[41][4-links->5], v[5]] 

so above its skipping paths from primary -> blue and primary -> yellow and only showing paths from primary -> red -> red-10.

I need all of the paths (without using repeat/until).

Any ideas?
Thanks.


Solution

  • The reason you do not get all of the paths is because only one of them satisfies the "2-hop" requirement. For this specific case you could use optional, but for paths of unknown depth this might become complex.

    In the Gremlin Console using this version of the data (modified so that it loads on the 3.6.2 level of Gremlin and with the quotes fixed) ...

    g.addV('color-group').property(id,1).property(single, 'Color', 'primary')
    g.addV('primary-color').property(id,2).property(single, 'Color', 'red')
    g.addV('primary-color').property(id,3).property(single, 'Color', 'blue')
    g.addV('primary-color').property(id,4).property(single, 'Color', 'yellow')
    g.addV('secondary-color').property(id,5).property(single, 'Color', 'red-10')
    g.V(1).addE('links').to(V(2))
    g.V(1).addE('links').to(V(3))
    g.V(1).addE('links').to(V(4))
    g.V(4).addE('links').to(V(5)) 
    

    The query can be re-written as:

    g.V().hasLabel('color-group').has('Color', 'primary').as_('color_group').
          outE().inV().
          optional(outE().inV()).
          path()
    

    When run:

    gremlin> g.V().hasLabel('color-group').has('Color', 'primary').
    ......1>       outE().inV().
    ......2>       optional(outE().inV()).
    ......3>       path()   
    
    ==>[v[1],e[12][1-links->2],v[2]]
    ==>[v[1],e[13][1-links->3],v[3]]
    ==>[v[1],e[14][1-links->4],v[4],e[15][4-links->5],v[5]] 
    

    With properties and labels for the nodes and edges:

    gremlin> g.V().hasLabel('color-group').has('Color', 'primary').
    ......1>       outE().inV().
    ......2>       optional(outE().inV()).
    ......3>       path().
    ......4>         by('Color').
    ......5>         by(label) 
    
    ==>[primary,links,red]
    ==>[primary,links,blue]
    ==>[primary,links,yellow,links,red-10]   
    

    Side note: The sample graph does not quite match the diagram. These results are correct for the sample data provided.