Comparing property values?

1,495 views
Skip to first unread message

John Whelan

unread,
Apr 29, 2015, 5:56:53 PM4/29/15
to gremli...@googlegroups.com
I'm looking to match a property value from a previously traversed vertex to the property value of a different vertex. I'm assuming that I should be keeping track of the previous value with as(), but I cannot figure out how to use the value later in the traversal for comparison. Am I close? (As highlighted below, if I hard code the value, it seems to work.However, I will not always know the value ahead of the traversal )


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


public class SameAge {


 
public static void main(String[] args) {
 
Graph graph = TinkerGraph.open();
 
Vertex a = graph.addVertex();
 
Vertex b = graph.addVertex();
 
Vertex c = graph.addVertex();
 a
.property("name", "Henry");
 a
.property("age", "9");
 b
.property("name", "Carlos");
 b
.property("age", "9");
 c
.property("name", "Teagan");
 c
.property("age", "7");
 a
.addEdge("knows", b);
 a
.addEdge("knows", c);


 
GraphTraversalSource g = graph.traversal();
 
GraphTraversal<Vertex, String> query = g.V(a).as("start").properties("age").as("years").select("start").out().has("age", "9").values("name");
 
 
while (query.hasNext()) {
 
String result = query.next();
 
System.out.println("result = " + result);
 
}
 
}
}

Daniel Kuppitz

unread,
Apr 29, 2015, 8:54:49 PM4/29/15
to gremli...@googlegroups.com
Using retain():

gremlin> g.V(a).as("x").values("age").as("age").select("x").out().has(values("age").retain("age")).values("name")
==>Carlos

Using match():

gremlin> g.V(a).match("a",
gremlin>   __.as("a").values("age").as("age1"),
gremlin>   __.as("a").out().as("b"),
gremlin>   __.as("b").values("age").as("age2")).where("age1", eq("age2")).select("b").values("name")
==>Carlos

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/6041a87d-a8b6-4fa6-863a-8fabcaac6e81%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Whelan

unread,
Apr 29, 2015, 11:00:01 PM4/29/15
to gremli...@googlegroups.com

Gwendal Daniel

unread,
Sep 26, 2016, 8:48:43 PM9/26/16
to Gremlin-users
Hi, do you know if it is possible to achieve the same with gremlin 2? I am stuck to this version and the traversal you propose does not seem to be compatible with previous versions of gremlin.
Thanks!

Daniel Kuppitz

unread,
Sep 27, 2016, 6:42:06 AM9/27/16
to Gremlin-users
No, TP2 didn't have anything close to this. You'll have to use good old lambdas,

Cheers,
Daniel

Suvendu Mohanty

unread,
Feb 9, 2023, 3:19:24 AM2/9/23
to Gremlin-users
Thannk you Daniel, this was helpful for a problem I was working. Kudos to you.
Reply all
Reply to author
Forward
0 new messages