diff --git a/janusgraph-test/src/test/java/org/janusgraph/blueprints/process/traversal/strategy/JanusGraphStrategySuite.java b/janusgraph-test/src/test/java/org/janusgraph/blueprints/process/traversal/strategy/JanusGraphStrategySuite.java deleted file mode 100644 index 3df8483be6..0000000000 --- a/janusgraph-test/src/test/java/org/janusgraph/blueprints/process/traversal/strategy/JanusGraphStrategySuite.java +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2017 JanusGraph Authors -// -// Licensed 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.janusgraph.blueprints.process.traversal.strategy; - -import org.janusgraph.blueprints.process.traversal.strategy.optimization.JanusGraphStepStrategyTest; -import org.apache.tinkerpop.gremlin.AbstractGremlinSuite; -import org.apache.tinkerpop.gremlin.process.traversal.TraversalEngine; -import org.junit.runners.model.InitializationError; -import org.junit.runners.model.RunnerBuilder; - -/** - * @author Marko A. Rodriguez (http://markorodriguez.com) - */ -public class JanusGraphStrategySuite extends AbstractGremlinSuite { - - public JanusGraphStrategySuite(final Class klass, final RunnerBuilder builder) throws InitializationError { - - super(klass, builder, - new Class[]{ - JanusGraphStepStrategyTest.class - }, new Class[]{ - JanusGraphStepStrategyTest.class - }, - false, - TraversalEngine.Type.STANDARD); - } - -} diff --git a/janusgraph-test/src/test/java/org/janusgraph/blueprints/process/traversal/strategy/JanusGraphStrategyTest.java b/janusgraph-test/src/test/java/org/janusgraph/blueprints/process/traversal/strategy/JanusGraphStrategyTest.java deleted file mode 100644 index c566f47ae0..0000000000 --- a/janusgraph-test/src/test/java/org/janusgraph/blueprints/process/traversal/strategy/JanusGraphStrategyTest.java +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2017 JanusGraph Authors -// -// Licensed 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.janusgraph.blueprints.process.traversal.strategy; - -import org.janusgraph.blueprints.InMemoryGraphProvider; -import org.janusgraph.core.JanusGraph; -import org.apache.tinkerpop.gremlin.GraphProviderClass; -import org.junit.runner.RunWith; - -/** - * @author Marko A. Rodriguez (http://markorodriguez.com) - */ -@RunWith(JanusGraphStrategySuite.class) -@GraphProviderClass(provider = InMemoryGraphProvider.class, graph = JanusGraph.class) -public class JanusGraphStrategyTest { -} diff --git a/janusgraph-test/src/test/java/org/janusgraph/blueprints/process/traversal/strategy/optimization/JanusGraphStepStrategyTest.java b/janusgraph-test/src/test/java/org/janusgraph/blueprints/process/traversal/strategy/optimization/JanusGraphStepStrategyTest.java index 040b293d90..3d616abdfd 100644 --- a/janusgraph-test/src/test/java/org/janusgraph/blueprints/process/traversal/strategy/optimization/JanusGraphStepStrategyTest.java +++ b/janusgraph-test/src/test/java/org/janusgraph/blueprints/process/traversal/strategy/optimization/JanusGraphStepStrategyTest.java @@ -14,81 +14,113 @@ package org.janusgraph.blueprints.process.traversal.strategy.optimization; -import org.janusgraph.blueprints.InMemoryGraphProvider; -import org.janusgraph.core.JanusGraph; -import org.janusgraph.graphdb.tinkerpop.optimize.JanusGraphStep; +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; +import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies; +import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__; +import org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer; +import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.FilterRankingStrategy; +import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.InlineFilterStrategy; +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.apache.tinkerpop.gremlin.process.AbstractGremlinProcessTest; -import org.apache.tinkerpop.gremlin.process.IgnoreEngine; import org.apache.tinkerpop.gremlin.process.traversal.P; -import org.apache.tinkerpop.gremlin.GraphProviderClass; -import org.apache.tinkerpop.gremlin.process.ProcessStandardSuite; -import org.apache.tinkerpop.gremlin.process.traversal.TraversalEngine; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; -import org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.graphdb.tinkerpop.optimize.JanusGraphStep; +import org.janusgraph.graphdb.tinkerpop.optimize.JanusGraphStepStrategy; import org.junit.Test; import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import static org.apache.tinkerpop.gremlin.process.traversal.P.eq; +import static org.apache.tinkerpop.gremlin.process.traversal.P.gt; +import static org.apache.tinkerpop.gremlin.process.traversal.P.lt; +import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.filter; +import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.has; +import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.not; +import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.properties; import static org.junit.Assert.assertEquals; -/** - * @author Marko A. Rodriguez (http://markorodriguez.com) - */ -public class JanusGraphStepStrategyTest extends AbstractGremlinProcessTest { +@RunWith(Parameterized.class) +public class JanusGraphStepStrategyTest { + + @Parameterized.Parameter(value = 0) + public Traversal original; + + @Parameterized.Parameter(value = 1) + public Traversal optimized; + + @Parameterized.Parameter(value = 2) + public Collection otherStrategies; @Test - @IgnoreEngine(TraversalEngine.Type.COMPUTER) - public void shouldFoldInHasContainers() { - GraphTraversal.Admin traversal = g.V().has("name", "marko").asAdmin(); - assertEquals(2, traversal.getSteps().size()); - assertEquals(HasStep.class, traversal.getEndStep().getClass()); - traversal.applyStrategies(); - assertEquals(1, traversal.getSteps().size()); - assertEquals(JanusGraphStep.class, traversal.getStartStep().getClass()); - assertEquals(JanusGraphStep.class, traversal.getEndStep().getClass()); - assertEquals(1, ((JanusGraphStep) traversal.getStartStep()).getHasContainers().size()); - assertEquals("name", ((JanusGraphStep) traversal.getStartStep()).getHasContainers().get(0).getKey()); - assertEquals("marko", ((JanusGraphStep) traversal.getStartStep()).getHasContainers().get(0).getValue()); - //// - traversal = g.V().has("name", "marko").has("age", P.gt(20)).asAdmin(); - traversal.applyStrategies(); - assertEquals(1, traversal.getSteps().size()); - assertEquals(JanusGraphStep.class, traversal.getStartStep().getClass()); - assertEquals(2, ((JanusGraphStep) traversal.getStartStep()).getHasContainers().size()); - //// - traversal = g.V().has("name", "marko").out().has("name", "daniel").asAdmin(); - traversal.applyStrategies(); - assertEquals(3, traversal.getSteps().size()); - assertEquals(JanusGraphStep.class, traversal.getStartStep().getClass()); - assertEquals(1, ((JanusGraphStep) traversal.getStartStep()).getHasContainers().size()); - assertEquals("name", ((JanusGraphStep) traversal.getStartStep()).getHasContainers().get(0).getKey()); - assertEquals("marko", ((JanusGraphStep) traversal.getStartStep()).getHasContainers().get(0).getValue()); - assertEquals(HasStep.class, traversal.getEndStep().getClass()); - //// - traversal = g.V().has("name", "marko").out().V().has("name", "daniel").asAdmin(); - traversal.applyStrategies(); - assertEquals(4, traversal.getSteps().size()); - assertEquals(JanusGraphStep.class, traversal.getStartStep().getClass()); - assertEquals(1, ((JanusGraphStep) traversal.getStartStep()).getHasContainers().size()); - assertEquals("name", ((JanusGraphStep) traversal.getStartStep()).getHasContainers().get(0).getKey()); - assertEquals("marko", ((JanusGraphStep) traversal.getStartStep()).getHasContainers().get(0).getValue()); - assertEquals(JanusGraphStep.class, traversal.getSteps().get(2).getClass()); - assertEquals(1, ((JanusGraphStep) traversal.getSteps().get(2)).getHasContainers().size()); - assertEquals("name", ((JanusGraphStep) traversal.getSteps().get(2)).getHasContainers().get(0).getKey()); - assertEquals("daniel", ((JanusGraphStep) traversal.getSteps().get(2)).getHasContainers().get(0).getValue()); - //// - traversal = g.V().hasId(1).has("name", "marko").asAdmin(); - assertEquals(3, traversal.getSteps().size()); - assertEquals(HasStep.class, traversal.getEndStep().getClass()); - traversal.applyStrategies(); - assertEquals(1, traversal.getSteps().size()); - assertEquals(JanusGraphStep.class, traversal.getStartStep().getClass()); - assertEquals(JanusGraphStep.class, traversal.getEndStep().getClass()); - assertEquals(1, ((JanusGraphStep) traversal.getStartStep()).getIds().length); - assertEquals(1, ((JanusGraphStep) traversal.getStartStep()).getHasContainers().size()); - assertEquals(1, ((JanusGraphStep) traversal.getStartStep()).getIds()[0]); - assertEquals("name", ((JanusGraphStep) traversal.getStartStep()).getHasContainers().get(0).getKey()); - assertEquals("marko", ((JanusGraphStep) traversal.getStartStep()).getHasContainers().get(0).getValue()); + public void doTest() { + final TraversalStrategies strategies = new DefaultTraversalStrategies(); + strategies.addStrategies(JanusGraphStepStrategy.instance()); + for (final TraversalStrategy strategy : this.otherStrategies) { + strategies.addStrategies(strategy); + } + this.original.asAdmin().setStrategies(strategies); + this.original.asAdmin().applyStrategies(); + assertEquals(this.optimized, this.original); + } + + private static GraphTraversal.Admin g_V(final Object... hasKeyValues) { + final GraphTraversal.Admin traversal = new DefaultGraphTraversal<>(); + final JanusGraphStep graphStep = new JanusGraphStep<>(new GraphStep<>(traversal, Vertex.class, true)); + for (int i = 0; i < hasKeyValues.length; i = i + 2) { + if(hasKeyValues[i].equals(T.id)) { + graphStep.addIds(Arrays.asList(hasKeyValues[i + 1])); + } else { + graphStep.addHasContainer(new HasContainer((String) hasKeyValues[i], (P) hasKeyValues[i + 1])); + } + } + return traversal.addStep(graphStep); + } + + @Parameterized.Parameters(name = "{0}") + public static Iterable generateTestParameters() { + return Arrays.asList(new Object[][]{ + {__.V().out(), g_V().out(), Collections.emptyList()}, + {__.V().has("name", "marko").out(), g_V("name", eq("marko")).out(), Collections.emptyList()}, + {__.V().has("name", "marko").has("age", gt(31).and(lt(10))).out(), + g_V("name", eq("marko"), "age", gt(31), "age", lt(10)).out(), Collections.emptyList()}, + {__.V().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"), + g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))), Collections.singletonList(FilterRankingStrategy.instance())}, + {__.V().has("name", "marko").as("a").or(has("age"), has("age", gt(32))).has("lang", "java"), + g_V("name", eq("marko")).as("a").or(has("age"), has("age", gt(32))).has("lang", "java"), Collections.emptyList()}, + {__.V().has("name", "marko").as("a").or(has("age"), has("age", gt(32))).has("lang", "java"), + g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).as("a"), Collections.singletonList(FilterRankingStrategy.instance())}, + {__.V().dedup().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"), + g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).dedup(), Collections.singletonList(FilterRankingStrategy.instance())}, + {__.V().as("a").dedup().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"), + g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).dedup().as("a"), Collections.singletonList(FilterRankingStrategy.instance())}, + {__.V().as("a").has("name", "marko").as("b").or(has("age"), has("age", gt(32))).has("lang", "java"), + g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).as("b", "a"), Collections.singletonList(FilterRankingStrategy.instance())}, + {__.V().as("a").dedup().has("name", "marko").or(has("age"), has("age", gt(32))).filter(has("name", "bob")).has("lang", "java"), + g_V("name", eq("marko"), "lang", eq("java"), "name", eq("bob")).or(has("age"), has("age", gt(32))).dedup().as("a"), Arrays.asList(InlineFilterStrategy.instance(), FilterRankingStrategy.instance())}, + {__.V().as("a").dedup().has("name", "marko").or(has("age", 10), has("age", gt(32))).filter(has("name", "bob")).has("lang", "java"), + g_V("name", eq("marko"), "lang", eq("java"), "name", eq("bob"), "age", eq(10).or(gt(32))).dedup().as("a"), TraversalStrategies.GlobalCache.getStrategies(JanusGraph.class).toList()}, + {__.V().has("name", "marko").or(not(has("age")), has("age", gt(32))).has("name", "bob").has("lang", "java"), + g_V("name", eq("marko"), "name", eq("bob"), "lang", eq("java")).or(not(filter(properties("age"))), has("age", gt(32))), TraversalStrategies.GlobalCache.getStrategies(JanusGraph.class).toList()}, + {__.V().has("name", eq("marko").or(eq("bob").and(eq("stephen")))).out("knows"), + g_V("name", eq("marko").or(eq("bob").and(eq("stephen")))).out("knows"), Collections.emptyList()}, + {__.V().has("name", eq("marko").and(eq("bob").and(eq("stephen")))).out("knows"), + g_V("name", eq("marko"), "name", eq("bob"), "name", eq("stephen")).out("knows"), Collections.emptyList()}, + {__.V().has("name", eq("marko").and(eq("bob").or(eq("stephen")))).out("knows"), + g_V("name", eq("marko"), "name", eq("bob").or(eq("stephen"))).out("knows"), Collections.emptyList()}, + {__.V().hasId(1), g_V(T.id, 1), Collections.emptyList()}, + {__.V().hasId(1).hasId(2), g_V(T.id, 1, T.id, 2), Collections.emptyList()}, + {__.V().hasId(1).has("name", "marko"), g_V(T.id, 1, "name", eq("marko")), Collections.emptyList()} + }); } }