I have a gremlin query to detect cycle nature of the graph. I have tested it with multiple large complex graph it works as expected. But it does not return anything with simple 1 depth graph.
Could you please have a look at the query and help me in correcting the same.
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()
The graph for which it does not work is created as below:-
%%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()
The expected output is as below as it is not a cycle scenario:
{'crn': 'RESTOX0', 'cycle': 'N'}
For cyclic case, the output should be:
{'crn': 'COMPANY0', 'cycle': 'Y'}
However, it does not return any output at all. Could you please help me with the issue in the query. Thank you.
Without deep diving into your query seems that the issue is on the
emitstep.which in the small loop case returns nothing.
You can remove the condition from the
emitand move it to thechoosestep like this:example: https://gremlify.com/6oxgibpxgne