tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ok...@apache.org
Subject incubator-tinkerpop git commit: All the enum tokens are now objects in Python. It 'just works'. So dope. A few random little hacks here and there, but for the most part, this is it.
Date Wed, 01 Jun 2016 19:49:42 GMT
Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/TINKERPOP-1278 e13684623 -> 762c2c84e


All the enum tokens are now objects in Python. It 'just works'. So dope. A few random little
hacks here and there, but for the most part, this is it.


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

Branch: refs/heads/TINKERPOP-1278
Commit: 762c2c84e4d2b03824af30646e1f3f62a048a034
Parents: e136846
Author: Marko A. Rodriguez <okrammarko@gmail.com>
Authored: Wed Jun 1 13:49:35 2016 -0600
Committer: Marko A. Rodriguez <okrammarko@gmail.com>
Committed: Wed Jun 1 13:49:35 2016 -0600

----------------------------------------------------------------------
 .../python/GremlinPythonGenerator.groovy        | 52 ++++++++++++--
 .../variant/VariantGraphTraversalSource.java    | 71 ++++++++++++++++++++
 .../variant/python/PythonVariantConverter.java  | 13 ++--
 .../VariantGraphTraversalSourceTest.java        | 56 +++++++++++++++
 .../variant/VariantGraphTraversalTest.java      | 56 +++++++++++++++
 5 files changed, 237 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/762c2c84/gremlin-variant/src/main/groovy/org/apache/tinkerpop/gremlin/process/variant/python/GremlinPythonGenerator.groovy
----------------------------------------------------------------------
diff --git a/gremlin-variant/src/main/groovy/org/apache/tinkerpop/gremlin/process/variant/python/GremlinPythonGenerator.groovy
b/gremlin-variant/src/main/groovy/org/apache/tinkerpop/gremlin/process/variant/python/GremlinPythonGenerator.groovy
index 1f60858..64f378e 100644
--- a/gremlin-variant/src/main/groovy/org/apache/tinkerpop/gremlin/process/variant/python/GremlinPythonGenerator.groovy
+++ b/gremlin-variant/src/main/groovy/org/apache/tinkerpop/gremlin/process/variant/python/GremlinPythonGenerator.groovy
@@ -19,6 +19,7 @@
 
 package org.apache.tinkerpop.gremlin.process.variant.python
 
+import org.apache.tinkerpop.gremlin.process.traversal.P
 import org.apache.tinkerpop.gremlin.process.traversal.Traversal
 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal
 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource
@@ -37,6 +38,7 @@ class Helper(object):
   @staticmethod
   def stringOrObject(arg):
     if (type(arg) is str and
+       not(arg.startswith("VertexProperty.Cardinality.")) and
        not(arg.startswith("P.")) and
        not(arg.startswith("Order.")) and
        not(arg.startswith("Scope.")) and
@@ -65,10 +67,23 @@ class Helper(object):
       return ", ".join(Helper.stringOrObject(i) for i in args)
 """);
 
+        final Map<String, String> methodMap = [as: "_as", in: "_in", and: "_and", or:
"_or", is: "_is", not: "_not", from: "_from"].withDefault {
+            it
+        }
+        final Map<String, String> invertedMethodMap = [_as: "as", _in: "in", _and:
"and", _or: "or", _is: "is", _not: "not", _from: "from"].withDefault {
+            it
+        }
+
 ///////////
 // Enums //
 ///////////
 
+        pythonClass.append("""class Cardinality(object):
+  single = "VertexProperty.Cardinality.single"
+  list = "VertexProperty.Cardinality.list"
+  set = "VertexProperty.Cardinality.set"
+""").append("\n\n");
+
         pythonClass.append("""class Column(object):
   keys = "Column.keys"
   values = "Column.values"
@@ -80,6 +95,20 @@ class Helper(object):
   BOTH = "Direction.BOTH"
 """).append("\n\n");
 
+        pythonClass.append("""class Operator(object):
+  sum = "Operator.sum"
+  minus = "Operator.minus"
+  mult = "Operator.mult"
+  div = "Operator.div"
+  min = "Operator.min"
+  max = "Operator.max"
+  assign = "Operator.assign"
+  _and = "Operator.and"
+  _or = "Operator.or"
+  addAll = "Operator.addAll"
+  sumLong = "Operator.sumLong"
+""").append("\n\n");
+
         pythonClass.append("""class Order(object):
   incr = "Order.incr"
   decr = "Order.decr"
@@ -90,12 +119,27 @@ class Helper(object):
   valueDecr = "Order.valueDecr"
 """).append("\n\n");
 
+        Set<String> methods = P.getMethods().collect { methodMap[it.name] } as Set;
[]
+        pythonClass.append("class P(object):\n");
+        methods.each { method ->
+            pythonClass.append(
+                    """  @staticmethod
+  def ${method}(*args):
+    return "P.${method}(" + Helper.stringify(*args) + ")"
+""")
+        };
+        pythonClass.append("\n\n")
+
         pythonClass.append("""class Pop(object):
   first = "Pop.first"
   last = "Pop.last"
   all = "Pop.all"
 """).append("\n\n");
 
+        pythonClass.append("""class Barrier(object):
+  normSack = "SackFunctions.Barrier.normSack"
+""").append("\n\n");
+
         pythonClass.append("""class Scope(object):
   _local = "Scope.local"
   _global = "Scope.global"
@@ -111,7 +155,7 @@ class Helper(object):
 //////////////////////////
 // GraphTraversalSource //
 //////////////////////////
-        Set<String> methods = GraphTraversalSource.getMethods().collect { it.name }
as Set;
+        methods = GraphTraversalSource.getMethods().collect { it.name } as Set;
         pythonClass.append(
                 """class PythonGraphTraversalSource(object):
   def __init__(self, traversalSourceString):
@@ -142,12 +186,6 @@ class Helper(object):
 ////////////////////
 // GraphTraversal //
 ////////////////////
-        final Map<String, String> methodMap = [as: "_as", in: "_in", and: "_and", or:
"_or", is: "_is", not: "_not", from: "_from"].withDefault {
-            it
-        }
-        final Map<String, String> invertedMethodMap = [_as: "as", _in: "in", _and:
"and", _or: "or", _is: "is", _not: "not", _from: "from"].withDefault {
-            it
-        }
         methods = GraphTraversal.getMethods().collect { methodMap[it.name] } as Set;
         methods.remove("toList")
         pythonClass.append(

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/762c2c84/gremlin-variant/src/main/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalSource.java
----------------------------------------------------------------------
diff --git a/gremlin-variant/src/main/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalSource.java
b/gremlin-variant/src/main/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalSource.java
index 3a9f7a7..9e4382f 100644
--- a/gremlin-variant/src/main/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalSource.java
+++ b/gremlin-variant/src/main/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalSource.java
@@ -19,11 +19,17 @@
 
 package org.apache.tinkerpop.gremlin.process.variant;
 
+import org.apache.tinkerpop.gremlin.process.computer.Computer;
+import org.apache.tinkerpop.gremlin.process.computer.GraphComputer;
 import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies;
+import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy;
 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
+import org.apache.tinkerpop.gremlin.process.traversal.step.map.AddVertexStartStep;
+import org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep;
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.Graph;
+import org.apache.tinkerpop.gremlin.structure.Transaction;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 
 import java.util.function.BinaryOperator;
@@ -63,6 +69,65 @@ public class VariantGraphTraversalSource extends GraphTraversalSource {
         return new VariantGraphTraversal<>(this.getGraph(), temp, this.variantConverter);
     }
 
+    @Deprecated
+    public GraphTraversal<Vertex, Vertex> addV(final Object... keyValues) {
+        final StringBuilder temp = new StringBuilder(this.sourceString.toString());
+        this.variantConverter.addStep(temp, getMethodName(), keyValues);
+        return new VariantGraphTraversal<>(this.getGraph(), temp, this.variantConverter);
+    }
+
+    public GraphTraversal<Vertex, Vertex> addV(final String label) {
+        final StringBuilder temp = new StringBuilder(this.sourceString.toString());
+        this.variantConverter.addStep(temp, getMethodName(), label);
+        return new VariantGraphTraversal<>(this.getGraph(), temp, this.variantConverter);
+    }
+
+    public GraphTraversal<Vertex, Vertex> addV() {
+        final StringBuilder temp = new StringBuilder(this.sourceString.toString());
+        this.variantConverter.addStep(temp, getMethodName());
+        return new VariantGraphTraversal<>(this.getGraph(), temp, this.variantConverter);
+    }
+
+    public <S> GraphTraversal<S, S> inject(S... starts) {
+        final StringBuilder temp = new StringBuilder(this.sourceString.toString());
+        this.variantConverter.addStep(temp, getMethodName(), starts);
+        return new VariantGraphTraversal<>(this.getGraph(), temp, this.variantConverter);
+    }
+
+    ///
+
+    @Override
+    public GraphTraversalSource withComputer(final Computer computer) {
+        this.variantConverter.addStep(this.sourceString, getMethodName(), computer);
+        return this;
+    }
+
+    @Override
+    public GraphTraversalSource withComputer(final Class<? extends GraphComputer> graphComputerClass)
{
+        this.variantConverter.addStep(this.sourceString, getMethodName(), graphComputerClass);
+        return this;
+    }
+
+    @Override
+    public GraphTraversalSource withComputer() {
+        this.variantConverter.addStep(this.sourceString, getMethodName());
+        return this;
+    }
+
+    @Override
+    public GraphTraversalSource withStrategies(final TraversalStrategy... traversalStrategies)
{
+        return super.withStrategies(traversalStrategies);
+        //this.variantConverter.addStep(this.sourceString, getMethodName(), traversalStrategies);
+        //return this;
+    }
+
+    @Override
+    @SuppressWarnings({"unchecked", "varargs"})
+    public GraphTraversalSource withoutStrategies(final Class<? extends TraversalStrategy>...
traversalStrategyClasses) {
+        this.variantConverter.addStep(this.sourceString, getMethodName(), traversalStrategyClasses);
+        return this;
+    }
+
     @Override
     public <A> GraphTraversalSource withSack(final Supplier<A> initialValue,
final UnaryOperator<A> splitOperator, final BinaryOperator<A> mergeOperator) {
         this.variantConverter.addStep(this.sourceString, getMethodName(), splitOperator,
mergeOperator);
@@ -145,4 +210,10 @@ public class VariantGraphTraversalSource extends GraphTraversalSource
{
         return this;
     }
 
+    @Override
+    public GraphTraversalSource withPath() {
+        this.variantConverter.addStep(this.sourceString, getMethodName());
+        return this;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/762c2c84/gremlin-variant/src/main/java/org/apache/tinkerpop/gremlin/process/variant/python/PythonVariantConverter.java
----------------------------------------------------------------------
diff --git a/gremlin-variant/src/main/java/org/apache/tinkerpop/gremlin/process/variant/python/PythonVariantConverter.java
b/gremlin-variant/src/main/java/org/apache/tinkerpop/gremlin/process/variant/python/PythonVariantConverter.java
index 3b99d62..879a14c 100644
--- a/gremlin-variant/src/main/java/org/apache/tinkerpop/gremlin/process/variant/python/PythonVariantConverter.java
+++ b/gremlin-variant/src/main/java/org/apache/tinkerpop/gremlin/process/variant/python/PythonVariantConverter.java
@@ -33,6 +33,7 @@ import org.apache.tinkerpop.gremlin.structure.Column;
 import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Element;
 import org.apache.tinkerpop.gremlin.structure.T;
+import org.apache.tinkerpop.gremlin.structure.VertexProperty;
 import org.apache.tinkerpop.gremlin.util.ScriptEngineCache;
 import org.apache.tinkerpop.gremlin.util.iterator.ArrayIterator;
 import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
@@ -112,23 +113,27 @@ public class PythonVariantConverter implements VariantConverter {
         else if (object instanceof List) {
             final List list = new ArrayList<>(((List) object).size());
             for (final Object item : (List) object) {
-                list.add(item instanceof String ? "'" + item + "'" : convertToString(item));
+                list.add(item instanceof String ? "'" + item + "'" : convertToString(item));
// hack
             }
             return list.toString();
         } else if (object instanceof Long)
             return object + "L";
+        else if (object instanceof Class)
+            return ((Class) object).getCanonicalName();
         else if (object instanceof SackFunctions.Barrier)
-            return "\"SackFunctions.Barrier." + object.toString() + "\"";
+            return "Barrier." + object.toString();
+        else if (object instanceof VertexProperty.Cardinality)
+            return "Cardinality." + object.toString();
         else if (object instanceof Direction)
             return "Direction." + object.toString();
         else if (object instanceof Operator)
-            return "\"Operator." + object.toString() + "\"";
+            return ((object == Operator.and || object == Operator.or) ? "Operator._" : "Operator.")
+ object.toString();
         else if (object instanceof Pop)
             return "Pop." + object.toString();
         else if (object instanceof Column)
             return "Column." + object.toString();
         else if (object instanceof P)
-            return "\"P." + ((P) object).getBiPredicate() + "(" + (((P) object).getValue()
instanceof String ? "'" + ((P) object).getValue() + "'" : convertToString(((P) object).getValue()))
+ ")" + "\"";
+            return "P." + ((P) object).getBiPredicate() + "(" + (((P) object).getValue()
instanceof String ? "'" + ((P) object).getValue() + "'" : convertToString(((P) object).getValue()))
+ ")";
         else if (object instanceof T)
             return "T." + object.toString();
         else if (object instanceof Order)

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/762c2c84/gremlin-variant/src/test/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalSourceTest.java
----------------------------------------------------------------------
diff --git a/gremlin-variant/src/test/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalSourceTest.java
b/gremlin-variant/src/test/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalSourceTest.java
new file mode 100644
index 0000000..672eeca
--- /dev/null
+++ b/gremlin-variant/src/test/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalSourceTest.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tinkerpop.gremlin.process.variant;
+
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class VariantGraphTraversalSourceTest {
+
+    private static Set<String> NO_GRAPH = new HashSet<>(Arrays.asList("clone",
"iterate"));
+
+    @Test
+    public void variantGraphTraversalSourceShouldHaveMethodsOfGraphTraversalSource() {
+        for (Method methodA : GraphTraversalSource.class.getMethods()) {
+            if ((GraphTraversal.class.isAssignableFrom(methodA.getReturnType()) || GraphTraversalSource.class.isAssignableFrom(methodA.getReturnType()))
&& !NO_GRAPH.contains(methodA.getName())) {
+                boolean found = false;
+                final String methodAName = methodA.getName();
+                final String methodAParameters = Arrays.asList(methodA.getParameterTypes()).toString();
+                for (final Method methodB : VariantGraphTraversalSource.class.getDeclaredMethods())
{
+                    final String methodBName = methodB.getName();
+                    final String methodBParameters = Arrays.asList(methodB.getParameterTypes()).toString();
+                    if (methodAName.equals(methodBName) && methodAParameters.equals(methodBParameters))
+                        found = true;
+                }
+                if (!found)
+                    throw new IllegalStateException(VariantGraphTraversal.class.getSimpleName()
+ " is missing the following method: " + methodAName + ":" + methodAParameters);
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/762c2c84/gremlin-variant/src/test/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalTest.java
----------------------------------------------------------------------
diff --git a/gremlin-variant/src/test/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalTest.java
b/gremlin-variant/src/test/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalTest.java
new file mode 100644
index 0000000..fd4ad0d
--- /dev/null
+++ b/gremlin-variant/src/test/java/org/apache/tinkerpop/gremlin/process/variant/VariantGraphTraversalTest.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tinkerpop.gremlin.process.variant;
+
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class VariantGraphTraversalTest {
+
+    private static Set<String> NO_GRAPH = new HashSet<>(Arrays.asList("asAdmin","iterate"));
+
+    @Test
+    public void variantGraphTraversalShouldHaveMethodsOfGraphTraversal() {
+        for (Method methodA : GraphTraversal.class.getMethods()) {
+            if (GraphTraversal.class.isAssignableFrom(methodA.getReturnType()) &&
!NO_GRAPH.contains(methodA.getName())) {
+                boolean found = false;
+                final String methodAName = methodA.getName();
+                final String methodAParameters = Arrays.asList(methodA.getParameterTypes()).toString();
+                for (final Method methodB : VariantGraphTraversal.class.getDeclaredMethods())
{
+                    final String methodBName = methodB.getName();
+                    final String methodBParameters = Arrays.asList(methodB.getParameterTypes()).toString();
+                    if (methodAName.equals(methodBName) && methodAParameters.equals(methodBParameters))
+                        found = true;
+                }
+                if (!found)
+                    throw new IllegalStateException(VariantGraphTraversal.class.getSimpleName()
+ " is missing the following method: " + methodAName + ":" + methodAParameters);
+            }
+        }
+    }
+
+}


Mime
View raw message