tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ok...@apache.org
Subject tinkerpop git commit: Got lambdas serialized into Bytecode GraphSON. I have a way to make it so you can do every thing via JavaTranslator --- Java functions/predication/etc. will just wrap a call to a ScriptEngine. Got is working in a 'hardcoded' way. To
Date Thu, 14 Jul 2016 02:05:18 GMT
Repository: tinkerpop
Updated Branches:
  refs/heads/TINKERPOP-1278 2ad524039 -> 1bfa333ea


Got lambdas serialized into Bytecode GraphSON. I have a way to make it so you can do every
thing via JavaTranslator --- Java functions/predication/etc. will just wrap a call to a ScriptEngine.
Got is working in a 'hardcoded' way. Tomorrow I will make it delicious.


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

Branch: refs/heads/TINKERPOP-1278
Commit: 1bfa333ea583619d6dae203436d527932814a188
Parents: 2ad5240
Author: Marko A. Rodriguez <okrammarko@gmail.com>
Authored: Wed Jul 13 20:05:14 2016 -0600
Committer: Marko A. Rodriguez <okrammarko@gmail.com>
Committed: Wed Jul 13 20:05:14 2016 -0600

----------------------------------------------------------------------
 .../structure/io/graphson/GraphSONModule.java   |  3 +
 .../graphson/GraphSONTraversalSerializers.java  | 92 ++++++++++++++------
 .../tinkerpop/gremlin/util/function/Lambda.java | 17 +++-
 .../structure/TinkerGraphPlayTest.java          |  4 +-
 4 files changed, 89 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/1bfa333e/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
index 09bf169..9af3d66 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
@@ -37,6 +37,7 @@ import org.apache.tinkerpop.gremlin.structure.T;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
 import org.apache.tinkerpop.gremlin.structure.util.star.StarGraphGraphSONSerializer;
+import org.apache.tinkerpop.gremlin.util.function.Lambda;
 import org.apache.tinkerpop.shaded.jackson.databind.module.SimpleModule;
 
 import java.time.Duration;
@@ -136,10 +137,12 @@ abstract class GraphSONModule extends SimpleModule {
             addSerializer(Scope.class, new GraphSONTraversalSerializers.EnumJacksonSerializer());
             addSerializer(T.class, new GraphSONTraversalSerializers.EnumJacksonSerializer());
             addSerializer(P.class, new GraphSONTraversalSerializers.PJacksonSerializer());
+            addSerializer(Lambda.class, new GraphSONTraversalSerializers.LambdaJacksonSerializer());
             // -- deserializers for traversal
             addDeserializer(Bytecode.class, new GraphSONTraversalSerializers.BytecodeJacksonDeserializer());
             addDeserializer(Enum.class, new GraphSONTraversalSerializers.EnumJacksonDeserializer());
             addDeserializer(P.class, new GraphSONTraversalSerializers.PJacksonDeserializer());
+            addDeserializer(Lambda.class, new GraphSONTraversalSerializers.LambdaJacksonDeserializer());
 
         }
 

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/1bfa333e/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTraversalSerializers.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTraversalSerializers.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTraversalSerializers.java
index 60c86f5..9b793a8 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTraversalSerializers.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTraversalSerializers.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.T;
 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
+import org.apache.tinkerpop.gremlin.util.function.Lambda;
 import org.apache.tinkerpop.shaded.jackson.core.JsonGenerator;
 import org.apache.tinkerpop.shaded.jackson.core.JsonParser;
 import org.apache.tinkerpop.shaded.jackson.core.JsonProcessingException;
@@ -145,6 +146,24 @@ public final class GraphSONTraversalSerializers {
 
     }
 
+    final static class LambdaJacksonSerializer extends StdSerializer<Lambda> {
+
+        public LambdaJacksonSerializer() {
+            super(Lambda.class);
+        }
+
+        @Override
+        public void serialize(final Lambda lambda, final JsonGenerator jsonGenerator, final
SerializerProvider serializerProvider)
+                throws IOException {
+            jsonGenerator.writeStartObject();
+            jsonGenerator.writeStringField("@type", "Lambda");
+            jsonGenerator.writeStringField("value", lambda.getLambdaScript());
+            jsonGenerator.writeNumberField("arguments", lambda instanceof Lambda.ZeroArgLambda
? 0 : lambda instanceof Lambda.OneArgLambda ? 1 : 2);
+            jsonGenerator.writeEndObject();
+        }
+
+    }
+
     ///////////////////
     // DESERIALIZERS //
     //////////////////
@@ -166,6 +185,8 @@ public final class GraphSONTraversalSerializers {
                         arguments.add(oc.readValue(argument.traverse(oc), Bytecode.class));
                     else if (type.equals("P"))
                         arguments.add(oc.readValue(argument.traverse(oc), P.class));
+                    else if (type.equals("Lambda"))
+                        arguments.add(oc.readValue(argument.traverse(oc), Lambda.class));
                     else
                         arguments.add(oc.readValue(argument.traverse(oc), Enum.class));
                 } else if (argument.getNodeType().equals(JsonNodeType.NUMBER)) {
@@ -207,6 +228,40 @@ public final class GraphSONTraversalSerializers {
         }
     }
 
+    final static class EnumJacksonDeserializer extends StdDeserializer<Enum> {
+
+        public EnumJacksonDeserializer() {
+            super(Enum.class);
+        }
+
+        @Override
+        public Enum deserialize(final JsonParser jsonParser, final DeserializationContext
deserializationContext) throws IOException, JsonProcessingException {
+
+            final ObjectCodec oc = jsonParser.getCodec();
+            final JsonNode node = oc.readTree(jsonParser);
+            final String type = node.get("@type").textValue();
+            if (type.equals("Cardinality"))
+                return VertexProperty.Cardinality.valueOf(node.get("value").textValue());
+            else if (type.equals("Column"))
+                return Column.valueOf(node.get("value").textValue());
+            else if (type.equals("Direction"))
+                return Direction.valueOf(node.get("value").textValue());
+            else if (type.equals("Operator"))
+                return Operator.valueOf(node.get("value").textValue());
+            else if (type.equals("Order"))
+                return Order.valueOf(node.get("value").textValue());
+            else if (type.equals("Pop"))
+                return Pop.valueOf(node.get("value").textValue());
+            else if (type.equals("Scope"))
+                return Scope.valueOf(node.get("value").textValue());
+            else if (type.equals("T"))
+                return T.valueOf(node.get("value").textValue());
+            else
+                throw new IOException("Unknown enum type: " + type);
+
+        }
+    }
+
     final static class PJacksonDeserializer extends StdDeserializer<P> {
 
         public PJacksonDeserializer() {
@@ -233,41 +288,28 @@ public final class GraphSONTraversalSerializers {
                     throw new IOException();
                 }
             }
-
         }
     }
 
