【问题标题】:How do I write Gremlin queries for following patterns?如何为以下模式编写 Gremlin 查询?
【发布时间】:2015-07-29 04:17:01
【问题描述】:

我有一些微型 Gremlin 有向图,其中每个顶点都有两个属性“类型”和“文本”。 “text”属性的值只是英文文本,而“type”属性可以有一个从这个集合中选择的值:

NP, PP, VP, ADVP, ADJP, SBAR, PRT, INTJ, O

这些图中的所有边都具有相同的标签:“下一个”。

我希望能够选择具有以下节点模式的图形:

1) [text=","] --> type="VP" --> type="ADVP" --> type="NP"
2) type="NP" --> [text="," Upto 3 nodes with any text and type text=","] --> type="VP" --> [text=":" OR "that"]

括号中的模式元素表示它是可选的。

因此,对于第一个模式,我需要选择具有文本“,”节点的图形,可选地,后跟“VP”类型的节点,然后是“ADVP”,然后是“NP”。

对于第二种模式,我需要选择节点类型为“NP”的图形,然后是一个可选的节点序列,从一个带有文本“,”的节点开始,然后是最多 3 个带有任何文本和类型的节点,然后是带有文本“,”的节点。此可选序列之后是“VP”类型的节点,最后是带有文本“:”或“that”的节点。

与第一个模式匹配的两个示例图是:

以下是与第二种模式匹配的示例图:

我了解基本的 Gremlin 遍历,但我不确定如何处理上述模式的可选元素。

有没有办法在 Gremlin 中为此类模式编写查询?如果没有,您能否建议一种基于非 Gremlin 的方法来创建此类图表并查询它们?

【问题讨论】:

    标签: graph gremlin tinkerpop tinkerpop-blueprint


    【解决方案1】:

    从 TinkerPop 3.0 开始,您可以在 Gremlin 中进行模式匹配。您将使用Match Step 来完成您的任务。我已经编写了 Gremlin 来作为您的第一个要求的示例。也许这会激发您为第二个需求开发遍历。

    我生成了一些数据如下:

    gremlin> graph = TinkerGraph.open()
    ==>tinkergraph[vertices:0 edges:0]
    gremlin> g = graph.traversal()
    ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
    gremlin> v1=g.addV(id, 1, "type", "o", "text", ",").next()
    ==>v[1]
    gremlin> v2=g.withSideEffect('x',v1).addV(id, 2, "type", "vp", "text", "a").addInE('next','x').inV().next()
    ==>v[2]
    gremlin> v3=g.withSideEffect('x',v2).addV(id, 3, "type", "advp", "text", "b").addInE('next','x').inV().next()
    ==>v[3]
    gremlin> g.withSideEffect('x',v3).addV(id, 4, "type", "np", "text", "c").addInE('next','x').inV().next()
    ==>v[4]
    gremlin> 
    gremlin> v5=g.addV(id, 5, "type", "vp", "text", "a").next()
    ==>v[5]
    gremlin> v6=g.withSideEffect('x',v5).addV(id, 6, "type", "advp", "text", "b").addInE('next','x').inV().next()
    ==>v[6]
    gremlin> g.withSideEffect('x',v6).addV(id, 7, "type", "np", "text", "c").addInE('next','x').inV().next()
    ==>v[7]
    gremlin> 
    gremlin> v8=g.addV(id, 8, "type", "vp", "text", "a").next()
    ==>v[8]
    gremlin> v9=g.withSideEffect('x',v8).addV(id, 9, "type", "o", "text", ",").addInE('next','x').inV().next()
    ==>v[9]
    gremlin> g.withSideEffect('x',v9).addV(id, 10, "type", "np", "text", "c").addInE('next','x').inV().next()
    ==>v[10]
    

    然后进行匹配遍历:

    gremlin> g.V().has('type','vp').match(__.as('vp').coalesce(__().in().has('text',','),constant("optional")).as('o'),
    gremlin>                              __.as('vp').out().has('type','advp').as('advp'),
    gremlin>                              __.as('advp').out().has('type','np').as('np')).select('o','vp','advp','np')
    ==>[o:v[1], vp:v[2], advp:v[3], np:v[4]]
    ==>[o:optional, vp:v[5], advp:v[6], np:v[7]]
    

    【讨论】:

      猜你喜欢
      • 2021-07-23
      • 2021-05-01
      • 2014-03-18
      • 2020-04-10
      • 2019-04-03
      • 1970-01-01
      • 2015-11-15
      • 2021-07-01
      • 1970-01-01
      相关资源
      最近更新 更多