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 2015/03/18 22:13:25 UTC
incubator-tinkerpop git commit: Add SubgraphStrategy implementation
over TraversalStrategy.
Repository: incubator-tinkerpop
Updated Branches:
refs/heads/master 839cb8ae2 -> f55f2d765
Add SubgraphStrategy implementation over TraversalStrategy.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/f55f2d76
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/f55f2d76
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/f55f2d76
Branch: refs/heads/master
Commit: f55f2d7652550913b2f0d0e041e9a1748270c13f
Parents: 839cb8a
Author: Stephen Mallette <sp...@apache.org>
Authored: Wed Mar 18 17:13:03 2015 -0400
Committer: Stephen Mallette <sp...@apache.org>
Committed: Wed Mar 18 17:13:03 2015 -0400
----------------------------------------------------------------------
.../traversal/step/filter/LambdaFilterStep.java | 4 +
.../strategy/decoration/SubgraphStrategy.java | 120 +++++++
.../decoration/SubgraphStrategyTest.java | 143 ++++++++
.../gremlin/process/ProcessComputerSuite.java | 4 +-
.../gremlin/process/ProcessStandardSuite.java | 5 +-
.../decoration/SubgraphStrategyProcessTest.java | 350 +++++++++++++++++++
6 files changed, 624 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f55f2d76/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/graph/traversal/step/filter/LambdaFilterStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/graph/traversal/step/filter/LambdaFilterStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/graph/traversal/step/filter/LambdaFilterStep.java
index e0c609f..96ebf75 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/graph/traversal/step/filter/LambdaFilterStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/graph/traversal/step/filter/LambdaFilterStep.java
@@ -36,6 +36,10 @@ public final class LambdaFilterStep<S> extends FilterStep<S> {
this.predicate = predicate;
}
+ public Predicate<Traverser<S>> getPredicate() {
+ return predicate;
+ }
+
@Override
protected boolean filter(final Traverser.Admin<S> traverser) {
return this.predicate.test(traverser);
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f55f2d76/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SubgraphStrategy.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SubgraphStrategy.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SubgraphStrategy.java
new file mode 100644
index 0000000..86d44c6
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SubgraphStrategy.java
@@ -0,0 +1,120 @@
+/*
+ * 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.decoration;
+
+import org.apache.tinkerpop.gremlin.process.Step;
+import org.apache.tinkerpop.gremlin.process.Traversal;
+import org.apache.tinkerpop.gremlin.process.Traverser;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.filter.LambdaFilterStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.AddEdgeByPathStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.AddEdgeStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.AddVertexStartStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.AddVertexStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.EdgeOtherVertexStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.EdgeVertexStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.VertexStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.sideEffect.GraphStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.strategy.AbstractTraversalStrategy;
+import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
+import org.apache.tinkerpop.gremlin.structure.Direction;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class SubgraphStrategy extends AbstractTraversalStrategy {
+
+ private final Predicate<Traverser<Vertex>> vertexPredicate;
+ private final Predicate<Traverser<Edge>> edgePredicate;
+
+ private SubgraphStrategy(final Predicate<Vertex> vertexPredicate, final Predicate<Edge> edgePredicate) {
+ this.vertexPredicate = t -> vertexPredicate.test(t.get());
+ this.edgePredicate = t -> edgePredicate.test(t.get()) && vertexPredicate.test(t.get().inVertex()) && vertexPredicate.test(t.get().outVertex());
+ }
+
+ @Override
+ public void apply(final Traversal.Admin<?, ?> traversal) {
+ final List<GraphStep> graphSteps = TraversalHelper.getStepsOfAssignableClass(GraphStep.class, traversal);
+ final List<VertexStep> vertexSteps = TraversalHelper.getStepsOfAssignableClass(VertexStep.class, traversal);
+
+ final List<Step> vertexStepsToInsertFilterAfter = new ArrayList<>();
+ vertexStepsToInsertFilterAfter.addAll(TraversalHelper.getStepsOfAssignableClass(EdgeOtherVertexStep.class, traversal));
+ vertexStepsToInsertFilterAfter.addAll(TraversalHelper.getStepsOfAssignableClass(EdgeVertexStep.class, traversal));
+ vertexStepsToInsertFilterAfter.addAll(TraversalHelper.getStepsOfAssignableClass(AddVertexStep.class, traversal));
+ vertexStepsToInsertFilterAfter.addAll(TraversalHelper.getStepsOfAssignableClass(AddVertexStartStep.class, traversal));
+ vertexStepsToInsertFilterAfter.addAll(graphSteps.stream().filter(s -> s.getReturnClass().equals(Vertex.class)).collect(Collectors.toList()));
+
+ vertexStepsToInsertFilterAfter.forEach(s -> TraversalHelper.insertAfterStep(new LambdaFilterStep(traversal, vertexPredicate), s, traversal));
+
+ final List<Step> edgeStepsToInsertFilterAfter = new ArrayList<>();
+ edgeStepsToInsertFilterAfter.addAll(TraversalHelper.getStepsOfAssignableClass(AddEdgeStep.class, traversal));
+ edgeStepsToInsertFilterAfter.addAll(TraversalHelper.getStepsOfAssignableClass(AddEdgeByPathStep.class, traversal));
+ edgeStepsToInsertFilterAfter.addAll(graphSteps.stream().filter(s -> s.getReturnClass().equals(Edge.class)).collect(Collectors.toList()));
+ edgeStepsToInsertFilterAfter.addAll(vertexSteps.stream().filter(s -> s.getReturnClass().equals(Edge.class)).collect(Collectors.toList()));
+
+ edgeStepsToInsertFilterAfter.forEach(s -> TraversalHelper.insertAfterStep(new LambdaFilterStep(traversal, edgePredicate), s, traversal));
+
+ // explode g.V().out() to g.V().outE().inV()
+ vertexSteps.stream().filter(s -> s.getReturnClass().equals(Vertex.class)).forEach(s -> {
+ final VertexStep replacementVertexStep = new VertexStep(traversal, Edge.class, s.getDirection(), s.getEdgeLabels());
+ Step intermediateFilterStep = null;
+ if (s.getDirection() == Direction.BOTH)
+ intermediateFilterStep = new EdgeOtherVertexStep(traversal);
+ else
+ intermediateFilterStep = new EdgeVertexStep(traversal, s.getDirection().opposite());
+
+ TraversalHelper.replaceStep(s, replacementVertexStep, traversal);
+ TraversalHelper.insertAfterStep(intermediateFilterStep, replacementVertexStep, traversal);
+ TraversalHelper.insertAfterStep(new LambdaFilterStep(traversal, edgePredicate), replacementVertexStep, traversal);
+ TraversalHelper.insertAfterStep(new LambdaFilterStep(traversal, vertexPredicate), intermediateFilterStep, traversal);
+ });
+ }
+
+ public static Builder build() {
+ return new Builder();
+ }
+
+ public static class Builder {
+
+ private Predicate<Vertex> vertexPredicate = v -> true;
+ private Predicate<Edge> edgePredicate = v -> true;
+
+ private Builder() {}
+
+ public Builder vertexPredicate(final Predicate<Vertex> predicate) {
+ vertexPredicate = predicate;
+ return this;
+ }
+
+ public Builder edgePredicate(final Predicate<Edge> predicate) {
+ edgePredicate = predicate;
+ return this;
+ }
+
+ public SubgraphStrategy create() {
+ return new SubgraphStrategy(vertexPredicate, edgePredicate);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f55f2d76/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SubgraphStrategyTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SubgraphStrategyTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SubgraphStrategyTest.java
new file mode 100644
index 0000000..5785dba
--- /dev/null
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SubgraphStrategyTest.java
@@ -0,0 +1,143 @@
+/*
+ * 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.decoration;
+
+import org.apache.tinkerpop.gremlin.process.Traversal;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.DefaultGraphTraversal;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.__;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.filter.HasStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.filter.LambdaFilterStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.AddEdgeByPathStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.AddEdgeStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.AddVertexStartStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.AddVertexStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.EdgeVertexStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.map.VertexStep;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.step.sideEffect.GraphStep;
+import org.apache.tinkerpop.gremlin.process.graph.util.HasContainer;
+import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
+import org.apache.tinkerpop.gremlin.structure.Contains;
+import org.apache.tinkerpop.gremlin.structure.Direction;
+import org.apache.tinkerpop.gremlin.structure.Graph;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
+import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
+import org.javatuples.Pair;
+import org.junit.Test;
+import org.junit.experimental.runners.Enclosed;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+@RunWith(Enclosed.class)
+public class SubgraphStrategyTest {
+
+ public static class BasicBehavior {
+ final SubgraphStrategy strategy = SubgraphStrategy.build().create();
+
+ @Test
+ public void shouldAddFilterAfterVertex() {
+ final Traversal t = __.inV();
+ strategy.apply(t.asAdmin());
+ final EdgeVertexStep edgeVertexStep = (EdgeVertexStep) t.asAdmin().getStartStep();
+ assertEquals(LambdaFilterStep.class, edgeVertexStep.getNextStep().getClass());
+ }
+
+ @Test
+ public void shouldAddFilterAfterEdge() {
+ final Traversal t = __.inE();
+ strategy.apply(t.asAdmin());
+ final VertexStep vertexStep = (VertexStep) t.asAdmin().getStartStep();
+ assertEquals(LambdaFilterStep.class, vertexStep.getNextStep().getClass());
+ }
+ }
+
+ @RunWith(Parameterized.class)
+ public static class TraverseBehavior {
+ private static Traversal traversalWithAddV;
+
+ static {
+ final Graph mockedGraph = mock(Graph.class);
+ final DefaultGraphTraversal t = new DefaultGraphTraversal<>(mockedGraph);
+ t.asAdmin().addStep(new GraphStep<>(t.asAdmin(), Vertex.class));
+ traversalWithAddV = t.addV();
+ }
+
+ @Parameterized.Parameters(name = "{0}")
+ public static Iterable<Object[]> data() {
+ return Arrays.asList(new Object[][]{
+ {"bothV()", __.bothV(), 1},
+ {"inV()", __.inV(), 1},
+ {"outV()", __.outV(), 1},
+ {"in()", __.in(), 2},
+ {"in(args)", __.in("test"), 2},
+ {"both()", __.both(), 2},
+ {"both(args)", __.both("test"), 2},
+ {"out()", __.out(), 2},
+ {"out(args)", __.out("test"), 2},
+ {"out().inE().otherV", __.out().inE().otherV(), 4},
+ {"addV()", traversalWithAddV, 2},
+ {"addInE()", __.addInE("test", "x"), 1},
+ {"addOutE()", __.addOutE("test", "x"), 1},
+ {"addInE()", __.addInE("test", "x", "other", "args"), 1},
+ {"addOutE()", __.addOutE("test", "x", "other", "args"), 1},
+ {"addE(OUT)", __.addE(Direction.OUT, "test", "x"), 1},
+ {"addE(IN)", __.addE(Direction.IN, "test", "x"), 1},
+ {"in().out()", __.in().out(), 4},
+ {"in().out().addInE()", __.in().out().addInE("test", "x"), 5},
+ {"in().out().addOutE()", __.in().out().addOutE("test", "x"), 5},
+ {"in().out().addE(OUT)", __.in().out().addE(Direction.OUT, "test", "x"), 5},
+ {"in().out().addE(IN)", __.in().out().addE(Direction.IN, "test", "x"), 5},
+ {"out().out().out()", __.out().out().out(), 6},
+ {"in().out().in()", __.in().out().in(), 6},
+ {"inE().outV().inE().outV()", __.inE().outV().inE().outV(), 4}});
+ }
+
+ @Parameterized.Parameter(value = 0)
+ public String name;
+
+ @Parameterized.Parameter(value = 1)
+ public Traversal traversal;
+
+ @Parameterized.Parameter(value = 2)
+ public int expectedInsertedSteps;
+
+ @Test
+ public void shouldSubgraph() {
+ final SubgraphStrategy strategy = SubgraphStrategy.build().create();
+ strategy.apply(traversal.asAdmin());
+
+ final List<LambdaFilterStep> steps = TraversalHelper.getStepsOfClass(LambdaFilterStep.class, traversal.asAdmin());
+ assertEquals(expectedInsertedSteps, steps.size());
+
+ // todo: better assert expectations when exploding g.V().out() to g.V().outE().inV() and the like
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f55f2d76/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/ProcessComputerSuite.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/ProcessComputerSuite.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/ProcessComputerSuite.java
index 805400c..7604248 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/ProcessComputerSuite.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/ProcessComputerSuite.java
@@ -68,6 +68,7 @@ import org.apache.tinkerpop.gremlin.process.graph.traversal.step.sideEffect.Stor
import org.apache.tinkerpop.gremlin.process.graph.traversal.step.sideEffect.TreeTest;
import org.apache.tinkerpop.gremlin.process.graph.traversal.strategy.TraversalVerificationStrategyTest;
import org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.ReadOnlyStrategyProcessTest;
+import org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.SubgraphStrategyProcessTest;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
@@ -176,7 +177,8 @@ public class ProcessComputerSuite extends AbstractGremlinSuite {
TraversalVerificationStrategyTest.ComputerTraversals.class,
// decorations
- ReadOnlyStrategyProcessTest.class
+ ReadOnlyStrategyProcessTest.class,
+ SubgraphStrategyProcessTest.class
};
/**
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f55f2d76/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/ProcessStandardSuite.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/ProcessStandardSuite.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/ProcessStandardSuite.java
index 5cd74b1..1878122 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/ProcessStandardSuite.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/ProcessStandardSuite.java
@@ -74,6 +74,8 @@ import org.apache.tinkerpop.gremlin.process.graph.traversal.strategy.TraversalVe
import org.apache.tinkerpop.gremlin.process.traversal.CoreTraversalTest;
import org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.PartitionStrategyProcessTest;
import org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.ReadOnlyStrategyProcessTest;
+import org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.SubgraphStrategy;
+import org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.SubgraphStrategyProcessTest;
import org.apache.tinkerpop.gremlin.process.util.PathTest;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
@@ -188,7 +190,8 @@ public class ProcessStandardSuite extends AbstractGremlinSuite {
// decorations
ReadOnlyStrategyProcessTest.class,
- PartitionStrategyProcessTest.class
+ PartitionStrategyProcessTest.class,
+ SubgraphStrategyProcessTest.class
};
/**
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f55f2d76/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SubgraphStrategyProcessTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SubgraphStrategyProcessTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SubgraphStrategyProcessTest.java
new file mode 100644
index 0000000..11d3bcc
--- /dev/null
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/SubgraphStrategyProcessTest.java
@@ -0,0 +1,350 @@
+/*
+ * 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.decoration;
+
+import org.apache.tinkerpop.gremlin.LoadGraphWith;
+import org.apache.tinkerpop.gremlin.process.AbstractGremlinProcessTest;
+import org.apache.tinkerpop.gremlin.process.Traversal;
+import org.apache.tinkerpop.gremlin.process.TraversalEngine;
+import org.apache.tinkerpop.gremlin.process.UseEngine;
+import org.apache.tinkerpop.gremlin.process.graph.traversal.GraphTraversalSource;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.junit.Test;
+
+import java.util.NoSuchElementException;
+import java.util.function.Predicate;
+
+import static org.apache.tinkerpop.gremlin.LoadGraphWith.GraphData.MODERN;
+import static org.apache.tinkerpop.gremlin.process.graph.traversal.__.bothE;
+import static org.apache.tinkerpop.gremlin.process.graph.traversal.__.outE;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+@UseEngine(TraversalEngine.Type.STANDARD)
+@UseEngine(TraversalEngine.Type.COMPUTER)
+public class SubgraphStrategyProcessTest extends AbstractGremlinProcessTest {
+
+ @Test
+ @LoadGraphWith(MODERN)
+ public void testVertexCriterion() throws Exception {
+ final Predicate<Vertex> vertexCriterion = vertex -> vertex.value("name").equals("josh") || vertex.value("name").equals("lop") || vertex.value("name").equals("ripple");
+ final Predicate<Edge> edgeCriterion = edge -> true;
+
+ final SubgraphStrategy strategy = SubgraphStrategy.build().edgePredicate(edgeCriterion).vertexPredicate(vertexCriterion).create();
+ final GraphTraversalSource sg = create(strategy);
+
+ // three vertices are included in the subgraph
+ assertEquals(6, g.V().count().next().longValue());
+ assertEquals(3, sg.V().count().next().longValue());
+
+ // only two edges are present, even though edges are not explicitly excluded
+ // (edges require their incident vertices)
+ assertEquals(6, g.E().count().next().longValue());
+ assertEquals(2, sg.E().count().next().longValue());
+
+ // from vertex
+
+ assertEquals(2, g.V(convertToVertexId("josh")).outE().count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).outE().count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).out().count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).out().count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).inE().count().next().longValue());
+ assertEquals(0, sg.V(convertToVertexId("josh")).inE().count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).in().count().next().longValue());
+ assertEquals(0, sg.V(convertToVertexId("josh")).in().count().next().longValue());
+ assertEquals(3, g.V(convertToVertexId("josh")).bothE().count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).bothE().count().next().longValue());
+ assertEquals(3, g.V(convertToVertexId("josh")).both().count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).both().count().next().longValue());
+
+ // with label
+
+ assertEquals(2, g.V(convertToVertexId("josh")).outE("created").count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).outE("created").count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).out("created").count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).out("created").count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).both("created").count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).both("created").count().next().longValue());
+
+ assertEquals(1, g.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
+ assertEquals(0, sg.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).in("knows").count().next().longValue());
+ assertEquals(0, sg.V(convertToVertexId("josh")).in("knows").count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
+ assertEquals(0, sg.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).both("knows").count().next().longValue());
+ assertEquals(0, sg.V(convertToVertexId("josh")).both("knows").count().next().longValue());
+
+ // with label and branch factor
+
+ assertEquals(1, g.V(convertToVertexId("josh")).local(outE("created").limit(1)).count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).local(outE("created").limit(1)).count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).local(outE("created").limit(1)).count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).local(outE("created").limit(1)).count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).local(bothE("created").limit(1)).count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).local(bothE("created").limit(1)).count().next().longValue());
+
+ // from edge
+
+ assertEquals(2, g.E(convertToEdgeId("josh", "created", "lop")).bothV().count().next().longValue());
+ assertEquals(2, sg.E(convertToEdgeId("josh", "created", "lop")).bothV().count().next().longValue());
+
+ assertEquals(2, g.E(convertToEdgeId("peter", "created", "lop")).bothV().count().next().longValue());
+ try {
+ sg.E(convertToEdgeId("peter", "created", "lop")).next();
+ fail("Edge 12 should not be in the graph because peter is not a vertex");
+ } catch (Exception ex) {
+ assertTrue(ex instanceof NoSuchElementException);
+ }
+
+ assertEquals(2, g.E(convertToEdgeId("marko", "knows", "vadas")).bothV().count().next().longValue());
+ try {
+ sg.E(convertToEdgeId("marko", "knows", "vadas")).next();
+ fail("Edge 7 should not be in the graph because marko is not a vertex");
+ } catch (Exception ex) {
+ assertTrue(ex instanceof NoSuchElementException);
+ }
+ }
+
+ @Test
+ @LoadGraphWith(MODERN)
+ public void shouldFilterEdgeCriterion() throws Exception {
+ final Predicate<Vertex> vertexCriterion = vertex -> true;
+ final Predicate<Edge> edgeCriterion = edge -> {
+ // 8
+ if (edge.<Double>value("weight") == 1.0d && edge.label().equals("knows"))
+ return true;
+ // 9
+ else if (edge.<Double>value("weight") == 0.4d && edge.label().equals("created") && edge.outVertex().value("name").equals("marko"))
+ return true;
+ // 10
+ else if (edge.<Double>value("weight") == 1.0d && edge.label().equals("created"))
+ return true;
+ else return false;
+ };
+
+ final SubgraphStrategy strategy = SubgraphStrategy.build().edgePredicate(edgeCriterion).vertexPredicate(vertexCriterion).create();
+ final GraphTraversalSource sg = create(strategy);
+
+ // all vertices are here
+ assertEquals(6, g.V().count().next().longValue());
+ assertEquals(6, sg.V().count().next().longValue());
+
+ // only the given edges are included
+ assertEquals(6, g.E().count().next().longValue());
+ assertEquals(3, sg.E().count().next().longValue());
+
+ assertEquals(2, g.V(convertToVertexId("marko")).outE("knows").count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("marko")).outE("knows").count().next().longValue());
+
+ // wrapped Traversal<Vertex, Vertex> takes into account the edges it must pass through
+ assertEquals(2, g.V(convertToVertexId("marko")).out("knows").count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("marko")).out("knows").count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).out("created").count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).out("created").count().next().longValue());
+
+ // from vertex
+
+ assertEquals(2, g.V(convertToVertexId("josh")).outE().count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).outE().count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).out().count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).out().count().next().longValue());
+
+ assertEquals(1, g.V(convertToVertexId("josh")).inE().count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).inE().count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).in().count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).in().count().next().longValue());
+
+ assertEquals(3, g.V(convertToVertexId("josh")).bothE().count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).bothE().count().next().longValue());
+ assertEquals(3, g.V(convertToVertexId("josh")).both().count().next().longValue());
+ final Traversal t = sg.V(convertToVertexId("josh")).both();
+ assertEquals(2, sg.V(convertToVertexId("josh")).both().count().next().longValue());
+
+ // with label
+
+ assertEquals(2, g.V(convertToVertexId("josh")).outE("created").count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).outE("created").count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).out("created").count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).out("created").count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).both("created").count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).both("created").count().next().longValue());
+
+ assertEquals(1, g.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).in("knows").count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).in("knows").count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).both("knows").count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).both("knows").count().next().longValue());
+
+ // with branch factor
+
+ assertEquals(1, g.V(convertToVertexId("josh")).limit(1).local(bothE().limit(1)).count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).limit(1).local(bothE().limit(1)).count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).limit(1).local(bothE().limit(1)).inV().count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).limit(1).local(bothE().limit(1)).inV().count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).inV().count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).inV().count().next().longValue());
+
+ // from edge
+
+ assertEquals(2, g.E(convertToEdgeId("marko", "knows", "josh")).bothV().count().next().longValue());
+ assertEquals(2, sg.E(convertToEdgeId("marko", "knows", "josh")).bothV().count().next().longValue());
+
+ assertEquals(3, g.E(convertToEdgeId("marko", "knows", "josh")).outV().outE().count().next().longValue());
+ assertEquals(2, sg.E(convertToEdgeId("marko", "knows", "josh")).outV().outE().count().next().longValue());
+ }
+
+ @Test
+ @LoadGraphWith(MODERN)
+ public void shouldFilterMixedCriteria() throws Exception {
+ final Predicate<Vertex> vertexCriterion = vertex -> vertex.value("name").equals("josh") || vertex.value("name").equals("lop") || vertex.value("name").equals("ripple");
+ final Predicate<Edge> edgeCriterion = edge -> {
+ // 9 isn't present because marko is not in the vertex list
+ // 11
+ if (edge.<Double>value("weight") == 0.4d && edge.label().equals("created"))
+ return true;
+ // 10
+ else if (edge.<Double>value("weight") == 1.0d && edge.label().equals("created"))
+ return true;
+ else return false;
+ };
+
+ final SubgraphStrategy strategy = SubgraphStrategy.build().edgePredicate(edgeCriterion).vertexPredicate(vertexCriterion).create();
+ final GraphTraversalSource sg = create(strategy);
+
+ // three vertices are included in the subgraph
+ assertEquals(6, g.V().count().next().longValue());
+ assertEquals(3, sg.V().count().next().longValue());
+
+ // three edges are explicitly included, but one is missing its out-vertex due to the vertex criterion
+ assertEquals(6, g.E().count().next().longValue());
+ assertEquals(2, sg.E().count().next().longValue());
+
+ // from vertex
+
+ assertEquals(2, g.V(convertToVertexId("josh")).outE().count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).outE().count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).out().count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).out().count().next().longValue());
+
+ assertEquals(1, g.V(convertToVertexId("josh")).inE().count().next().longValue());
+ assertEquals(0, sg.V(convertToVertexId("josh")).inE().count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).in().count().next().longValue());
+ assertEquals(0, sg.V(convertToVertexId("josh")).in().count().next().longValue());
+
+ assertEquals(3, g.V(convertToVertexId("josh")).bothE().count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).bothE().count().next().longValue());
+ assertEquals(3, g.V(convertToVertexId("josh")).both().count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).both().count().next().longValue());
+
+ // with label
+
+ assertEquals(2, g.V(convertToVertexId("josh")).outE("created").count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).outE("created").count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).out("created").count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).out("created").count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
+ assertEquals(2, g.V(convertToVertexId("josh")).both("created").count().next().longValue());
+ assertEquals(2, sg.V(convertToVertexId("josh")).both("created").count().next().longValue());
+
+ assertEquals(1, g.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
+ assertEquals(0, sg.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).in("knows").count().next().longValue());
+ assertEquals(0, sg.V(convertToVertexId("josh")).in("knows").count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
+ assertEquals(0, sg.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).both("knows").count().next().longValue());
+ assertEquals(0, sg.V(convertToVertexId("josh")).both("knows").count().next().longValue());
+
+ // with branch factor
+
+ assertEquals(1, g.V(convertToVertexId("josh")).local(bothE().limit(1)).count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).local(bothE().limit(1)).count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).local(bothE().limit(1)).inV().count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).local(bothE().limit(1)).inV().count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).count().next().longValue());
+ assertEquals(1, g.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).inV().count().next().longValue());
+ assertEquals(1, sg.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).inV().count().next().longValue());
+
+ // from edge
+
+ assertEquals(2, g.E(convertToEdgeId("marko", "created", "lop")).bothV().count().next().longValue());
+ try {
+ sg.E(convertToEdgeId("marko", "created", "lop")).next();
+ fail("Edge 9 should not be in the graph because marko is not a vertex");
+ } catch (Exception ex) {
+ assertTrue(ex instanceof NoSuchElementException);
+ }
+ }
+
+ @Test(expected = NoSuchElementException.class)
+ @LoadGraphWith(MODERN)
+ public void shouldGetExcludedVertex() throws Exception {
+ final Predicate<Vertex> vertexCriterion = vertex -> vertex.value("name").equals("josh") || vertex.value("name").equals("lop") || vertex.value("name").equals("ripple");
+ final Predicate<Edge> edgeCriterion = edge -> true;
+
+ final SubgraphStrategy strategy = SubgraphStrategy.build().edgePredicate(edgeCriterion).vertexPredicate(vertexCriterion).create();
+ final GraphTraversalSource sg = create(strategy);
+
+ sg.V(convertToVertexId("marko")).next();
+ }
+
+ @Test(expected = NoSuchElementException.class)
+ @LoadGraphWith(MODERN)
+ public void shouldGetExcludedEdge() throws Exception {
+ final Predicate<Vertex> vertexCriterion = vertex -> true;
+ final Predicate<Edge> edgeCriterion = edge -> {
+ // 8
+ if (edge.<Double>value("weight") == 1.0d && edge.label().equals("knows"))
+ return true;
+ // 9
+ else if (edge.<Double>value("weight") == 0.4d && edge.label().equals("created") && edge.outVertex().value("name").equals("marko"))
+ return true;
+ // 10
+ else if (edge.<Double>value("weight") == 1.0d && edge.label().equals("created"))
+ return true;
+ else return false;
+ };
+
+ final SubgraphStrategy strategy = SubgraphStrategy.build().edgePredicate(edgeCriterion).vertexPredicate(vertexCriterion).create();
+ final GraphTraversalSource sg = create(strategy);
+
+ sg.E(sg.E(convertToEdgeId("marko", "knows", "vadas")).next()).next();
+ }
+
+ private GraphTraversalSource create(final SubgraphStrategy strategy) {
+ return graphProvider.traversal(graph, strategy);
+ }
+}