Issue with Groovy Scripts for Gremlin in Java

408 views
Skip to first unread message

nirmal j

unread,
Jul 19, 2018, 12:09:30 PM7/19/18
to Gremlin-users
Hello team, 

I am trying out a new graph implementation using the Tinkerpop stack. I have implemented all structure elements and underlying persistent store interactions. For example I have a CustomVertex implementing the Vertex, CustomGraph implementing Graph etc.. The graph is working fine and gremlin queries are executed as expected.

Now I need to run Groovy Script against this custom Graph implementation. Can anyone please suggest the best method for that.

I tried the below methods, which is not working properly. The Tinkerpop pipe implementations internally trying to cast the vertices into blueprint vertex versions and is failing there. Please find below the code and the exception stack trace.

    CustomGraph graph = (CustomGraph) GraphFactory.open(cfg);
    Pipe pipe = Gremlin.compile("_().out('customLabel').property");
pipe.setStarts(new SingleIterator<Vertex>(graph.vertex("1")));
while (pipe.hasNext()) {
pipe.next();
}

Exception in thread "main" java.lang.ClassCastException: com.nirmal.structure.CustomVertex cannot be cast to com.tinkerpop.blueprints.Vertex
	at com.tinkerpop.pipes.transform.VertexQueryPipe.processNextStart(VertexQueryPipe.java:85)
	at com.tinkerpop.pipes.transform.VertexQueryPipe.processNextStart(VertexQueryPipe.java:19)
	at com.tinkerpop.pipes.AbstractPipe.next(AbstractPipe.java:89)
	at com.tinkerpop.pipes.transform.PropertyPipe.processNextStart(PropertyPipe.java:29)
	at com.tinkerpop.pipes.AbstractPipe.hasNext(AbstractPipe.java:98)
	at com.tinkerpop.pipes.util.Pipeline.hasNext(Pipeline.java:105)
	at com.nirmal.GraphFramework.main(GraphFramework.java:39)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)


I have also tried using Groovy classes, which also throws an exception trying to cast to Blueprints versions.

Please find below the Groovy class.

class GraphAlgorithms {
static {
Gremlin.load()
}

public static Map<Vertex, Integer> eigenvectorRank(Graph g) {
Map<Vertex,Integer> m = [:]; int c = 0
g._().has("label", "customLabel").as('x').out.groupCount(m).loop('x') {c++ < 1000}.iterate()
return m
}
}

Below given is the Java Code

    CustomGraph graph = (CustomGraph) GraphFactory.open(cfg);
    GraphAlgorithms.eigenvectorRank(graph);

Please find below the exception.

Exception in thread "main" java.lang.ClassCastException: com.nirmal.structure.CustomGraph cannot be cast to com.tinkerpop.blueprints.Edge
	at com.tinkerpop.pipes.filter.LabelFilterPipe.processNextStart(LabelFilterPipe.java:25)
	at com.tinkerpop.pipes.filter.LabelFilterPipe.processNextStart(LabelFilterPipe.java:13)
	at com.tinkerpop.pipes.AbstractPipe.next(AbstractPipe.java:89)
	at com.tinkerpop.pipes.util.AsPipe.processNextStart(AsPipe.java:42)
	at com.tinkerpop.pipes.AbstractPipe.hasNext(AbstractPipe.java:98)
	at com.tinkerpop.pipes.branch.LoopPipe$ExpandableLoopBundleIterator.next(LoopPipe.java:143)
	at com.tinkerpop.pipes.util.iterators.HistoryIterator.next(HistoryIterator.java:25)
	at com.tinkerpop.pipes.transform.VertexQueryPipe.processNextStart(VertexQueryPipe.java:85)
	at com.tinkerpop.pipes.transform.VertexQueryPipe.processNextStart(VertexQueryPipe.java:19)
	at com.tinkerpop.pipes.AbstractPipe.next(AbstractPipe.java:89)
	at com.tinkerpop.pipes.sideeffect.GroupCountPipe.processNextStart(GroupCountPipe.java:28)
	at com.tinkerpop.pipes.AbstractPipe.next(AbstractPipe.java:89)
	at com.tinkerpop.pipes.util.Pipeline.next(Pipeline.java:115)
	at com.tinkerpop.pipes.branch.LoopPipe.processNextStart(LoopPipe.java:44)
	at com.tinkerpop.pipes.AbstractPipe.next(AbstractPipe.java:89)
	at com.tinkerpop.pipes.util.Pipeline.next(Pipeline.java:115)
	at com.tinkerpop.pipes.util.PipeHelper.iterate(PipeHelper.java:35)
	at com.tinkerpop.gremlin.java.GremlinPipeline.iterate(GremlinPipeline.java:1542)
	at com.tinkerpop.gremlin.java.GremlinFluentPipeline$iterate.call(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
	at com.nirmal.GraphAlgorithms.eigenvectorRank(GraphAlgorithms.groovy:17)
	at com.nirmal.GraphFramework.main(GraphFramework.java:31)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)


I am not sure if I have missed anything here. Could you please check if this is and see if any enhancements are required for Tinkerpop implementations here.

Thanks,
Nirmal J


Stephen Mallette

unread,
Jul 19, 2018, 12:21:12 PM7/19/18
to Gremlin-users
We stopped maintaining TinkerPop 2.x years and years ago - any reason you chose to implement your graph on that old stuff?

