Traversal Induced Values with sums of values

36 views
Skip to first unread message

Theodoros Georgiou

unread,
May 17, 2018, 8:06:24 PM5/17/18
to Gremlin-users
Hello,

First of all, I need to apologise for my naivety. 
I am a complete noob working with Gremlin/Groovy.
This is probably a silly question, but here goes nothing...

I am using the code from the Recipes page on "Traversal Induced Values".
Specifically the "complex" example:

gremlin> g.addV('tank').property('name', 'a').property('amount', 100.0).as('a').
           addV
('tank').property('name', 'b').property('amount', 0.0).as('b').
           addV
('tank').property('name', 'c').property('amount', 0.0).as('c').
           addE
('drain').property('factor', 0.5).from('a').to('b').
           addE
('drain').property('factor', 0.1).from('b').to('c').iterate()
gremlin
> a = g.V().has('name','a').next()
==>v[0]
gremlin
> g.withSack(a.value('amount')).
           V
(a).repeat(outE('drain').sack(mult).by('factor').
                       inV
().property('amount', sack())).
               
until(__.outE('drain').count().is(0)).iterate()

However, this is replacing the 'amount' in each node. I would like to accumulate it.
So instead of 

==>[amount:[100.0],name:[a]]
==>[
amount:[50.00],name:[b]]
==>[
amount:[5.000],name:[c]]

I would like to have a starting 'amount' for each vertice and in every 'hop' to add the new value to the existing.
i.e. if amount for 'a' was 100, 'b' 10, and 'c' 20, the output would be:

==>[amount:[100.0],name:[a]]
==>[
amount:[60.00],name:[b]]
==>[
amount:[25.000],name:[c]]

I expect this to be a dead easy solution, but I really cannot figure it out... :|

Thank you in advance.
Theo.



Daniel Kuppitz

unread,
May 20, 2018, 1:46:02 PM5/20/18
to gremli...@googlegroups.com
Hi,

first, let's shorten your code a bit.

a = g.V().has('name','a').next()
g.withSack(a.value('amount')).
  V(a).repeat(outE('drain').sack(mult).by('factor').
              inV().property('amount', sack())).
         until(__.outE('drain').count().is(0)).iterate()

...can be rewritten as:

g.V().has('name','a').sack(assign).by('amount').
  repeat(outE('drain').sack(mult).by('factor').
         inV().property('amount', sack())).
    until(__.not(outE('drain'))).iterate()

Next, the traversal will terminate anyway if there is no drain out-edge left, hence we don't need to check that after every repeat() iteration and we can shorten the traversal further:

g.V().has('name','a').sack(assign).by('amount').
  repeat(outE('drain').sack(mult).by('factor').
         inV().property('amount', sack(sum).by('amount').sack())).
  iterate()

Now, to accumulate the values you just need to add a sack(sum):

g.V().has('name','a').sack(assign).by('amount').
  repeat(outE('drain').sack(mult).by('factor').
         inV().property('amount', sack(sum).by('amount').sack())).
  iterate()

Output:

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('tank').property('name', 'a').property('amount', 100.0).as('a').
......1>   addV('tank').property('name', 'b').property('amount', 10.0).as('b').
......2>   addV('tank').property('name', 'c').property('amount', 20.0).as('c').
......3>   addE('drain').property('factor', 0.5).from('a').to('b').
......4>   addE('drain').property('factor', 0.1).from('b').to('c').iterate()
gremlin> g.V().has('name','a').sack(assign).by('amount').
......1>   repeat(outE('drain').sack(mult).by('factor').
......2>          inV().property('amount', sack(sum).by('amount').sack())).
......3>   iterate()
gremlin> 
gremlin> g.V().valueMap()
==>[amount:[100.0],name:[a]]
==>[amount:[60.00],name:[b]]
==>[amount:[25.000],name:[c]]

Cheers,
Daniel



--
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/3359b903-8660-4988-9633-f3a4e32a2b0c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages