You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by ok...@apache.org on 2015/08/24 19:40:38 UTC

incubator-tinkerpop git commit: Gremlin now supports g.V([1, 2, 3]) -- i.e. a single id argument that is a Collection of either ids or elements. TINKERPOP3-803 #close.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master 1b5c0e6ed -> 722948e0a


Gremlin now supports g.V([1,2,3]) -- i.e. a single id argument that is a Collection of either ids or elements. TINKERPOP3-803 #close.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/722948e0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/722948e0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/722948e0

Branch: refs/heads/master
Commit: 722948e0a6abfeab5f362f285b63626e788a78ee
Parents: 1b5c0e6
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Mon Aug 24 11:40:33 2015 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Mon Aug 24 11:40:33 2015 -0600

----------------------------------------------------------------------
 CHANGELOG.asciidoc                              |  1 +
 .../traversal/TraversalVertexProgram.java       | 10 +++---
 .../traversal/step/sideEffect/GraphStep.java    |  8 ++---
 .../gremlin/structure/util/ElementHelper.java   | 13 +++++++
 .../traversal/step/map/GroovyVertexTest.groovy  | 12 ++++++-
 .../tinkerpop/gremlin/AbstractGremlinTest.java  | 38 ++++++++++----------
 .../process/traversal/step/map/VertexTest.java  | 29 ++++++++++++++-
 .../step/sideEffect/Neo4jGraphStep.java         |  2 +-
 .../step/sideEffect/TinkerGraphStep.java        |  2 +-
 9 files changed, 83 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/722948e0/CHANGELOG.asciidoc
----------------------------------------------------------------------
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 33d9413..8dcc868 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -26,6 +26,7 @@ image::https://raw.githubusercontent.com/apache/incubator-tinkerpop/master/docs/
 TinkerPop 3.1.0 (NOT OFFICIALLY RELEASED YET)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+* `GraphStep` can now take a single argument `Collection` which is either elements or element ids (i.e. `g.V([1,2,3])` is supported now).
 * Added `LoopsStep` to make the loop counter accessible within `repeat()`, `until()` and `emit()`.
 * Gephi Plugin no longer requires manual insert of `store` steps to visualize a traversal.
 * Gephi Plugin visualizes `Path` objects.

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/722948e0/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/traversal/TraversalVertexProgram.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/traversal/TraversalVertexProgram.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/traversal/TraversalVertexProgram.java
index 31a5e96..bf706af 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/traversal/TraversalVertexProgram.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/traversal/TraversalVertexProgram.java
@@ -61,7 +61,6 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Optional;
 import java.util.Set;
-import java.util.function.Function;
 import java.util.function.Supplier;
 
 
@@ -155,9 +154,10 @@ public final class TraversalVertexProgram implements VertexProgram<TraverserSet<
 
             final GraphStep<Element> graphStep = (GraphStep<Element>) this.traversal.getStartStep();
             final String future = graphStep.getNextStep().getId();
