tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pie...@apache.org
Subject [04/40] incubator-tinkerpop git commit: added PathProcessStratergyTest to ensure that the compilation is as expected. Found a bug around TraversalFilterStep conversion required when a WhereTraversal is stripped of its start/end labels. Fixed. This is rea
Date Wed, 30 Mar 2016 06:38:58 GMT
added PathProcessStratergyTest to ensure that the compilation is as expected. Found a bug around
TraversalFilterStep conversion required when a WhereTraversal is stripped of its start/end
labels. Fixed. This is ready to go.


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

Branch: refs/heads/TINKERPOP-968
Commit: 69d8ce69c148481c88f733fe90b6076c6f3d0341
Parents: 9ff33dd
Author: Marko A. Rodriguez <okrammarko@gmail.com>
Authored: Fri Mar 25 13:50:47 2016 -0600
Committer: Marko A. Rodriguez <okrammarko@gmail.com>
Committed: Fri Mar 25 13:50:47 2016 -0600

----------------------------------------------------------------------
 .../optimization/PathProcessorStrategy.java     | 29 +++----
 .../optimization/PathProcessorStrategyTest.java | 89 ++++++++++++++++++++
 2 files changed, 99 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/69d8ce69/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PathProcessorStrategy.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PathProcessorStrategy.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PathProcessorStrategy.java
index ec3dc40..dd73b46 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PathProcessorStrategy.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PathProcessorStrategy.java
@@ -25,13 +25,11 @@ import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
 import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy;
 import org.apache.tinkerpop.gremlin.process.traversal.step.PathProcessor;
 import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent;
-import org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep;
+import org.apache.tinkerpop.gremlin.process.traversal.step.filter.TraversalFilterStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.filter.WhereTraversalStep;
-import org.apache.tinkerpop.gremlin.process.traversal.step.map.MatchStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectOneStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.TraversalMapStep;
-import org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep;
 import org.apache.tinkerpop.gremlin.process.traversal.strategy.AbstractTraversalStrategy;
 import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
 
@@ -48,14 +46,14 @@ public final class PathProcessorStrategy extends AbstractTraversalStrategy<Trave
 
     private static final PathProcessorStrategy INSTANCE = new PathProcessorStrategy();
 
-    private static final Set<Class<? extends OptimizationStrategy>> POST = new
HashSet<>(Arrays.asList(MatchPredicateStrategy.class));
+    private static final Set<Class<? extends OptimizationStrategy>> PRIORS =
new HashSet<>(Arrays.asList(MatchPredicateStrategy.class));
 
     private PathProcessorStrategy() {
     }
 
     @Override
-    public Set<Class<? extends OptimizationStrategy>> applyPost() {
-        return POST;
+    public Set<Class<? extends OptimizationStrategy>> applyPrior() {
+        return PRIORS;
     }
 
     @Override
@@ -68,8 +66,7 @@ public final class PathProcessorStrategy extends AbstractTraversalStrategy<Trave
         for (final WhereTraversalStep<?> whereTraversalStep : whereTraversalSteps)
{
             final Traversal.Admin<?, ?> localChild = whereTraversalStep.getLocalChildren().get(0);
             if ((localChild.getStartStep() instanceof WhereTraversalStep.WhereStartStep)
&&
-                    !((WhereTraversalStep.WhereStartStep) localChild.getStartStep()).getScopeKeys().isEmpty()
&&
-                    !previousMatch(whereTraversalStep)) {
+                    !((WhereTraversalStep.WhereStartStep) localChild.getStartStep()).getScopeKeys().isEmpty())
{
                 boolean done = false;
                 while (!done) {
                     done = true;
@@ -85,6 +82,11 @@ public final class PathProcessorStrategy extends AbstractTraversalStrategy<Trave
                 final SelectOneStep<?, ?> selectOneStep = new SelectOneStep<>(traversal,
Pop.last, whereStartStep.getScopeKeys().iterator().next());
                 traversal.addStep(index, selectOneStep);
                 whereStartStep.removeScopeKey();
+                if (!(localChild.getEndStep() instanceof WhereTraversalStep.WhereEndStep))
{
+                    localChild.removeStep(localChild.getStartStep());
+                    traversal.addStep(index + 1, new TraversalFilterStep<>(traversal,
localChild));
+                    traversal.removeStep(whereTraversalStep);
+                }
             }
         }
 
@@ -151,15 +153,4 @@ public final class PathProcessorStrategy extends AbstractTraversalStrategy<Trave
         }
         return count;
     }
-
-    private static boolean previousMatch(Step<?, ?> step) {
-        while (step instanceof WhereTraversalStep || step instanceof WherePredicateStep)
{
-            step = step.getPreviousStep();
-            if (step instanceof MatchStep)
-                return true;
-            else if (step instanceof EmptyStep)
-                return false;
-        }
-        return false;
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/69d8ce69/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PathProcessorStrategyTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PathProcessorStrategyTest.java
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PathProcessorStrategyTest.java
new file mode 100644
index 0000000..0fa8f27
--- /dev/null
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PathProcessorStrategyTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.strategy.optimization;
+
+import org.apache.tinkerpop.gremlin.process.computer.traversal.step.map.TraversalVertexProgramStep;
+import org.apache.tinkerpop.gremlin.process.traversal.P;
+import org.apache.tinkerpop.gremlin.process.traversal.Pop;
+import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
+import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies;
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
+import org.apache.tinkerpop.gremlin.process.traversal.lambda.ElementValueTraversal;
+import org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversalStrategies;
+import org.apache.tinkerpop.gremlin.process.traversal.util.EmptyTraversal;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+@RunWith(Parameterized.class)
+public class PathProcessorStrategyTest {
+
+    @Parameterized.Parameter(value = 0)
+    public Traversal original;
+
+    @Parameterized.Parameter(value = 1)
+    public Traversal optimized;
+
+
+    void applyPathProcessorStrategy(final Traversal traversal) {
+        final TraversalStrategies strategies = new DefaultTraversalStrategies();
+        strategies.addStrategies(PathProcessorStrategy.instance());
+        traversal.asAdmin().setStrategies(strategies);
+        traversal.asAdmin().applyStrategies();
+    }
+
+    @Test
+    public void doTest() {
+        original.asAdmin().setParent(new TraversalVertexProgramStep(EmptyTraversal.instance(),
EmptyTraversal.instance())); // trick it
+        applyPathProcessorStrategy(original);
+        assertEquals(optimized, original);
+    }
+
+    @Parameterized.Parameters(name = "{0}")
+    public static Iterable<Object[]> generateTestParameters() {
+
+        return Arrays.asList(new Traversal[][]{
+                // select("a")
+                {__.select("a").by(__.outE().count()), __.select("a").map(__.outE().count())},
+                {__.select("a").by("name"), __.select("a").map(new ElementValueTraversal<>("name"))},
+                {__.select("a").out(), __.select("a").out()},
+                {__.select(Pop.all, "a").by(__.values("name")), __.select(Pop.all, "a").by(__.values("name"))},
+                {__.select(Pop.last, "a").by(__.values("name")), __.select(Pop.last, "a").map(__.values("name"))},
+                {__.select(Pop.first, "a").by(__.values("name")), __.select(Pop.first, "a").map(__.values("name"))},
+                // select("a","b")
+                {__.select("a", "b").by("name").by("age"), __.select("b").map(new ElementValueTraversal<>("age")).as("b").select("a").map(new
ElementValueTraversal<>("name")).as("a").select(Pop.last, "a", "b")},
+                {__.select(Pop.first, "a", "b").by("name").by("age"), __.select(Pop.first,
"b").map(new ElementValueTraversal<>("age")).as("b").select(Pop.first, "a").map(new
ElementValueTraversal<>("name")).as("a").select(Pop.last, "a", "b")},
+                {__.select(Pop.last, "a", "b").by("name").by("age"), __.select(Pop.last,
"b").map(new ElementValueTraversal<>("age")).as("b").select(Pop.last, "a").map(new ElementValueTraversal<>("name")).as("a").select(Pop.last,
"a", "b")},
+                {__.select(Pop.all, "a", "b").by("name").by("age"), __.select(Pop.all, "a",
"b").by("name").by("age")},
+                // where(as("a")...)
+                {__.where(__.out("knows")), __.where(__.out("knows"))},
+                {__.where(__.as("a").out("knows")), __.select(Pop.last, "a").where(__.out("knows"))},
+                {__.where(__.as("a").out("knows").as("b")), __.select(Pop.last, "a").where(__.out("knows").as("b"))},
+                {__.where("a", P.eq("b")), __.where("a", P.eq("b"))}
+        });
+    }
+}


Mime
View raw message