3
votes

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
Gremlin2 or Gremlin3?Daniel Kuppitz

1 Answers

0
votes

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