简体   繁体   English

如何在 Gremlin 中按边值对顶点进行排序

[英]How sort vertices by edge values in Gremlin

Given the air-routes graph, say I want to get all possible one-stopover routes, like so:给定空中路线图,假设我想获得所有可能的一站式路线,如下所示:

[home] --distance--> [stopover] --distance--> [destination]

where [home], [stopover] and [destination] are airport nodes that each have a property 'code' that represent an airport code;其中 [home]、[stopover] 和 [destination] 是机场节点,每个节点都有一个属性“code”,表示机场代码; and distance is an integer weight given to each edge connecting two airport nodes.距离是 integer 权重,赋予连接两个机场节点的每条边。

How could I write a query that gets me the airport codes for [home], [stopover] and [destination] such that the results are sorted as follows:我如何编写一个查询来获取 [home]、[stopover] 和 [destination] 的机场代码,以便结果按如下方式排序:

  1. [home] airport codes are sorted alphabetically. [home] 机场代码按字母顺序排序。
  2. For each group of [home] airport, the [stopover] airport codes are sorted by the distance between [home] and [stopover] (ascending).对于每组 [home] 机场,[stopover] 机场代码按 [home] 和 [stopover] 之间的距离(升序)排序。
  3. After sorting 1 and 2, [destination] airport codes are sorted by the distance between [stopover] and [destination].排序 1 和 2 后,[destination] 机场代码按照 [stopover] 和 [destination] 之间的距离排序。

(Note: it doesn't matter if [home] and [destination] are the same airport) (注意:[home] 和 [destination] 是否是同一个机场无关紧要)

One way you could do this is through group with nested by modulation.您可以做到这一点的一种方法是by调制嵌套的group

g.V().
  group().
    by('code').
    by(
      outE('route').
      order().by('dist').
      inV().
      group().
        by('code').
        by(
          outE('route').
          order().by('dist').
          inV().
          values('code').fold())).
  unfold()

The result is something like:结果是这样的:

1.  {'SHH': {'WAA': ['KTS', 'SHH', 'OME'], 'OME': ['TLA', 'WMO', 'KTS', 'GLV', 'ELI', 'TNC', 'WAA', 'WBB', 'SHH', 'SKK', 'KKA', 'UNK', 'SVA', 'OTZ', 'GAM', 'ANC']}}
2.  {'KWN': {'BET': ['WNA', 'KWT', 'ATT', 'KUK', 'TLT', 'EEK', 'WTL', 'KKH', 'KWN', 'KLG', 'MLL', 'KWK', 'PQS', 'CYF', 'KPN', 'NME', 'OOK', 'GNU', 'VAK', 'SCM', 'HPB', 'EMK', 'ANC'], 'EEK': ['KWN', 'BET'], 'TOG': ['KWN']}}

3.  {'NUI': {'SCC': ['NUI', 'BTI', 'BRW', 'FAI', 'ANC'], 'BRW': ['ATK', 'AIN', 'NUI', 'PIZ', 'SCC', 'FAI', 'ANC']}}

4.  {'PSG': {'JNU': ['HNH', 'GST', 'HNS', 'SGY', 'SIT', 'KAE', 'PSG', 'YAK', 'KTN', 'ANC', 'SEA'], 'WRG': ['PSG', 'KTN']}}

5.  {'PIP': {'UGB': ['PTH']}}
.
.
.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM