org.apache.tinkerpop.gremlin.process.traversal.Path Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.Path. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ShortestPathTestHelper.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public Path makePath(final boolean includeEdges, final String... names) {
    Path path = ImmutablePath.make();
    boolean first = true;
    for (final String name : names) {
        final Vertex vertex = vertexCache.computeIfAbsent(name, test::convertToVertex);
        if (!first) {
            if (includeEdges) {
                final Object id1 = ((Vertex) path.get(path.size() - 1)).id();
                final Object id2 = vertex.id();
                final Edge edge;
                if (edgeCache.containsKey(id1)) {
                    edge = edgeCache.get(id1).computeIfAbsent(id2, id -> getEdge(id1, id));
                } else if (edgeCache.containsKey(id2)) {
                    edge = edgeCache.get(id2).computeIfAbsent(id1, id -> getEdge(id, id2));
                } else {
                    edgeCache.put(id1, new HashMap<>());
                    edgeCache.get(id1).put(id2, edge = getEdge(id1, id2));
                }
                path = path.extend(edge, Collections.emptySet());
            }
        }
        path = path.extend(vertex, Collections.emptySet());
        first = false;
    }
    return path;
}
 
Example #2
Source File: MatchStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private boolean hasMatched(final ConnectiveStep.Connective connective, final Traverser.Admin<S> traverser) {
    int counter = 0;
    boolean matched = false;
    for (final Traversal.Admin<Object, Object> matchTraversal : this.matchTraversals) {
        if (traverser.getTags().contains(matchTraversal.getStartStep().getId())) {
            if (connective == ConnectiveStep.Connective.OR) {
                matched = true;
                break;
            }
            counter++;
        }
    }
    if (!matched)
        matched = this.matchTraversals.size() == counter;
    if (matched && this.dedupLabels != null) {
        final Path path = traverser.path();
        final List<Object> objects = new ArrayList<>(this.dedupLabels.size());
        for (final String label : this.dedupLabels) {
            objects.add(path.get(Pop.last, label));
        }
        this.dedups.add(objects);
    }
    return matched;
}
 
Example #3
Source File: MatchStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
protected Traverser.Admin<Object> processNextStart() throws NoSuchElementException {
    if (null == this.parent)
        this.parent = ((MatchStep) this.getTraversal().getParent().asStep());

    while (true) {
        final Traverser.Admin traverser = this.starts.next();
        // no end label
        if (null == this.matchKey) {
            // if (this.traverserStepIdAndLabelsSetByChild) -- traverser equality is based on stepId, lets ensure they are all at the parent
            traverser.setStepId(this.parent.getId());
            this.parent.getMatchAlgorithm().recordEnd(traverser, this.getTraversal());
            return this.retractUnnecessaryLabels(traverser);
        }
        // TODO: sideEffect check?
        // path check
        final Path path = traverser.path();
        if (!path.hasLabel(this.matchKey) || traverser.get().equals(path.get(Pop.last, this.matchKey))) {
            // if (this.traverserStepIdAndLabelsSetByChild) -- traverser equality is based on stepId and thus, lets ensure they are all at the parent
            traverser.setStepId(this.parent.getId());
            traverser.addLabels(this.matchKeyCollection);
            this.parent.getMatchAlgorithm().recordEnd(traverser, this.getTraversal());
            return this.retractUnnecessaryLabels(traverser);
        }
    }
}
 
Example #4
Source File: MutablePath.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public boolean equals(final Object other) {
    if (!(other instanceof Path))
        return false;
    final Path otherPath = (Path) other;
    if (otherPath.size() != this.objects.size())
        return false;

    final List<Object> otherPathObjects = otherPath.objects();
    final List<Set<String>> otherPathLabels = otherPath.labels();
    for (int i = this.objects.size() - 1; i >= 0; i--) {
        if (!this.objects.get(i).equals(otherPathObjects.get(i)))
            return false;
        if (!this.labels.get(i).equals(otherPathLabels.get(i)))
            return false;
    }
    return true;
}
 
Example #5
Source File: NetAggregateTest.java    From sqlg with MIT License 6 votes vote down vote up
@Test
    public void test2() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