I don't have much recollection of how the 2.x interfaces/core worked. Hard to say what your problem is but this line looks strange:

g._().has("label", "customLabel").as('x').out.groupCount(m).loop('x') {c++ < 1000}.iterate()

why are you doing:

g.__()

Shouldn't that just be 

g.V().has("label", "customLabel").as('x').out.groupCount(m).loop('x') {c++ < 1000}.iterate()

??


--
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/502d40b2-0210-4222-aa82-f42edacb694d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

nirmal j

unread,
Jul 20, 2018, 2:10:29 AM7/20/18
to Gremlin-users
Hello Stephen,

Thanks for looking into this issue.

I was using Tinkerpop version 3.3.1 and gremlin-groovy version 2.6.0. Now I changed to gremlin-groovy 3.3.1 

<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-groovy</artifactId>
<version>3.3.1</version>
</dependency>

I have updated the groovy script as well

import org.apache.tinkerpop.gremlin.structure.Graph
import org.apache.tinkerpop.gremlin.structure.Vertex
import org.apache.tinkerpop.gremlin.util.Gremlin


class GraphAlgorithms {
static {
Gremlin.load()
}

public static Map<Vertex, Integer> eigenvectorRank(Graph g) {
Map<Vertex,Integer> m = [:]; int c = 0
        g.V().has("label", "customLabel").as('x').out.groupCount(m).loop('x') {c++ < 1000}.iterate()
        return m
}
}

But still I am getting another exception

Exception in thread "main" java.lang.ExceptionInInitializerError
	at com.nirmal.GraphFramework.main(GraphFramework.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: groovy.lang.MissingMethodException: No signature of method: static org.apache.tinkerpop.gremlin.util.Gremlin.load() is applicable for argument types: () values: []
Possible solutions: find(), find(groovy.lang.Closure), wait(), dump(), grep(), any()
	at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1501)
	at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1487)
	at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:53)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
	at com.glora.zeus.GraphAlgorithms.<clinit>(GraphAlgorithms.groovy:12)
	... 6 more

Do you have any idea, why this happen? Could you please point out if I m doing anything wrong here.

Thanks,
Nirmal J

Stephen Mallette

unread,
Jul 20, 2018, 5:56:39 AM7/20/18
to Gremlin-users
I was using Tinkerpop version 3.3.1 and gremlin-groovy version 2.6.0. Now I changed to gremlin-groovy 3.3.1  

yeah - mixing those version is bad. interesting that you even got the error that you did....i would have assumed even less compatibility than that.

> Do you have any idea, why this happen? Could you please point out if I m doing anything wrong here.

There is no more Gremlin.load() and your Gremlin syntax is all 2.x  - you are still mixing TinkerPop 2.x and 3.x concepts. I sense you are looking at:


don't do that....it specifically states at the top of every wiki page that the content is "outdated" with a link to the new documentation. If you have a 3.x implementation then you need to use 3.x syntax:

CustomGraph graph = (CustomGraph) GraphFactory.open(cfg);
GraphTraversalSource g = graph.traversal()
g.V()........

See if you can get some basic queries working with that then consider:



nirmal j

unread,
Jul 23, 2018, 2:03:54 AM7/23/18
to Gremlin-users
Hello Stephen,

The gremlin queries are working fine with my implementation.

I am having trouble with using Groovy scripts. I was referring the below links for that.


I could not find the latest references for those. Could you please help if you know.

Thanks,
Nirmal J

Stephen Mallette

unread,
Jul 23, 2018, 6:46:45 AM7/23/18
to Gremlin-users
The gremlin queries are working fine with my implementation. 

If this line of Gremlin

g.V().has("label", "customLabel").as('x').out.groupCount(m).loop('x') {c++ < 1000}.iterate()

does not fail like this:

gremlin> g.V().has("label", "customLabel").as('x').out().groupCount(m).loop('x') {c++ < 1000}.iterate()
No signature of method: org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal.groupCount() is applicable for argument types: (java.util.LinkedHashMap) values: [[:]]
Type ':help' or ':h' for help.
Display stack trace? [yN]  

then you have something wrong. groupCount() does not take a Map as an argument and loops() does not take an argument. Those is the TinkerPop 2.x API. I think we need to agree on that much before I can be of much more help. There is something critically wrong with a TinkerPop 3.x implementation that is not failing in that fashion for that Gremlin. I can't imagine how that can be working.

nirmal j

unread,
Jul 23, 2018, 7:51:01 AM7/23/18
to Gremlin-users
Hi Stephen,

Sorry for the confusion. I am not saying that the above mentioned query works from a gremlin console.

My actual problem is with using groovy scripts with gremlin. The above mentioned query is one I got from the references with respect to gremlin-groovy . The below are the only references that I got.


I understand these are outdated. But I am not able to find the latest ones in this regard.

Thanks,
Nirmal J

Stephen Mallette

unread,
Jul 23, 2018, 8:07:46 AM7/23/18
to Gremlin-users
There is little need for those references in TinkerPop 3.x. That's why you can't find them. In TinkerPop 3.x, "Gremlin through Java" is just Java. Gremlin is written in Java so just use it like a library as you normally would. Same for Groovy. There is nothing special to do.

Do you really need dynamic script evaluation in Groovy for some reason? what is your use case?

Reply all
Reply to author
Forward
0 new messages