首页 文章

Gremlin图遍历使用先前的边属性值来过滤后边

提问于
浏览
6

在图遍历中,我只想考虑具有属性的边,该边等于遍历中前一步中访问的边之一的属性 .

我发现http://tinkerpop.apache.org/docs/current/recipes/#traversal-induced-values但这看起来只适用于单个对象,在我的情况下,我需要在遍历时更改值 . 例如,从具有出站边缘(E1,E2,E3 ......)的V1开始,我想要将E1遍历到V2,然后沿着V2的任何边缘遍历,其中edge.property(x)== E1.property(x ),并对V1(E2,E3,...)中的所有边做同样的事情

我找不到任何支持在Gremlin中执行此操作的文档,是否可能?

1 回答

  • 6

    我们可以使用TinkerPop附带的现代玩具图:

    gremlin> graph = TinkerFactory.createModern()
    ==>tinkergraph[vertices:6 edges:6]
    gremlin> g = graph.traversal()
    ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
    

    此图已包含6条边,其中两条边 weight1.0

    gremlin> g.E().valueMap()
    ==>[weight:0.5]
    ==>[weight:1.0]
    ==>[weight:0.4]
    ==>[weight:1.0]
    ==>[weight:0.4]
    ==>[weight:0.2]
    

    我们使用 weight1.0 这两个边中的一个来获得具有相同 weight 的另一个边 . 通过遍历这两个边中的第一个,我们降落在一个有两个外边的顶点:

    gremlin> g.V(1).outE().has('weight',1.0).inV().outE()
    ==>e[10][4-created->5]
    ==>e[11][4-created->3]
    gremlin> g.V(1).outE().has('weight',1.0).inV().outE().valueMap()
    ==>[weight:1.0]
    ==>[weight:0.4]
    

    其中一个边缘是另一个边缘,重量为 1.0 . 所以,我们只需要根据第一条边的权重过滤这些边:

    gremlin> g.V(1).outE().has('weight',1.0).
                    as('firstEdge'). // save the first edge
                    inV().outE().
                    where(eq('firstEdge')). // compare with the first edge
                    by('weight') // use only the 'weight' property for the equality check
    ==>e[10][4-created->5]
    

相关问题