graph
graph copied to clipboard
Add example of how to use multiple values in a sack
trafficstars
Gremlin currently does not provide a way for a sack to contain multiple values. You can do it using a map, but it gets a bit messy. It would be good to have an example in the book that shows how to work around this. Such as, given the following graph:
g.addV('stop').property(id,'s1').as('s1').
addV('stop').property(id,'s2').as('s2').
addV('stop').property(id,'s3').as('s3').
addV('stop').property(id,'s4').as('s4').
addV('stop').property(id,'s5').as('s5').
addV('stop').property(id,'s6').as('s6').
addV('stop').property(id,'s7').as('s7').
addE('link').from('s1').to('s2').property('p1',1).property('p2',2).
addE('link').from('s2').to('s3').property('p1',3).property('p2',4).
addE('link').from('s3').to('s4').property('p1',5).property('p2',6).
addE('link').from('s4').to('s5').property('p1',7).property('p2',8).
addE('link').from('s5').to('s6').property('p1',9).property('p2',10).
addE('link').from('s6').to('s7').property('p1',11).property('p2',12)
We could work with two sets of edge properties as follows
g.withSack(['a':0,'b':0]).
V('s1').
repeat(
outE().as('x').
sack(assign).
by(project('a','b').
by(sack().math('a + _').by().by(select('x').values('p1'))).
by(sack().math('b + _').by().by(select('x').values('p2')))).
inV()).
until(not(out())).
sack()
Which produces
{'a': 36.0, 'b': 42.0}
Other examples could include complex list manipulation such as
gremlin> g.V(1,2,3).values('city').fold().sack(assign)
==>[Atlanta,Anchorage,Austin]
gremlin> g.V(1,2,3).values('city').fold().sack(assign).sack()
==>[Atlanta,Anchorage,Austin]
gremlin> g.V(1,2,3).values('city').fold().sack(assign).V(4).union(values('city'),sack().unfold()).fold().sac
k(assign).sack()
==>[Nashville,Atlanta,Anchorage,Austin]