找到所有与希望息息相关的顶点

问题描述

我想找到所有与动作相关的顶点(允许带有边缘标签),这些顶点由CriticalAction1标记为关键。

条件是它应该与所有由CriticalAction1顶点标记为关键动作的动作相关。

因此在下图中,由于CriticalAction1具有2个动作Action1和Action2。 Node1与总共3个动作相连,包括Action1和Action2。因此它应该是输出结果。 Node2仅通过1个动作连接。不符合应与CriticalAction1标记为关键的所有操作相关的条件。

enter image description here

样本数据脚本:

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() 

解决方法

您可以使用aggregate存储所有关键操作连接,然后使用groupCount 您从他们那里找到的节点。

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)

示例:https://gremlify.com/wn9cge4knrh

编辑:

针对多个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())

示例:https://gremlify.com/fg2inyhnm9n