Search in sources :

Example 56 with SqlgGraphStep

use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.

the class TestLocalVertexStepOptionalWithOrder method testVertexStepAfterOptional.

@Test
public void testVertexStepAfterOptional() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 1);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "order", 1);
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "order", 1);
    Vertex c3 = this.sqlgGraph.addVertex(T.label, "C", "order", 1);
    b1.addEdge("bc", c1);
    b1.addEdge("bc", c2);
    b1.addEdge("bc", c3);
    Vertex d1 = this.sqlgGraph.addVertex(T.label, "D", "order", 1);
    Vertex d2 = this.sqlgGraph.addVertex(T.label, "D", "order", 2);
    Vertex d3 = this.sqlgGraph.addVertex(T.label, "D", "order", 3);
    c1.addEdge("cd", d1);
    c1.addEdge("cd", d2);
    c1.addEdge("cd", d3);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "order", 1);
    Vertex bb1 = this.sqlgGraph.addVertex(T.label, "BB", "order", -1);
    Vertex bb2 = this.sqlgGraph.addVertex(T.label, "BB", "order", -2);
    Vertex bb3 = this.sqlgGraph.addVertex(T.label, "BB", "order", -3);
    a2.addEdge("abb", bb1);
    a2.addEdge("abb", bb2);
    a2.addEdge("abb", bb3);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").local(__.optional(__.out("ab").optional(__.out()))).out().order().by("order", Order.decr);
    Assert.assertEquals(5, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(3, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgLocalStepBarrier);
    Assert.assertTrue(traversal.getSteps().get(2) instanceof SqlgVertexStep);
    SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier<?, ?>) traversal.getSteps().get(1);
    List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexSteps.size());
    Assert.assertEquals(6, vertices.size());
    Assert.assertEquals(d3, vertices.get(0));
    Assert.assertEquals(d2, vertices.get(1));
    Assert.assertEquals(d1, vertices.get(2));
    Assert.assertEquals(bb1, vertices.get(3));
    Assert.assertEquals(bb2, vertices.get(4));
    Assert.assertEquals(bb3, vertices.get(5));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 57 with SqlgGraphStep

use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.

the class TestLocalVertexStepOptionalWithOrder method testOptionalWithOrderBy2.

