Search in sources :

Example 11 with EdgeStep

use of com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep 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)

Example 12 with EdgeStep

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

the class PathsAPI 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.sources, "The sources of request can't be null");
    E.checkArgumentNotNull(request.targets, "The targets of request can't be null");
    E.checkArgumentNotNull(request.step, "The step of request can't be null");
    E.checkArgument(request.depth > 0 && request.depth <= DEFAULT_MAX_DEPTH, "The depth of request must be in (0, %s], " + "but got: %s", DEFAULT_MAX_DEPTH, request.depth);
    LOG.debug("Graph [{}] get paths from source vertices '{}', target " + "vertices '{}', with step '{}', max depth '{}', " + "capacity '{}', limit '{}' and with_vertex '{}'", graph, request.sources, request.targets, request.step, request.depth, request.capacity, request.limit, request.withVertex);
    HugeGraph g = graph(manager, graph);
    Iterator<Vertex> sources = request.sources.vertices(g);
    Iterator<Vertex> targets = request.targets.vertices(g);
    EdgeStep step = step(g, request.step);
    CollectionPathsTraverser traverser = new CollectionPathsTraverser(g);
    Collection<HugeTraverser.Path> paths;
    paths = traverser.paths(sources, targets, step, request.depth, request.nearest, request.capacity, request.limit);
    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) CollectionPathsTraverser(com.baidu.hugegraph.traversal.algorithm.CollectionPathsTraverser) HugeTraverser(com.baidu.hugegraph.traversal.algorithm.HugeTraverser) Id(com.baidu.hugegraph.backend.id.Id) 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)

Example 13 with EdgeStep

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

the class CountAPI method post.

@POST
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String post(@Context GraphManager manager, @PathParam("graph") String graph, CountRequest request) {
    LOG.debug("Graph [{}] get count from '{}' with request {}", graph, request);
    E.checkArgumentNotNull(request.source, "The source of request can't be null");
    Id sourceId = HugeVertex.getIdValue(request.source);
    E.checkArgumentNotNull(request.steps != null && !request.steps.isEmpty(), "The steps of request can't be null or empty");
    E.checkArgumentNotNull(request.dedupSize == NO_LIMIT || request.dedupSize >= 0L, "The dedup size of request " + "must >= 0 or == -1, but got: '%s'", request.dedupSize);
    HugeGraph g = graph(manager, graph);
    List<EdgeStep> steps = steps(g, request);
    CountTraverser traverser = new CountTraverser(g);
    long count = traverser.count(sourceId, steps, request.containsTraversed, request.dedupSize);
    return manager.serializer(g).writeMap(ImmutableMap.of("count", count));
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) Id(com.baidu.hugegraph.backend.id.Id) CountTraverser(com.baidu.hugegraph.traversal.algorithm.CountTraverser) POST(jakarta.ws.rs.POST) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 14 with EdgeStep

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

the class PredictionTraverser method adamicAdar.

public double adamicAdar(Id source, Id target, Directions dir, String label, long degree, long limit) {
    Set<Id> neighbors = checkAndGetCommonNeighbors(source, target, dir, label, degree, limit);
    EdgeStep step = label == null ? new EdgeStep(graph(), dir) : new EdgeStep(graph(), dir, ImmutableList.of(label));
    double sum = 0.0;
    for (Id vid : neighbors) {
        long currentDegree = this.edgesCount(vid, step);
        if (currentDegree > 0) {
            sum += 1.0 / Math.log(currentDegree);
        }
    }
    return sum;
}
Also used : EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) Id(com.baidu.hugegraph.backend.id.Id)

Example 15 with EdgeStep

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

the class PathTraverser method forward.

public void forward() {
    EdgeStep currentStep = this.nextStep(true);
    if (currentStep == null) {
        return;
    }
    this.beforeTraverse(true);
    // Traversal vertices of previous level
    this.traverseOneLayer(this.sources, currentStep, this::forward);
    this.afterTraverse(currentStep, true);
}
Also used : EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep)

Aggregations

EdgeStep (com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep)15 Id (com.baidu.hugegraph.backend.id.Id)12 HugeGraph (com.baidu.hugegraph.HugeGraph)10 Timed (com.codahale.metrics.annotation.Timed)6 POST (jakarta.ws.rs.POST)6 Produces (jakarta.ws.rs.Produces)6 Consumes (jakarta.ws.rs.Consumes)5 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)4 HugeTraverser (com.baidu.hugegraph.traversal.algorithm.HugeTraverser)4 E (com.baidu.hugegraph.util.E)4 HashSet (java.util.HashSet)4 Iterator (java.util.Iterator)4 Set (java.util.Set)4 Edge (org.apache.tinkerpop.gremlin.structure.Edge)4 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)4 Directions (com.baidu.hugegraph.type.define.Directions)3 HugeException (com.baidu.hugegraph.HugeException)2 QueryResults (com.baidu.hugegraph.backend.query.QueryResults)2 FilterIterator (com.baidu.hugegraph.iterator.FilterIterator)2 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)2