在前边一节中我们用一个闭表来过滤值,用到了正则表达式。现在我们知道了如何创建我们自己的断言, 我们可以更进一步,创建一个断言来接收正则表达式。
In the previous section we used a closure to filter values using a regular expression. Now that we
know how to create our own predicates we could go one step further and create a predicate that
accepts regular expressions for us.
 

 

我们可以再进一步,创建一个自定义的名称是regex的方法而不是直接使用测试方法。如果下南的代码看起来有点不明白,别太担心。它工作,那是您需要知道的全部。然而,如果您想要理解TinkerPop API 的细节,文档可以从阿帕奇Tinkerpop 网站找到,它解释了像P这样的细节。总是记得小精灵语言是用Groovy和Java编写的,我们在这也可以利用这点。

We can actually go one step further and create a custom method called regex rather than use the
test method directly. If the following code seems a bit unclear don’t worry too much. It works and
that may be all you need to know. However if you want to understand the TinkerPop API in more
detail the documentation that can be found on the Apache TinkerPop web page explains things like
P in detail. Also remember that Gremlin is written in Groovy/Java and we take advantage of that
here as well.
下面的例子不是直接用了测试test,我们用了BiPredicate方法接口,它是Java8的一部分。BiPredicate有时也被称为是双参数断言,因为它可以接收两个参数。我们会创建一个接口的实现称为bp. 这个接口需要我们提供一个称为test的方法,实际上就是比较两个对象,返回真或假。就像我们在前一节做的一样,我们可以进行一个简单的正则比较,用至==~运算符。
In the following example, rather than use test directly we use the BiPredicate functional interface
that is part of Java 8. BiPredicate is sometimes referred to was a two-arity predicate as it takes two
parameters. We will create an implementation of the interface called bp. The interface requires that
we provide one method called test that does the actual comparison between two objects and
returns a simple true or false result. Like we did in the previous section we simply perform a
regular expression compare using the ==~ operator.
我们就可以用我们的bp实现来创建一个命名的闭包,我们将它称为regex.TinkerPop包含了一个断言类P,这是java Predicate 函数接口的一个实现,我们可以用P来创建新的正则regex方法。我们可以把regex方法直接传递给has这样的步骤。
We can then use our bp implementation to build a named closure that we will call regex. TinkerPop
includes a predicate class P that is an implementation of the Java Predicate functional interface. We
 can use P to build our new regex method. We can then pass regex directly to steps like has.
 
 
posted on 2022-04-25 15:54  bokeyuannicheng0000  阅读(39)  评论(0)    收藏  举报