Tinkerpop3 choose step in Java 8

574 views
Skip to first unread message

Uday

unread,
Jul 2, 2015, 1:08:56 PM7/2/15
to gremli...@googlegroups.com
Hi,

I'm trying to convert a gremlin query from tinkerpop2 to tinkerpop3 in java.

Gremlin (TP2):
g.V().has("subjectId", "uday").outE("isAssigned").has("receivedStatus", T.in, ["New", "In progress"]).order{it.b.assignedOn <=> it.a.assignedOn}[0..15].as("e_asg").inV().as("v_svc").ifThenElse{it.status == "New"}{it,m -> ["id":it.id, "instanceId":m.v_svc.instanceId, "receivedStatus":m.e_asg.receivedStatus]}{it,m -> it.as('x').outE().inV().loop('x'){it.loops % 2 == 1 || it.object.status == "Completed"}.transform{["id":it.id, "instanceId":m.v_svc.instanceId, "receivedStatus":m.e_asg.receivedStatus, "subFlowId":it.subFlowId, "status":it.status]}}

Java (TP3):
GraphTraversal<Vertex, Vertex> traversal = graph.traversal().V().
                has("subjectId", "test").
                outE("isAssigned").
                has("receivedStatus", P.within(receivedStatuses)).
                order().by("assignedOn", Order.decr).range(0, 15).as("e_asg").
                inV().as("v_svc");

        GraphTraversal<Vertex, Object> t = traversal.
                choose((Predicate<Vertex>) it -> it.property("status").value().equals("New"),
                        traversal.identity().map(it -> it.get().value("status")),
                        traversal.identity().map(it -> it.get().value("previousStatus")));

Running the java code is throwing this exception:
Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep.toString()

What am doing wrong? How can I use the identity _() step in TP3 java? Can I use identity in choose step? An example of choose step in Java 8 would be helpful.

Any help much appreciated.

 

Daniel Kuppitz

unread,
Jul 3, 2015, 6:42:25 AM7/3/15
to gremli...@googlegroups.com
Hi Uday,

you don't need any lamda steps. This is the fixed version of your TP3 traversal:

GraphTraversal<Vertex, Vertex> traversal = graph.traversal().V().has("subjectId", "test").
        outE("isAssigned").has("receivedStatus", P.within(receivedStatuses)).
        order().by("assignedOn", Order.decr).range(0, 15).as("e_asg").inV().as("v_svc").
        choose(__.values("status").is("New"),
               __.values("status"),
               __.values("previousStatus"));

And here's the actual "translation" of your TP2 traversal:

g.V().has("subjectId", "uday").outE("isAssigned").has("receivedStatus", P.within(Arrays.asList("New", "In progress")).
      order().by("assignedOn", Order.decr).limit(15).as("e_asg").inV().as("v_svc").
      choose(__.values("status").is("New"),
             __.match(__.as("v_svc").id().as("id"),
                      __.as("v_svc").values("instanceId").as("instanceId"),
                      __.as("e_asg").values("receivedStatus").as("receivedStatus")),
             __.repeat(__.out()).until(filter {it.loops() % 2 != 1 && !it.get().value("status").equals("Completed") }).as("x")
                match(__.as("x").id().as("id"),
                      __.as("x").values("subFlowId").as("subFlowId"),
                      __.as("x").values("status").as("status"),
                      __.as("v_svc").values("instanceId").as("instanceId"),
                      __.as("e_asg").values("receivedStatus").as("receivedStatus")));

It's untested, thus it may still contain some bugs. If you need any further help with this query, please provide a sample graph (and an expected result for the query).

Cheers,
Daniel



--
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/19cde15b-8e8e-4969-b7b2-c75a19ce7c5a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Uday

unread,
Jul 4, 2015, 2:09:50 PM7/4/15
to gremli...@googlegroups.com
Thanks Daniel. You helped me understand how to write TP3 in Java. Now, I'm trying to convert the gremlin query in TP3 that you provided into Java.

Here's what I started with:
graph.traversal().V().has("subjectId", "uday").
        outE("isAssigned").has("receivedStatus", P.within(RECEIVED_STATUSES)).
        order().by("assignedOn", Order.decr).range(low, high).as("e_asg").inV().as("v_svc").
        choose(__.values("status").is("New"),
                __.match("a", __.as("a").id().as("id"), __.as("e_asg").values("receivedStatus").as("receivedStatus")),
                __.match("b", __.as("b").id().as("id"), __.as("e_asg").values("receivedStatus").as("receivedStatus")));

And I get this error:
The provided traversal set contains unreachable as-label(s)
java.lang.IllegalArgumentException: The provided traversal set contains unreachable as-label(s)
at org.apache.tinkerpop.gremlin.process.traversal.step.map.match.MatchStep.checkSolvability(MatchStep.java:248)
at org.apache.tinkerpop.gremlin.process.traversal.step.map.match.MatchStep.<init>(MatchStep.java:86)
at org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal.match(GraphTraversal.java:180)
at org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.match(__.java:176) 


How can I access "v_svc" and "e_asg" inside the match step? I will create a reproducer project in github and add the link here. 

Thanks,
Uday

Daniel Kuppitz

unread,
Jul 8, 2015, 2:20:11 PM7/8/15
to gremli...@googlegroups.com
Note that the match step as I used it will only work with the current snapshot. Here's an example using the Grateful Dead graph, where match uses named steps from the outside:

// what songs are written by Garcia (s2) and follow a song that was sung by Garcia (s1):
gremlin> g.V().hasLabel("song").as("s1").out("followedBy").as("s2").out("sungBy").match(
              __.as("artist").has("name","Garcia"),
              __.as("s1").out("writtenBy").as("artist")
         ).select("s1","s2","artist").by("name")

==>[s1:CRYPTICAL ENVELOPMENT, s2:WHARF RAT, artist:Garcia]
==>[s1:CRYPTICAL ENVELOPMENT, s2:THE OTHER ONE, artist:Garcia]
==>[s1:CRYPTICAL ENVELOPMENT, s2:DRUMS, artist:Garcia]

Cheers,
Daniel

--
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.
Reply all
Reply to author
Forward
0 new messages