Katlin
ATApache TinkerPop
•Created by Katlin on 4/4/2024 in #questions
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!5 replies