tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From twil...@apache.org
Subject tinkerpop git commit: work.
Date Wed, 06 Jul 2016 21:38:58 GMT
Repository: tinkerpop
Updated Branches:
  refs/heads/TINKERPOP-1254 6473f4aef -> e32808a32


work.


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

Branch: refs/heads/TINKERPOP-1254
Commit: e32808a323f303046abb516e6612c2164d2d466e
Parents: 6473f4a
Author: Ted Wilmes <twilmes@gmail.com>
Authored: Wed Jul 6 16:36:12 2016 -0500
Committer: Ted Wilmes <twilmes@gmail.com>
Committed: Wed Jul 6 16:36:12 2016 -0500

----------------------------------------------------------------------
 .../process/traversal/step/PathProcessor.java   |   6 +-
 .../step/filter/WherePredicateStep.java         |  11 +-
 .../step/filter/WhereTraversalStep.java         |  10 +-
 .../process/traversal/step/map/MatchStep.java   |  55 +++---
 .../traversal/step/util/ImmutablePath.java      |  12 ++
 .../traversal/step/util/MutablePath.java        |   9 +-
 .../optimization/PrunePathStrategy.java         | 172 ++++++++++++++-----
 .../traverser/B_LP_O_S_SE_SL_Traverser.java     |  12 +-
 .../process/traversal/util/PathUtil.java        |  72 ++++++++
 .../traversal/step/map/MatchStepTest.java       |  38 ++++
 .../optimization/PrunePathStrategyTest.java     |  33 +++-
 .../structure/TinkerGraphPlayTest.java          |  13 +-
 12 files changed, 355 insertions(+), 88 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e32808a3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/PathProcessor.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/PathProcessor.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/PathProcessor.java
