【问题标题】:How stop Repeat when first results are found in Gremlin TinkerPop Graph在 Gremlin TinkerPop 图表中找到第一个结果时如何停止重复
【发布时间】:2023-01-15 08:50:48
【问题描述】:

当找到第一个结果时,如何使用 Repeat 停止向下遍历图形? 我曾尝试使用聚合变量,但 until 步骤似乎忽略了它。

硒演示 https://gremlify.com/2gr4460avv1

我无法让这条线在 until 步骤中工作:

sack().is(within('level'))

例如。使用下图:

g.addV('User 0').as('0').
  property(single, 'name', 'v0').
  addV('User 1').as('1').
  property(single, 'name', 'v1').
  addV('User 2').as('2').
  property(single, 'name', 'v2').
  addV('User 3').as('3').
  property(single, 'name', 'v3').
  addV('User 4').as('4').
  property(single, 'name', 'v3').
  addV('Subject 1').as('5').
  property(single, 'name', 's1').
  addE('Trust').from('0').to('1').property("distance", 1).
  addE('Trust').from('0').to('2').property("distance", 1).
  addE('Trust').from('0').to('3').property("distance", 1).
  addE('Trust').from('1').to('2').property("distance", 1).
  addE('Trust').from('1').to('4').property("distance", 1).
  addE('Audit').from('2').to('5').property("distance", 0).
  addE('Trust').from('2').to('3').property("distance", 1).
  addE('Audit').from('3').to('5').property("distance", 0).
  addE('Trust').from('3').to('0').property("distance", 1).
  addE('Audit').from('4').to('2').property("distance", 0)

询问:

g.withSack(0).V().has("name", "v0").
    repeat(outE("Trust", "Audit").as("e").sack(sum).by('distance').
        inV().simplePath().
        choose(
            select("e").hasLabel("Trust"),
            choose(
                select("e").has("distance", gte(0)),
                    aggregate('allow'),
                    aggregate('distrust')
            )
        )
    ).
    until( 
        has('name', 's1').
        or().
        sack().is(gte(3)).
        or().
        sack().is(within('level')).
        or().
        where(within('distrust'))
        
    ).
    has('name', 's1').
    sack(sum).by(constant(1)).sack().aggregate('level').
    path()

结果:

==>[v[0],e[13][0-Trust->4],v[4],e[17][4-Audit->10],v[10],2]
==>[v[0],e[14][0-Trust->6],v[6],e[19][6-Audit->10],v[10],2]
==>[v[0],e[12][0-Trust->2],v[2],e[16][2-Trust->8],v[8],e[21][8-Audit->10],v[10],3]
==>[v[0],e[12][0-Trust->2],v[2],e[15][2-Trust->4],v[4],e[17][4-Audit->10],v[10],3]
==>[v[0],e[13][0-Trust->4],v[4],e[18][4-Trust->6],v[6],e[19][6-Audit->10],v[10],3]

但我希望 gremlin 停止在第一个结果的水平。例如所以结果应该是这样的。

==>[v[0],e[13][0-Trust->4],v[4],e[17][4-Audit->10],v[10],2]
==>[v[0],e[14][0-Trust->6],v[6],e[19][6-Audit->10],v[10],2]

查询应该找到一个或多个相同程度的结果(如果存在),然后停止寻找更长的路线。

这个想法是,在一个非常大的图中,如果查询发现一个或多个相同程度的结果,查询就会提前停止。

附言! withComputer() 会很好,但我需要动态地从路由计算中过滤掉不受信任的用户,并且看不到如何使用 shortestPath() 函数来完成此操作。

【问题讨论】:

  • 只是一个快速的评论。如果时间允许,我将尝试写出一个完整的答案——除非有人抢先一步 :-) 对于这些您本质上想要在值在某个范围内时循环的情况,您可能需要 and 逻辑而不是 @ 987654328@。另请注意,within('level') 会将“级别”视为文字字符串。你将需要像where(within('level')).by(sack())这样的东西

标签: graph gremlin tinkerpop tinkerpop3


【解决方案1】:

我还没有时间尝试你的数据集,但这里有一个使用我必须提交的数据集的示例,它显示了如何根据 sack 值超过给定阈值来结束 repeat。

gremlin> g.withSideEffect('level',3000).
......1>   withSack(0).
......2>   V(44).
......3>   repeat(outE().sack(sum).by('dist').inV().simplePath()).
......4>   until(where(gt('level')).by(sack()).by().or().loops().is(3)).
......5>   path().
......6>     by('code').
......7>     by('dist').
......8>   limit(3) 

==>[SAF,549,DFW,4736,LHR]
==>[SAF,549,DFW,4933,CDG]
==>[SAF,549,DFW,5127,FRA] 

我添加了 loops 测试只是为了避免任何失控的查询,但如果数据集被很好地理解为没有这种潜力,则不需要它。

gremlin> g.withSideEffect('level',3000).
......1>   withSack(0).
......2>   V(44).
......3>   repeat(outE().sack(sum).by('dist').inV().simplePath()).
......4>   until(where(gt('level')).by(sack()).by()).
......5>   path().
......6>     by('code').
......7>     by('dist').
......8>   limit(3) 

==>[SAF,549,DFW,4736,LHR]
==>[SAF,549,DFW,4933,CDG]
==>[SAF,549,DFW,5127,FRA] 

这里的关键是where(predicate('string')) 是where 的特例,因此字符串被视为“变量”的名称而不是文字字符串,如where(within('my-list')) 中那样。

还要注意,如果你真正想要的只是一个深度计数器,你可以只使用loops而不用担心管理sack。

这是最后一个示例,它向 until 添加了一个 and 步骤。当发现距离大于 3000 并且这些距离在 3001 到 4500 英里之间,或者 loops 达到 3 时,这将停止。

gremlin> g.withSideEffect('level',3000).
......1>   withSack(0).
......2>   V(44).
......3>   repeat(outE().sack(sum).by('dist').inV().simplePath()).
......4>   until(and((where(gt('level')).by(sack()).by()),
......5>             sack().is(between(3001,4500))).
......6>         or().loops().is(3)).
......7>   path().
......8>     by('code').
......9>     by('dist').
.....10>   limit(3)    

==>[SAF,549,DFW,3368,LIM]
==>[SAF,549,DFW,3702,OGG]
==>[SAF,549,DFW,3733,KEF]    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多