Search in sources :

Example 1 with MultiNodeShortestPathTraverser

use of com.baidu.hugegraph.traversal.algorithm.MultiNodeShortestPathTraverser in project incubator-hugegraph by apache.

the class MultiNodeShortestPathAPI method post.

@POST
@Timed
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String post(@Context GraphManager manager, @PathParam("graph") String graph, Request request) {
    E.checkArgumentNotNull(request, "The request body can't be null");
    E.checkArgumentNotNull(request.vertices, "The vertices of request can't be null");
    E.checkArgument(request.step != null, "The steps of request can't be null");
    LOG.debug("Graph [{}] get multiple node shortest path from " + "vertices '{}', with step '{}', max_depth '{}', capacity " + "'{}' and with_vertex '{}'", graph, request.vertices, request.step, request.maxDepth, request.capacity, request.withVertex);
    HugeGraph g = graph(manager, graph);
    Iterator<Vertex> vertices = request.vertices.vertices(g);
    EdgeStep step = step(g, request.step);
    List<HugeTraverser.Path> paths;
    try (MultiNodeShortestPathTraverser traverser = new MultiNodeShortestPathTraverser(g)) {
        paths = traverser.multiNodeShortestPath(vertices, step, request.maxDepth, request.capacity);
    }
    if (!request.withVertex) {
        return manager.serializer(g).writePaths("paths", paths, false);
    }
    Set<Id> ids = new HashSet<>();
    for (HugeTraverser.Path p : paths) {
        ids.addAll(p.vertices());
    }
    Iterator<Vertex> iter = QueryResults.emptyIterator();
    if (!ids.isEmpty()) {
        iter = g.vertices(ids.toArray());
    }
    return manager.serializer(g).writePaths("paths", paths, false, iter);
}
Also used : Path(jakarta.ws.rs.Path) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) HugeTraverser(com.baidu.hugegraph.traversal.algorithm.HugeTraverser) Id(com.baidu.hugegraph.backend.id.Id) MultiNodeShortestPathTraverser(com.baidu.hugegraph.traversal.algorithm.MultiNodeShortestPathTraverser) HashSet(java.util.HashSet) POST(jakarta.ws.rs.POST) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

HugeGraph (com.baidu.hugegraph.HugeGraph)1 Id (com.baidu.hugegraph.backend.id.Id)1 HugeTraverser (com.baidu.hugegraph.traversal.algorithm.HugeTraverser)1 MultiNodeShortestPathTraverser (com.baidu.hugegraph.traversal.algorithm.MultiNodeShortestPathTraverser)1 EdgeStep (com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep)1 Timed (com.codahale.metrics.annotation.Timed)1 Consumes (jakarta.ws.rs.Consumes)1 POST (jakarta.ws.rs.POST)1 Path (jakarta.ws.rs.Path)1 Produces (jakarta.ws.rs.Produces)1 HashSet (java.util.HashSet)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1