1
votes

I have been trying to figure this if-else in a Gremlin query. Assume g.V({0}) is the group vertex below.

    var q = "g.V({0}).as('groupName', 'groupId', 'ownerId').inE(eIsAdminOf, eIsMemberOf).as('rel', 'joinDate').outV().hasLabel(userLabel).as('memberId')";
  //TODO:var q = "g.V({0}).as('groupName', 'groupId', 'ownerId').inE(eIsAdminOf";
  //if .has('mCanList',true).inE(eIsAdminOf, eIsMemberOf)
  //if .has('mCanList',false).inE(eIsAdminOf)

  //, eIsMemberOf).as('rel', 'joinDate').outV().hasLabel(userLabel).as('memberId')";

I want the .inE(eIsAdminOf, eIsMemberOf) to be based on property value mCanList of true or false as in the comments above.

Have been trying a choose to no avail:

var q = "g.V({0}).as('groupName', 'groupId', 'ownerId','mCanList');
  q += ".by(values('mCanList').choose(is(true),.inE(eIsAdminOf, eIsMemberOf), .inE(eIsAdminOf))";
  q += '.as('rel', 'joinDate').outV().hasLabel(userLabel).as('memberId')”;

I am using node.js to build the gremlin query with the gremlin library. The worst option for me is to build 2 separate async queries which build the results separately based on

if .has('mCanList',true).inE(eIsAdminOf, eIsMemberOf) or
 if .has('mCanList',false).inE(eIsAdminOf) 

TIA

1

1 Answers

7
votes

I'm not sure that I follow the reasoning behind all the step labeling that you have so I've mostly omitted that to demonstrate use of choose() which seems to be the focus of your question. I roughly approximated what I think your graph structure is based on how you described the problem (if you have future questions, please consider providing some sample graph creation code that can be easily cut/paste into the a Gremlin Console session). In any case, here's what I think you need:

gremlin>  g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV(id,1).property("mCanList",true).as('a').
......1>   addV(id,2).as('b').
......2>   addV(id,3).as('c').
......3>   addE("isAdminOf").from("b").to("a").
......4>   addE("isMemberOf").from("c").to("a").iterate()
gremlin> g.V(1).choose(has('mCanList',true),inE("isAdminOf","isMemberOf"),inE("isAdminOf"))
==>e[1][2-isAdminOf->1]
==>e[2][3-isMemberOf->1]
gremlin> 
gremlin> g.V(1).property('mCanList',false)
==>v[1]
gremlin> g.V(1).choose(has('mCanList',true),inE("isAdminOf","isMemberOf"),inE("isAdminOf"))
==>e[1][2-isAdminOf->1]

If I try to directly edit your Gremlin I think your traversal basically just needs to look like this:

var q = "g.V({0});
  q += ".choose(has('mCanList',true),inE(eIsAdminOf, eIsMemberOf), inE(eIsAdminOf))";
  q += ".outV().hasLabel(userLabel)"; 

I presume that "eIsAdminOf", "eIsMemberOf" and "userLabel" are JS variables - if not they would need quotes around them if they happen to be actual label names. Again, I'm not clear on what you were doing with all the uses of as() - none of that seemed relevant to your traversal based on your question.