Augustus
Augustus

Reputation: 634

How to create new edges using common nodes in Gremlin?

Suppose ,

A , B , C, D are companies (companiesName) and P1, P2, P3, P4 are parent companies (parentCompanyName)

They are related as :

P1 <- sub_of - A ->sells ->B -> sub_of -> P1   
P1 <- sub_of - C ->sells-> D -> sub_of -> P2  
P3 <- sub_of - A ->sells ->C -> sub_of -> P3  
P4 <- sub_of - D ->sells ->B -> sub_of -> P1
P1 <- sub_of - A ->sells2010-> B -> sub_of -> P1  
P1 <- sub_of - A ->sells2011-> B -> sub_of -> P4  
P1 <- sub_of - A ->in_country-> B -> sub_of -> P1

where sells edge has attribute totalCount and amount.

I want to create new edges like sells_I, sells2010_I etc if the companies have commonparentNode as per line 1, 3, 5 .But in line 6 in_country edge also satisfy the condition.I don't want to include this edge.

As per answer given By @Stephen :

g = new TinkerGraph()
P1 = g.addVertex("P1")
P2 = g.addVertex("P2")
P3 = g.addVertex("P3")
P4 = g.addVertex("P4")
A = g.addVertex("A")
B = g.addVertex("B")
C = g.addVertex("C")
D = g.addVertex("D")

g.addEdge(A, P1, "SUB_OF")
g.addEdge(B, P1, "SUB_OF")
g.addEdge(C, P1, "SUB_OF")
g.addEdge(D, P2, "SUB_OF")
g.addEdge(A, P3, "SUB_OF")
g.addEdge(C, P3, "SUB_OF")
g.addEdge(D, P4, "SUB_OF")
g.addEdge(B, P4, "SUB_OF")
g.addEdge(A, B, "sells")
g.addEdge(C, D, "sells")
g.addEdge(A, C, "sells")
g.addEdge(D, B, "sells")
g.addEdge(A, B, "sells2010")
g.addEdge(A, B, "sells2011")
g.addEdge(A, B, "IN_COUNTRY")

g.E.has('label','sells').filter{it.inV.out('SUB_OF').next()==it.outV.out('SUB_OF').next()}.transform({[it.inV().next(),it.outV().next()]})

==>[v[C], v[A]]
==>[v[B], v[A]]

x = []
g.E.has('label','sells').filter{it.inV.out('SUB_OF').next()==it.outV.out('SUB_OF').next()}.transform({[it.inV().next(),it.outV().next()]}).dedup().aggregate(x)
for ( i in x ) {
    c = i[0] 
    d = i[1]
    g.addEdge(c, d, "sells_I", [amt : 100])
}

Is it possible to create the new edge sells_I and after that remove the old sells edge like below ? Because there are so many edges/nodes in my original dataset. Can i use BatchGraph for this.

below queries throwing error.

g.E.has('label','sells').filter{it.inV.out('SUB_OF').next()==it.outV.out('SUB_OF').next()}.transform({[it.inV().next(),it.outV().next()]}).sideEffect{g.addEdge(it.inVertex, it.outVertex, 'sells_I'); g.removeEdge(it)}


or

g.E.has('label','sells').filter{it.inV.out('SUB_OF').next()==it.outV.out('SUB_OF').next()}.transform({[it.inV().next(),it.outV().next()]}).sideEffect{g.addEdge(it.inV().next(), it.outV().next(), 'sells_I'); g.removeEdge(it)}

any suggestions or help ?

thanks. :)

Upvotes: 0

Views: 141

Answers (1)

stephen mallette
stephen mallette

Reputation: 46226

Iterate the edges for the edge labels you care about, then filter where the parents are equals:

g.E.has('label','sells').filter{it.inV.out('sub_of').next()==it.outV.out('sub_of').next()}

That will give you the list of vertex pairs that need a new edge. To avoid duplicates you might extend the above statement use dedup or aggregate them into a Set on your own. Once you have that set you can create you new edges.

Upvotes: 1

Related Questions