Pavan Kumar R R
Pavan Kumar R R

Reputation: 53

Gremlin Distance matrix with multiple edge properties of a path

I'm new to gremlin, please help me with a query for below graph data.

Gremlin sample graph

graph = TinkerGraph.open()
g = graph.traversal()
v1 = g.addV('4630').property('loc','B_1_1').next()
v2 = g.addV('4630').property('loc','C_1_1').next()
e1 = g.addE('sp').from(v1).to(v2).property('dist',1).property('anglein',90).property('angleout',45).next()
e2 = g.addE('sp').from(v2).to(v1).property('dist',2).property('anglein',190).property('angleout',145)

Expected result:

source destination dist angein angleout
B_1_1  C_1_1       1    90     145
C_1_1  B_1_1       2    190    145

Query that I'm trying is:

g.V().has('4630','loc',within('B_1_1','C_1_1')).
  outE('sp').
  inV().has('4630','loc',within('B_1_1','C_1_1')).
  path().
    by('loc').
    by(valueMap().select(values)).
    by('loc')

With below result

==>[B_1_1,[90,1,45],C_1_1]
==>[C_1_1,[190,2,145],B_1_1]

Want to have all the path edge properties in the result without any inner result. Please help how can I achieve the expected result?

Upvotes: 0

Views: 185

Answers (2)

Daniel Kuppitz
Daniel Kuppitz

Reputation: 10904

Adding to what Stephen already said, you could also get rid of the by() modulation in your path step and instead use the path elements to collect all the values you need afterward. This will save you a few traversers and thus it should be slightly faster.

g.V().has('4630','loc',within('B_1_1','C_1_1')).
  outE('sp').inV().has('4630','loc',within('B_1_1','C_1_1')).
  path().
  map(unfold().values('loc','dist','anglein','angleout').fold())

Also, note that even if you prefer the other query, you shouldn't use valueMap. valueMap().select(values) is just a waste of resources in my opinion.

Upvotes: 1

stephen mallette
stephen mallette

Reputation: 46206

It sounds like you just want to flatten your result.

gremlin> g.V().has('4630','loc',within('B_1_1','C_1_1')).
......1>   outE('sp').
......2>   inV().has('4630','loc',within('B_1_1','C_1_1')).
......3>   path().
......4>     by('loc').
......5>     by(valueMap().select(values)).
......6>     by('loc').
......7>   map(unfold().unfold().fold())
==>[B_1_1,90,1,45,C_1_1]
==>[C_1_1,190,2,145,B_1_1]

Each path will need to be flattened so you want to apply that operation with map(). To flatten you need to first unfold() the path and then unfold() each item in the path. Since the map() operation will only next() that child traversal you need to include a final fold() to convert that flattened stream of objects back to a List.

Upvotes: 1

Related Questions