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
S
spmallette12d ago
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').
......4> by('requirement1').
......5> by('requirement2').
......6> by('requirement3').
......7> by(choose(has('requirement1', true).has('requirement2', true), sack(sum).by(constant(1))).
......8> choose(has('requirement3', true), sack(sum).by(constant(1))).
......9> sack())
==>[name:A,requirement1:true,requirement2:true,value:1]
==>[name:B,requirement1:true,requirement2:false,value:0]
==>[name:C,requirement1:false,requirement2:false,requirement3:true,value:1]
gremlin> g.withSack(0).V().
......1> project('id', 'name', 'requirement1', 'requirement2', 'requirement3', 'value').
......2> by('id').
......3> by('name').
......4> by('requirement1').
......5> by('requirement2').
......6> by('requirement3').
......7> by(choose(has('requirement1', true).has('requirement2', true), sack(sum).by(constant(1))).
......8> choose(has('requirement3', true), sack(sum).by(constant(1))).
......9> sack())
==>[name:A,requirement1:true,requirement2:true,value:1]
==>[name:B,requirement1:true,requirement2:false,value:0]
==>[name:C,requirement1:false,requirement2:false,requirement3:true,value:1]
I'm not sure how complex your conditionals will end up being but with some proper indentation i think it can stay readable.
K
Katlin8d ago
Thank you, this is a much better solution!
Want results from more Discord servers?
Add your server
More Posts
Systems Analysis Report on Apache TinkerPop - Where to Start?Hey all, I'm currently writing an alaysis on Apache TinkerPop for grad school and was just hoping thLambda example in TypeScriptDoes anyone know where I can find example code that demonstrates up-to-date best practices for writimergeE(): increment counter on matchHi, is there an easy way to increment an existing edge property based on its current value using `meSerialization IssueI have a weird error, when I am connecting with JanusGraph gremlin client using `conf/remote-graph-Design decision related to multiple heterogenous relational graphsI'm working with over 100k instances of heterogeneous, relational node-and-edge attributed graphs, eStackoverflow when adding a larger list of property values using traverser.property()Hey, we encounter a stack overflow: ``` Exception during Transaction, rolling back ... org.apache.tijava: package org.apache.tinkerpop.shaded.jackson.core does not existWhile trying to `mvn clean install` with jdk11, I ran into the above error using the master branch. Performance issue in large graphsWhen performing changes in large graph (ca. 100K nodes, 500K edges) which is stored in one kryo fileConcurrent queries to authentication required sever resulted in 401 errorHey guys, playing around with gremlin & encountered this very odd error where concurrent queries wilDiscrepancy between console server id conventions and NeptuneSo I'm working with my test server and on Neptune--and I'm noticing a difference in the type of the