【问题标题】:Calculate connection between consecutive vertices on the path计算路径上连续顶点之间的连接
【发布时间】:2019-06-10 05:42:43
【问题描述】:

如下图所示:

找出person1和person5之间的所有路径,然后计算路径上连续顶点之间的连接。

为了说明connection的定义,以person1和person2为例:
1.person1创建comment1回复post2创建person2
2.person2创建comment3回复post1创建person1

所以,person1 和 person2 之间的连接是 2;而person2 和person5 之间的那个是0。

上图中的路径是[v[person1],v[person2],v[person5]]:

gremlin> g.V('person1').
......1>   repeat(both('knows').simplePath()).
......2>     until(hasId('person5')).path()
==>[v[person1],v[person2],v[person5]]

目前,我只能设法获得 dsl:

gremlin> g.V('person1').
......1>   repeat(both('knows').simplePath()).
......2>     until(hasId('person5').or().loops().is(eq(2))).hasId('person5').path().
......3>       repeat(
......4>         filter(count(local).is(gt(1))).
......5>           sack(assign).by(
......6>             sideEffect(range(local,1,2).aggregate('m')).
......7>             range(local,0,1).
......8>             in('hasCreator').hasLabel('comment').
......9>             out('replyOf').hasLabel('post').
.....10>             out('hasCreator').where(within('m')).count()
.....11>           ).
.....12>           sack(sum).by(
.....13>             sideEffect(range(local,0,1).aggregate('n')).
.....14>             range(local,1,2).
.....15>             in('hasCreator').hasLabel('comment').
.....16>             out('replyOf').hasLabel('post').
.....17>             out('hasCreator').where(within('n')).count()
.....18>           ).
.....19>           skip(local, 1)
.....20>         ).
.....21>       emit().sack().fold()
==>[2,1]

但是结果是错误的,应该是[2,0]。我知道我不应该使用aggregate 进行过滤,但根据我的知识我找不到合适的方法。

示例图可以通过以下方式生成:

g.addV('person').property(id, 'person1')
g.addV('person').property(id, 'person2')
g.addV('person').property(id, 'person5')

g.addE('knows').from(V('person1')).to(V('person2'))
g.addE('knows').from(V('person2')).to(V('person5'))

g.addV('post').property(id, 'post1')
g.addV('post').property(id, 'post2')

g.addV('comment').property(id, 'comment1')
g.addV('comment').property(id, 'comment2')
g.addV('comment').property(id, 'comment3')

g.addE('hasCreator').from(V('post1')).to(V('person1'))
g.addE('hasCreator').from(V('post2')).to(V('person2'))

g.addE('hasCreator').from(V('comment1')).to(V('person1'))
g.addE('hasCreator').from(V('comment2')).to(V('person2'))
g.addE('hasCreator').from(V('comment3')).to(V('person2'))

g.addE('replyOf').from(V('comment1')).to(V('post2'))
g.addE('replyOf').from(V('comment2')).to(V('post2'))
g.addE('replyOf').from(V('comment3')).to(V('post1'))

【问题讨论】:

    标签: gremlin


    【解决方案1】:

    在我用select替换aggregate的用法后,我现在可以得到正确的答案:

    g.V('person1').
      repeat(both('knows').simplePath()).
        until(hasId('person5').or().loops().is(eq(2))).hasId('person5').path().
            repeat(
                filter(count(local).is(gt(1))).
                    sack(assign).by(
                      __.as('orig').
                      range(local,1,2).as('v2').
                      select('orig').range(local,0,1).
                        in('hasCreator').hasLabel('comment').
                        out('replyOf').hasLabel('post').
                        out('hasCreator').where(eq('v2')).count()
                    ).
                    sack(sum).by(
                      __.as('orig').
                      range(local,0,1).as('v1').
                      select('orig').range(local,1,2).
                        in('hasCreator').hasLabel('comment').
                        out('replyOf').hasLabel('post').
                        out('hasCreator').where(eq('v1')).count()
                    ).
                    skip(local, 1)
                ).
            emit().sack().fold()
    

    【讨论】:

      猜你喜欢
      • 2016-03-26
      • 1970-01-01
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      相关资源
      最近更新 更多