You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2018/09/24 23:11:34 UTC

[2/5] tinkerpop git commit: Merge branch 'groovy-translator-tp32' into groovy-translator-tp33

Merge branch 'groovy-translator-tp32' into groovy-translator-tp33


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

Branch: refs/heads/tp33
Commit: 02e412d92d6f6c7004bdeef206e3d7adb9bd148a
Parents: 957347f aff2037
Author: Justin Chu <15...@users.noreply.github.com>
Authored: Mon Sep 24 11:41:36 2018 -0400
Committer: Justin Chu <15...@users.noreply.github.com>
Committed: Mon Sep 24 11:41:36 2018 -0400

----------------------------------------------------------------------
 .../gremlin/groovy/jsr223/GroovyTranslator.java |  4 +-
 .../groovy/jsr223/GroovyTranslatorTest.java     | 71 ++++++++++++++++++++
 2 files changed, 74 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/02e412d9/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslator.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/02e412d9/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslatorTest.java
----------------------------------------------------------------------
diff --cc gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslatorTest.java
index ea832f4,0000000..547fa20
mode 100644,000000..100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslatorTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GroovyTranslatorTest.java
@@@ -1,145 -1,0 +1,216 @@@
 +/*
 + *  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.commons.configuration.MapConfiguration;
 +import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
 +import org.apache.tinkerpop.gremlin.process.traversal.Traverser;
 +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.dsl.graph.__;
 +import org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.SubgraphStrategy;
 +import org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.TranslationStrategy;
 +import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
++import org.apache.tinkerpop.gremlin.structure.Edge;
 +import org.apache.tinkerpop.gremlin.structure.Vertex;
 +import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
 +import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
++import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge;
++import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex;
 +import org.apache.tinkerpop.gremlin.util.function.Lambda;
 +import org.junit.Test;
 +
 +import javax.script.Bindings;
 +import javax.script.SimpleBindings;
 +import java.util.ArrayList;
 +import java.util.Arrays;
 +import java.util.Collections;
 +import java.util.HashMap;
 +import java.util.LinkedHashMap;
 +import java.util.List;
 +import java.util.function.Function;
 +
 +import static org.junit.Assert.assertEquals;
 +import static org.junit.Assert.assertFalse;
 +
 +/**
 + * @author Marko A. Rodriguez (http://markorodriguez.com)
 + */
 +public class GroovyTranslatorTest {
 +
 +    @Test
 +    public void shouldHandleStrategies() throws Exception {
 +        final TinkerGraph graph = TinkerFactory.createModern();
 +        GraphTraversalSource g = graph.traversal();
 +        g = g.withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{
 +            put(SubgraphStrategy.VERTICES, __.has("name", "marko"));
 +        }})));
 +        final Bindings bindings = new SimpleBindings();
 +        bindings.put("g", g);
 +        Traversal.Admin<Vertex, Object> traversal = new GremlinGroovyScriptEngine().eval(g.V().values("name").asAdmin().getBytecode(), bindings, "g");
 +        assertEquals("marko", traversal.next());
 +        assertFalse(traversal.hasNext());
 +        //
 +        traversal = new GremlinGroovyScriptEngine().eval(g.withoutStrategies(SubgraphStrategy.class).V().count().asAdmin().getBytecode(), bindings, "g");
 +        assertEquals(new Long(6), traversal.next());
 +        assertFalse(traversal.hasNext());
 +        //
 +        traversal = new GremlinGroovyScriptEngine().eval(g.withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{
 +            put(SubgraphStrategy.VERTICES, __.has("name", "marko"));
 +        }})), ReadOnlyStrategy.instance()).V().values("name").asAdmin().getBytecode(), bindings, "g");
 +        assertEquals("marko", traversal.next());
 +        assertFalse(traversal.hasNext());
 +    }
 +
 +    @Test
 +    public void shouldSupportStringSupplierLambdas() throws Exception {
 +        final TinkerGraph graph = TinkerFactory.createModern();
 +        GraphTraversalSource g = graph.traversal();
 +        g = g.withStrategies(new TranslationStrategy(g, GroovyTranslator.of("g")));
 +        GraphTraversal.Admin<Vertex, Integer> t = g.withSideEffect("lengthSum", 0).withSack(1)
 +                .V()
 +                .filter(Lambda.predicate("it.get().label().equals('person')"))
 +                .flatMap(Lambda.function("it.get().vertices(Direction.OUT)"))
 +                .map(Lambda.<Traverser<Object>, Integer>function("it.get().value('name').length()"))
 +                .sideEffect(Lambda.consumer("{ x -> x.sideEffects(\"lengthSum\", x.<Integer>sideEffects('lengthSum') + x.get()) }"))
 +                .order().by(Lambda.comparator("a,b -> a <=> b"))
 +                .sack(Lambda.biFunction("{ a,b -> a + b }"))
 +                .asAdmin();
 +        final List<Integer> sacks = new ArrayList<>();
 +        final List<Integer> lengths = new ArrayList<>();
 +        while (t.hasNext()) {
 +            final Traverser.Admin<Integer> traverser = t.nextTraverser();
 +            sacks.add(traverser.sack());
 +            lengths.add(traverser.get());
 +        }
 +        assertFalse(t.hasNext());
 +        //
 +        assertEquals(6, lengths.size());
 +        assertEquals(3, lengths.get(0).intValue());
 +        assertEquals(3, lengths.get(1).intValue());
 +        assertEquals(3, lengths.get(2).intValue());
 +        assertEquals(4, lengths.get(3).intValue());
 +        assertEquals(5, lengths.get(4).intValue());
 +        assertEquals(6, lengths.get(5).intValue());
 +        ///
 +        assertEquals(6, sacks.size());
 +        assertEquals(4, sacks.get(0).intValue());
 +        assertEquals(4, sacks.get(1).intValue());
 +        assertEquals(4, sacks.get(2).intValue());
 +        assertEquals(5, sacks.get(3).intValue());
 +        assertEquals(6, sacks.get(4).intValue());
 +        assertEquals(7, sacks.get(5).intValue());
 +        //
 +        assertEquals(24, t.getSideEffects().<Number>get("lengthSum").intValue());
++
++        final String script = GroovyTranslator.of("g").translate(t.getBytecode());
++        assertEquals("g.withSideEffect(\"lengthSum\",(int) 0).withSack((int) 1)" +
++                        ".V()" +
++                        ".filter({it.get().label().equals('person')})" +
++                        ".flatMap({it.get().vertices(Direction.OUT)})" +
++                        ".map({it.get().value('name').length()})" +
++                        ".sideEffect({ x -> x.sideEffects(\"lengthSum\", x.<Integer>sideEffects('lengthSum') + x.get()) })" +
++                        ".order().by({a,b -> a <=> b})" +
++                        ".sack({ a,b -> a + b })",
++                script);
 +    }
 +
 +    @Test
 +    public void shouldHandleMaps() {
 +        final TinkerGraph graph = TinkerFactory.createModern();
 +        GraphTraversalSource g = graph.traversal();
 +        String script = GroovyTranslator.of("g").translate(g.V().id().is(new LinkedHashMap<Object,Object>() {{
 +            put(3, "32");
 +            put(Arrays.asList(1, 2, 3.1d), 4);
 +        }}).asAdmin().getBytecode());
 +        assertEquals("g.V().id().is([((int) 3):(\"32\"),([(int) 1, (int) 2, 3.1d]):((int) 4)])", script);
 +    }
 +
 +    @Test
 +    public void shouldHandleEmptyMaps() {
 +        final TinkerGraph graph = TinkerFactory.createModern();
 +        final Function identity = new Lambda.OneArgLambda("it.get()", "gremlin-groovy");
 +        final GraphTraversalSource g = graph.traversal();
 +        final String script = GroovyTranslator.of("g").translate(g.inject(Collections.emptyMap()).map(identity).asAdmin().getBytecode());
 +        assertEquals("g.inject([]).map({it.get()})", script);
 +    }
 +
 +    @Test
 +    public void shouldHaveValidToString() {
 +        assertEquals("translator[h:gremlin-groovy]", GroovyTranslator.of("h").toString());
 +    }
