You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2019/05/10 20:15:45 UTC

[tinkerpop] 01/01: TINKERPOP-2089 Added DSL support to gremlin-javascript

This is an automated email from the ASF dual-hosted git repository.

spmallette pushed a commit to branch TINKERPOP-2089
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit d09d7fba478c6c03d985ad8c828c9821184798f5
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Fri May 10 16:15:16 2019 -0400

    TINKERPOP-2089 Added DSL support to gremlin-javascript
---
 .../glv/GraphTraversalSource.template              | 13 ++++++---
 .../lib/process/anonymous-traversal.js             | 10 +++++--
 .../lib/process/graph-traversal.js                 | 31 +++++++++++---------
 .../gremlin-javascript/lib/structure/graph.js      |  5 ++--
 .../test/integration/traversal-test.js             | 33 ++++++++++++++++++++++
 5 files changed, 70 insertions(+), 22 deletions(-)

diff --git a/gremlin-javascript/glv/GraphTraversalSource.template b/gremlin-javascript/glv/GraphTraversalSource.template
index 58be16c..d96279a 100644
--- a/gremlin-javascript/glv/GraphTraversalSource.template
+++ b/gremlin-javascript/glv/GraphTraversalSource.template
@@ -37,11 +37,15 @@ class GraphTraversalSource {
    * @param {Graph} graph
    * @param {TraversalStrategies} traversalStrategies
    * @param {Bytecode} [bytecode]
+   * @param {Class} graphTraversalSourceClass
+   * @param {Class} graphTraversalClass
    */
-  constructor(graph, traversalStrategies, bytecode) {
+  constructor(graph, traversalStrategies, bytecode, graphTraversalSourceClass, graphTraversalClass) {
     this.graph = graph;
     this.traversalStrategies = traversalStrategies;
     this.bytecode = bytecode || new Bytecode();
+    this.graphTraversalSourceClass = graphTraversalSourceClass || GraphTraversalSource;
+    this.graphTraversalClass = graphTraversalClass || GraphTraversal;
   }
 
   /**
@@ -51,7 +55,8 @@ class GraphTraversalSource {
   withRemote(remoteConnection) {
     const traversalStrategy = new TraversalStrategies(this.traversalStrategies);
     traversalStrategy.addStrategy(new remote.RemoteStrategy(remoteConnection));
-    return new GraphTraversalSource(this.graph, traversalStrategy, new Bytecode(this.bytecode));
+    var object = Object.create(this.constructor.prototype);
+    return new this.graphTraversalSourceClass(this.graph, traversalStrategy, new Bytecode(this.bytecode), this.graphTraversalSourceClass, this.graphTraversalClass);
   }
 
   /**
@@ -69,7 +74,7 @@ class GraphTraversalSource {
    */
   <%= toJs.call(method) %>(...args) {
     const b = new Bytecode(this.bytecode).addSource('<%= method %>', args);
-    return new GraphTraversalSource(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalSourceClass(this.graph, new TraversalStrategies(this.traversalStrategies), b, this.graphTraversalSourceClass, this.graphTraversalClass);
   }
   <%
   }
@@ -81,7 +86,7 @@ class GraphTraversalSource {
    */
   <%= toJs.call(method) %>(...args) {
     const b = new Bytecode(this.bytecode).addStep('<%= method %>', args);
-    return new GraphTraversal(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalClass(this.graph, new TraversalStrategies(this.traversalStrategies), b);
   }
   <% } %>
 }
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/anonymous-traversal.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/anonymous-traversal.js
index ba5532f..3fbe7eb 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/anonymous-traversal.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/anonymous-traversal.js
@@ -31,13 +31,17 @@ const Graph = require('../structure/graph').Graph;
  */
 class AnonymousTraversalSource {
 
+  constructor(traversalSourceClass) {
+    this.traversalSourceClass = traversalSourceClass;
+  }
+
   /**
    * Constructs an {@code AnonymousTraversalSource} which will then be configured to spawn a
    * {@link GraphTraversalSource}.
    * @returns {AnonymousTraversalSource}.
    */
-  static traversal() {
-    return new AnonymousTraversalSource();
+  static traversal(traversalSourceClass) {
+    return new AnonymousTraversalSource(traversalSourceClass || GraphTraversalSource);
   }
 
   /**
@@ -57,7 +61,7 @@ class AnonymousTraversalSource {
    * @return {GraphTraversalSource}
    */
   withGraph(graph) {
-    return new GraphTraversalSource(graph, new TraversalStrategies());
+    return new this.traversalSourceClass(graph, new TraversalStrategies());
   }
 }
 
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/graph-traversal.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/graph-traversal.js
index edeb2cb..89285e3 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/graph-traversal.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/graph-traversal.js
@@ -37,11 +37,15 @@ class GraphTraversalSource {
    * @param {Graph} graph
    * @param {TraversalStrategies} traversalStrategies
    * @param {Bytecode} [bytecode]
+   * @param {Class} graphTraversalSourceClass
+   * @param {Class} graphTraversalClass
    */
-  constructor(graph, traversalStrategies, bytecode) {
+  constructor(graph, traversalStrategies, bytecode, graphTraversalSourceClass, graphTraversalClass) {
     this.graph = graph;
     this.traversalStrategies = traversalStrategies;
     this.bytecode = bytecode || new Bytecode();
+    this.graphTraversalSourceClass = graphTraversalSourceClass || GraphTraversalSource;
+    this.graphTraversalClass = graphTraversalClass || GraphTraversal;
   }
 
   /**
@@ -51,7 +55,8 @@ class GraphTraversalSource {
   withRemote(remoteConnection) {
     const traversalStrategy = new TraversalStrategies(this.traversalStrategies);
     traversalStrategy.addStrategy(new remote.RemoteStrategy(remoteConnection));
-    return new GraphTraversalSource(this.graph, traversalStrategy, new Bytecode(this.bytecode));
+    var object = Object.create(this.constructor.prototype);
+    return new this.graphTraversalSourceClass(this.graph, traversalStrategy, new Bytecode(this.bytecode), this.graphTraversalSourceClass, this.graphTraversalClass);
   }
 
   /**
@@ -69,7 +74,7 @@ class GraphTraversalSource {
    */
   withBulk(...args) {
     const b = new Bytecode(this.bytecode).addSource('withBulk', args);
-    return new GraphTraversalSource(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalSourceClass(this.graph, new TraversalStrategies(this.traversalStrategies), b, this.graphTraversalSourceClass, this.graphTraversalClass);
   }
   
   /**
@@ -79,7 +84,7 @@ class GraphTraversalSource {
    */
   withPath(...args) {
     const b = new Bytecode(this.bytecode).addSource('withPath', args);
-    return new GraphTraversalSource(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalSourceClass(this.graph, new TraversalStrategies(this.traversalStrategies), b, this.graphTraversalSourceClass, this.graphTraversalClass);
   }
   
   /**
@@ -89,7 +94,7 @@ class GraphTraversalSource {
    */
   withSack(...args) {
     const b = new Bytecode(this.bytecode).addSource('withSack', args);
-    return new GraphTraversalSource(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalSourceClass(this.graph, new TraversalStrategies(this.traversalStrategies), b, this.graphTraversalSourceClass, this.graphTraversalClass);
   }
   
   /**
@@ -99,7 +104,7 @@ class GraphTraversalSource {
    */
   withSideEffect(...args) {
     const b = new Bytecode(this.bytecode).addSource('withSideEffect', args);
-    return new GraphTraversalSource(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalSourceClass(this.graph, new TraversalStrategies(this.traversalStrategies), b, this.graphTraversalSourceClass, this.graphTraversalClass);
   }
   
   /**
@@ -109,7 +114,7 @@ class GraphTraversalSource {
    */
   withStrategies(...args) {
     const b = new Bytecode(this.bytecode).addSource('withStrategies', args);
-    return new GraphTraversalSource(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalSourceClass(this.graph, new TraversalStrategies(this.traversalStrategies), b, this.graphTraversalSourceClass, this.graphTraversalClass);
   }
   
   /**
@@ -119,7 +124,7 @@ class GraphTraversalSource {
    */
   withoutStrategies(...args) {
     const b = new Bytecode(this.bytecode).addSource('withoutStrategies', args);
-    return new GraphTraversalSource(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalSourceClass(this.graph, new TraversalStrategies(this.traversalStrategies), b, this.graphTraversalSourceClass, this.graphTraversalClass);
   }
   
   /**
@@ -129,7 +134,7 @@ class GraphTraversalSource {
    */
   E(...args) {
     const b = new Bytecode(this.bytecode).addStep('E', args);
-    return new GraphTraversal(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalClass(this.graph, new TraversalStrategies(this.traversalStrategies), b);
   }
   
   /**
@@ -139,7 +144,7 @@ class GraphTraversalSource {
    */
   V(...args) {
     const b = new Bytecode(this.bytecode).addStep('V', args);
-    return new GraphTraversal(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalClass(this.graph, new TraversalStrategies(this.traversalStrategies), b);
   }
   
   /**
@@ -149,7 +154,7 @@ class GraphTraversalSource {
    */
   addE(...args) {
     const b = new Bytecode(this.bytecode).addStep('addE', args);
-    return new GraphTraversal(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalClass(this.graph, new TraversalStrategies(this.traversalStrategies), b);
   }
   
   /**
@@ -159,7 +164,7 @@ class GraphTraversalSource {
    */
   addV(...args) {
     const b = new Bytecode(this.bytecode).addStep('addV', args);
-    return new GraphTraversal(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalClass(this.graph, new TraversalStrategies(this.traversalStrategies), b);
   }
   
   /**
@@ -169,7 +174,7 @@ class GraphTraversalSource {
    */
   inject(...args) {
     const b = new Bytecode(this.bytecode).addStep('inject', args);
-    return new GraphTraversal(this.graph, new TraversalStrategies(this.traversalStrategies), b);
+    return new this.graphTraversalClass(this.graph, new TraversalStrategies(this.traversalStrategies), b);
   }
   
 }
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/graph.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/graph.js
index cd74e25..8a5def1 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/graph.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/graph.js
@@ -31,8 +31,9 @@ class Graph {
    * @returns {GraphTraversalSource}
    * @deprecated As of release 3.3.5, replaced by the traversal() anonymous function.
    */
-  traversal() {
-    return new gt.GraphTraversalSource(this, new TraversalStrategies());
+  traversal(traversalSourceClass) {
+    const clazz = traversalSourceClass || gt.GraphTraversalSource;
+    return new clazz(this, new TraversalStrategies());
   }
 
   toString() {
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js
index d454552..77aaa18 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/traversal-test.js
@@ -26,11 +26,33 @@ const assert = require('assert');
 const graphModule = require('../../lib/structure/graph');
 const Vertex = graphModule.Vertex;
 const traversal = require('../../lib/process/anonymous-traversal').traversal;
+const GraphTraversalSource = require('../../lib/process/graph-traversal').GraphTraversalSource;
+const GraphTraversal = require('../../lib/process/graph-traversal').GraphTraversal;
 const utils = require('../../lib/utils');
 const helper = require('../helper');
 
 let connection;
 
+class SocialTraversal extends GraphTraversal {
+  constructor(graph, traversalStrategies, bytecode) {
+    super(graph, traversalStrategies, bytecode);
+  }
+
+  aged(age) {
+    return this.has('person', 'age', age);
+  }
+}
+
+class SocialTraversalSource extends GraphTraversalSource {
+  constructor(graph, traversalStrategies, bytecode) {
+    super(graph, traversalStrategies, bytecode, SocialTraversalSource, SocialTraversal);
+  }
+
+  person(name) {
+    return this.V().has('person', 'name', name);
+  }
+}
+
 describe('Traversal', function () {
   before(function () {
     connection = helper.getConnection('gmodern');
@@ -66,4 +88,15 @@ describe('Traversal', function () {
         });
     });
   });
+  describe('dsl', function() {
+    it('should expose DSL methods', function() {
+      var g = traversal(SocialTraversalSource).withRemote(connection);
+      var t = g.person('marko').aged(29).values('name');
+      return g.person('marko').aged(29).values('name').toList().then(function (list) {
+          assert.ok(list);
+          assert.strictEqual(list.length, 1);
+          assert.strictEqual(list[0], 'marko');
+        });
+    });
+  });
 });
\ No newline at end of file