Create custom predicate in Gremlin Python

597 views
Skip to first unread message

ha...@nugit.co

unread,
Mar 20, 2018, 8:09:03 AM3/20/18
to Gremlin-users
Hello,

I looked at the example of creating custom predicates or tests in Kelvin Lawrence's book using Java.
// Create a new BiPredicate that handles regular expression pattern matching
bp = new java.util.function.BiPredicate<String, String>() {
         boolean test(String val, String pattern) {
           return val ==~ pattern  }}

// Create a new closure we can use for regular expression pattern matching.
regex = {new P(bp, it)}

// Use our new closure to find descriptions that start with 'Dal'. As this
// unwinds, the contents of 'desc' are passed to the test method as the first parameter
// and the regex pattern as the second paramter.
g.V().has('desc', regex(/^Dal.*/)).values('desc')

Would anybody be able to guide me on the way to do this in Gremlin Python?
Thanks,
Harsh

Stephen Mallette

unread,
Mar 21, 2018, 11:12:10 AM3/21/18
to Gremlin-users
In python you have to write lambdas as follows:

>>> g.V().out().map(lambda: "lambda x: len(x.get().value('name'))").sum().toList() #(1)
[24]

or if not using gremlin-python on the server then you would write your lambda with groovy:

>>> g.V().out().map(lambda: ("it.get().value('name').length()", "gremlin-groovy")).sum().toList() #(3)
[24]

which is described more here:


So, you could basically just extract that lambda, defined that way, to a python function and then re-use it as needed. 




--
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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/11918a5c-222a-4f25-bf9e-fca35965392a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Harshvardhan Joshi

unread,
Mar 22, 2018, 8:25:36 AM3/22/18
to gremli...@googlegroups.com
Hey Stephen,

Thank you for taking the time to reply.

I see that it is possible to add custom lambdas to the map / filter step.

But I wanted to know if it’s possible to do so in the has step.
For example,
g.V().has(“name”, lambda: ‘lambda x: x.get().value(“name”).startswith(“abc”)’).toList()
This traversal returns only an empty list. Even if there are nodes whose “name” start with “abc”.

Also, I did encounter issues when trying to use the filter step in the where step with or/and conditions.
For example, the below traversal works
g.V().has("name").where(__.filter(lambda: "lambda x: ‘abc' in x.get().value('name').lower()")).values("name").toList()

But something like,
g.V().has("name").where(__.or_(__.filter(lambda: "lambda x: 'dev' in x.get().value('name').lower()"), __.values("age").is_(P.between(10,40)))).values("name").toList()

Returns this error:-
type object 'org.apache.tinkerpop.gremlin.process.traversal.dsl' has no attribute 'or_' in <script> at line number 1

Your input on this would be helpful.

Thank you,
Harsh

You received this message because you are subscribed to a topic in the Google Groups "Gremlin-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gremlin-users/dUsxCC-OpmY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/CAA-H43_E6bOVoFYnANuPxq8rBhCOztM%3D_mbPWO1diHd-kUmZUA%40mail.gmail.com.

Stephen Mallette

unread,
Mar 26, 2018, 7:49:03 AM3/26/18
to Gremlin-users
A lambda won't work with has() that way because the has() step doesn't expect a lambda - it expects an instance of the P predicate:


So you'd need an instance of P to get that in there. Unfortunately, P is not an abstract class or an interface so all that can really happen is this:

gremlin> g.V().has('name', new P({testValue,value -> testValue.startsWith(value)}, 'm'))
==>v[1]

So, more "unfortunately", i don't think that this will work with our lambda system. If you have to do something like this then I guess you are stuck with scripts in this case.

type object 'org.apache.tinkerpop.gremlin.process.traversal.dsl' has no attribute 'or_' in <script> at line number 1

i'm aware of that bug. it will be fixed in 3.2.8/3.3.2 which should release in about a week.


To unsubscribe from this group and all its topics, send an email to gremlin-users+unsubscribe@googlegroups.com.

--
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-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages