TechnologyAugust 15, 2017

Gremlin DSLs in Java with DSE Graph

Stephen Mallette
Stephen Mallette
Gremlin DSLs in Java with DSE Graph
dr.patients("55600064").                 // find a patient by id
   prescriptions(Rx.CURRENT).            // get prescriptions they are currently prescribed
     consider("lipitor", "40mg").        // for purpose of analysis add lipitor to that list
     consider("lunesta", "1mg").         // for purpose of analysis add lunesta to that list
   interactions(Type.DRUG, Level.SEVERE) // find possible drug interactions that are severe
schema.vertexLabel("movie").properties("movieId","title","year","duration","country","production").create();
schema.vertexLabel("person").properties("personId","name").create();
schema.edgeLabel("actor").connection("movie","person").create();
g.V().has("movie", "title","Young Guns").out("actor")
killr.movies("Young Guns").actors()
public class KillrVideoTraversalSource extends GraphTraversalSource {
  public KillrVideoTraversalSource(Graph graph) {
    super(graph);
  }
  public KillrVideoTraversalSource(Graph graph, TraversalStrategies strategies) {
    super(graph, strategies);
  }
  public KillrVideoTraversal<Vertex, Vertex> movies(String title) {
    // implementation omitted for brevity
  }
}
killr.movies("Young Guns")
killr.movies("Young Guns").out("actors").in("actors")
// not possible because actors() is not a method GraphTraversal which is returned by in()
killr.movies("Young Guns").out("actors").in("actors").actors()
// possible with casting which isn't ideal
((KillrVideoTraversal) killr.movies("Young Guns").out("actors").in("actors")).actors()
@GremlinDsl
public interface KillrVideoTraversalDsl<S, E> extends GraphTraversal.Admin<S, E> {
}
@GremlinDsl(traversalSource = "com.killrvideo.KillrVideoTraversalSourceDsl")
public interface KillrVideoTraversalDsl<S, E> extends GraphTraversal.Admin<S, E> {
}
public class KillrVideoTraversalSourceDsl extends GraphTraversalSource {
    public KillrVideoTraversalSourceDsl(final Graph graph, final TraversalStrategies traversalStrategies) {
        super(graph, traversalStrategies);
    }
    public KillrVideoTraversalSourceDsl(final Graph graph) {
        super(graph);
    }
}
@GremlinDsl(traversalSource = "com.killrvideo.KillrVideoTraversalSourceDsl")
public interface KillrVideoTraversalDsl<S, E> extends GraphTraversal.Admin<S, E> {
    public default GraphTraversal<S, Vertex> actors() {
        return out("actor").hasLabel("person");
    }
}
public class KillrVideoTraversalSourceDsl extends GraphTraversalSource {
    public KillrVideoTraversalSourceDsl(final Graph graph, final TraversalStrategies traversalStrategies) {
        super(graph, traversalStrategies);
    }
    public KillrVideoTraversalSourceDsl(final Graph graph) {
        super(graph);
    }
     public GraphTraversal<Vertex, Vertex> movies(String... titles) {
       GraphTraversal traversal = this.clone().V();       
       traversal = traversal.hasLabel("movie");
        if (titles.length == 1)
            traversal = traversal.has("title", titles[0]);
        else if (titles.length > 1)
            traversal = traversal.has("title", P.within(titles));
        return traversal;
    }
}
DseCluster dseCluster = DseCluster.builder()
                                  .addContactPoint("127.0.0.1")
                                  .build();
DseSession dseSession = dseCluster.connect();
KillrVideoTraversalSource killr = DseGraph.traversal(dseSession,
        new GraphOptions().setGraphName("killrvideo"), KillrVideoTraversalSource.class)
killr.movies("Young Guns").actors().values("name").toList();
public interface KillrVideoTraversal<S, E> extends KillrVideoTraversalDsl<S, E> {
  @Override
  default KillrVideoTraversal<S, Vertex> actors() {
    return (KillrVideoTraversal) KillrVideoTraversalDsl.super.actors();
  }
  // other DSL steps from the full project omitted for brevity
  @Override
  default KillrVideoTraversal<S, Vertex> out(String... edgeLabels) {
    return (KillrVideoTraversal) KillrVideoTraversalDsl.super.out(edgeLabels);
  }
  // remaining Graph steps omitted for brevity
}
public class KillrVideoTraversalSource extends KillrVideoTraversalSourceDsl {
  public KillrVideoTraversalSource(Graph graph) {
    super(graph);
  }
  public KillrVideoTraversalSource(Graph graph, TraversalStrategies strategies) {
    super(graph, strategies);
  }
  @Override
  public KillrVideoTraversal<Vertex, Vertex> movies(String title, String... additionalTitles) {
    KillrVideoTraversalSource clone = this.clone();
    return new DefaultKillrVideoTraversal (clone, super.movies(title,additionalTitles).asAdmin());
  }
  // remaining DSL steps omitted for brevity
  @Override
  public KillrVideoTraversalSource withStrategies(TraversalStrategy... traversalStrategies) {
    return (KillrVideoTraversalSource) super.withStrategies(traversalStrategies);
  }
  // remaining Graph steps omitted for brevity
}
killr.movie("m100000", "Manos: The Hands of Fate", "USA", "Sun City Films", 1966, 70).
        ensure(actor("p1000000", "Tom Neyman")).
        ensure(actor("p1000001", "John Reynolds")).
        ensure(actor("p1000002", "Diane Mahree"))
g.V().
  has("movie", "movieId", "m100000").
  fold().
  coalesce(
    __.unfold(),
    __.addV("movie").property("movieId", "m100000")).
  property("title", "Manos: The Hands of Fate").
  property("country", "USA").
  property("production", "Sun City Films").
  property("year", 1966).
  property("duration", 70).as("^movie").
  sideEffect(coalesce(out("actor").has("person", "personId", "p1000000"),
                      coalesce(V().has("person", "personId", "p1000000"),
                               addV("person").property("personId", "p1000000")).
                      property("name", "Tom Neyman").
                      addE("actor").
                        from("^movie").
                      inV())).
  sideEffect(coalesce(out("actor").has("person", "personId", "p1000001"),
                      coalesce(V().has("person", "personId", "p1000001"),
                               addV("person").property("personId", "p1000001")).
                      property("name", "John Reynolds").
                      addE("actor").
                        from("^movie").
                      inV())).
  sideEffect(coalesce(out("actor").has("person", "personId", "p1000002"),
                      coalesce(V().has("person", "personId", "p1000002"),
                               addV("person").property("personId", "p1000002")).
                      property("name", "Diane Mahree").
                      addE("actor").
                        from("^movie").
                      inV())).
dr.patients("55600064").                 // find a patient by id
   prescriptions(Rx.CURRENT).            // get prescriptions they are currently prescribed
     consider("lipitor", "40mg").        // for purpose of analysis add lipitor to that list
     consider("lunesta", "1mg").         // for purpose of analysis add lunesta to that list
   interactions(Type.DRUG, Level.SEVERE) // find possible drug interactions that are severe
Discover more
GremlinJavaDSE Graph
Share

NoSQL and Vector DB
for Generative AI,
Instantly, At Scale

Vector search capabilities on Astra DB enable complex, context-sensitive searches across diverse data formats for use in Generative AI applications, powered by Apache Cassandra®.