我有一个 gremlin 查询来检测图形的循环性质。我已经用多个大型复杂图表对其进行了测试,它按预期工作。但它不会返回任何简单的 1 深度图。
您能否查看一下该查询并帮助我纠正该问题。
g
.V('RESTOX0').as('crn')
.map(
repeat(
outE('HAS_VOTING_PC_TO')
.inV()
.where(
and(
hasNot('superseded_dt'),
or(has('status', 'active'), hasNot('status'))))
.simplePath())
.emit(loops().is(gt(1)))
.both('HAS_VOTING_PC_TO').choose(where(eq('crn')),
sack(assign).by(constant('Y')),
sack(assign).by(constant('N')))
.select('crn')
.project('crn', 'cycle')
.by(id)
.by(sack()))
.fold()
.V('RESTOX0').as('crn')
.map(
repeat(
outE('HAS_SHRHLDING_PC_TO')
.inV()
.where(
and(
hasNot('superseded_dt'),
or(has('status', 'active'), hasNot('status'))))
.simplePath())
.emit(loops().is(gt(1)))
.both('HAS_SHRHLDING_PC_TO').choose(where(eq('crn')),
sack(assign).by(constant('Y')),
sack(assign).by(constant('N')))
.select('crn')
.project('crn', 'cycle')
.by(id)
.by(sack()))
.fold()
.aggregate('x')
.dedup()
.unfold()
它不起作用的图表创建如下:-
%%gremlin
g.addV('company').property(id,'RESTOX0').property('name','Alphabet').next()
g.addV('person').property(id,'RESTOX0_1900-01-01_1_1').property('bu_id', 'RESTOX0_1900-01-01_1_1').property('name', 'W Karl David Laxton').next()
g.addV('person').property(id,'RESTOX0_1900-01-01_1_2').property('bu_id', 'RESTOX0_1900-01-01_1_2').property('name', 'Steven H Strong').next()
g.addE('HAS_SHRHLDING_PC_TO').from(__.V('RESTOX0')).to(__.V('RESTOX0_1900-01-01_1_1')).property(id,'RESTOX0_HAS_SHRHLDING_PC_TO_RESTOX0_1900-01-01_1_1').property('perc_value', 25).next()
g.addE('HAS_VOTING_PC_TO').from(__.V('RESTOX0')).to(__.V('RESTOX0_1900-01-01_1_1')).property(id,'RESTOX0_HAS_VOTING_PC_TO_RESTOX0_1900-01-01_1_1').property('perc_value', 50).next()
g.addE('HAS_SHRHLDING_PC_TO').from(__.V('RESTOX0')).to(__.V('RESTOX0_1900-01-01_1_2')).property(id,'RESTOX0_HAS_SHRHLDING_PC_TO_RESTOX0_1900-01-01_1_2').property('perc_value', 25).next()
g.addE('HAS_VOTING_PC_TO').from(__.V('RESTOX0')).to(__.V('RESTOX0_1900-01-01_1_2')).property(id,'RESTOX0_HAS_VOTING_PC_TO_RESTOX0_1900-01-01_1_2').property('perc_value', 25).next()
预期输出如下:
{'crn': 'RESTOX0', 'cycle': 'N'}
但是,它根本不返回任何输出。您能帮我解决查询中的问题吗?谢谢你。
如果没有深入研究您的查询,问题似乎出在
emit
步骤上。
在小循环情况下不会返回任何内容。
您可以从
emit
中删除条件并将其移至 choose
步骤,如下所示:
g.V('RESTOX0').as('crn').
map(repeat(outE('HAS_VOTING_PC_TO').inV().
where(and(
hasNot('superseded_dt'),
or(
has('status', 'active'),
hasNot('status')
)
)).
simplePath()).
emit().
both('HAS_VOTING_PC_TO').choose(
where(eq('crn')),
sack(assign).
by(constant('Y')),
sack(assign).
by(constant('N'))
).
select('crn').
project('crn', 'cycle').by(id).by(sack())).
fold().V('RESTOX0').as('crn').
map(repeat(outE('HAS_SHRHLDING_PC_TO').inV().
where(and(
hasNot('superseded_dt'),
or(
has('status', 'active'),
hasNot('status')
)
)).
simplePath()).
emit().
both('HAS_SHRHLDING_PC_TO').choose(
where(eq('crn')).where(loops().is(gt(1))),
sack(assign).
by(constant('Y')),
sack(assign).
by(constant('N'))
).
select('crn').
project('crn', 'cycle').by(id).by(sack())).
fold().aggregate('x').dedup().unfold()