[
https://issues.apache.org/jira/browse/TINKERPOP-2452?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17283140#comment-17283140
]
ASF GitHub Bot commented on TINKERPOP-2452:
-------------------------------------------
FlorianHockmann commented on a change in pull request #1387:
URL: https://github.com/apache/tinkerpop/pull/1387#discussion_r574646116
##########
File path:
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/translator/DotNetTranslator.java
##########
@@ -0,0 +1,434 @@
+/*
+ * 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.traversal.translator;
+
+import org.apache.commons.configuration2.ConfigurationConverter;
+import org.apache.commons.text.StringEscapeUtils;
+import org.apache.tinkerpop.gremlin.process.traversal.Bytecode;
+import org.apache.tinkerpop.gremlin.process.traversal.P;
+import org.apache.tinkerpop.gremlin.process.traversal.SackFunctions;
+import org.apache.tinkerpop.gremlin.process.traversal.Script;
+import org.apache.tinkerpop.gremlin.process.traversal.TextP;
+import org.apache.tinkerpop.gremlin.process.traversal.Translator;
+import org.apache.tinkerpop.gremlin.process.traversal.TraversalSource;
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
+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.Direction;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+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.StringFactory;
+import org.apache.tinkerpop.gremlin.util.function.Lambda;
+
+import java.sql.Timestamp;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.UUID;
+
+/**
+ * Converts bytecode to a C# string of Gremlin.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public final class DotNetTranslator implements Translator.ScriptTranslator {
+
+ private final String traversalSource;
+ private final TypeTranslator typeTranslator;
+
+ private static final List<String> methodsWithArgsNotNeedingGeneric =
Arrays.asList(GraphTraversal.Symbols.group,
+ GraphTraversal.Symbols.groupCount, GraphTraversal.Symbols.sack);
+
+ private DotNetTranslator(final String traversalSource, final
TypeTranslator typeTranslator) {
+ this.traversalSource = traversalSource;
+ this.typeTranslator = typeTranslator;
+ }
+
+ /**
+ * Creates the translator with a {@code false} argument to {@code
withParameters} using
+ * {@link #of(String, boolean)}.
+ */
+ public static DotNetTranslator of(final String traversalSource) {
+ return of(traversalSource, false);
+ }
+
+ /**
+ * Creates the translator with the {@link DefaultTypeTranslator} passing
the {@code withParameters} option to it
+ * which will handle type translation in a fashion that should typically
increase cache hits and reduce
+ * compilation times if enabled at the sacrifice to rewriting of the
script that could reduce readability.
+ */
+ public static DotNetTranslator of(final String traversalSource, final
boolean withParameters) {
+ return of(traversalSource, new DefaultTypeTranslator(withParameters));
+ }
+
+ /**
+ * Creates the translator with a custom {@link TypeTranslator} instance.
+ */
+ public static DotNetTranslator of(final String traversalSource, final
TypeTranslator typeTranslator) {
+ return new DotNetTranslator(traversalSource, typeTranslator);
+ }
+
+ @Override
+ public Script translate(final Bytecode bytecode) {
+ return typeTranslator.apply(traversalSource, bytecode);
+ }
+
+ @Override
+ public String getTargetLanguage() {
+ return "gremlin-dotnet";
+ }
+
+ @Override
+ public String toString() {
+ return StringFactory.translatorString(this);
+ }
+
+ @Override
+ public String getTraversalSource() {
+ return this.traversalSource;
+ }
+
+ /**
+ * Performs standard type translation for the TinkerPop types to C#.
+ */
+ public static class DefaultTypeTranslator extends AbstractTypeTranslator {
+
+ public DefaultTypeTranslator(final boolean withParameters) {
+ super(withParameters);
+ }
+
+ @Override
+ protected String getNullSyntax() {
+ return "null";
+ }
+
+ @Override
+ protected String getSyntax(final String o) {
+ return "\"" + StringEscapeUtils.escapeJava(o) + "\"";
+ }
+
+ @Override
+ protected String getSyntax(final Boolean o) {
+ return o.toString();
+ }
+
+ @Override
+ protected String getSyntax(final Date o) {
+ return "DateTimeOffset.FromUnixTimeMillisecond(" + o.getTime() +
")";
+ }
+
+ @Override
+ protected String getSyntax(final Timestamp o) {
+ return "DateTimeOffset.FromUnixTimeMillisecond(" + o.getTime() +
")";
+ }
+
+ @Override
+ protected String getSyntax(final UUID o) {
+ return "new Guid(\"" + o.toString() + "\")";
+ }
+
+ @Override
+ protected String getSyntax(final Lambda o) {
+ return "() => \"" +
StringEscapeUtils.escapeEcmaScript(o.getLambdaScript().trim()) + "\"";
+ }
+
+ @Override
+ protected String getSyntax(final SackFunctions.Barrier o) {
+ return "Barrier." + SymbolHelper.toCSharp(o.toString());
+ }
+
+ @Override
+ protected String getSyntax(final VertexProperty.Cardinality o) {
+ return "Cardinality." + SymbolHelper.toCSharp(o.toString());
+ }
+
+ @Override
+ protected String getSyntax(final TraversalOptionParent.Pick o) {
+ return "Pick." + SymbolHelper.toCSharp(o.toString());
+ }
+
+ @Override
+ protected String getSyntax(final Number o) {
+ return o.toString();
+ }
+
+ @Override
+ protected Script produceScript(final Set<?> o) {
+ final Iterator<?> iterator = ((List<?>) o).iterator();
+ script.append("new HashSet<object> {");
+
+ while (iterator.hasNext()) {
+ final Object nextItem = iterator.next();
+ convertToScript(nextItem);
+ if (iterator.hasNext())
+ script.append(",").append(" ");
+ }
+
+ return script.append("}");
+ }
+
+ @Override
+ protected Script produceScript(final List<?> o) {
+ final Iterator<?> iterator = ((List<?>) o).iterator();
+ script.append("new List<object> {");
+
+ while (iterator.hasNext()) {
+ final Object nextItem = iterator.next();
+ convertToScript(nextItem);
+ if (iterator.hasNext())
+ script.append(",").append(" ");
+ }
+
+ return script.append("}");
+ }
+
+ @Override
+ protected Script produceScript(final Map<?, ?> o) {
+ script.append("new Dictionary<object,object> {");
+ produceKeyValuesForMap(o);
+ return script.append("}");
+ }
+
+ @Override
+ protected Script produceScript(final Class<?> o) {
+ return script.append(o.getCanonicalName());
+ }
+
+ @Override
+ protected Script produceScript(final Enum<?> o) {
+ final String e = o instanceof Direction || o instanceof T ?
+ o.name().substring(0,1).toUpperCase() +
o.name().substring(1).toLowerCase() :
+ o.name().substring(0,1).toUpperCase() +
o.name().substring(1);
+ return script.append(o.getDeclaringClass().getSimpleName() + "." +
e);
+ }
+
+ @Override
+ protected Script produceScript(final Vertex o) {
+ script.append("new Vertex(");
+ convertToScript(o.id());
+ script.append(",");
+ convertToScript(o.label());
+ return script.append(")");
+ }
+
+ @Override
+ protected Script produceScript(final Edge o) {
+ script.append("new Edge(");
+ convertToScript(o.id());
+ script.append(", new Vertex(");
+ convertToScript(o.outVertex().id());
+ script.append(",");
+ convertToScript(o.outVertex().label());
+ script.append("),");
+ convertToScript(o.label());
+ script.append(", new Vertex(");
+ convertToScript(o.inVertex().id());
+ script.append(",");
+ convertToScript(o.inVertex().label());
+ return script.append("))");
+ }
+
+ @Override
+ protected Script produceScript(final VertexProperty<?> o) {
+ script.append("new Property(");
+ convertToScript(o.id());
+ script.append(",");
+ convertToScript(o.label());
+ script.append(",");
+ convertToScript(o.value());
+ script.append(",");
+ return script.append("null)");
+ }
+
+ @Override
+ protected Script produceScript(final TraversalStrategyProxy<?> o) {
+ if (o.getConfiguration().isEmpty()) {
+ return script.append("new " +
o.getStrategyClass().getSimpleName() + "()");
+ } else {
+ script.append("new " + o.getStrategyClass().getSimpleName() +
"(configuration: ");
Review comment:
Looks like it's really complicated to get this fully working with
Gremlin.Net. I just tried an example from the reference docs and got C# code
that doesn't compile:
```groovy
gremlin>
translator.translate(g.withStrategies(SubgraphStrategy.build().vertexProperties(hasNot('endTime')).create()).V().has('name','marko')).getScript()
==>g.WithStrategies(new SubgraphStrategy(checkAdjacentVertices: true,
vertexProperties: __.HasNot("endTime"))).V().Has("name","marko")
```
The C# `SubgraphStrategy` class doesn't have a `checkAdjacentVertices`
parameter in its constructor.
But I guess that is actually not a mistake of the translator, but simply a
configuration option that is missing in Gremlin.Net, right?
----------------------------------------------------------------
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:
[email protected]
> DotNetTranslator for Java
> -------------------------
>
> Key: TINKERPOP-2452
> URL: https://issues.apache.org/jira/browse/TINKERPOP-2452
> Project: TinkerPop
> Issue Type: Improvement
> Components: translator
> Affects Versions: 3.4.8
> Reporter: Stephen Mallette
> Priority: Major
>
> Add a {{DotNetpTranslator}} to Java to translate Gremlin into C#. This one
> may be tricky given the need to often specify generics in C#. Of course, even
> if the implementation got someone most of the way to a translation and they
> had to specify the generics themselves it would be considerably less work
> than what they are in store for today.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)