小精灵提供一种repeat...until的循环结构,这个与一些编程语句很类似。这为我们提供了一种简单的执行查询的方式。我们可以使用这一结构来查找两个机场顶点之间的路径,不需要指定out步骤的次数。
Gremlin provides a repeat…until looping construct similar to those found in many programming
languages. This gives us a nice way to perform simple shortest path type queries. We can use a
repeat…until loop to look for paths between two airports without having to specify an explicit
number of out steps to try.
在进行这样的计算时,我们可能不想再次走过我们已经走过的路径。我们可以使用simplePath步骤来实现这上目标。这样做可以加快查询的速度,不需要在一幅图中多次走过同一路径。如果没有simplePath步骤,在遍历访问一幅图时,我们会花费更多的时间。limit步骤也是相当重要的,没有它,查询会运行更多的时间去查看每一个可能的路径。
While performing such computations, we may not want paths we have already travelled to be
travelled again. We can ask for this behavior using the simplePath step. Doing so will speed up
queries that do not need to travel the same paths through a graph multiple times. Without the
simplePath step being used the query we are about to look at could take a lot longer. The addition of
a limit step is also important as without it this query will run for a LONG time looking for every
possible path!!
下面的查询是找出AUS和AGR之间的路线。这是AUS想到访问Taj Mahal的一个重要的查询。
The query below looks for routes between Austin (AUS) and Agra (AGR). An important query for
those Austinites wanting to visit the Taj Mahal!
 

 

运行这个查询的结果在这。注意repeat until结构的使用,我们不需要指定要试多少步。

Here are the results from running the query. Notice how, using the repeat…until construct we did
not have to specify how many steps to try.
 

 

您也可以把until放在repeat步骤之前,如下所示:

You can also place the until before the repeat as shown below.
 

 

这是运行查询的结果。

Here are the results from running the query.
 

 

我们可以在使用repeat times 循环结构时指定out步骤尝试的次数,但是这个假定我们提前知道我们要找的两个机场之前有多少次中转。在下一节中,我们将会介绍emit步骤,您可以更好的控制repeat循环的返回内容。

We can also specify an explicit number of out steps to try using a repeat…times loop but this of
course assumes that we know ahead of time how many stops we want to look for between airports.
In the next section we will introduce the emit step that gives you more control over the behavior of
what is returned from repeat loops.
 

 

前边的查询和下边的这个效果相同,但是这个更不灵活一些,我们不能灵活的改变out步骤的次数,如查我们想把当前的中转两次变为中转五次。

The previous query is equivalent to this next one but doing it this way is less flexible in that we can
not as easily vary the number of out steps, should, for example we want to next try five hops
instead of the current two.
 

 

在使用小精灵时,总有多种方式可以获得相同的结果。loop步骤,常用于控制重复循环的运行,本质上它和times步骤是一样的。看下面的两个查询。都得到了相同的结果。第一个用到了loop,而第二个用到了times.笔者喜欢使用times带来的好的可读性。

As is often the case when working with Gremlin there is more than one way to achieve the same
result. The loops step, that can be used to control how long a repeat loop runs, is essentially
equivalent to the times step. Take a look at the two queries below. Both achieve the same result. The
first uses loops while the second uses times. I prefer the readability offered by the use of times.
 

 

如果您只是想找到两个机场之间的路径。有这一个优化的方法可以使用。在“两个机场之间存在路径吗?”这一节中将研究该内容。

If you simply want to find out if any route exists between two airports, there is a
nice optimization that can be used. This is discussed in the "Does any route exist
between two airports?" section later in the book.
 下一节,我们一起看一下如何使用emit来调节repeat...times 循环的行为。
In the next section, we will look at how emit can be used to adjust the behavior of a repeat…times
loop.
posted on 2022-04-20 12:18  bokeyuannicheng0000  阅读(83)  评论(0)    收藏  举报