简体   繁体   中英

Check the sack() value in a gremlin query

We have a query to find the possible paths to a node. We represented ABC paths with numbers as shown in the table and we are using bitwise and operation to see available paths to a node. We are repeating the query if there is at least one available path (A, B or C) to a node (values('availablePath').is(gt(0))). However, we want to stop the query if the sack value is 0 since it does not make sense to continue traversing after the Node 3 because sack value is 0, which means that you can not access node 3 by A, B or C paths. Is there a way to get the sack value and stop traversing if it is 0?
Our query is:

g.withSack(7).
 V().has('id','1')
 local(
   repeat(bothE().where(values('availablePath').is(gt(0))).
          sack{f,l -> f & l}.
            by('availablePath').
          otherV().
          simplePath().as('node')).
   emit().
   sack().as('path')).
 select('node','path').
   by().
   by().
 dedup()

Sample graph creation query:

g.addV('node').property('id','1').as('1')
.addV('node').property('id','2').as('2')
.addV('node').property('id','3').as('3')
.addV('node').property('id','4').as('4')
.addE('edge').property('availablePath',3).from('1').to('2').as('edge1')
.addE('edge').property('availablePath',4).from('2').to('3').as('edge2')
.addE('edge').property('availablePath',7).from('3').to('4').as('edge3')

Traversal begins from node 1.

GRAPH

TABLE

You can test the sack value inside the where step as follows:

gremlin> g.withSack(7).
......1>  V().has('id','1').
......2>  local(
......3>    repeat(bothE().where(values('availablePath').is(gt(0)).and().sack().is(gt(0))).
......4>           sack{f,l -> f & l}.
......5>             by('availablePath').
......6>           otherV().
......7>           simplePath().as('node')).
......8>    emit().
......9>    sack().as('path')).
.....10>  select('node','path').
.....11>    by().
.....12>    by().
.....13>  dedup()

==>[node:v[4],path:3]
==>[node:v[6],path:0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM