笔者描述的问题大部分情况下都不是一个问题。然而在一些场景下,小精灵步骤的名称会与Groovy语言的保留字或者方法名冲突。记住,小精灵的代码是采用Java和Groovy语言写的。如果您遇到了这样的情况,遇到了错误信息提示,但是您根本没这么做,显然您是遇到了这个特别的情况。让我们来起几个例子吧。在小精灵里一个步骤的名称有时会在这个步骤里遇到命名冲突。然而您根本不用担心这种情况。先看下面这个查询。
Most of the time the issue I am about to describe will not be a problem. However, there are cases
where names of Gremlin steps conflict with reserved words and method names in Groovy.
Remember that Gremlin is coded in Groovy and Java. If you hit one of these cases, often the error
message that you will get presented with does not make it at all clear that you have run into this
particular issue. Let’s look at some examples. One step name in Gremlin that can sometimes run
into this naming conflict is the in step. However, you do not have to worry about this in all cases.
First take a look at the following query.

 这个查询根本没有产生错误,它正确的返回了所有与AUS顶点相连的入边的那些顶点。这没有名称冲突,这是因为in步骤引用的是has步骤的结果。然而看下这个例子。

That query does not cause an error and correctly returns all of the vertices that are connected by
an incoming edge, to the AUS vertex. There is no conflict of names here because it is clear that the
in reference applies to the result of the has step. However, now take a look at this query. 

 在这个例子中,in步骤是对于自身的操作而没有关联前边的步骤。小精灵气运行时(记住它是用Groovy语言编写的)将会试着解析它,这将会抛出一个错误,因为它认为这上引用了它自己的in 方法。为了让这个查询工作,我们可以对语法稍稍调整,写成下面这样。

In this case the in is on its own and not dot connected to a previous step. The Gremlin runtime
(which remember is written in Groovy) will try to interpret this and will throw an error because it
thinks this is a reference to its own in method. To make this query work we have to adjust the
syntax slightly as follows.

 注意,笔者加了双下划线,在in步骤之前。这是"从当前我们在的东西里查找"的简写。在这个例子中,就是has步骤的结果。

Notice that I added the "__." (underscore underscore period) in front of the in step. This is shorthand
for "the thing we are currently looking at", so in this case, the result of the has step.
现在没有太多的Groovy保留字需要担心的。有三个您需要格外的小心,它们是in, not 和as,在小精灵和Groovy中这几个词都有特别的作用。尽管如此,您只需要记得在不是很清楚什么保留字,比如in应用的时候,使用双下划线标记符。
There are currently not too many Groovy reserved words to worry about. The three that you have
to watch out for are in, not and as which have special meanings in both Gremlin and Groovy.
Remember though, you will only need to use the "__." notation when it is not clear what the
reserved word, like in, applies to.
在“把一个有序二叉树建模成图”这一节中您将会找到不使用双下划线前缀的例子。
You will find an example of not being used with the "__." prefix in the "Modelling an ordered binary
tree as a graph" section a bit later on. 
posted on 2022-04-21 10:32  bokeyuannicheng0000  阅读(21)  评论(0)    收藏  举报