Vertex not in descendants

68 views
Skip to first unread message

John Timm

unread,
Mar 5, 2021, 5:23:15 PM3/5/21
to Gremlin-users
I am  using the following gremlin (Java DSL) to compute the descendants of a node:

g.V().has("name", "John").repeat(__.in("parent")).emit()

I'd like to get all vertices that are not a descendant of John and I've tried using the not(...) step but can seem to find a working solution.

Thanks,

JT

Øyvind Sæbø

unread,
Mar 5, 2021, 8:39:56 PM3/5/21
to Gremlin-users
Hi, John

Another way to get all the vertices which are descendants of John (you?) would be like this:

g.V().filter(repeat(out("parent")).until(has("name", "John")))

Since this query filters away all nodes which are not descendants of John, rather than searching for them from John, it can more easily be transformed to a query which finds all nodes which are not descendants of John by simply replacing the filter(...) with a not(...), like this:

g.V().not(repeat(out("parent")).until(has("name", "John")))

John Timm

unread,
Mar 5, 2021, 8:55:28 PM3/5/21
to Gremlin-users
Very good! Thanks for the response. One more thing, what if I wanted to include "self" in the set making the query "not descendant or self"?

Thanks,

JT

John Timm

unread,
Mar 5, 2021, 9:09:13 PM3/5/21
to Gremlin-users
It looks like it is just another not in the chain:

g.V().not(repeat(out("parent")).until(has("name", "John"))).not(has("name", "John")

Øyvind Sæbø

unread,
Mar 6, 2021, 5:35:37 AM3/6/21
to Gremlin-users
Yes:)

Alternatively, you could also swap position of the repeat(...) and until(...), so that we check before every parent step if the current node is John, instead of after. Then the code inside not(...) will find John and all his descendants, and the full query will find all nodes except John and his descendants:

g.V().not(until(has("name", "John")).repeat(out("parent")))

John Timm

unread,
Mar 6, 2021, 11:39:58 AM3/6/21
to Gremlin-users
Thanks. I tried this alternative with the Java DSL as follows:

g.V().not(__.until(__.has("name", "John")).repeat(__.out("parent")))

and am getting a syntax error:

The method repeat(Traversal<?,Object>) in the type GraphTraversal<Object,Object> is not applicable for the arguments (GraphTraversal<Vertex,Vertex>). Any one out there using the Java DSL that can help me tweak this one?

Thanks,

JT


Graham Wallis

unread,
Mar 8, 2021, 6:57:32 AM3/8/21
to Gremlin-users
Hi John

I find the following avoids the parameter type warning:

GraphTraversal<Vertex,Object> rt = (__.out("parent");
GraphTraversal<Vertex, Vertex> gt = g.V().not(__.until(__.has("name""John")).repeat(rt)); 

Even trying to explicitly cast the repeat traversal resulted in warnings for me too. For some reason splitting it apart makes my IDE happier.

All the best 
  Graham

John Timm

unread,
Mar 8, 2021, 10:08:01 AM3/8/21
to Gremlin-users
Graham:

Thanks for the suggestion. I gave it a try and I still get a the following compiler error:

Type mismatch: cannot convert from GraphTraversal<Vertex,Vertex> to GraphTraversal<Vertex,Object>

on this statement:

GraphTraversal<Vertex,Object> rt = __.out("parent");

Any suggestion?

Thanks,

JT


Nicolas Trangosi

unread,
Mar 8, 2021, 10:26:33 AM3/8/21
to gremli...@googlegroups.com
You could just cast: g.V().not(__.until(__.has("name", "John")).repeat((GraphTraversal) __.out("parent")));

--
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/67e13459-46d2-4fed-b557-4416b402b280n%40googlegroups.com.


--

  

Nicolas Trangosi

Lead back

+33 (0)6 77 86 66 44      

   


https://dcbrain.com/documentation/

Ce message et ses pièces jointes peuvent contenir des informations confidentielles ou privilégiées et ne doivent donc pas être diffusés, exploités ou copiés sans autorisation. 
Si vous avez reçu ce message par erreur, veuillez le signaler a l'expéditeur et le détruire ainsi que les pièces jointes. 
Les messages électroniques étant susceptibles d'altération, DCbrain décline toute responsabilité si ce message a été altéré, déformé ou falsifié. Merci. 

This message and its attachments may contain confidential or privileged information that may be protected by law; they should not be distributed, used or copied without authorisation. If you have received this email in error, please notify the sender and delete this message and its attachments. As emails may be altered, DCbrain is not liable for messages that have been modified, changed or falsified. Thank you.

Graham Wallis

unread,
Mar 8, 2021, 12:18:12 PM3/8/21
to gremli...@googlegroups.com
Thanks Nicolas

That certainly removes the compiler error - although in my IDE (IJ) I try to strongly type everything to avoid getting “unchecked assignment” warnings.

I’m not really sure how to advise John. 

Best regards
  Graham 

Reply all
Reply to author
Forward
0 new messages