我正在尝试以编程方式向图表添加边。
这个想法是,我将压缩两个顶点流,如果连续两个顶点满足某些条件,我将在它们之间创建一条边。
问题出在拉链上。我无法压缩两个流,其中一个流前进了一个位置。
(
g.V().hasLabel("person").
order().by("age")
.as("x")
.local(
union(
select("x"),
select("x") // .skip(1)
).fold()
)
)
==>[v[2],v[2]]
==>[v[1],v[1]]
==>[v[4],v[4]]
==>[v[6],v[6]]
以上作品。但是,当我取消注释skip
部分时,我得到
==>[v[2]]
==>[v[1]]
==>[v[4]]
==>[v[6]]
但我需要:
==>[v[2],v[1]]
==>[v[1],v[4]]
==>[v[4],v[6]]
我附上后续部分以供参考。
(
g.V().hasLabel("person").
order().by("age")
.as("x")
.local(
union(
select("x"),
select("x")
).fold()
).sideEffect(
project("first", "second")
.by(unfold().limit(1))
.by(unfold().skip(1))
.coalesce(
select("first").out("age_lt").unfold(),
choose(
math("second - first").by("age").is(lt(5)),
addE("age_lt").from(select("first")).to(select("second"))
)
)
).none()
)
这将为您提供成对的连续顶点:
g.V().hasLabel("person").order().by("age").store("x").local(
select(all,"x").tail(local, 2)
).skip(1)
请注意最后的跳过以过滤出第一个单个顶点“对”。
您可以使用具有这种形式的分区模式(其中“ 2”是您的分区大小)来做到这一点:
g.V().fold().
emit().
until(__.not(__.unfold())).
repeat(__.skip(local, 2)).
filter(__.unfold()).
limit(local, 2)
而不是跳过repeat()
中的“ 2”,而是跳过“ 1”,因为您只想将行进器提前“ 1”,因此:
gremlin> g.V().hasLabel("person").
......1> order().by("age").fold().
......2> emit().
......3> until(__.not(__.unfold())).
......4> repeat(__.skip(local,1)).
......5> filter(__.unfold()).
......6> limit(local,2)
==>[v[2],v[1]]
==>[v[1],v[4]]
==>[v[4],v[6]]
==>[v[6]]
然后,删除所有未配对的尾随项目:
gremlin> g.V().hasLabel("person").
......1> order().by("age").fold().
......2> emit().
......3> until(__.not(__.unfold())).
......4> repeat(__.skip(local,1)).
......5> filter(__.unfold()).
......6> limit(local,2).
......7> filter(count(local).is(2))
==>[v[2],v[1]]
==>[v[1],v[4]]
==>[v[4],v[6]]