DSL functions nested within queries

58 views
Skip to first unread message

Martin McLaughlin

unread,
Sep 12, 2022, 2:44:51 AM9/12/22
to Gremlin-users
Hi all,

I am writing a Gremlin-Java DSL based on the Maven archetype gremlin-archetype-dsl. The DSL compiles correctly in Maven and it works in a standard linear query such as this one in the Gremlin Console ("test" spawns the traverser):

>test.function1('arg1').
>     function2('arg2','arg3').
>     function2('arg2','arg3')

However, all the DSL functions return an error when they are nested inside another query function:

>test.function1('arg1').
>     repeat(function2('arg2','arg3')).
>     times(2)
No signature of method: groovysh_evaluate.function2() is applicable for argument types: (String, String) values: [arg2, arg3]

This happens regardless of the specific DSL function (AnonymousMethod or not, number & types of arguments, etc.) and the outer function (repeat, where, emit, choose, not, or, etc.). Standard Gremlin functions still work correctly.

Here are the imports in "TestTraversalDsl.java", some of which may be unnecessary:

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.IO;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.GremlinDsl;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.GremlinDsl.AnonymousMethod;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;
import static org.apache.tinkerpop.gremlin.process.traversal.Operator.*;
import static org.apache.tinkerpop.gremlin.process.traversal.Order.*;
import static org.apache.tinkerpop.gremlin.process.traversal.P.*;
import static org.apache.tinkerpop.gremlin.process.traversal.Pop.*;
import static org.apache.tinkerpop.gremlin.process.traversal.SackFunctions.*;
import static org.apache.tinkerpop.gremlin.process.traversal.Scope.*;
import static org.apache.tinkerpop.gremlin.process.traversal.TextP.*;
import static org.apache.tinkerpop.gremlin.structure.Column.*;
import static org.apache.tinkerpop.gremlin.structure.Direction.*;
import static org.apache.tinkerpop.gremlin.structure.T.*;
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.*;

I am fairly new to Java + Gremlin. Am I missing something basic, or are nested DSL functions not supported?

Thanks,
Martin

Stephen Mallette

unread,
Sep 12, 2022, 7:22:32 AM9/12/22
to gremli...@googlegroups.com
looks like you're importing the standard anonymous traversal:

import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.*;

and so i assume it can't find your function there. you would want to import the generated __ class instead which has all of the methods of the standard class plus your DSL ones.

--
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/58a533ad-f56d-47df-b31e-75c6ea57366bn%40googlegroups.com.

Martin McLaughlin

unread,
Sep 12, 2022, 8:59:04 PM9/12/22
to Gremlin-users
All those import static lines are at the beginning of the TestTraversalDsl source code, which Maven uses to generate the new __ class. After compiling the package, I use this command in the Gremlin console:

import com.company.GremlinTestDsl.*

which should import all the newly generated classes:

__.class
DefaultTestTraversal.class
TestTraversal.class
TestTraversalDsl.class
TestTraversalSource.class
TestTraversalSourceDsl.class

Regardless, I also tried adding this line to the TestTraversalDsl source code and re-compiling:

import static com.company.GremlinTestDsl.__.*;

It compiled correctly, but the problem is still there.

Stephen Mallette

unread,
Sep 13, 2022, 8:21:27 AM9/13/22
to gremli...@googlegroups.com
I'm not sure I understand where your problem is. Is it in compiling the your DSL or is it in using your DSL? If it is using your DSL, then is your problem in using it Gremlin Console or simply part of your java code?

In any event, I think you just have an import issue. Note that using the gremlin-archetype-dsl I modified the test quickly to use an anonymous traversal with a DSL step and it works fine:

package com.my;

import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;

import org.junit.Test;

import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static com.my.__.youngestFriendsAge;

public class SocialDslTest {

    private Graph graph = TinkerFactory.createModern();
    private SocialTraversalSource social = traversal(SocialTraversalSource.class).withGraph(graph);
 
    @Test
    public void shouldGetAgeOfYoungestFriendOfMarko() {
        assertEquals(27, social.V().has("name","marko").map(youngestFriendsAge()).next().intValue());
        assertEquals(27, social.persons("marko").map(youngestFriendsAge()).next().intValue());
    }

}





Martin McLaughlin

unread,
Sep 13, 2022, 2:44:19 PM9/13/22
to gremli...@googlegroups.com
Thank you so much for your help, Stephen! Yes, the problem turned out to be an import in the Gremlin Console. The __ class had to be imported individually in addition to the blanket import statement.

import com.my.GremlinTestDsl.*;
import com.my.GremlinTestDsl.__;

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/6xkRaJBYQP0/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-H4383ERc2O7YqZ_4pMSbc-HvU%2BntU6GRiZSm9z_r6QL%2B_ww%40mail.gmail.com.

Thirumal M

unread,
Sep 14, 2022, 12:43:51 PM9/14/22
to gremli...@googlegroups.com
This repo will help you to set up DSL in eclipse and to get started. gremlin-dsl example in java spring boot

Reply all
Reply to author
Forward
0 new messages