@Test
public void testOptionalWithOrderBy2() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "b3");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "name", "c2");
    Vertex c3 = this.sqlgGraph.addVertex(T.label, "C", "name", "c3");
    Vertex d1 = this.sqlgGraph.addVertex(T.label, "D", "name", "d1");
    Vertex d2 = this.sqlgGraph.addVertex(T.label, "D", "name", "d2");
    Vertex d3 = this.sqlgGraph.addVertex(T.label, "D", "name", "d3");
    Edge ab1 = a1.addEdge("ab", b1, "order", 3);
    Edge ab2 = a1.addEdge("ab", b2, "order", 2);
    Edge ab3 = a1.addEdge("ab", b3, "order", 1);
    Edge bc1 = b1.addEdge("bc", c1, "order", 3);
    Edge bc2 = b1.addEdge("bc", c2, "order", 2);
    Edge bc3 = b1.addEdge("bc", c3, "order", 1);
    Edge cd1 = c1.addEdge("cd", d1, "order", 3);
    Edge cd2 = c1.addEdge("cd", d2, "order", 2);
    Edge cd3 = c1.addEdge("cd", d3, "order", 1);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal().V(a1.id()).local(__.optional(__.outE("ab").as("ab").otherV().as("vb").order().by(__.select("ab").by("order"), Order.incr).optional(__.outE("bc").as("bc").otherV().as("vc").order().by(__.select("bc").by("order"), Order.incr).optional(__.outE("cd").as("cd").inV().as("vd").order().by(__.select("cd").by("order"), Order.incr))))).path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(3, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgLocalStepBarrier);
    Assert.assertTrue(traversal.getSteps().get(2) instanceof PathStep);
    SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier<?, ?>) traversal.getSteps().get(1);
    List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexSteps.size());
    Assert.assertEquals(7, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 5 && p.get(0).equals(a1) && p.get(1).equals(ab1) && p.get(2).equals(b1) && p.get(3).equals(bc3) && p.get(4).equals(c3)));
    paths.remove(paths.stream().filter(p -> p.size() == 5 && p.get(0).equals(a1) && p.get(1).equals(ab1) && p.get(2).equals(b1) && p.get(3).equals(bc3) && p.get(4).equals(c3)).findAny().get());
    Assert.assertEquals(6, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 5 && p.get(0).equals(a1) && p.get(1).equals(ab1) && p.get(2).equals(b1) && p.get(3).equals(bc2) && p.get(4).equals(c2)));
    paths.remove(paths.stream().filter(p -> p.size() == 5 && p.get(0).equals(a1) && p.get(1).equals(ab1) && p.get(2).equals(b1) && p.get(3).equals(bc2) && p.get(4).equals(c2)).findAny().get());
    Assert.assertEquals(5, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 7 && p.get(0).equals(a1) && p.get(1).equals(ab1) && p.get(2).equals(b1) && p.get(3).equals(bc1) && p.get(4).equals(c1) && p.get(5).equals(cd1) && p.get(6).equals(d1)));
    paths.remove(paths.stream().filter(p -> p.size() == 7 && p.get(0).equals(a1) && p.get(1).equals(ab1) && p.get(2).equals(b1) && p.get(3).equals(bc1) && p.get(4).equals(c1) && p.get(5).equals(cd1) && p.get(6).equals(d1)).findAny().get());
    Assert.assertEquals(4, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 7 && p.get(0).equals(a1) && p.get(1).equals(ab1) && p.get(2).equals(b1) && p.get(3).equals(bc1) && p.get(4).equals(c1) && p.get(5).equals(cd2) && p.get(6).equals(d2)));
    paths.remove(paths.stream().filter(p -> p.size() == 7 && p.get(0).equals(a1) && p.get(1).equals(ab1) && p.get(2).equals(b1) && p.get(3).equals(bc1) && p.get(4).equals(c1) && p.get(5).equals(cd2) && p.get(6).equals(d2)).findAny().get());
    Assert.assertEquals(3, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 7 && p.get(0).equals(a1) && p.get(1).equals(ab1) && p.get(2).equals(b1) && p.get(3).equals(bc1) && p.get(4).equals(c1) && p.get(5).equals(cd3) && p.get(6).equals(d3)));
    paths.remove(paths.stream().filter(p -> p.size() == 7 && p.get(0).equals(a1) && p.get(1).equals(ab1) && p.get(2).equals(b1) && p.get(3).equals(bc1) && p.get(4).equals(c1) && p.get(5).equals(cd3) && p.get(6).equals(d3)).findAny().get());
    Assert.assertEquals(2, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(ab2) && p.get(2).equals(b2)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(ab2) && p.get(2).equals(b2)).findAny().get());
    Assert.assertEquals(1, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(ab3) && p.get(2).equals(b3)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(ab3) && p.get(2).equals(b3)).findAny().get());
    Assert.assertEquals(0, paths.size());
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) TraversalHelper(org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper) BaseTest(org.umlg.sqlg.test.BaseTest) SqlgOptionalStepBarrier(org.umlg.sqlg.step.barrier.SqlgOptionalStepBarrier) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Test(org.junit.Test) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) T(org.apache.tinkerpop.gremlin.structure.T) Collectors(java.util.stream.Collectors) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) List(java.util.List) PathStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PathStep) Element(org.apache.tinkerpop.gremlin.structure.Element) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) Order(org.apache.tinkerpop.gremlin.process.traversal.Order) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Assert(org.junit.Assert) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) PathStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PathStep) Edge(org.apache.tinkerpop.gremlin.structure.Edge) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 58 with SqlgGraphStep

use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.

the class TestLocalVertexStepOptionalWithOrder method testOptionalWithOrder2.

