gpt4 book ai didi

gremlin - 比较 Gremlin 的属性

转载 作者:行者123 更新时间:2023-12-04 02:58:47 26 4
gpt4 key购买 nike

我有一个简单的图,其中 parent 和 child 是顶点。 parent 与他们的 child 有“isParentOf”关系。所有顶点都有一个属性:“familyName”。

我想使用 gremlin 匹配所有 child 的 familyName 与他们的不同的 parent 。

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

最佳答案

GremlinPipeline 应该是这样的:

  • 找到所有的 parent
  • 按照“isParentOf”关系,得到所有的 child
  • 通过将父项的“familyName”与子项的“familyName”进行比较的 PipeFunction 过滤子项

问题出在最后一步。当此管道步骤只能访问(也就是说,来自上一步的内容)子级时,如何检索父级的“familyName”?

我的回答:

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

让我们看看javadoc here :

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

所以你应该像这样设置 GremlinPipeline:

  1. 找到所有的 parent
  2. 将该步骤命名为“theParent”
  3. 按照“isParentOf”关系,得到所有的 child
  4. 像这样用 PipesFunction 过滤 child :

    .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”顶点。

关于gremlin - 比较 Gremlin 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28085762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com