You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by GitBox <gi...@apache.org> on 2019/07/09 11:38:43 UTC

[GitHub] [tinkerpop] spmallette commented on a change in pull request #1158: Tinkerpop 2252 Support Parameterized Groovy Script Transformation

spmallette commented on a change in pull request #1158: Tinkerpop 2252 Support Parameterized Groovy Script Transformation
URL: https://github.com/apache/tinkerpop/pull/1158#discussion_r301538265
 
 

 ##########
 File path: gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/ParameterizedGroovyTranslator.java
 ##########
 @@ -0,0 +1,346 @@
+/*
+ *  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.groovy.jsr223;
+
+import org.apache.tinkerpop.gremlin.process.traversal.P;
+import org.apache.tinkerpop.gremlin.process.traversal.TextP;
+import org.apache.commons.configuration.ConfigurationConverter;
+import org.apache.tinkerpop.gremlin.process.traversal.Bytecode;
+import org.apache.tinkerpop.gremlin.process.traversal.Translator;
+import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
+import org.apache.tinkerpop.gremlin.process.traversal.SackFunctions;
+import org.apache.tinkerpop.gremlin.process.traversal.ParameterizedScript;
+import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy;
+import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalOptionParent;
+import org.apache.tinkerpop.gremlin.process.traversal.strategy.TraversalStrategyProxy;
+import org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP;
+import org.apache.tinkerpop.gremlin.process.traversal.util.OrP;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.apache.tinkerpop.gremlin.structure.Element;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.apache.tinkerpop.gremlin.structure.VertexProperty;
+import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
+import org.apache.tinkerpop.gremlin.util.function.Lambda;
+
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+/**
+ * Converts bytecode to a Parameterized Groovy string of Gremlin, include script and parameters
+ * <p>
+ * {@link GroovyTranslator } implementation provide another way to convert bytecode to Groovy string of Gremlin.
+ * <p>
+ * The only difference between ParameterizedGroovyTranslator and GroovyTranslator is that in GroovyTranslator,
+ * the translation process does not extract parameters.
+ * <p>
+ * Example:
+ * <pre>
+ * Input bytecode: g.addV("person").property("name","marko")
+ * GroovyTranslator:
+ *  dsl: g.addV("person").property("name","marko");
+ *  bindings:  empty map
+ * ParameterizedGroovyTranslator:
+ *  dsl: g.addV(_args_0).property(_args_1,_args_2);
+ *  bindings: {"_args_0":"person", "_args_1":"name", "_args_2":"marko"}
+ * </pre>
+ *
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ * @author Stark Arya (sandszhou.zj@alibaba-inc.com)
+ */
+
+public class ParameterizedGroovyTranslator implements Translator.ParameterizedScriptTranslator {
+    private final String traversalSource;
+    private final TypeTranslator typeTranslator;
+
+    private ParameterizedGroovyTranslator(final String traversalSource, TypeTranslator typeTranslator) {
+        this.traversalSource = traversalSource;
+        this.typeTranslator = typeTranslator;
+    }
+
+    public static final ParameterizedGroovyTranslator of(final String traversalSource) {
+        return of(traversalSource,null);
+    }
+
+    public static final ParameterizedGroovyTranslator of(final String traversalSource, final TypeTranslator typeTranslator) {
+        return new ParameterizedGroovyTranslator(traversalSource,
+                Optional.ofNullable(typeTranslator).orElseGet(DefaultTypeTranslator::new));
+    }
+
+    //////
+
+    @Override
+    public ParameterizedScript translate(final Bytecode bytecode) {
+        return (ParameterizedScript) typeTranslator.apply(traversalSource, bytecode);
+    }
+
+    @Override
+    public String getTargetLanguage() {
+        return "gremlin-groovy";
+    }
+
+    @Override
+    public String toString() {
+        return StringFactory.translatorString(this);
+    }
+
+    @Override
+    public String getTraversalSource() {
+        return this.traversalSource;
+    }
+
+    /**
+     * Performs standard type translation for the TinkerPop types to Parameterized Groovy.
+     */
+    public static class DefaultTypeTranslator implements TypeTranslator {
 
 Review comment:
   It bothers me a bit that the `TypeTranslator` is being used for more than just converting types which was its original intent so that providers/users could plugin their own conversions for custom types. If we implement things the way they are now, they not only have to write two separate `TypeTranslators` (if they want to support both forms of translation) but they also have to reason about `Parameters`. I'm not sure what the solution is there, but I think that's the next thing that's concerning me about this change that I don't have a good answer for yet.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services