Comparing properties in Gremlin

会有一股神秘感。 提交于 2019-12-10 21:46:56

问题


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".

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

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


回答1:


The GremlinPipeline should look like this:

  • find all parents
  • follow the "isParentOf" relationship and get all the children
  • filter the children through a PipeFunction that compares the parent's "familyName" with the child's "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?

My answer:

Accessing previous steps of a GremlinPipeline in a filter's PipeFunction is not possible. BUT it is possible if you use a PipesFunction instead (note the 's' !).

Let's look at the javadoc here:

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

So you should setup the GremlinPipeline like this:

  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));
       }
    
     })
    

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).



来源:https://stackoverflow.com/questions/28085762/comparing-properties-in-gremlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!