-    final static class EnumJacksonDeserializer extends StdDeserializer<Enum> {
+    final static class LambdaJacksonDeserializer extends StdDeserializer<Lambda> {
 
-        public EnumJacksonDeserializer() {
-            super(Enum.class);
+        public LambdaJacksonDeserializer() {
+            super(Lambda.class);
         }
 
         @Override
-        public Enum deserialize(final JsonParser jsonParser, final DeserializationContext
deserializationContext) throws IOException, JsonProcessingException {
+        public Lambda deserialize(final JsonParser jsonParser, final DeserializationContext
deserializationContext) throws IOException, JsonProcessingException {
 
             final ObjectCodec oc = jsonParser.getCodec();
             final JsonNode node = oc.readTree(jsonParser);
-            final String type = node.get("@type").textValue();
-            if (type.equals("Cardinality"))
-                return VertexProperty.Cardinality.valueOf(node.get("value").textValue());
-            else if (type.equals("Column"))
-                return Column.valueOf(node.get("value").textValue());
-            else if (type.equals("Direction"))
-                return Direction.valueOf(node.get("value").textValue());
-            else if (type.equals("Operator"))
-                return Operator.valueOf(node.get("value").textValue());
-            else if (type.equals("Order"))
-                return Order.valueOf(node.get("value").textValue());
-            else if (type.equals("Pop"))
-                return Pop.valueOf(node.get("value").textValue());
-            else if (type.equals("Scope"))
-                return Scope.valueOf(node.get("value").textValue());
-            else if (type.equals("T"))
-                return T.valueOf(node.get("value").textValue());
-            else
-                throw new IOException("Unknown enum type: " + type);
-
+            assert node.get("@type").textValue().equals("Lambda");
+            final JsonNode lambda = node.get("value");
+            final int arguments = node.get("arguments").intValue();
+            return 0 == arguments ?
+                    new Lambda.ZeroArgLambda<>(lambda.textValue()) :
+                    1 == arguments ?
+                            new Lambda.OneArgLambda<>(lambda.textValue()) :
+                            new Lambda.TwoArgLambda<>(lambda.textValue());
         }
     }
 

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/1bfa333e/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/function/Lambda.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/function/Lambda.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/function/Lambda.java
index 75fd1af..ebc0b65 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/function/Lambda.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/function/Lambda.java
@@ -45,6 +45,21 @@ public interface Lambda extends Serializable {
         public String getLambdaScript() {
             return this.lambdaSource;
         }
+
+        @Override
+        public String toString() {
+            return this.lambdaSource;
+        }
+
+        @Override
+        public int hashCode() {
+            return this.lambdaSource.hashCode();
+        }
+
+        @Override
+        public boolean equals(final Object object) {
+            return object instanceof Lambda && ((Lambda) object).getLambdaScript().equals(this.lambdaSource);
+        }
     }
 
     public static class ZeroArgLambda<A> extends AbstractLambda implements Supplier<A>
{
@@ -68,7 +83,7 @@ public interface Lambda extends Serializable {
 
         @Override
         public B apply(final A a) {
-            return null;
+            return (B) new ScriptEngineLambda("gremlin-groovy", this.getLambdaScript()).apply(a);
// TODO: GremlinScriptEngine.lambda(String, Object... arguments)
         }
 
         @Override

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/1bfa333e/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphPlayTest.java
----------------------------------------------------------------------
diff --git a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphPlayTest.java
b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphPlayTest.java
index 86a091c..2fc5673 100644
--- a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphPlayTest.java
+++ b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphPlayTest.java
@@ -36,6 +36,7 @@ import org.apache.tinkerpop.gremlin.structure.io.graphml.GraphMLIo;
 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader;
 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
 import org.apache.tinkerpop.gremlin.util.TimeUtil;
+import org.apache.tinkerpop.gremlin.util.function.Lambda;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.slf4j.Logger;
@@ -61,6 +62,7 @@ import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.outE;
 import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.select;
 import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.union;
 import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.valueMap;
+import static org.apache.tinkerpop.gremlin.util.function.Lambda.function;
 
 /**
  * @author Stephen Mallette (http://stephen.genoprime.com)
@@ -74,7 +76,7 @@ public class TinkerGraphPlayTest {
         Graph graph = TinkerFactory.createModern();
         GraphTraversalSource g = graph.traversal().withComputer();
 
-        Traversal<?, ?> traversal1 = g.V().has("age", P.gt(10).and(P.lt(30))).out("knows",
"created").repeat(__.as("a").out().as("b").hasLabel("software")).times(1).select("b").by(T.label).groupCount();
+        Traversal<?, ?> traversal1 = g.V().has("age", P.gt(10).and(P.lt(30))).out("knows",
"created").repeat(__.as("a").out().as("b").hasLabel("software")).times(1).select("b").by(T.label).groupCount().map(function("a.get()"));
         Bytecode bytecode1 = traversal1.asAdmin().getBytecode();
         System.out.println("BYTECODE 1: \n  " + bytecode1 + "\n");
         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();


Mime
View raw message