Search code examples
gremlin

Find all the Vertexes which are connected to hope through all edges


I want to find all the Vertexes which are connected to actions(with edge label allow), those are marked as critical by CriticalAction1.

The condition is that It should be connected with all the actions which are marked as critical action by CriticalAction1 vertex.

So in following image, since CriticalAction1 has 2 actions Action1 & Action2. Node1 is connected with total 3 actions including Action1 & Action2. So it should be output result. Node2 is connected with only 1 action. which doesn't satisfy the criteria that it should be connected with all the actions which are marked as critical by CriticalAction1.

enter image description here

Sample data script:

g.addV('Node1').as('node1'). addV('Node2').as('node2'). addV('Action1').as('action1'). addV('Action2').as('action2'). addV('Action3').as('action3'). addV('CriticalAction1').as('criticalaction1'). addE('allow').from('node1').to('action1'). addE('allow').from('node1').to('action2'). addE('allow').from('node1').to('action3'). addE('allow').from('node2').to('action1'). addE('critical action').from('criticalaction1').to('action1'). addE('critical action').from('criticalaction1').to('action2').iterate() 

Solution

  • You can use aggregate to store all the critical action connections and then groupCount the node you found from them.

    g.V().hasLabel('CriticalAction1').
      out('critical action').
      aggregate('actions').in('allow').
      groupCount().by().unfold().
      where(eq('actions')).
        by(select(values)).
        by(count(local)).
      select(keys)
    

    example: https://gremlify.com/wn9cge4knrh

    EDIT:

    for multiple CriticalAction:

    g.V().hasLabel('CriticalAction').project('CriticalAction', 'Nodes')
        .by(valueMap())
        .by(
          out('has critical action').fold()
          aggregate('actions').unfold().in('allow').
          groupCount().by().unfold().
          where(eq('actions')).
            by(select(values)).
            by(unfold().tail(1).count(local))
        .select(keys).valueMap().fold())
    

    example: https://gremlify.com/fg2inyhnm9n