Created
April 5, 2013 02:54
-
-
Save dkuppitz/5316249 to your computer and use it in GitHub Desktop.
Link 2 vertices in Gremlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// link two vertices, if the link does not already exists | |
// return the new or the existing link / edge | |
Gremlin.defineStep('link', [Vertex,Pipe], { direction, label, other -> | |
exists = false | |
dirs = direction.toString() | |
dir = dirs.toLowerCase() | |
edge = dir + "E" | |
oppV = direction.opposite().toString().toLowerCase() + "V" | |
link = "link" + dirs.substring(0,1) + dir.substring(1) | |
_()."$dir"(label).filter{it==other}.sideEffect{exists=true}.optional(3).ifThenElse{exists} \ | |
{it."$edge"(label)."$oppV"().retain([other]).back(2)} \ | |
{it."$link"(label, other)."$edge"(label)."$oppV".retain([other]).back(2)} | |
}) | |
// example | |
v1 = g.addVertex() | |
v2 = g.addVertex() | |
v3 = g.addVertex() | |
v1.link(OUT, 'knows', v3) // v1 knows v3 | |
v2.link(IN, 'knows', v3) // v3 knows v2 | |
v1.link(BOTH, 'knows', v2) // v1 knows v2, v2 knows v1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment