Converting Groovy to Java not working for coalesce/unfold

651 views
Skip to first unread message

Douglas Moore

unread,
Mar 1, 2018, 5:49:11 PM3/1/18
to Gremlin-users
I'm trying to convert groovy coalesce/unfold to java, unfold is not recognized, recommendations?
From Groovy:
g.V().has('name','Query1').fold().coalesce(unfold(),addV('SQL').property('name','Query1')).unfold().as("q").coalesce(g.V().has('name','Table2'),addV('Table').property('name','Table2')).addE('INSERT').from("q").iterate()

to Java:
g.V().has(NAME,"Query1").fold().coalesce(unfold(),addV("SQL").property(NAME,"Query1")).unfold().as("q").coalesce(g.V().has(NAME,"Table2"),addV("Table").property("name","Table2")).addE("INSERT").from("q").iterate();

 unfold() is not recognized because it's not part of my junit class.

I also tried .coalesce(__.unfold(),.... however the types are incompatible

Suggestions please?

- Douglas

Daniel Kuppitz

unread,
Mar 2, 2018, 10:35:43 AM3/2/18
to gremli...@googlegroups.com
I don't think you want to have a nested g.V(). Just use __.V() instead.

g.V().has("name","Query1").fold().
coalesce(
__.unfold(),
__.addV("SQL").
property("name","Query1")).
unfold().as("q").
coalesce(
__.V().has("name","Table2"),
__.addV("Table").
property("name","Table2")).
addE("INSERT").from("q").iterate();
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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/7b1824a4-60e5-4f99-95a4-87317c27f458%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Douglas Moore

unread,
Mar 2, 2018, 11:17:51 PM3/2/18
to Gremlin-users
Thanks Daniel.

So, I was getting this error:

The method coalesce(Traversal<?,E2>...) in the type GraphTraversal<Vertex,List<Vertex>> is not applicable for the arguments (GraphTraversal<Object,Object>, GraphTraversal<Object,Vertex>)

The method as(String, String...) in the type GraphTraversal<Vertex,Object> is not applicable for the arguments (String, GraphTraversal<Vertex,Vertex>)

Syntax error on token ")", delete this token


and it turns out my 'g' was a GraphTraversalSource rather than GraphTraversal. Changing the 'g' type clears up that error allowing the improved statement to compile.
I picked GraphTraversalSource because that's what graph.traversal().withRemote(conf) returns.

I have another error now after changing the type... g.E() ... .E() doesn't exist in GraphTraversal but does exist in GraphTraversalSource.
What's the difference between these two types?

I'm on Gremlin core 3.2.6

- Douglas
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Douglas Moore

unread,
Mar 2, 2018, 11:46:35 PM3/2/18
to Gremlin-users

With 

((GraphTraversal) g).V().has(NAME,"Query1").fold().

coalesce(

  __.unfold(),

  __.addV("SQL").

  property(NAME,"Query1")).

unfold().as("q").

coalesce(

__.V().has(NAME,"Table2"),

__.addV("Table").

property("name","Table2")).

addE("INSERT").from("q").iterate();


I get runtime exception message:

org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource cannot be cast to org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal

Jason Plurad

unread,
Mar 3, 2018, 10:03:07 AM3/3/18
to Gremlin-users
You should use the GraphTraversalSource and then __ for the anonymous traversals (see doc note). If you do a static import on __, queries can use a shorthand syntax that is the same as Groovy, as long as you double quote the strings.

import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.*;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;

public class RemoteExample {
   
public static void main(String[] args) {
       
Graph graph = EmptyGraph.instance();
       
GraphTraversalSource g = null;
       
try {
            g
= graph.traversal().withRemote("conf/remote-graph.properties");
       
} catch (Exception e) {
            e
.printStackTrace();
           
System.exit(1);

       
}

        g
.V().has("name", "Query1").fold().

                coalesce
(unfold(), addV("SQL").property("name", "Query1")).unfold().as("q").
                coalesce
(V().has("name", "Table2"), addV("Table").property("name", "Table2")).

                addE
("INSERT").from("q").iterate();


       
System.out.println("vertices: " + g.V().valueMap(true).toList().toString());
       
System.out.println("edges: " + g.E().valueMap(true).toList().toString());
       
System.exit(0);
   
}
}

If you use a regular import instead of a static import, the syntax that Daniel provided works fine.

Douglas Moore

unread,
Mar 3, 2018, 7:25:17 PM3/3/18
to Gremlin-users
This example works very well!!

One ancillary issue I encountered, my eclipse & maven settings were not explicit in specifying Java 1.8 output. Once set, that took care of many of the compile issues.

Thanks All!
Reply all
Reply to author
Forward
0 new messages