Conditionally updating a variable with choose()
How do I create and update a variable with a conditional? I need a number to be calculated based on an arrangement of requirements. Here is a very simplified example of what I was trying to do:
g.addV('App').property('name', 'A').property('requirement1', true).property('requirement2', true).
addV('App').property('name', 'B').property('requirement1', true).property('requirement2', false).
addV('App').property('name', 'C').property('requirement1', false).property('requirement2', false).property('requirement3', true)
g.V().
project('id', 'name', 'requirement1', 'requirement2', 'value').
by('id').
by('name').
by('requirement1').
by('requirement2').
by(
map{
total = 0;
choose(
and(
has('requirement1', true),
has('requirement2', true)),
constant(total + 1),
identity()
)
choose(
has('requirement3', true),
constant(total + 1),
identity()
)
total
}
)
I ultimately found a solution, but I ended up having to create different conditionals for every possible combination. It is repetitive and hard to read. Is there a way to do this, that is similar to what I shared above? Thank you!Solution:Jump to solution
sorry, but it looks like this question was missed last week somehow. i think you're mostly on track for the best way to do this by using
choose()
. you could simplify your syntax a fair bit i think by using sack()
:
```gremlin> g.withSack(0).V().
......1> project('id', 'name', 'requirement1', 'requirement2', 'requirement3', 'value').
......2> by('id').
......3> by('name')....2 Replies
Solution
sorry, but it looks like this question was missed last week somehow. i think you're mostly on track for the best way to do this by using
choose()
. you could simplify your syntax a fair bit i think by using sack()
:
I'm not sure how complex your conditionals will end up being but with some proper indentation i think it can stay readable.Thank you, this is a much better solution!