Examining the current path from within a filter closure?

66 views
Skip to first unread message

Paul Grebenc

unread,
Sep 14, 2012, 4:14:39 PM9/14/12
to gremli...@googlegroups.com
I've been experimenting with the possibility of examining the current path from within a loop's while closure, so that I can base a loop's continuation on portions of the path followed up to that point.  There is a 'path' property available, which makes it possible to examine the current path from within the closure.

I am wondering, is there a way to do something similar with a filter closure?

For example, can I selectively filter something, for example, based on a comparison of the value of a property of the current vertex/edge, with the value of that property contained a specific number of steps back in the current path?

Thanks,

Paul

Marko Rodriguez

unread,
Sep 14, 2012, 4:17:45 PM9/14/12
to gremli...@googlegroups.com
Hi,

> I've been experimenting with the possibility of examining the current path from within a loop's while closure, so that I can base a loop's continuation on portions of the path followed up to that point. There is a 'path' property available, which makes it possible to examine the current path from within the closure.

Yep. With Gremlin 2.x, be sure to set Pipeline.enablePath() as when path calls are in a closure, its impossible for the compiler to know that you want paths enabled. Path calculations are expensive and thus, assumed to be off. Note that when you do like paths(), simplePath(), etc., it knows you want paths enabled.

> I am wondering, is there a way to do something similar with a filter closure?

Huh. No, as you only have reference to 'it' which is the current object. ............................................................ could be possible, but would require some redos. You could do it with step() as step's 'it' is the previous pipe. it.getCurrentPath().

HTH,
Marko.

http://thinkaurelius.com

MC

unread,
Sep 30, 2015, 11:37:48 PM9/30/15
to Gremlin-users
Marco,

Is this still the best way to achieve the desired behavior, access the current path from within a filter closure? I'm assuming things have changed since 2012. Is there any example code for how to do this?

I would like to use the current path to filter out certain edges.

MC

Marko Rodriguez

unread,
Oct 1, 2015, 10:10:41 AM10/1/15
to gremli...@googlegroups.com
Hi,

Are you using TinkerPop3 or TinkerPop2?

Marko.
--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/659c25c2-3f21-4991-9b75-fd1a82a551f2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

MC

unread,
Oct 1, 2015, 10:15:46 AM10/1/15
to Gremlin-users
Marko,

TP2.

MC

Marko Rodriguez

unread,
Oct 1, 2015, 10:53:50 AM10/1/15
to gremli...@googlegroups.com
Hi MC,

I'll have to defer to Kuppitz as I honestly don't know -- I haven't touched TinkerPop2 in 1.5 years :D.

Marko.

Daniel Kuppitz

unread,
Oct 1, 2015, 12:21:30 PM10/1/15
to Gremlin-users
Not sure about the best approach, but playing with it for a while I liked this variant most:

// starting at v[1], find all vertices at a distance that is a multiple of 1
// distance is defined by the cumulative edge weights between the two vertices
// stop at a distance greater than 5

g.v(1).as("source").ifThenElse {it instanceof Vertex} {it} {it.last()} \
 .bothE().bothV().dedup().as("target").path().loop("source") {
    it.getObject().grep(Edge)*.getProperty("weight").sum() <= 5
  } {
    def distance = it.getObject().grep(Edge)*.getProperty("weight").sum()
    0 == ((int) distance * 10) % 10
  }.select(["target"])

Output:

gremlin> g.v(1).as("source").ifThenElse {it instanceof Vertex} {it} {it.last()} \
gremlin>  .bothE().bothV().dedup().as("target").path().loop("source") {
gremlin>     it.getObject().grep(Edge)*.getProperty("weight").sum() <= 5
gremlin>   } {
gremlin>     def distance = it.getObject().grep(Edge)*.getProperty("weight").sum()
gremlin>     0 == ((int) distance * 10) % 10
gremlin>   }.select(["target"])
==>[target:v[4]]
==>[target:v[5]]


Basically simply create the path outside the closure, so that it's passed as the current object to the closures.

Cheers,
Daniel
Reply all
Reply to author
Forward
0 new messages