index 11e51cc..9282571 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/PathProcessor.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/PathProcessor.java
@@ -63,9 +63,11 @@ public interface PathProcessor {
     void setKeepLabels(final Set<String> labels);
 
     static void keepLabels(final Traverser traverser, final Set<String> labels) {
-        if(labels == null || labels.isEmpty()) {
-            //traverser.asAdmin().dropPath();
+        if(labels == null) {
+            return;
         } else {
+//            final Set<String> keepers = traverser.asAdmin().getTags();
+//            keepers.addAll(labels);
             traverser.asAdmin().keepLabels(labels);
         }
     }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e32808a3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WherePredicateStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WherePredicateStep.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WherePredicateStep.java
index 05ae39d..ee7a1b3 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WherePredicateStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WherePredicateStep.java
@@ -122,11 +122,14 @@ public final class WherePredicateStep<S> extends FilterStep<S>
implements Scopin
     protected Traverser.Admin<S> processNextStart() {
         final Traverser.Admin<S> traverser = super.processNextStart();
         // add traverser tags in
-        Set<String> labels = new HashSet<>();
-        labels.addAll(traverser.getTags());
-        if(keepLabels != null ) labels.addAll(keepLabels);
+//        Set<String> labels = new HashSet<>();
+//        labels.addAll(traverser.getTags());
+//        if(keepLabels != null ) labels.addAll(keepLabels);
 //        if(keepLabels != null) System.out.println(labels);
-        PathProcessor.keepLabels(traverser, labels);
+//        System.out.println(traverser);
+//        System.out.println("Before: " + traverser.path().labels());
+        PathProcessor.keepLabels(traverser, keepLabels);
+//        System.out.println("After: " + traverser.path().labels());
         return traverser;
     }
 

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e32808a3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereTraversalStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereTraversalStep.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereTraversalStep.java
index 1952090..5ba7004 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereTraversalStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereTraversalStep.java
@@ -93,10 +93,16 @@ public final class WhereTraversalStep<S> extends FilterStep<S>
implements Traver
     }
 
     @Override
-    public void setKeepLabels(Set<String> labels) {
-
+    public void setKeepLabels(Set<String> keepLabels) {
+        this.keepLabels = keepLabels;
     }
 
+    @Override
+    protected Traverser.Admin<S> processNextStart() {
+        final Traverser.Admin<S> traverser = super.processNextStart();
+        PathProcessor.keepLabels(traverser, keepLabels);
+        return traverser;
+    }
 
     @Override
     protected boolean filter(final Traverser.Admin<S> traverser) {

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e32808a3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MatchStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MatchStep.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MatchStep.java
index 3c629ce..8565f22 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MatchStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MatchStep.java
@@ -323,13 +323,13 @@ public final class MatchStep<S, E> extends ComputerAwareStep<S,
Map<String, E>>
                 if (this.connective == ConnectiveStep.Connective.AND) {
                     final Traversal.Admin<Object, Object> matchTraversal = this.getMatchAlgorithm().apply(traverser);
                     // drop any labels that we can
-                    dropUnnecessaryLabels(traverser, matchTraversal);
+//                    dropUnnecessaryLabels(traverser, matchTraversal);
                     traverser.getTags().add(matchTraversal.getStartStep().getId());
                     matchTraversal.addStart(traverser); // determine which sub-pattern the
traverser should try next
                 } else {  // OR
                     for (final Traversal.Admin<?, ?> matchTraversal : this.matchTraversals)
{
                         final Traverser.Admin split = traverser.split();
-                        dropUnnecessaryLabels(traverser, matchTraversal);
+//                        dropUnnecessaryLabels(traverser, matchTraversal);
                         split.getTags().add(matchTraversal.getStartStep().getId());
                         matchTraversal.addStart(split);
                     }
@@ -340,6 +340,9 @@ public final class MatchStep<S, E> extends ComputerAwareStep<S,
Map<String, E>>
 
     private void dropUnnecessaryLabels(final Traverser.Admin<Object> traverser,
                                        final Traversal.Admin matchTraversal) {
+        if(keepLabels == null) {
+            return;
+        }
         final List<Traversal.Admin> remainingTraversals = getRemainingTraversals(traverser);
         remainingTraversals.add(matchTraversal);
         HashSet<String> requiredLabels = new HashSet<>();
@@ -347,11 +350,14 @@ public final class MatchStep<S, E> extends ComputerAwareStep<S,
Map<String, E>>
             getRequiredLabels(trav, requiredLabels);
         });
         requiredLabels.addAll(keepLabels);
+        if(dedupLabels != null) {
+            requiredLabels.addAll(dedupLabels);
+        }
         requiredLabels.add(matchTraversal.getStartStep().getId());
-        System.out.println(requiredLabels);
-        System.out.println("Before:\t" + traverser.path().labels() + "\t" + traverser.path().objects());
+//        System.out.println(requiredLabels);
+//        System.out.println("Before:\t" + traverser.path().labels() + "\t" + traverser.path().objects());
         traverser.keepLabels(requiredLabels);
-        System.out.println("After:\t" + traverser.path().labels() + "\t" + traverser.path().objects());
+//        System.out.println("After:\t" + traverser.path().labels() + "\t" + traverser.path().objects());
     }
 
     @Override
@@ -376,13 +382,13 @@ public final class MatchStep<S, E> extends ComputerAwareStep<S,
Map<String, E>>
                     final Traversal.Admin<Object, Object> matchTraversal = this.getMatchAlgorithm().apply(traverser);
// determine which sub-pattern the traverser should try next
                     traverser.getTags().add(matchTraversal.getStartStep().getId());
                     traverser.setStepId(matchTraversal.getStartStep().getId()); // go down
the traversal match sub-pattern
-                    dropUnnecessaryLabels(traverser, matchTraversal);
+//                    dropUnnecessaryLabels(traverser, matchTraversal);
                     return IteratorUtils.of(traverser);
                 } else { // OR
                     final List<Traverser.Admin<Map<String, E>>> traversers
= new ArrayList<>(this.matchTraversals.size());
                     for (final Traversal.Admin<?, ?> matchTraversal : this.matchTraversals)
{
                         final Traverser.Admin split = traverser.split();
-                        //dropUnnecessaryLabels(traverser, matchTraversal);
+//                        dropUnnecessaryLabels(traverser, matchTraversal);
                         split.getTags().add(matchTraversal.getStartStep().getId());
                         split.setStepId(matchTraversal.getStartStep().getId());
                         traversers.add(split);
@@ -429,8 +435,8 @@ public final class MatchStep<S, E> extends ComputerAwareStep<S,
Map<String, E>>
 //                requiredLabels.addAll(((Scoping) step).getScopeKeys());
 //            }
             if(step instanceof PathProcessor) {
-                Set<String> keep = ((PathProcessor) step).getKeepLabels();
-                if(keep != null) requiredLabels.addAll(keep);
+                //Set<String> keep = ((PathProcessor) step).getKeepLabels();
+                //if(keep != null) requiredLabels.addAll(keep);
             } else if(step instanceof Scoping) {
                 Set<String> keep = ((Scoping)step).getScopeKeys();
                 if(keep != null) requiredLabels.addAll(keep);
@@ -462,6 +468,7 @@ public final class MatchStep<S, E> extends ComputerAwareStep<S,
Map<String, E>>
     protected Traverser.Admin<Map<String, E>> processNextStart() throws NoSuchElementException
{
         final Traverser.Admin<Map<String, E>> traverser = super.processNextStart();
         // remove labels that we don't need
+//        System.out.println("KEEPERS: " + keepLabels);
         if(keepLabels != null) {
             // add traverser tags in
             Set<String> labels = new HashSet<>();
@@ -472,18 +479,25 @@ public final class MatchStep<S, E> extends ComputerAwareStep<S,
Map<String, E>>
 //            labels.addAll(getMatchEndLabels());
 //            if (keepLabels != null) System.out.println(labels);
             List<Traversal.Admin> remainingTraversals = getRemainingTraversals(traverser);
-            if(remainingTraversals.size() == 0) {
-                System.out.println("none");
+//            System.out.println("REMAINING: " + remainingTraversals.size());
+//            if(remainingTraversals.size() == 1) {
+//                // also remove tags...
+//                System.out.println("Before droppppping: " + traverser.path().labels());
+//                traverser.dropLabels(traverser.getTags());
+//                System.out.println("DROPPPPPING TAGS: " + traverser.path().labels());
+//            }
+//                System.out.println("none");
                 for (Traversal.Admin trav : remainingTraversals) {
                     getRequiredLabels(trav, labels);
                 }
 //                labels.addAll(traverser.getTags());
                 if (keepLabels != null) labels.addAll(keepLabels);
-                System.out.println(labels);
-                System.out.println(traverser.path().labels());
+//                System.out.println(labels);
+//                System.out.println("Before: " + traverser.path().labels());
+//                System.out.println("Keepers: " + labels);
                 PathProcessor.keepLabels(traverser, labels);
-                System.out.println(traverser.path().labels());
-            }
+//                System.out.println("After: " + traverser.path().labels());
+//            }
         }
         return traverser;
     }
@@ -626,12 +640,10 @@ public final class MatchStep<S, E> extends ComputerAwareStep<S,
Map<String, E>>
             final boolean hasExecuted = traverser.getTags().contains(traversal.getStartStep().getId());
             if(hasExecuted) {
                 String traversalId = traversal.getStartStep().getId();
-                List<String> dropMe = new ArrayList();
-                // drop labels that matched this step id
-                dropMe.add(traversalId);
-                traverser.path().retract(new HashSet(dropMe));
+//                System.out.println("Tag drop: " + traverser.path().labels());
+                traverser.dropLabels(Collections.singleton(traversalId));
+//                System.out.println(traverser.path().labels());
             }
-            // drop all tags
             return hasExecuted;
         }
 
@@ -796,10 +808,11 @@ public final class MatchStep<S, E> extends ComputerAwareStep<S,
Map<String, E>>
 
     @Override
     public void setKeepLabels(Set<String> labels) {
+//        System.out.println("MATCH KEEP: " + labels);
         if(this.keepLabels != null) {
             this.keepLabels.addAll(labels);
         } else {
-            this.keepLabels = labels;
+            this.keepLabels = new HashSet<>(labels);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e32808a3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/ImmutablePath.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/ImmutablePath.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/ImmutablePath.java
index a3cb350..d0d2f16 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/ImmutablePath.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/ImmutablePath.java
@@ -100,6 +100,7 @@ public class ImmutablePath implements Path, ImmutablePathImpl, Serializable,
Clo
     }
 
     private Path retract(final Set<String> labels, final List<Path> pathTrail)
{
+
         Path current = pathTrail.get(0);
         if (current instanceof TailPath) {
             // return head
@@ -137,6 +138,16 @@ public class ImmutablePath implements Path, ImmutablePathImpl, Serializable,
Clo
 
     @Override
     public Path retract(final Set<String> labels) {
+        if(labels == null || labels.isEmpty()) {
+            return this;
+        }
+
+//        System.out.println("RETRACTING: " + labels);
+//
+//        labels.removeIf(val -> (val.indexOf("0") >= 0));
+//
+//        System.out.println("AFTER LABELS: " + labels);
+
         ImmutablePath parent;
         ImmutablePath child;
 
@@ -383,6 +394,7 @@ public class ImmutablePath implements Path, ImmutablePathImpl, Serializable,
Clo
 
         @Override
         public Path extend(final Set<String> labels) {
+            if(labels.size() == 0) { return this; }
             throw new UnsupportedOperationException("A head path can not have labels added
to it");
         }
 

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e32808a3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/MutablePath.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/MutablePath.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/MutablePath.java
index 26d277e..8d7ac4e 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/MutablePath.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/MutablePath.java
@@ -83,18 +83,23 @@ public class MutablePath implements Path, Serializable {
 
     @Override
     public Path extend(final Set<String> labels) {
-        this.labels.get(this.labels.size() - 1).addAll(labels);
+        if(labels.size() > 0) {
+            this.labels.get(this.labels.size() - 1).addAll(labels);
+        }
         return this;
     }
 
     @Override
     public Path retract(final Set<String> removeLabels) {
+
+//        removeLabels.removeIf(val -> (val.indexOf("0") >= 0));
+
         for (int i = this.labels.size() - 1; i >= 0; i--) {
             for (final String label : removeLabels) {
                 synchronized (this.labels.get(i)) {
                     if (this.labels().get(i).contains(label)) {
                         this.labels.get(i).remove(label);
-                        System.out.println("REMOVED: " + label);
+//                        System.out.println("REMOVED: " + label);
                         boolean empty = false;
                         if (this.labels.get(i).size() == 0) {
                             this.labels.remove(i);

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e32808a3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PrunePathStrategy.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PrunePathStrategy.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PrunePathStrategy.java
index 6312d4e..7eb0afb 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PrunePathStrategy.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PrunePathStrategy.java
@@ -18,9 +18,7 @@
  */
 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.Parameterizing;
-import org.apache.tinkerpop.gremlin.process.traversal.Path;
 import org.apache.tinkerpop.gremlin.process.traversal.Step;
 import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
 import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy;
@@ -28,11 +26,15 @@ import org.apache.tinkerpop.gremlin.process.traversal.step.PathProcessor;
 import org.apache.tinkerpop.gremlin.process.traversal.step.Scoping;
 import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent;
 import org.apache.tinkerpop.gremlin.process.traversal.step.map.MatchStep;
+import org.apache.tinkerpop.gremlin.process.traversal.step.map.PathStep;
+import org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.SubgraphStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Parameters;
 import org.apache.tinkerpop.gremlin.process.traversal.strategy.AbstractTraversalStrategy;
-import org.apache.tinkerpop.gremlin.process.traversal.util.FastNoSuchElementException;
+import org.apache.tinkerpop.gremlin.process.traversal.util.PathUtil;
+import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
 
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -47,6 +49,7 @@ public final class PrunePathStrategy extends AbstractTraversalStrategy<Traversal
 
     static {
         PRIORS.add(PathProcessorStrategy.class);
+        PRIORS.add(MatchPredicateStrategy.class);
     }
 
     private PrunePathStrategy() {
@@ -56,6 +59,76 @@ public final class PrunePathStrategy extends AbstractTraversalStrategy<Traversal
         return INSTANCE;
     }
 
+    private void setKeepLabels(final Traversal.Admin<?, ?> traversal, final Set<String>
keepLabels) {
+        final List<Step> steps = traversal.getSteps();
+        for(int i = steps.size() - 1; i >= 0; i--) {
+            final Step step = steps.get(i);
+            if (step instanceof PathProcessor) {
+//                System.out.println("Keeping: " + keepLabels);
+                if(keepLabels == null) {
+                    ((PathProcessor) step).setKeepLabels(null);
+                } else {
+                    ((PathProcessor) step).setKeepLabels(new HashSet<>(keepLabels));
+                }
+            }
+            Set<String> referencedLabels = PathUtil.getReferencedLabels(step);
+            if(step instanceof MatchStep && step.getNextStep() instanceof EmptyStep)
{
+                ((PathProcessor) step).setKeepLabels(referencedLabels);
+            } else if (step instanceof MatchStep) {
+                keepLabels.addAll(referencedLabels);
+            }
+
+            if(step instanceof TraversalParent) {
+                TraversalParent traversalParent = ((TraversalParent) step);
+                final List<Traversal.Admin<?, ?>> subTraversals = new ArrayList<>();
+                subTraversals.addAll(traversalParent.getLocalChildren());
+                subTraversals.addAll(traversalParent.getGlobalChildren());
+                HashSet<String> keepers = new HashSet<>();
+                for(final Traversal.Admin<?, ?> subTraversal : subTraversals) {
+                    if(keepLabels != null) {
+                        keepers.addAll(keepLabels);
+                        keepers.addAll(referencedLabels);
+                        if(step instanceof MatchStep) {
+                            System.out.println(step);
+                            keepers.addAll(((MatchStep)step).getMatchStartLabels());
+                            keepers.addAll(((MatchStep)step).getMatchEndLabels());
+                        }
+                        setKeepLabels(subTraversal, keepers);
+                    } else {
+                        setKeepLabels(subTraversal, null);
+                    }
+                }
+            }
+            if(keepLabels != null) {
+                keepLabels.addAll(referencedLabels);
+            }
+        }
+    }
+
+//    @Override
+    public void apply3(final Traversal.Admin<?, ?> traversal) {
+        boolean hasPathStep = false;
+        final List<PathStep> pathSteps = TraversalHelper.getStepsOfAssignableClassRecursively(PathStep.class,
traversal);
+        final List<SubgraphStep> subgraphSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SubgraphStep.class,
traversal);
+        if(!pathSteps.isEmpty() || !subgraphSteps.isEmpty()) {
+            hasPathStep = true;
+        }
+
+        // This is a bit weird at this point because the strategy application already happens
in a recursive fashion
+        // but we shortcut that for this strategy because we want to be able to collect all
of the referenced labels
+        // at all levels of the traversal and then set the keep labels accordingly for each
PathProcessor
+        if(!(traversal.getParent() instanceof EmptyStep)) {
+            return;
+        }
+
+        Set<String> keepLabels = null;
+        if(!hasPathStep) {
+            keepLabels = new HashSet<>();
+        }
+
+        setKeepLabels(traversal, keepLabels);
+    }
+
     @Override
     public void apply(final Traversal.Admin<?, ?> traversal) {
         TraversalParent parent = traversal.getParent();
@@ -73,59 +146,78 @@ public final class PrunePathStrategy extends AbstractTraversalStrategy<Traversal
             }
         }
 
+        // check if the traversal contains any path or subgraph steps and
+        boolean hasPathStep = false;
+        final List<PathStep> pathSteps = TraversalHelper.getStepsOfAssignableClassRecursively(PathStep.class,
traversal);
+        final List<SubgraphStep> subgraphSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SubgraphStep.class,
traversal);
+        if(!pathSteps.isEmpty() || !subgraphSteps.isEmpty()) {
+            hasPathStep = true;
+        }
 
         final List<Step> steps = traversal.getSteps();
         for(int i = steps.size() - 1; i >= 0; i--) {
-            // maintain our list of labels to keep, repeatedly adding labels that were found
during
-            // the last iteration
-            keepLabels.addAll(foundLabels);
             Step currentStep = steps.get(i);
+            if(!hasPathStep) {
+                // maintain our list of labels to keep, repeatedly adding labels that were
found during
+                // the last iteration
+                keepLabels.addAll(foundLabels);
 
-            if(currentStep instanceof Parameterizing) {
-                Parameters parameters = ((Parameterizing) currentStep).getParameters();
-                for(Traversal.Admin trav : parameters.getTraversals()) {
-                    for(Object ss : trav.getSteps()) {
-                        if(ss instanceof Scoping) {
-                            Set<String> labels = ((Scoping) ss).getScopeKeys();
-                            for(String label : labels) {
-                                if (foundLabels.contains(label)) {
-                                    keepLabels.add(label);
-                                } else {
-                                    // it'll get added later, b/c we can drop after this
step
-                                    foundLabels.add(label);
+                // check child traversals
+                if(currentStep instanceof TraversalParent) {
+                    TraversalParent traversalParent = (TraversalParent) currentStep;
+                    List<Traversal.Admin<Object, Object>> localChildren = traversalParent.getLocalChildren();
+                    List<Traversal.Admin<Object, Object>> globalChildren = traversalParent.getGlobalChildren();
+
+                }
+
+                if (currentStep instanceof Parameterizing) {
+                    Parameters parameters = ((Parameterizing) currentStep).getParameters();
+                    for (Traversal.Admin trav : parameters.getTraversals()) {
+                        for (Object ss : trav.getSteps()) {
+                            if (ss instanceof Scoping) {
+                                Set<String> labels = ((Scoping) ss).getScopeKeys();
+                                for (String label : labels) {
+                                    if (foundLabels.contains(label)) {
+                                        keepLabels.add(label);
+                                    } else {
+                                        // it'll get added later, b/c we can drop after this
step
+                                        foundLabels.add(label);
+                                    }
                                 }
                             }
                         }
                     }
                 }
-            }
 
-            if(currentStep instanceof Scoping) {
-                Set<String> labels = new HashSet<>(((Scoping) currentStep).getScopeKeys());
-                if(currentStep instanceof MatchStep) {
-                    // if this is the last step, keep everything, else just add founds
-                    if(currentStep.getNextStep() instanceof EmptyStep) {
-                        labels.addAll(((MatchStep) currentStep).getMatchEndLabels());
-                        labels.addAll(((MatchStep) currentStep).getMatchStartLabels());
+                if (currentStep instanceof Scoping) {
+                    Set<String> labels = new HashSet<>(((Scoping) currentStep).getScopeKeys());
+                    if (currentStep instanceof MatchStep) {
+                        // if this is the last step, keep everything, else just add founds
+                        if (currentStep.getNextStep() instanceof EmptyStep) {
+                            labels.addAll(((MatchStep) currentStep).getMatchEndLabels());
+                            labels.addAll(((MatchStep) currentStep).getMatchStartLabels());
+                        }
+                    }
+                    for (final String label : labels) {
+                        if (foundLabels.contains(label)) {
+                            keepLabels.add(label);
+                        } else {
+                            foundLabels.add(label);
+                        }
                     }
                 }
-                for(final String label : labels) {
-                    if(foundLabels.contains(label)) {
-                        keepLabels.add(label);
-                    } else {
-                        foundLabels.add(label);
+                if (currentStep instanceof PathProcessor) {
+                    if (i != traversal.getSteps().size()) {
+                        // add in all match labels
+                        ((PathProcessor) currentStep).setKeepLabels(new HashSet<>(foundLabels));
                     }
+                    ((PathProcessor) currentStep).setKeepLabels(new HashSet<>(keepLabels));
                 }
-            }
-
-            if(currentStep instanceof PathProcessor) {
-//                        System.out.println(currentStep);
-//                        System.out.println(keepLabels);
-                if(i != traversal.getSteps().size()) {
-                    // add in all match labels
-                    ((PathProcessor) currentStep).setKeepLabels(new HashSet<>(foundLabels));
+            } else {
+                if (currentStep instanceof PathProcessor) {
+                    // set keep labels to null so that no labels are dropped
+                    ((PathProcessor) currentStep).setKeepLabels(null);
                 }
-                ((PathProcessor) currentStep).setKeepLabels(new HashSet<>(keepLabels));
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e32808a3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/traverser/B_LP_O_S_SE_SL_Traverser.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/traverser/B_LP_O_S_SE_SL_Traverser.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/traverser/B_LP_O_S_SE_SL_Traverser.java
index e65a032..793ead6 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/traverser/B_LP_O_S_SE_SL_Traverser.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/traverser/B_LP_O_S_SE_SL_Traverser.java
@@ -97,11 +97,15 @@ public class B_LP_O_S_SE_SL_Traverser<T> extends B_O_S_SE_SL_Traverser<T>
{
                     if(labels.contains(l) == false) { retractLabels.add(l); };
                 }
             }
-            try {
+            this.path = this.path.retract(retractLabels);
+        } else if (labels.isEmpty()) {
+            Set<String> retractLabels = new HashSet<>();
+            List<Set<String>> existingLabels = this.path.labels();
+            for(Set<String> labelSet : existingLabels) {
+                retractLabels.addAll(labelSet);
+            }
+            if(!retractLabels.isEmpty()) {
                 this.path = this.path.retract(retractLabels);
-            } catch (Exception e) {
-                // todo don't retract if it's a head path
-                e.printStackTrace();
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e32808a3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/PathUtil.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/PathUtil.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/PathUtil.java
new file mode 100644
index 0000000..426b971
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/PathUtil.java
@@ -0,0 +1,72 @@
+/*
+ * 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.util;
+
+import org.apache.tinkerpop.gremlin.process.traversal.Parameterizing;
+import org.apache.tinkerpop.gremlin.process.traversal.Step;
+import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
+import org.apache.tinkerpop.gremlin.process.traversal.step.PathProcessor;
+import org.apache.tinkerpop.gremlin.process.traversal.step.Scoping;
+import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent;
+import org.apache.tinkerpop.gremlin.process.traversal.step.map.MatchStep;
+import org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep;
+import org.apache.tinkerpop.gremlin.process.traversal.step.util.Parameters;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author Ted Wilmes (http://twilmes.org)
+ */
+public class PathUtil {
+
+    public static Set<String> getReferencedLabels(Step step) {
+        final Set<String> referencedLabels = new HashSet<>();
+
+        if (step instanceof Parameterizing) {
+            Parameters parameters = ((Parameterizing) step).getParameters();
+            for (Traversal.Admin trav : parameters.getTraversals()) {
+                for (Object ss : trav.getSteps()) {
+                    if (ss instanceof Scoping) {
+                        Set<String> labels = ((Scoping) ss).getScopeKeys();
+                        for (String label : labels) {
+                            referencedLabels.add(label);
+                        }
+                    }
+                }
+            }
+        }
+
+        if (step instanceof Scoping) {
+            Set<String> labels = new HashSet<>(((Scoping) step).getScopeKeys());
+            if (step instanceof MatchStep) {
+                // if this is the last step, keep everything, else just add founds
+                if (step.getNextStep() instanceof EmptyStep) {
+                    labels.addAll(((MatchStep) step).getMatchEndLabels());
+                    labels.addAll(((MatchStep) step).getMatchStartLabels());
+                }
+            }
+            referencedLabels.addAll(labels);
+
+        }
+
+        return referencedLabels;
+    }
+}

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e32808a3/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MatchStepTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MatchStepTest.java
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MatchStepTest.java
index 37e70bf..b04667e 100644
--- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MatchStepTest.java
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/MatchStepTest.java
@@ -21,17 +21,23 @@ package org.apache.tinkerpop.gremlin.process.traversal.step.map;
 import org.apache.tinkerpop.gremlin.process.traversal.P;
 import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
 import org.apache.tinkerpop.gremlin.process.traversal.TraversalEngine;
+import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies;
 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.__;
+import org.apache.tinkerpop.gremlin.process.traversal.step.PathProcessor;
 import org.apache.tinkerpop.gremlin.process.traversal.step.StepTest;
 import org.apache.tinkerpop.gremlin.process.traversal.step.filter.CoinStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.filter.ConnectiveStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.filter.WhereTraversalStep;
 import org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep;
+import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.PrunePathStrategy;
 import org.apache.tinkerpop.gremlin.process.traversal.traverser.B_LP_O_P_S_SE_SL_TraverserGenerator;
 import org.apache.tinkerpop.gremlin.process.traversal.traverser.util.EmptyTraverser;
+import org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversalStrategies;
 import org.apache.tinkerpop.gremlin.structure.T;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.junit.Test;
 
 import java.util.Arrays;
@@ -418,4 +424,36 @@ public class MatchStepTest extends StepTest {
                 as("b").in("created").count().is(P.gt(1))).asAdmin();
         assertEquals("a", MatchStep.Helper.computeStartLabel(((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren()));
     }
+
+    @Test
+    public void testGetRemainingTraversals() {
+        Traverser.Admin traverser = B_LP_O_P_S_SE_SL_TraverserGenerator.instance().generate(1,
EmptyStep.instance(), 1l);
+        traverser.addLabels(Collections.singleton("a"));
+
+        Traversal mTraversal1 = as("a").out().as("b");   // 1
+        Traversal<Object, Vertex> mTraversal2 = as("b").out().as("c");    // 2
+        Traversal<Object, Vertex> mTraversal3 = as("a").out().as("d");    // 5
+        Traversal<Object, Vertex> mTraversal4 = as("c").out().as("e");    // 4
+        Traversal<Object, Vertex> mTraversal5 = as("c").out().as("f");
+
+
+        Traversal.Admin<?, ?> traversal = __.match(
+                mTraversal1,
+                mTraversal2,
+                mTraversal3,
+                mTraversal4,
+                mTraversal5).asAdmin();
+
+        final TraversalStrategies strategies = new DefaultTraversalStrategies();
+        strategies.addStrategies(PrunePathStrategy.instance());
+        traversal.asAdmin().setStrategies(strategies);
+        traversal.asAdmin().applyStrategies();
+
+        PathProcessor matchStep = (PathProcessor) traversal.getStartStep();
+        assertEquals(new HashSet<>(Arrays.asList("a", "b", "c", "d", "e", "f")), matchStep.getKeepLabels());
+
+        traverser.getTags().add(mTraversal1.asAdmin().getStartStep().getId());
+        traverser.setStepId(mTraversal2.asAdmin().getStartStep().getId());
+
+    }
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e32808a3/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PrunePathStrategyTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PrunePathStrategyTest.java
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PrunePathStrategyTest.java
index cf46292..acee612 100644
--- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PrunePathStrategyTest.java
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/PrunePathStrategyTest.java
@@ -23,6 +23,7 @@ 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.step.PathProcessor;
+import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent;
 import org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversalStrategies;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -64,16 +65,33 @@ public class PrunePathStrategyTest {
     public void doTest() {
         applyPrunePathStrategy(traversal);
         // get all path processors
-        List<Set<String>> keepLabels = getKeepLabels(traversal);
+        List<Object> keepLabels = getKeepLabels(traversal);
 
         assertEquals(labels, keepLabels);
     }
 
-    private List<Set<String>> getKeepLabels(Traversal.Admin traversal) {
-        List<Set<String>> keepLabels = new ArrayList<>();
+    private List<Object> getKeepLabels(Traversal.Admin traversal) {
+        List<Object> keepLabels = new ArrayList<>();
         for(Step step : (List<Step>)traversal.getSteps()) {
             if(step instanceof PathProcessor) {
-                keepLabels.add(((PathProcessor) step).getKeepLabels());
+                final Set<String> keepers = ((PathProcessor) step).getKeepLabels();
+                if(keepers != null) {
+                    keepLabels.add(keepers);
+                }
+            }
+            if(step instanceof TraversalParent) {
+                for(Traversal.Admin child : ((TraversalParent) step).getGlobalChildren())
{
+                    List<Object> childLabels = getKeepLabels(child);
+                    if(childLabels.size() > 0) {
+                        keepLabels.add(childLabels);
+                    }
+                }
+                for(Traversal.Admin child : ((TraversalParent) step).getLocalChildren())
{
+                    List<Object> childLabels = getKeepLabels(child);
+                    if(childLabels.size() > 0) {
+                        keepLabels.add(childLabels);
+                    }
+                }
             }
         }
         return keepLabels;
@@ -94,7 +112,12 @@ public class PrunePathStrategyTest {
                 {__.V().out().out().match(
                         as("a").in("created").as("b"),
                         as("b").in("knows").as("c")).select("c").out("created").where(neq("a")).values("name"),
-                        Arrays.asList(new HashSet<>(Arrays.asList("a", "b", "c")),
Collections.singleton("a"), Collections.EMPTY_SET)}
+                        Arrays.asList(new HashSet<>(Arrays.asList("a", "b", "c")),
Collections.singleton("a"), Collections.EMPTY_SET)},
+                {__.V().as("a").out().select("a").path(), Arrays.asList()},
+                {__.V().as("a").out().select("a").subgraph("b"), Arrays.asList()},
+                {__.V().out().as("a").where(neq("a")).out().where(neq("a")), Arrays.asList(Collections.singleton("a"),
Collections.EMPTY_SET)},
+//                {__.V().out().as("a").where(neq("a")).out().where(__.out().where(neq("a"))),
Arrays.asList(Collections.singleton("a"), Arrays.asList(Collections.EMPTY_SET))}
+//                {__.V().as("a").repeat(__.out().where(neq("a"))), Arrays.asList(Collections.EMPTY_SET)}
         });
     }
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e32808a3/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 c16c2e1..1ab0465 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
@@ -46,6 +46,7 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.function.Supplier;
 
+import static org.apache.tinkerpop.gremlin.process.traversal.P.eq;
 import static org.apache.tinkerpop.gremlin.process.traversal.P.neq;
 import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.and;
 import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.as;
@@ -329,8 +330,8 @@ public class TinkerGraphPlayTest {
 
         graph = TinkerFactory.createModern();
 //        graph = TinkerGraph.open();
-        //graph.io(GraphMLIo.build()).readGraph("/Users/twilmes/work/repos/incubator-tinkerpop/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/grateful-dead.xml");
-        g = graph.traversal().withComputer(Computer.compute(TinkerGraphComputer.class).workers(1));
+//        graph.io(GraphMLIo.build()).readGraph("/Users/twilmes/work/repos/incubator-tinkerpop/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/grateful-dead.xml");
+        g = graph.traversal().withComputer();//Computer.compute(TinkerGraphComputer.class).workers(1));
 
 //        System.out.println(
 //                g.V().as("a").out().as("b").
@@ -342,13 +343,9 @@ public class TinkerGraphPlayTest {
 //                                )
 //                        ).select("a").toList());
 
-        System.out.println(g.V().out().out().match(
-                as("a").in("created").as("b"),
-                as("b").in("knows").as("c")).select("c").out("created").values("name").toList());
+//        System.out.println(g.V().as("a").out().where(neq("a")).profile().next());
 
-        System.out.println(g.V().match(
-                as("a").out().as("b")).
-                    select("b").by(T.id).toList());
+        System.out.println(g.V().choose(__.outE().count().is(0L), __.as("a"), __.as("b")).choose(__.select("a"),
__.select("a"), __.select("b")).toList());
 
         // [{a=v[1], b=v[3], c=3}, {a=v[1], b=v[2], c=3}, {a=v[1], b=v[4], c=3}]
         // [{a=v[1], b=v[3], c=3}, {a=v[1], b=v[2], c=3}, {a=v[1], b=v[4], c=3}]


Mime
View raw message