Repository: tinkerpop Updated Branches: refs/heads/TINKERPOP-1278 18e3d64ca -> 87be8a7d1
Have CPython testing of side_effects working -- ghetto. Made _list,_set,_all in python as these are Python built in functions and our static enums shouldn't override these symbols. Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/87be8a7d Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/87be8a7d Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/87be8a7d Branch: refs/heads/TINKERPOP-1278 Commit: 87be8a7d1b7f3f000a6f2aa1fb7e7aa877c5a1cf Parents: 18e3d64 Author: Marko A. Rodriguez <[email protected]> Authored: Thu Aug 25 16:06:24 2016 -0600 Committer: Marko A. Rodriguez <[email protected]> Committed: Thu Aug 25 16:06:24 2016 -0600 ---------------------------------------------------------------------- .../gremlin/python/jsr223/PythonTranslator.java | 4 +- .../gremlin/python/jsr223/SymbolHelper.java | 3 + .../driver/driver_remote_connection.py | 10 ++- .../jython/gremlin_python/process/traversal.py | 10 +-- .../gremlin_python/structure/io/graphson.py | 5 +- .../driver/DriverRemoteConnectionTest.java | 73 ++++++++++++++------ .../io/graphson/GraphSONWriterTest.java | 6 +- 7 files changed, 74 insertions(+), 37 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/87be8a7d/gremlin-python/src/main/java/org/apache/tinkerpop/gremlin/python/jsr223/PythonTranslator.java ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/java/org/apache/tinkerpop/gremlin/python/jsr223/PythonTranslator.java b/gremlin-python/src/main/java/org/apache/tinkerpop/gremlin/python/jsr223/PythonTranslator.java index 0721c34..08295e0 100644 --- a/gremlin-python/src/main/java/org/apache/tinkerpop/gremlin/python/jsr223/PythonTranslator.java +++ b/gremlin-python/src/main/java/org/apache/tinkerpop/gremlin/python/jsr223/PythonTranslator.java @@ -146,9 +146,9 @@ public class PythonTranslator implements Translator.ScriptTranslator { else if (object instanceof Class) return ((Class) object).getCanonicalName(); else if (object instanceof VertexProperty.Cardinality) - return "Cardinality." + object.toString(); + return "Cardinality." + SymbolHelper.toPython(object.toString()); else if (object instanceof SackFunctions.Barrier) - return "Barrier." + object.toString(); + return "Barrier." + SymbolHelper.toPython(object.toString()); else if (object instanceof Enum) return convertStatic(((Enum) object).getDeclaringClass().getSimpleName() + ".") + SymbolHelper.toPython(object.toString()); else if (object instanceof P) http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/87be8a7d/gremlin-python/src/main/java/org/apache/tinkerpop/gremlin/python/jsr223/SymbolHelper.java ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/java/org/apache/tinkerpop/gremlin/python/jsr223/SymbolHelper.java b/gremlin-python/src/main/java/org/apache/tinkerpop/gremlin/python/jsr223/SymbolHelper.java index 2b4c112..5264ce7 100644 --- a/gremlin-python/src/main/java/org/apache/tinkerpop/gremlin/python/jsr223/SymbolHelper.java +++ b/gremlin-python/src/main/java/org/apache/tinkerpop/gremlin/python/jsr223/SymbolHelper.java @@ -39,6 +39,9 @@ public final class SymbolHelper { TO_PYTHON_MAP.put("is", "_is"); TO_PYTHON_MAP.put("not", "_not"); TO_PYTHON_MAP.put("from", "_from"); + TO_PYTHON_MAP.put("list", "_list"); + TO_PYTHON_MAP.put("set", "_set"); + TO_PYTHON_MAP.put("all", "_all"); // TO_PYTHON_MAP.forEach((k, v) -> FROM_PYTHON_MAP.put(v, k)); } http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/87be8a7d/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py b/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py index 165eed6..f1aaeee 100644 --- a/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py +++ b/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py @@ -86,7 +86,10 @@ class DriverRemoteConnection(RemoteConnection): } } } - keys = yield self._execute_message(message) + try: + keys = yield self._execute_message(message) + except: + keys = [] raise gen.Return(set(keys)) @gen.coroutine @@ -107,7 +110,10 @@ class DriverRemoteConnection(RemoteConnection): "aliases": {"g": self.traversal_source} } } - value = yield self._execute_message(message) + try: + value = yield self._execute_message(message) + except: + raise KeyError(key) raise gen.Return(value) @gen.coroutine http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/87be8a7d/gremlin-python/src/main/jython/gremlin_python/process/traversal.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/gremlin_python/process/traversal.py b/gremlin-python/src/main/jython/gremlin_python/process/traversal.py index 7b9d4d5..b39ec35 100644 --- a/gremlin-python/src/main/jython/gremlin_python/process/traversal.py +++ b/gremlin-python/src/main/jython/gremlin_python/process/traversal.py @@ -77,10 +77,10 @@ class Traversal(object): Barrier = Enum('Barrier', 'normSack') statics.add_static('normSack', Barrier.normSack) -Cardinality = Enum('Cardinality', 'list set single') +Cardinality = Enum('Cardinality', '_list _set single') statics.add_static('single', Cardinality.single) -statics.add_static('list', Cardinality.list) -statics.add_static('set', Cardinality.set) +statics.add_static('_list', Cardinality._list) +statics.add_static('_set', Cardinality._set) Column = Enum('Column', 'keys values') statics.add_static('keys', Column.keys) @@ -113,10 +113,10 @@ statics.add_static('keyDecr', Order.keyDecr) statics.add_static('valueDecr', Order.valueDecr) statics.add_static('shuffle', Order.shuffle) -Pop = Enum('Pop', 'all first last') +Pop = Enum('Pop', '_all first last') statics.add_static('first', Pop.first) statics.add_static('last', Pop.last) -statics.add_static('all', Pop.all) +statics.add_static('_all', Pop._all) Scope = Enum('Scope', '_global local') statics.add_static('_global', Scope._global) http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/87be8a7d/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py index c3199c6..b8305ac 100644 --- a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py +++ b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py @@ -70,7 +70,7 @@ class GraphSONReader(object): type = object[_SymbolHelper._TYPE] if type in deserializers: return deserializers[type]._objectify(object) - # list and map are treated as normal json objects (could be isolated deserializers) + # list and map are treated as normal json objects (could be isolated deserializers) newDict = {} for key in object: newDict[GraphSONReader._objectify(key)] = GraphSONReader._objectify(object[key]) @@ -247,7 +247,8 @@ class PropertyDeserializer(GraphSONDeserializer): class _SymbolHelper(object): symbolMap = {"_global": "global", "_as": "as", "_in": "in", "_and": "and", - "_or": "or", "_is": "is", "_not": "not", "_from": "from"} + "_or": "or", "_is": "is", "_not": "not", "_from": "from", + "_set": "set", "_list": "list", "_all": "all"} _TYPE = "@type" _VALUE = "@value" http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/87be8a7d/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/driver/DriverRemoteConnectionTest.java ---------------------------------------------------------------------- diff --git a/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/driver/DriverRemoteConnectionTest.java b/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/driver/DriverRemoteConnectionTest.java index ba0f5d0..0434809 100644 --- a/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/driver/DriverRemoteConnectionTest.java +++ b/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/driver/DriverRemoteConnectionTest.java @@ -20,6 +20,7 @@ package org.apache.tinkerpop.gremlin.python.driver; import org.apache.tinkerpop.gremlin.TestHelper; +import org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversalSideEffects; import org.apache.tinkerpop.gremlin.server.GremlinServer; import org.apache.tinkerpop.gremlin.server.Settings; import org.junit.BeforeClass; @@ -63,30 +64,36 @@ public class DriverRemoteConnectionTest { } } - private static List<String> submit(final String traversal) throws IOException { - final StringBuilder script = new StringBuilder(); - script.append("from gremlin_python import statics\n"); - script.append("from gremlin_python.structure.graph import Graph\n"); - script.append("from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection\n"); - script.append("from gremlin_python.structure.io.graphson import GraphSONWriter\n\n"); - script.append("statics.load_statics(globals())\n"); - script.append("graph = Graph()\n"); - script.append("g = graph.traversal().withRemote(DriverRemoteConnection('ws://localhost:8182','g',username='stephen', password='password'))\n"); - script.append("results = " + traversal + ".toList()\n"); - script.append("print results\n\n"); + private static List<String> submit(final String... scriptLines) throws IOException { + final StringBuilder builder = new StringBuilder(); + builder.append("from gremlin_python import statics\n"); + builder.append("from gremlin_python.structure.graph import Graph\n"); + builder.append("from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection\n"); + builder.append("from gremlin_python.structure.io.graphson import GraphSONWriter\n\n"); + builder.append("statics.load_statics(globals())\n"); + builder.append("graph = Graph()\n"); + builder.append("g = graph.traversal().withRemote(DriverRemoteConnection('ws://localhost:8182','g',username='stephen', password='password'))\n"); + for (int i = 0; i < scriptLines.length - 1; i++) { + builder.append(scriptLines[i] + "\n"); + } + builder.append("final = " + scriptLines[scriptLines.length - 1] + "\n"); + builder.append("if isinstance(final,dict):\n"); + builder.append(" for key in final.keys():\n"); + builder.append(" print (str(key),str(final[key]))\n"); + builder.append("elif isinstance(final,str):\n"); + builder.append(" print final\n"); + builder.append("else:\n"); + builder.append(" for result in final:\n"); + builder.append(" print result\n\n"); File file = TestHelper.generateTempFile(DriverRemoteConnectionTest.class, "temp", "py"); final Writer writer = new BufferedWriter(new FileWriter(file.getAbsoluteFile())); - writer.write(script.toString()); + writer.write(builder.toString()); writer.flush(); writer.close(); final BufferedReader reader = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("python " + file.getAbsolutePath()).getInputStream())); - final List<String> lines = reader.lines() - .map(line -> line.substring(1, line.length() - 1)) - .flatMap(line -> Arrays.stream(line.split(","))) - .map(String::trim) - .collect(Collectors.toList()); + final List<String> lines = reader.lines().map(String::trim).collect(Collectors.toList()); reader.close(); file.delete(); return lines; @@ -99,27 +106,47 @@ public class DriverRemoteConnectionTest { List<String> result = DriverRemoteConnectionTest.submit("g.V().count()"); assertEquals(1, result.size()); - assertEquals("6L", result.get(0)); + assertEquals("6", result.get(0)); // result = DriverRemoteConnectionTest.submit("g.V(1).out('created').name"); assertEquals(1, result.size()); - assertEquals("u'lop'", result.get(0)); + assertEquals("lop", result.get(0)); // result = DriverRemoteConnectionTest.submit("g.V(1).out()"); assertEquals(3, result.size()); assertTrue(result.contains("v[4]")); assertTrue(result.contains("v[2]")); assertTrue(result.contains("v[3]")); + // + result = DriverRemoteConnectionTest.submit("g.V().repeat(out()).times(2).name"); + assertEquals(2, result.size()); + assertTrue(result.contains("lop")); + assertTrue(result.contains("ripple")); } @Test - public void testAnonymousTraversals() throws Exception { + public void testSideEffects() throws Exception { if (!PYTHON_EXISTS) return; - List<String> result = DriverRemoteConnectionTest.submit("g.V().repeat(out()).times(2).name"); + List<String> result = DriverRemoteConnectionTest.submit( + "t = g.V().out().iterate()", + "str(t.side_effects)"); + assertEquals(1, result.size()); + assertEquals(new DefaultTraversalSideEffects().toString(), result.get(0)); + // + result = DriverRemoteConnectionTest.submit( + "t = g.V().out('created').groupCount('m').by('name').iterate()", + "t.side_effects['m']"); + assertEquals(2, result.size()); + assertTrue(result.contains("('ripple', '1')")); + assertTrue(result.contains("('lop', '3')")); + // + result = DriverRemoteConnectionTest.submit( + "t = g.V().out('created').groupCount('m').by('name').aggregate('n').iterate()", + "t.side_effects.keys()"); assertEquals(2, result.size()); - assertTrue(result.contains("u'lop'")); - assertTrue(result.contains("u'ripple'")); + assertTrue(result.contains("m")); + assertTrue(result.contains("n")); } } http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/87be8a7d/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/structure/io/graphson/GraphSONWriterTest.java ---------------------------------------------------------------------- diff --git a/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/structure/io/graphson/GraphSONWriterTest.java b/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/structure/io/graphson/GraphSONWriterTest.java index 1b438b6..5e71bda 100644 --- a/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/structure/io/graphson/GraphSONWriterTest.java +++ b/gremlin-python/src/test/java/org/apache/tinkerpop/gremlin/python/structure/io/graphson/GraphSONWriterTest.java @@ -101,9 +101,9 @@ public class GraphSONWriterTest { for (final T t : T.values()) { assertEquals(t, mapper.readValue(jythonEngine.eval("GraphSONWriter.writeObject(T." + t.name() + ")").toString(), Object.class)); } - for (final Pop t : Pop.values()) { - assertEquals(t, mapper.readValue(jythonEngine.eval("GraphSONWriter.writeObject(Pop." + t.name() + ")").toString(), Object.class)); - } + assertEquals(Pop.first, mapper.readValue(jythonEngine.eval("GraphSONWriter.writeObject(Pop.first)").toString(), Object.class)); + assertEquals(Pop.last, mapper.readValue(jythonEngine.eval("GraphSONWriter.writeObject(Pop.last)").toString(), Object.class)); + assertEquals(Pop.all, mapper.readValue(jythonEngine.eval("GraphSONWriter.writeObject(Pop._all)").toString(), Object.class)); assertEquals(Scope.global, mapper.readValue(jythonEngine.eval("GraphSONWriter.writeObject(Scope._global)").toString(), Object.class)); assertEquals(Scope.local, mapper.readValue(jythonEngine.eval("GraphSONWriter.writeObject(Scope.local)").toString(), Object.class)); }
