use of org.umlg.sqlg.step.SqlgVertexStep in project sqlg by pietermartin.
the class TestRepeatStepGraphOut method testRepeatToSelf.
@Test
public void testRepeatToSelf() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
a1.addEdge("aa", a2);
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().repeat(__.out()).until(__.hasLabel("A"));
Assert.assertEquals(2, traversal.getSteps().size());
printTraversalForm(traversal);
Assert.assertEquals(2, traversal.getSteps().size());
Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgRepeatStepBarrier);
SqlgRepeatStepBarrier repeatStep = (SqlgRepeatStepBarrier) traversal.getSteps().get(1);
DefaultGraphTraversal traversal1 = (DefaultGraphTraversal) repeatStep.getGlobalChildren().get(0);
Assert.assertEquals(2, traversal1.getSteps().size());
Assert.assertTrue(traversal1.getSteps().get(0) instanceof SqlgVertexStep);
List<Vertex> vertices = traversal.toList();
Assert.assertEquals(1, vertices.size());
}
use of org.umlg.sqlg.step.SqlgVertexStep in project sqlg by pietermartin.
the class TestLocalVertexStepOptional method testLocalVertexStepOptimized.
@Test
public void testLocalVertexStepOptimized() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
Vertex a11 = this.sqlgGraph.addVertex(T.label, "A", "name", "a11");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
a1.addEdge("ab", b1);
b1.addEdge("bc", c1);
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.gt.V(a11).local(__.optional(__.out()).values("name")).path();
Assert.assertEquals(3, traversal.getSteps().size());
List<Path> paths = traversal.toList();
Assert.assertEquals(3, traversal.getSteps().size());
Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgLocalStepBarrier);
SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier) traversal.getSteps().get(1);
List<SqlgVertexStep> sqlgVertexStepCompileds = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
Assert.assertEquals(1, sqlgVertexStepCompileds.size());
SqlgVertexStep sqlgVertexStepCompiled = sqlgVertexStepCompileds.get(0);
assertStep(sqlgVertexStepCompiled, false, false, true, true);
Assert.assertEquals(1, paths.size());
List<Predicate<Path>> pathsToAssert = Arrays.asList(p -> p.size() == 2 && p.get(0).equals(a11) && p.get(1).equals("a11"));
for (Predicate<Path> pathPredicate : pathsToAssert) {
Optional<Path> path = paths.stream().filter(pathPredicate).findAny();
Assert.assertTrue(path.isPresent());
Assert.assertTrue(paths.remove(path.get()));
}
Assert.assertTrue(paths.isEmpty());
paths = this.gt.V(a1).local(__.out().out().values("name")).path().toList();
Assert.assertEquals(1, paths.size());
pathsToAssert = Arrays.asList(p -> p.size() == 4 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1) && p.get(3).equals("c1"));
for (Predicate<Path> pathPredicate : pathsToAssert) {
Optional<Path> path = paths.stream().filter(pathPredicate).findAny();
Assert.assertTrue(path.isPresent());
Assert.assertTrue(paths.remove(path.get()));
}
Assert.assertTrue(paths.isEmpty());
}
use of org.umlg.sqlg.step.SqlgVertexStep in project sqlg by pietermartin.
the class TestLocalVertexStepOptional method testUnoptimizableChooseStep.
@Test
public void testUnoptimizableChooseStep() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
a1.addEdge("ab", b1);
a1.addEdge("ab", b2);
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V(a1).local(__.<Vertex, Vertex>choose(v -> v.label().equals("A"), __.out(), __.in()));
List<Vertex> vertices = traversal.toList();
Assert.assertEquals(2, traversal.getSteps().size());
Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgLocalStepBarrier);
SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier) traversal.getSteps().get(1);
List<SqlgVertexStep> sqlgVertexStepCompileds = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
Assert.assertEquals(2, sqlgVertexStepCompileds.size());
SqlgVertexStep sqlgVertexStepCompiled = sqlgVertexStepCompileds.get(0);
assertStep(sqlgVertexStepCompiled, false, false, false, true);
sqlgVertexStepCompiled = sqlgVertexStepCompileds.get(1);
assertStep(sqlgVertexStepCompiled, false, false, false, true);
Assert.assertEquals(2, vertices.size());
}
use of org.umlg.sqlg.step.SqlgVertexStep in project sqlg by pietermartin.
the class TestLocalVertexStepOptional method testLocalOptional2.
@Test
public void testLocalOptional2() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
a1.addEdge("ab", b1);
b1.addEdge("bc", c1);
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
a2.addEdge("ab", b2);
Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "name", "a3");
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal().V().local(__.optional(__.out()).optional(__.out())).path();
List<Path> paths = traversal.toList();
Assert.assertEquals(3, traversal.getSteps().size());
Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgLocalStepBarrier);
SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier) traversal.getSteps().get(1);
List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
Assert.assertEquals(2, sqlgVertexSteps.size());
SqlgVertexStep sqlgVertexStep = sqlgVertexSteps.get(0);
// isForMultipleQueries is arbitary here.
// Some will be true and some false, depending on the out from which vertex.
assertStep(sqlgVertexStep, false, false, true);
sqlgVertexStep = sqlgVertexSteps.get(1);
// isForMultipleQueries is arbitary here.
// Some will be true and some false, depending on the out from which vertex.
assertStep(sqlgVertexStep, false, false, true);
Assert.assertEquals(6, paths.size());
List<Predicate<Path>> pathsToAssert = Arrays.asList(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1), p -> p.size() == 2 && p.get(0).equals(b1) && p.get(1).equals(c1), p -> p.size() == 1 && p.get(0).equals(c1), p -> p.size() == 2 && p.get(0).equals(a2) && p.get(1).equals(b2), p -> p.size() == 1 && p.get(0).equals(b2), p -> p.size() == 1 && p.get(0).equals(a3));
for (Predicate<Path> pathPredicate : pathsToAssert) {
Optional<Path> path = paths.stream().filter(pathPredicate).findAny();
Assert.assertTrue(path.isPresent());
Assert.assertTrue(paths.remove(path.get()));
}
Assert.assertTrue(paths.isEmpty());
}
use of org.umlg.sqlg.step.SqlgVertexStep in project sqlg by pietermartin.
the class TestLocalVertexStepOptionalWithOrder method testOptionalWithOrderAndRange.
@Test
public void testOptionalWithOrderAndRange() {
Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a");
Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "aa");
Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "name", "aaa");
Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "d");
Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "c");
Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "b");
Vertex bb1 = this.sqlgGraph.addVertex(T.label, "BB", "name", "g");
Vertex bb2 = this.sqlgGraph.addVertex(T.label, "BB", "name", "f");
Vertex bb3 = this.sqlgGraph.addVertex(T.label, "BB", "name", "e");
Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "h");
Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "name", "i");
Vertex c3 = this.sqlgGraph.addVertex(T.label, "C", "name", "j");
Vertex cc1 = this.sqlgGraph.addVertex(T.label, "CC", "name", "k");
Vertex cc2 = this.sqlgGraph.addVertex(T.label, "CC", "name", "l");
Vertex cc3 = this.sqlgGraph.addVertex(T.label, "CC", "name", "m");
a1.addEdge("ab", b1);
a1.addEdge("ab", b2);
a1.addEdge("ab", b3);
a1.addEdge("abb", bb1);
a1.addEdge("abb", bb2);
a1.addEdge("abb", bb3);
b1.addEdge("bc", c1);
b1.addEdge("bc", c2);
b1.addEdge("bc", c3);
b2.addEdge("bcc", cc1);
b2.addEdge("bcc", cc2);
b2.addEdge("bcc", cc3);
this.sqlgGraph.tx().commit();
DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal().V().hasLabel("A").local(__.optional(__.out().order().by("name").optional(__.out().order().by("name", Order.decr).range(2, 3)))).path();
Assert.assertEquals(4, traversal.getSteps().size());
List<Path> paths = traversal.toList();
Assert.assertEquals(3, traversal.getSteps().size());
Assert.assertTrue(traversal.getSteps().get(1) instanceof LocalStep);
LocalStep<?, ?> localStep = (LocalStep<?, ?>) traversal.getSteps().get(1);
List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
Assert.assertEquals(1, sqlgVertexSteps.size());
SqlgVertexStep sqlgVertexStep = sqlgVertexSteps.get(0);
assertStep(sqlgVertexStep, false, true, true, true);
Assert.assertEquals(7, paths.size());
// all the paths of length 2 and 3 must be sorted
List<Path> pathsOfLength3 = paths.stream().filter(p -> p.size() == 3).collect(Collectors.toList());
Assert.assertEquals(1, pathsOfLength3.size());
Vertex v = (Vertex) pathsOfLength3.get(0).objects().get(2);
Assert.assertEquals("k", v.value("name"));
List<Path> pathsOfLength2 = paths.stream().filter(p -> p.size() == 2).collect(Collectors.toList());
Assert.assertEquals(4, pathsOfLength2.size());
v = (Vertex) pathsOfLength2.get(0).objects().get(1);
Assert.assertEquals("b", v.value("name"));
v = (Vertex) pathsOfLength2.get(1).objects().get(1);
Assert.assertEquals("e", v.value("name"));
v = (Vertex) pathsOfLength2.get(2).objects().get(1);
Assert.assertEquals("f", v.value("name"));
v = (Vertex) pathsOfLength2.get(3).objects().get(1);
Assert.assertEquals("g", v.value("name"));
List<Path> pathsOfLength1 = paths.stream().filter(p -> p.size() == 1).collect(Collectors.toList());
Assert.assertEquals(2, pathsOfLength1.size());
}
Aggregations