I am stuck on a graph traversal problem I seem to be having. I use gremlinpython and my DB is stored in AWS Neptune.
The graph looks similar to this example:
g = TinkerGraph.open().traversal()
o1 = g.addV('o').property('order','order1').next()
r1 = g.addV('r').property('recipe', 'recipe1').property('price', 3).next()
r2 = g.addV('r').property('recipe', 'recipe2').property('price', 3).next()
r3 = g.addV('r').property('recipe', 'recipe3').property('price', 3).next()
g.V(o1).addE('orders').to(r1).property('amount',2)
g.V(o1).addE('orders').to(r2).property('amount',3)
g.V(o1).addE('orders').to(r2).property('amount',4)
Now I would like to calculate the total cost of the order by multiplying the recipe values with their respecitve edge amounts and summing them up.
So far my code looks like this:
g.withSack(0).V('o1')\
.out()\
.sack(Operator.sum).by('price')\
.sack(Operator.mult)\
.by(g.V('o1').outE().values('amount')).sack().toList()
==>[6,6,6]
I noticed that this code will only multiply each recipe value with the first edges amount, but not the respective amounts. I am unsure how to specify in the sack().by() operation that each vertex should be multiplied with the respective edge amount.
Your Gremlin script to create the data needed some fixes:
g = TinkerGraph.open().traversal()
o1 = g.addV('o').property('order','order1').next()
r1 = g.addV('r').property('recipe', 'recipe1').property('price', 3).next()
r2 = g.addV('r').property('recipe', 'recipe2').property('price', 3).next()
r3 = g.addV('r').property('recipe', 'recipe3').property('price', 3).next()
g.V(o1).addE('orders').to(r1).property('amount',2)
g.V(o1).addE('orders').to(r2).property('amount',3)
g.V(o1).addE('orders').to(r2).property('amount',4)
You were pretty close with your approach so hopefully my small changes make sense to you:
gremlin> g.withSack(0).V(o1).
......1> outE().
......2> sack(Operator.assign).by('amount').
......3> inV().
......4> sack(Operator.mult).by('price').
......5> sack().
......6> sum()
==>27