比较属性Gremlin [英] Comparing properties in Gremlin

查看:75
本文介绍了比较属性Gremlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的图形,父母和孩子是顶点.父母与子女的关系为"isParentOf".顶点都有一个属性:"familyName".

I have a simple graph, with parents and children being vertices. Parents have the relationship "isParentOf" to their children. The vertices all have one property: the "familyName".

我想使用gremlin来匹配其孩子的姓氏​​与他们的孩子的姓氏​​不同的所有父母.

I want to use gremlin to match all the parents whose child's familyName is different from theirs.

注意:我不能使用Gremlin的Groovy语法.我只能使用纯Java代码.

Note: I cannot use the Groovy syntax of Gremlin. I must use pure Java code only.

推荐答案

GremlinPipeline应该如下所示:

The GremlinPipeline should look like this:

  • 找到所有父母
  • 遵循"isParentOf"关系并获取所有子项
  • 通过PipeFunction过滤子项,该函数将父项的"familyName"与子项的"familyName"进行比较

问题出在最后一步.当该管道步骤仅可以访问(即来自上一步的信息)孩子时,如何获取父母的"familyName"?

The problem is in the last step. How to retrieve the parent's "familyName", when this pipeline step only has access to (what is coming from the previous step, that is to say) the children?

我的答案:

无法在过滤器的PipeFunction中访问GremlinPipeline的先前步骤.但是,如果您改用PipesFunction(请注意"s"!),则是可能的.

让我们看一下javadoc 此处:

Let's look at the javadoc here:

public PipesFunction extends PipeFunction{
   public AsMap getAsMap();
}

因此,您应该像这样设置GremlinPipeline:

So you should setup the GremlinPipeline like this:

  1. 找到所有父母
  2. 将该步骤命名为"theParent"
  3. 遵循"isParentOf"关系并获取所有子项
  4. 使用PipesFunction过滤子项,如下所示:

  1. find all parents
  2. name that step as "theParent"
  3. follow the "isParentOf" relationship and get all the children
  4. filter the children with a PipesFunction like this:

.filter(new PipesFunction<Vertex,Boolean>() 
{

   public Boolean compute(Vertex child) {
      return parentHasDifferentFamilyName(child);
   }

   private Boolean parentHasDifferentFamilyName(child){
      Vertex theParent = getAsMap().get("theParent");
      String theParentFamilyName = theParent.getProperty("familyName");
      String childFamilyName = child.getParameter("familyName");
      return !(childFamilyName.equals(parentFamilyName));
   }

 })

注意:在第4步中,我们可以借助getAsMap()方法并在第2步(隐式填充"As"图)中检索"theParent"顶点.

Note: in the step 4, we could retrieve the "theParent" vertex thanks to the getAsMap() method, and thanks to the step 2 (that implicitly filled the "As" map).

这篇关于比较属性Gremlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