//        List<Vertex> vertices = this.sqlgGraph.traversal().V().hasLabel("Transmission").toList();
        List<Path> vertices = this.sqlgGraph.traversal()
                .V()
                .hasLabel("Transmission")
                .repeat(__.both("link").simplePath())
                .until(
                        __.or(
                                __.has("type", P.within("HubSite", "ASwitch", "BSwitch")),
                                __.has("excluded", true),
                                __.loops().is(12)
                        )
                )
                .path()
                .toList();
        System.out.println(vertices.size());
        stopWatch.stop();
        System.out.println(stopWatch.toString());
    }
 
Example #6
Source File: ShortestPathVertexProgramTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldFindDirectedShortestPathsWithEdgesIncluded() throws Exception {
    final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()).
            program(ShortestPathVertexProgram.build().edgeTraversal(__.outE()).includeEdges(true).create(graph)).submit().get();
    assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS));
    final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS);
    final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS)
            .filter(p -> (p[0].equals("marko") && !p[p.length - 1].equals("peter"))
                    || (p[0].equals("vadas") && p.length == 1)
                    || (p[0].equals("lop") && p.length == 1)
                    || (p[0].equals("josh") && Arrays.asList("lop", "josh", "ripple").contains(p[p.length - 1]))
                    || (p[0].equals("ripple") && p.length == 1)
                    || (p[0].equals("peter") && Arrays.asList("lop", "peter").contains(p[p.length - 1])))
            .map(p -> helper.makePath(true, p)).collect(Collectors.toList());
    helper.checkResults(expected, shortestPaths);
}
 
Example #7
Source File: TestRepeatStepVertexOut.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOnDuplicatePathsFromVertexTimesAfter() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    a1.addEdge("ab", b1);
    b1.addEdge("ba", a2);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal()
            .V(a1, a2).emit().repeat(__.out("ab", "ba")).times(3).path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertEquals(4, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a1)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a2)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(a2)).findAny().get());
    Assert.assertTrue(paths.isEmpty());
}
 
Example #8
Source File: PathTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_VX1X_outEXcreatedX_inV_inE_outV_path() {
    final Traversal<Vertex, Path> traversal = get_g_VX1X_outEXcreatedX_inV_inE_outV_path(convertToVertexId("marko"));
    printTraversalForm(traversal);
    final List<Path> paths = traversal.toList();
    assertEquals(3, paths.size());
    for (final Path path : paths) {
        assertEquals(5, path.size());
        assertEquals(convertToVertexId("marko"), ((Vertex) path.get(0)).id());
        assertEquals(convertToEdgeId("marko", "created", "lop"), ((Edge) path.get(1)).id());
        assertEquals(convertToVertexId("lop"), ((Vertex) path.get(2)).id());
        assertEquals("created", ((Edge) path.get(3)).label());
        assertTrue(convertToVertexId("josh").equals(((Vertex) path.get(4)).id()) ||
                convertToVertexId("peter").equals(((Vertex) path.get(4)).id()) ||
                convertToVertexId("marko").equals(((Vertex) path.get(4)).id()));
    }
    assertFalse(traversal.hasNext());
}
 
Example #9
Source File: ImmutablePath.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Path retract(final Set<String> labels) {
    if (labels.isEmpty())
        return this;

    // get all the immutable path sections
    final List<ImmutablePath> immutablePaths = new ArrayList<>();
    ImmutablePath currentPath = this;
    while (true) {
        if (currentPath.isTail())
            break;
        immutablePaths.add(0, currentPath);
        currentPath = currentPath.previousPath;
    }
    // build a new immutable path using the respective path sections that are not to be retracted
    Path newPath = TAIL_PATH;
    for (final ImmutablePath immutablePath : immutablePaths) {
        final Set<String> temp = new LinkedHashSet<>(immutablePath.currentLabels);
        temp.removeAll(labels);
        if (!temp.isEmpty())
            newPath = newPath.extend(immutablePath.currentObject, temp);
    }
    return newPath;
}
 
Example #10
Source File: TestLocalVertexStepRepeatStep.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOnDuplicatePathsFromVertexTimes2After() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    a1.addEdge("ab", b1);
    b1.addEdge("ba", a2);
    this.sqlgGraph.tx().commit();
    List<Path> paths = this.sqlgGraph.traversal().V().local(__.<Vertex>emit().repeat(__.out("ab", "ba")).times(2).path()).toList();
    assertEquals(6, paths.size());
    assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a1)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a1)).findAny().orElseThrow(IllegalStateException::new));
    assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a2)).findAny().orElseThrow(IllegalStateException::new));
    assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(b1)).findAny().orElseThrow(IllegalStateException::new));
    assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)).findAny().orElseThrow(IllegalStateException::new));
    assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(b1) && p.get(1).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(b1) && p.get(1).equals(a2)).findAny().orElseThrow(IllegalStateException::new));
    assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(a2)).findAny().orElseThrow(IllegalStateException::new));
    assertTrue(paths.isEmpty());
}
 
Example #11
Source File: ShortestPathVertexProgramTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldFindInDirectedShortestPaths() throws Exception {
    final List<ShortestPathVertexProgram> programs = Arrays.asList(
            ShortestPathVertexProgram.build().edgeTraversal(__.inE()).create(graph),
            ShortestPathVertexProgram.build().edgeDirection(Direction.IN).create(graph));
    for (final ShortestPathVertexProgram program : programs) {
        final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()).
                program(program).submit().get();
        assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS));
        final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS);
        final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS)
                .filter(p -> (p[0].equals("marko") && p.length == 1)
                        || (p[0].equals("vadas") && Arrays.asList("marko", "vadas").contains(p[p.length - 1]))
                        || (p[0].equals("lop") && Arrays.asList("marko", "lop", "josh", "peter").contains(p[p.length - 1]))
                        || (p[0].equals("josh") && Arrays.asList("marko", "josh").contains(p[p.length - 1]))
                        || (p[0].equals("ripple") && Arrays.asList("marko", "josh", "ripple").contains(p[p.length - 1]))
                        || (p[0].equals("peter") && p.length == 1))
                .map(helper::makePath).collect(Collectors.toList());
        helper.checkResults(expected, shortestPaths);
    }
}
 
Example #12
Source File: TestGremlinOptional.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOptionalWithNestedOptionalAndRepeat() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "halo");
    a1.addEdge("ab", b1);
    b1.addEdge("bc", c1);
    this.sqlgGraph.tx().commit();

    GraphTraversal<Vertex, Path> gt = this.sqlgGraph.traversal().V().hasLabel("A")
            .out("ab").as("b")
            .optional(
                    __.outE("bc").otherV().as("c")
                            .optional(
                                    __.repeat(__.out("cd")).times(3)
                            )
            )
            .path();

    List<Path> paths = gt.toList();
    Assert.assertEquals(1, paths.size());
}
 
Example #13
Source File: ShortestPathVertexProgram.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private void processEdges(final Vertex vertex, final Path currentPath, final Number currentDistance,
                          final Messenger<Triplet<Path, Edge, Number>> messenger) {

    final Traversal.Admin<Vertex, Edge> edgeTraversal = this.edgeTraversal.getPure();
    edgeTraversal.addStart(edgeTraversal.getTraverserGenerator().generate(vertex, edgeTraversal.getStartStep(), 1));

    while (edgeTraversal.hasNext()) {
        final Edge edge = edgeTraversal.next();
        final Number distance = getDistance(edge);

        Vertex otherV = edge.inVertex();
        if (otherV.equals(vertex))
            otherV = edge.outVertex();

        // only send message if the adjacent vertex is not yet part of the current path
        if (!currentPath.objects().contains(otherV)) {
            messenger.sendMessage(MessageScope.Global.of(otherV),
                    Triplet.with(currentPath, this.includeEdges ? edge : null,
                            NumberHelper.add(currentDistance, distance)));
        }
    }
}
 
Example #14
Source File: TestGremlinOptional.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOptionalOutNotThere() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    b1.addEdge("knows", a1);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal()
            .V(a1)
            .optional(
                    __.out("knows")
            )
            .path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertEquals(1, paths.size());
}
 
Example #15
Source File: TestLocalVertexStepRepeatStep.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testRepeatWithTimesBefore() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    a1.addEdge("ab", b1);
    this.sqlgGraph.tx().commit();
    List<Path> paths = this.sqlgGraph.traversal().V().local(__.<Vertex>emit().times(2).repeat(__.out()).path()).toList();
    assertEquals(3, paths.size());
    assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a1)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a1)).findAny().orElseThrow(IllegalStateException::new));
    assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)).findAny().orElseThrow(IllegalStateException::new));
    assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(b1)).findAny().orElseThrow(IllegalStateException::new));
    assertTrue(paths.isEmpty());
}
 