++
++    @Test
++    public void shouldEscapeStrings() {
++        final TinkerGraph graph = TinkerFactory.createModern();
++        final GraphTraversalSource g = graph.traversal();
++        final String script = GroovyTranslator.of("g").translate(g.addV("customer")
++                .property("customer_id", 501L)
++                .property("name", "Foo\u0020Bar")
++                .property("age", 25)
++                .property("special", "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?")
++                .asAdmin().getBytecode());
++
++        assertEquals("g.addV(\"customer\")" +
++                        ".property(\"customer_id\",501L)" +
++                        ".property(\"name\",\"Foo Bar\")" +
++                        ".property(\"age\",(int) 25)" +
++                        ".property(\"special\",\"\"\"`~!@#\\$%^&*()-_=+[{]}\\\\|;:'\\\",<.>/?\"\"\")",
++                script);
++    }
++
++    @Test
++    public void shouldHandleVertexAndEdge() {
++        final TinkerGraph graph = TinkerFactory.createModern();
++        final GraphTraversalSource g = graph.traversal();
++
++        final Object id1 = "customer:10:foo\u0020bar\u0020\u0024100#90"; // customer:10:foo bar $100#90
++        final Vertex vertex1 = DetachedVertex.build().setLabel("customer").setId(id1)
++                .create();
++        final String script1 = GroovyTranslator.of("g").translate(g.V(vertex1).asAdmin().getBytecode());
++        assertEquals("g.V(new org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex(" +
++                        "\"customer:10:foo bar \\$100#90\"," +
++                        "\"customer\", Collections.emptyMap()))",
++                script1);
++
++        final Object id2 = "user:20:foo\\u0020bar\\u005c\\u0022mr\\u005c\\u0022\\u00241000#50"; // user:20:foo\u0020bar\u005c\u0022mr\u005c\u0022\u00241000#50
++        final Vertex vertex2 = DetachedVertex.build().setLabel("user").setId(id2)
++                .create();
++        final String script2 = GroovyTranslator.of("g").translate(g.V(vertex2).asAdmin().getBytecode());
++        assertEquals("g.V(new org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex(" +
++                        "\"user:20:foo\\\\u0020bar\\\\u005c\\\\u0022mr\\\\u005c\\\\u0022\\\\u00241000#50\"," +
++                        "\"user\", Collections.emptyMap()))",
++                script2);
++
++        final Object id3 = "knows:30:foo\u0020bar\u0020\u0024100:\\u0020\\u0024500#70";
++        final Edge edge = DetachedEdge.build().setLabel("knows").setId(id3)
++                .setOutV((DetachedVertex) vertex1)
++                .setInV((DetachedVertex) vertex2)
++                .create();
++        final String script3 = GroovyTranslator.of("g").translate(g.E(edge).asAdmin().getBytecode());
++        assertEquals("g.E(new org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge(" +
++                        "\"knows:30:foo bar \\$100:\\\\u0020\\\\u0024500#70\"," +
++                        "\"knows\",Collections.emptyMap()," +
++                        "\"customer:10:foo bar \\$100#90\",\"customer\"," +
++                        "\"user:20:foo\\\\u0020bar\\\\u005c\\\\u0022mr\\\\u005c\\\\u0022\\\\u00241000#50\",\"user\"))",
++                script3);
++    }
++
 +}