+            final boolean elementIds = graphStep.getIds().length > 0 && graphStep.getIds()[0] instanceof Element;
             final TraverserGenerator traverserGenerator = this.traversal.getTraverserGenerator();
             if (graphStep.returnsVertex()) {  // VERTICES (process the first step locally)
-                if (ElementHelper.idExists(vertex.id(), graphStep.getIds())) {
+                if (elementIds ? ElementHelper.elementExists(vertex, graphStep.getIds()) : ElementHelper.idExists(vertex.id(), graphStep.getIds())) {
                     final Traverser.Admin<Element> traverser = traverserGenerator.generate(vertex, graphStep, 1l);
                     traverser.setStepId(future);
                     traverser.detach();
@@ -170,9 +170,9 @@ public final class TraversalVertexProgram implements VertexProgram<TraverserSet<
                 boolean voteToHalt = true;
                 final Iterator<Edge> starts = vertex.edges(Direction.OUT);
                 while (starts.hasNext()) {
-                    final Edge start = starts.next();
-                    if (ElementHelper.idExists(start.id(), graphStep.getIds())) {
-                        final Traverser.Admin<Element> traverser = traverserGenerator.generate(start, graphStep, 1l);
+                    final Edge edge = starts.next();
+                    if (elementIds ? ElementHelper.elementExists(edge, graphStep.getIds()) : ElementHelper.idExists(edge.id(), graphStep.getIds())) {
+                        final Traverser.Admin<Element> traverser = traverserGenerator.generate(edge, graphStep, 1l);
                         traverser.setStepId(future);
                         traverser.detach();
                         if (traverser.isHalted())

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/722948e0/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/sideEffect/GraphStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/sideEffect/GraphStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/sideEffect/GraphStep.java
index 1cfdbee..da6f80f 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/sideEffect/GraphStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/sideEffect/GraphStep.java
@@ -29,6 +29,7 @@ import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
 import org.apache.tinkerpop.gremlin.util.iterator.EmptyIterator;
 
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.function.Supplier;
@@ -46,11 +47,10 @@ public class GraphStep<S extends Element> extends StartStep<S> implements Engine
     public GraphStep(final Traversal.Admin traversal, final Class<S> returnClass, final Object... ids) {
         super(traversal);
         this.returnClass = returnClass;
-        this.ids = ids;
-
+        this.ids = (ids.length == 1 && ids[0] instanceof Collection) ? ((Collection) ids[0]).toArray(new Object[((Collection) ids[0]).size()]) : ids;
         this.iteratorSupplier = () -> (Iterator<S>) (Vertex.class.isAssignableFrom(this.returnClass) ?
-                    this.getTraversal().getGraph().get().vertices(this.ids) :
-                    this.getTraversal().getGraph().get().edges(this.ids));
+                this.getTraversal().getGraph().get().vertices(this.ids) :
+                this.getTraversal().getGraph().get().edges(this.ids));
     }
 
     public String toString() {

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/722948e0/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java
index 7a7cf03..2e86c5b 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java
@@ -550,4 +550,17 @@ public final class ElementHelper {
             return false;
         }
     }
+
+    public static boolean elementExists(final Element element, final Object... providedElements) {
+        if (0 == providedElements.length) return true;
+
+        if (1 == providedElements.length) return ElementHelper.areEqual(element, providedElements[0]);
+        else {
+            for (final Object temp : providedElements) {
+                if (ElementHelper.areEqual(element, temp))
+                    return true;
+            }
+            return false;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/722948e0/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/process/traversal/step/map/GroovyVertexTest.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/process/traversal/step/map/GroovyVertexTest.groovy b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/process/traversal/step/map/GroovyVertexTest.groovy
index 71041e4..34a567e 100644
--- a/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/process/traversal/step/map/GroovyVertexTest.groovy
+++ b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/process/traversal/step/map/GroovyVertexTest.groovy
@@ -18,8 +18,8 @@
  */
 package org.apache.tinkerpop.gremlin.process.traversal.step.map
 
-import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalScriptHelper
 import org.apache.tinkerpop.gremlin.process.traversal.Traversal
+import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalScriptHelper
 import org.apache.tinkerpop.gremlin.structure.Edge
 import org.apache.tinkerpop.gremlin.structure.Vertex
 
@@ -31,6 +31,16 @@ public abstract class GroovyVertexTest {
     public static class Traversals extends VertexTest {
 
         @Override
+        public Traversal<Vertex, String> get_g_VXlistXv1_v2_v3XX_name() {
+            TraversalScriptHelper.compute("g.V(ids).name", g, "ids", [convertToVertex(graph, "marko"), convertToVertex(graph, "vadas"), convertToVertex(graph, "lop")])
+        }
+
+        @Override
+        public Traversal<Vertex, String> get_g_VXlistX1_2_3XX_name() {
+            TraversalScriptHelper.compute("g.V(ids).name", g, "ids", [convertToVertexId(graph, "marko"), convertToVertexId(graph, "vadas"), convertToVertexId(graph, "lop")])
+        }
+
+        @Override
         public Traversal<Vertex, Vertex> get_g_V() {
             TraversalScriptHelper.compute("g.V", g);
         }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/722948e0/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/AbstractGremlinTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/AbstractGremlinTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/AbstractGremlinTest.java
index d05e162..97dfea2 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/AbstractGremlinTest.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/AbstractGremlinTest.java
@@ -149,12 +149,12 @@ public abstract class AbstractGremlinTest {
     /**
      * Looks up the identifier as generated by the current source graph being tested.
      *
-     * @param g          the graph to get the element id from
+     * @param graph          the graph to get the element id from
      * @param vertexName a unique string that will identify a graph element within a graph
      * @return the id as generated by the graph
      */
-    public Object convertToVertexId(final Graph g, final String vertexName) {
-        return convertToVertex(g, vertexName).id();
+    public Object convertToVertexId(final Graph graph, final String vertexName) {
+        return convertToVertex(graph, vertexName).id();
     }
 
     public Vertex convertToVertex(final Graph graph, final String vertexName) {
@@ -166,8 +166,8 @@ public abstract class AbstractGremlinTest {
         return convertToVertexPropertyId(graph, vertexName, vertexPropertyKey);
     }
 
-    public GraphTraversal<Vertex, Object> convertToVertexPropertyId(final Graph g, final String vertexName, final String vertexPropertyKey) {
-        return convertToVertexProperty(g, vertexName, vertexPropertyKey).id();
+    public GraphTraversal<Vertex, Object> convertToVertexPropertyId(final Graph graph, final String vertexName, final String vertexPropertyKey) {
+        return convertToVertexProperty(graph, vertexName, vertexPropertyKey).id();
     }
 
     public GraphTraversal<Vertex, VertexProperty<Object>> convertToVertexProperty(final Graph graph, final String vertexName, final String vertexPropertyKey) {
@@ -186,34 +186,34 @@ public abstract class AbstractGremlinTest {
     /**
      * Utility method that commits if the graph supports transactions.
      */
-    public void tryCommit(final Graph g) {
-        if (g.features().graph().supportsTransactions())
-            g.tx().commit();
+    public void tryCommit(final Graph graph) {
+        if (graph.features().graph().supportsTransactions())
+            graph.tx().commit();
     }
 
-    public void tryRandomCommit(final Graph g) {
-        if (g.features().graph().supportsTransactions() && new Random().nextBoolean())
-            g.tx().commit();
+    public void tryRandomCommit(final Graph graph) {
+        if (graph.features().graph().supportsTransactions() && new Random().nextBoolean())
+            graph.tx().commit();
     }
 
     /**
      * Utility method that commits if the graph supports transactions and executes an assertion function before and
      * after the commit.  It assumes that the assertion should be true before and after the commit.
      */
-    public void tryCommit(final Graph g, final Consumer<Graph> assertFunction) {
-        assertFunction.accept(g);
-        if (g.features().graph().supportsTransactions()) {
-            g.tx().commit();
-            assertFunction.accept(g);
+    public void tryCommit(final Graph graph, final Consumer<Graph> assertFunction) {
+        assertFunction.accept(graph);
+        if (graph.features().graph().supportsTransactions()) {
+            graph.tx().commit();
+            assertFunction.accept(graph);
         }
     }
 
     /**
      * Utility method that rollsback if the graph supports transactions.
      */
-    public void tryRollback(final Graph g) {
-        if (g.features().graph().supportsTransactions())
-            g.tx().rollback();
+    public void tryRollback(final Graph graph) {
+        if (graph.features().graph().supportsTransactions())
+            graph.tx().rollback();
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/722948e0/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/VertexTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/VertexTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/VertexTest.java
index f23c2c7..73bb8b8 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/VertexTest.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/VertexTest.java
@@ -21,14 +21,15 @@ package org.apache.tinkerpop.gremlin.process.traversal.step.map;
 import org.apache.tinkerpop.gremlin.LoadGraphWith;
 import org.apache.tinkerpop.gremlin.process.AbstractGremlinProcessTest;
 import org.apache.tinkerpop.gremlin.process.GremlinProcessRunner;
+import org.apache.tinkerpop.gremlin.process.traversal.P;
 import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
 import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
-import org.apache.tinkerpop.gremlin.process.traversal.P;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -46,6 +47,10 @@ import static org.junit.Assert.*;
 @RunWith(GremlinProcessRunner.class)
 public abstract class VertexTest extends AbstractGremlinProcessTest {
 
+    public abstract Traversal<Vertex, String> get_g_VXlistXv1_v2_v3XX_name();
+
+    public abstract Traversal<Vertex, String> get_g_VXlistX1_2_3XX_name();
+
     public abstract Traversal<Vertex, Vertex> get_g_V();
 
     public abstract Traversal<Vertex, Vertex> get_g_VX1X_out(final Object v1Id);
@@ -98,6 +103,17 @@ public abstract class VertexTest extends AbstractGremlinProcessTest {
 
     public abstract Traversal<Vertex, Vertex> get_g_VX1X_to_XOUT_knowsX(final Object v1Id);
 
+    // GRAPH VERTEX/EDGE
+
+    @Test
+    @LoadGraphWith(MODERN)
+    public void g_VXlistX1_2_3XX_name() {
+        Arrays.asList(get_g_VXlistX1_2_3XX_name(), get_g_VXlistXv1_v2_v3XX_name()).forEach(traversal -> {
+            printTraversalForm(traversal);
+            checkResults(Arrays.asList("marko", "vadas", "lop"), traversal);
+        });
+    }
+
     // VERTEX ADJACENCY
 
     @Test
@@ -517,6 +533,17 @@ public abstract class VertexTest extends AbstractGremlinProcessTest {
     }
 
     public static class Traversals extends VertexTest {
+
+        @Override
+        public Traversal<Vertex, String> get_g_VXlistXv1_v2_v3XX_name() {
+            return g.V(Arrays.asList(convertToVertex(graph, "marko"), convertToVertex(graph, "vadas"), convertToVertex(graph, "lop"))).values("name");
+        }
+
+        @Override
+        public Traversal<Vertex, String> get_g_VXlistX1_2_3XX_name() {
+            return g.V(Arrays.asList(convertToVertexId(graph, "marko"), convertToVertexId(graph, "vadas"), convertToVertexId(graph, "lop"))).values("name");
+        }
+
         @Override
         public Traversal<Vertex, Vertex> get_g_V() {
             return g.V();

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/722948e0/neo4j-gremlin/src/main/java/org/apache/tinkerpop/gremlin/neo4j/process/traversal/step/sideEffect/Neo4jGraphStep.java
----------------------------------------------------------------------
diff --git a/neo4j-gremlin/src/main/java/org/apache/tinkerpop/gremlin/neo4j/process/traversal/step/sideEffect/Neo4jGraphStep.java b/neo4j-gremlin/src/main/java/org/apache/tinkerpop/gremlin/neo4j/process/traversal/step/sideEffect/Neo4jGraphStep.java
index b2bfbb6..8b613e4 100644
--- a/neo4j-gremlin/src/main/java/org/apache/tinkerpop/gremlin/neo4j/process/traversal/step/sideEffect/Neo4jGraphStep.java
+++ b/neo4j-gremlin/src/main/java/org/apache/tinkerpop/gremlin/neo4j/process/traversal/step/sideEffect/Neo4jGraphStep.java
@@ -47,7 +47,7 @@ public final class Neo4jGraphStep<S extends Element> extends GraphStep<S> implem
         super(originalGraphStep.getTraversal(), originalGraphStep.getReturnClass(), originalGraphStep.getIds());
         originalGraphStep.getLabels().forEach(this::addLabel);
         //No need to do anything if the first element is an Element, all elements are guaranteed to be an element and will be return as is
-        if ((this.ids.length == 0 || !(this.ids[0] instanceof Element)))
+        if (this.ids.length == 0 || !(this.ids[0] instanceof Element))
             this.setIteratorSupplier(() -> (Iterator<S>) (Vertex.class.isAssignableFrom(this.returnClass) ? this.vertices() : this.edges()));
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/722948e0/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphStep.java
----------------------------------------------------------------------
diff --git a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphStep.java b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphStep.java
index 56bac20..5e154ab 100644
--- a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphStep.java
+++ b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphStep.java
@@ -49,7 +49,7 @@ public final class TinkerGraphStep<S extends Element> extends GraphStep<S> imple
         super(originalGraphStep.getTraversal(), originalGraphStep.getReturnClass(), originalGraphStep.getIds());
         originalGraphStep.getLabels().forEach(this::addLabel);
         //No need to do anything if the first element is an Element, all elements are guaranteed to be an element and will be return as is
-        if ((this.ids.length == 0 || !(this.ids[0] instanceof Element)))
+        if (this.ids.length == 0 || !(this.ids[0] instanceof Element))
             this.setIteratorSupplier(() -> (Iterator<S>) (Vertex.class.isAssignableFrom(this.returnClass) ? this.vertices() : this.edges()));
     }