Example #16
Source File: TestReplacedStepEmitComparator.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOrderFollowedByVertexStep() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "name", "a3");
    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");
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    a2.addEdge("ab", b1);
    a2.addEdge("ab", b2);
    a2.addEdge("ab", b3);
    a3.addEdge("ab", b1);
    a3.addEdge("ab", b2);
    a3.addEdge("ab", b3);
    this.sqlgGraph.tx().commit();

    List<Path> paths = this.sqlgGraph.traversal()
            .V().hasLabel("A").order().by("name", Order.decr)
            .out("ab")
            .path()
            .toList();
    Assert.assertEquals(9, paths.size());
    Assert.assertEquals(a3, paths.get(0).objects().get(0));
    Assert.assertEquals(a3, paths.get(1).objects().get(0));
    Assert.assertEquals(a3, paths.get(2).objects().get(0));
    Assert.assertEquals(a2, paths.get(3).objects().get(0));
    Assert.assertEquals(a2, paths.get(4).objects().get(0));
    Assert.assertEquals(a2, paths.get(5).objects().get(0));
    Assert.assertEquals(a1, paths.get(6).objects().get(0));
    Assert.assertEquals(a1, paths.get(7).objects().get(0));
    Assert.assertEquals(a1, paths.get(8).objects().get(0));
}
 
Example #17
Source File: MutablePath.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public <A> A get(final Pop pop, final String label) {
    if (Pop.mixed == pop) {
        return this.get(label);
    } else if (Pop.all == pop) {
        if (this.hasLabel(label)) {
            final Object object = this.get(label);
            if (object instanceof List)
                return (A) object;
            else
                return (A) Collections.singletonList(object);
        } else {
            return (A) Collections.emptyList();
        }
    } else {
        // Override default to avoid building temporary list, and to stop looking when we find the label.
        if (Pop.last == pop) {
            for (int i = this.labels.size() - 1; i >= 0; i--) {
                if (labels.get(i).contains(label))
                    return (A) objects.get(i);
            }
        } else {
            for (int i = 0; i != this.labels.size(); i++) {
                if (labels.get(i).contains(label))
                    return (A) objects.get(i);
            }
        }
        throw Path.Exceptions.stepWithProvidedLabelDoesNotExist(label);
    }
}
 
Example #18
Source File: GraphSONSerializersV2d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(final Path path, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
        throws IOException, JsonGenerationException {
    jsonGenerator.writeStartObject();

    // paths shouldn't serialize with properties if the path contains graph elements
    final Path p = DetachedFactory.detach(path, false);
    jsonGenerator.writeObjectField(GraphSONTokens.LABELS, p.labels());
    jsonGenerator.writeObjectField(GraphSONTokens.OBJECTS, p.objects());

    jsonGenerator.writeEndObject();
}
 
Example #19
Source File: GraphSONSerializersV1d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static void ser(final Path path, final JsonGenerator jsonGenerator, final TypeSerializer typeSerializer)
        throws IOException {
    jsonGenerator.writeStartObject();
    if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName());
    jsonGenerator.writeObjectField(GraphSONTokens.LABELS, path.labels());
    jsonGenerator.writeObjectField(GraphSONTokens.OBJECTS, path.objects());
    jsonGenerator.writeEndObject();
}
 
Example #20
Source File: ShortestPathVertexProgramTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldFindShortestPathsWithStartVertexFilter() throws Exception {
    final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()).
            program(ShortestPathVertexProgram.build().source(__.has("name", "marko")).create(graph)).submit().get();
    assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS));
    final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS);
    final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS)
            .filter(p -> p[0].equals("marko")).map(helper::makePath).collect(Collectors.toList());
    helper.checkResults(expected, shortestPaths);
}
 
Example #21
Source File: TestRepeatStepVertexOut.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testEmitTimes2MultiplePaths() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
    a1.addEdge("aa", a2);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    a2.addEdge("ab", b1);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal()
            .V(a1, a2, b1).emit().times(2).repeat(__.out()).path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertEquals(6, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a1)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(a2)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(a2) && p.get(2).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(a2) && p.get(2).equals(b1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a2)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a2) && p.get(1).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a2) && p.get(1).equals(b1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(b1)).findAny().get());
    Assert.assertTrue(paths.isEmpty());
}
 