@Test
public void testOptionalWithOrder2() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 13);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "order", 12);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "order", 11);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").as("a").local(__.optional(__.outE().as("e").otherV().as("v"))).order().by("order");
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(4, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertTrue(traversal.getSteps().get(1) instanceof IdentityStep);
    Assert.assertTrue(traversal.getSteps().get(2) instanceof SqlgLocalStepBarrier);
    Assert.assertTrue(traversal.getSteps().get(3) instanceof OrderGlobalStep);
    SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier<?, ?>) traversal.getSteps().get(2);
    List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexSteps.size());
    Assert.assertEquals(5, vertices.size());
    Assert.assertEquals(b3, vertices.get(0));
    Assert.assertEquals(b2, vertices.get(1));
    Assert.assertEquals(b1, vertices.get(2));
    Assert.assertEquals(a3, vertices.get(3));
    Assert.assertEquals(a2, vertices.get(4));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 59 with SqlgGraphStep

use of org.umlg.sqlg.step.SqlgGraphStep in project sqlg by pietermartin.

the class BaseTest method assertStep.

protected void assertStep(Step<?, ?> step, boolean isGraph, boolean isEagerLoad, boolean isForMultipleQueries, boolean comparatorsNotOnDb) {
    if (isGraph) {
        Assert.assertTrue("Expected SqlgGraphStep, found " + step.getClass().getName(), step instanceof SqlgGraphStep);
    } else {
        Assert.assertTrue("Expected SqlgVertexStep, found " + step.getClass().getName(), step instanceof SqlgVertexStep);
    }
    SqlgStep sqlgStep = (SqlgStep) step;
    Assert.assertEquals("isEagerLoad should be " + isEagerLoad, isEagerLoad, sqlgStep.isEargerLoad());
    Assert.assertEquals("isForMultipleQueries should be " + isForMultipleQueries, isForMultipleQueries, sqlgStep.isForMultipleQueries());
    Assert.assertEquals("comparatorsNotOnDb should be " + comparatorsNotOnDb, comparatorsNotOnDb, sqlgStep.getReplacedSteps().stream().allMatch(r -> r.getDbComparators().isEmpty()));
}
Also used : Configuration(org.apache.commons.configuration.Configuration) CoreMatchers(org.hamcrest.CoreMatchers) Io(org.apache.tinkerpop.gremlin.structure.io.Io) java.util(java.util) Connection(java.sql.Connection) SqlgUtil(org.umlg.sqlg.util.SqlgUtil) TestRule(org.junit.rules.TestRule) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) GraphSONIo(org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONIo) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) SqlgStep(org.umlg.sqlg.step.SqlgStep) PropertyVetoException(java.beans.PropertyVetoException) SQLException(java.sql.SQLException) AbstractGremlinTest(org.apache.tinkerpop.gremlin.AbstractGremlinTest) ResultSet(java.sql.ResultSet) org.apache.tinkerpop.gremlin.structure(org.apache.tinkerpop.gremlin.structure) Assert.fail(org.junit.Assert.fail) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) IteratorUtils(org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils) MapHelper(org.apache.tinkerpop.gremlin.process.traversal.step.util.MapHelper) GraphSONVersion(org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONVersion) Logger(org.slf4j.Logger) Description(org.junit.runner.Description) IOException(java.io.IOException) StopWatch(org.apache.commons.lang3.time.StopWatch) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) TestWatcher(org.junit.rules.TestWatcher) TimeUnit(java.util.concurrent.TimeUnit) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Statement(java.sql.Statement) org.junit(org.junit) GraphReader(org.apache.tinkerpop.gremlin.structure.io.GraphReader) ConfigurationException(org.apache.commons.configuration.ConfigurationException) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) SqlgStep(org.umlg.sqlg.step.SqlgStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep)

Aggregations

SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)59 DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)57 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)57 Test (org.junit.Test)57 BaseTest (org.umlg.sqlg.test.BaseTest)57 ReplacedStep (org.umlg.sqlg.sql.parse.ReplacedStep)31 HashSet (java.util.HashSet)10 SqlgVertexStep (org.umlg.sqlg.step.SqlgVertexStep)6 Path (org.apache.tinkerpop.gremlin.process.traversal.Path)3 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)3 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)3 SqlgLocalStepBarrier (org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier)3 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 Step (org.apache.tinkerpop.gremlin.process.traversal.Step)2 GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)2 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__)2 OrderGlobalStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep)2