Create edges on unrelated vertices using property equality

40 views
Skip to first unread message

Colin Hall

unread,
Sep 23, 2016, 2:44:58 PM9/23/16
to Gremlin-users
This seems like it should be a trivial think to accomplish but I'm struggling to find a solution to this using standard steps.

The generic statement of the problem is that there is a graph with pre-existing vertices that do not currently have an edge between them and I want to write a mutating traversal that will create an edge between them based on some common property value. 

So if when setting up the tinkergraph, if we did something like this:

graph.addVertex(T.label, "person","name", "marko", "age", 29,"dept_id",5);
graph
.addVertex(T.label, "person","name", "jim", "age", 42,"dept_id",5);
graph
.addVertex(T.label, "person","name", "joe", "age", 99,"dept_id",5);
graph
.addVertex(T.label, "person","name", "fred", "age", 33,"dept_id",8);
graph
.addVertex(T.label, "person","name", "hank", "age", 66,"dept_id",8);


graph
.addVertex(T.label,"department","name","engineering","dept_id",5);
graph
.addVertex(T.label,"department","name","management","dept_id",8);

but did not setup any edges, is there a mutating traversal one can concoct that will add edges from people to departments based on the common dept_id?

I realize that at this scale, one can do it with a series of one-off traversals such as

g.V().has('person','dept_id', '5').as('p').V().has('department','dept_id','5').addE('org_unit').from('p');

but if this was scaled to hundreds of departments and thousands of person vertices, that would not do. Intuitively, it seems like there should be a way to do this with a match but I can't figure out how to express the equality check on the property.

Any help greatly appreciated.

Thanks,
ch.




Robert Dale

unread,
Sep 23, 2016, 4:02:25 PM9/23/16
to gremli...@googlegroups.com
You are looking for Traversal Induced Values  - http://tinkerpop.apache.org/docs/current/recipes/#traversal-induced-values

graph = TinkerGraph.open()
g = graph.traversal()

depts = 20
people = 100

for (int dept = 0; dept < depts; dept++) {
    g.addV(label, "dept", "dept_id", dept, "name", "n"+dept).iterate()
}

for (int person = 0; person < people; person++) {
    g.addV(label, "person", "name", "n"+person, "age", (person%40+20), "dept_id", (person%depts)).iterate()
}

// the magic
g.V().hasLabel('person').as('p').V().hasLabel('dept').as('d').filter(select('p','d').by('dept_id').where('p', eq('d'))).addE('org_unit').from('p').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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/c5b91799-94c0-46a8-92bb-846ce169503c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Robert Dale

Colin Hall

unread,
Sep 23, 2016, 4:32:24 PM9/23/16
to Gremlin-users
Robert - this is pure gold!  I was actually just reading through the info on traversal induced values when your response came but your code has helped bring it all into focus.

Thanks very much.

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



--
Robert Dale
Reply all
Reply to author
Forward
0 new messages