Example #22
Source File: LoopsTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_VX1X_repeatXboth_simplePathX_untilXhasXname_peterX_or_loops_isX2XX_hasXname_peterX_path_byXnameX() {
    final Traversal<Vertex, Path> traversal = get_g_VX1X_repeatXboth_simplePathX_untilXhasXname_peterX_or_loops_isX2XX_hasXname_peterX_path_byXnameX(convertToVertexId("marko"));
    printTraversalForm(traversal);
    assertTrue(traversal.hasNext());
    final Path path = traversal.next();
    assertEquals(3, path.size());
    assertEquals("marko", path.get(0));
    assertEquals("lop", path.get(1));
    assertEquals("peter", path.get(2));
    assertFalse(traversal.hasNext());
}
 
Example #23
Source File: CyclicPathTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_VX1X_asXaX_outXcreatedX_asXbX_inXcreatedX_asXcX_cyclicPath_fromXaX_toXbX_path() {
    final Traversal<Vertex, Path> traversal = get_g_VX1X_asXaX_outXcreatedX_asXbX_inXcreatedX_asXcX_cyclicPath_fromXaX_toXbX_path(convertToVertexId("marko"));
    printTraversalForm(traversal);
    assertFalse(traversal.hasNext());
}
 
Example #24
Source File: MutablePath.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Path retract(final Set<String> removeLabels) {
    for (int i = this.labels.size() - 1; i >= 0; i--) {
        this.labels.get(i).removeAll(removeLabels);
        if (this.labels.get(i).isEmpty()) {
            this.labels.remove(i);
            this.objects.remove(i);
        }
    }
    return this;
}
 
Example #25
Source File: RepeatTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_emit_repeatXoutX_timesX2X_path() {
    final Traversal<Vertex, Path> traversal = get_g_V_emit_repeatXoutX_timesX2X_path();
    printTraversalForm(traversal);
    assertPath(traversal);
}
 
Example #26
Source File: TestMultipleLabels.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testSameElementHasMultipleLabels() {
    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", "b2");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    b1.addEdge("bc", c1);
    b2.addEdge("bc", c1);
    b3.addEdge("bc", c1);
    this.sqlgGraph.tx().commit();

    List<Path> paths = this.sqlgGraph.traversal()
            .V(a1.id()).as("a", "b")
            .out("ab").as("a", "b")
            .out("bc")
            .path()
            .toList();

    List<Vertex> vertices = this.sqlgGraph.traversal()
            .V(a1.id())
            .out("ab")
            .out("bc")
            .toList();
    for (Vertex v: vertices) {
        System.out.println(v);
    }

}
 
Example #27
Source File: TestRepeatStepVertexOut.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOnDuplicatePathsFromVertexTimes1After() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    a1.addEdge("ab", b1);
    b1.addEdge("ba", a2);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal()
            .V(a1, a2, b1).emit().repeat(__.out("ab", "ba")).times(2).path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());

    Assert.assertEquals(6, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a1)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(a2)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a2)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(b1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(b1) && p.get(1).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(b1) && p.get(1).equals(a2)).findAny().get());
    Assert.assertTrue(paths.isEmpty());
}
 
Example #28
Source File: ShortestPathTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private void checkOutDirectedPaths(final boolean includeEdges, final Traversal<Vertex, Path> traversal) {
    final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS)
            .filter(p -> (p[0].equals("marko") && !p[p.length - 1].equals("peter"))
                    || (p[0].equals("vadas") && p.length == 1)
                    || (p[0].equals("lop") && p.length == 1)
                    || (p[0].equals("josh") && Arrays.asList("lop", "josh", "ripple").contains(p[p.length - 1]))
                    || (p[0].equals("ripple") && p.length == 1)
                    || (p[0].equals("peter") && Arrays.asList("lop", "peter").contains(p[p.length - 1])))
            .map(names -> helper.makePath(includeEdges, names)).collect(Collectors.toList());
    checkResults(expected, traversal);
}
 
Example #29
Source File: AbstractTraverser.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public T attach(final Function<Attachable<T>, T> method) {
    // you do not want to attach a path because it will reference graph objects not at the current vertex
    if (this.t instanceof Attachable && !(((Attachable) this.t).get() instanceof Path))
        this.t = ((Attachable<T>) this.t).attach(method);
    return this.t;
}
 
Example #30
Source File: SimplePathTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_repeatXboth_simplePathX_timesX3X_path() {
    final Traversal<Vertex, Path> traversal = get_g_V_repeatXboth_simplePathX_timesX3X_path();
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        assertTrue(traversal.next().isSimple());
    }
    assertEquals(18, counter);
    assertFalse(traversal.hasNext());
}