Search in sources :

Example 1 with WeightedEdgeStep

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

the class CustomizePathsTraverser method customizedPaths.

public List<Path> customizedPaths(Iterator<Vertex> vertices, List<WeightedEdgeStep> steps, boolean sorted, long capacity, long limit) {
    E.checkArgument(vertices.hasNext(), "The source vertices can't be empty");
    E.checkArgument(!steps.isEmpty(), "The steps can't be empty");
    checkCapacity(capacity);
    checkLimit(limit);
    MultivaluedMap<Id, Node> sources = newMultivalueMap();
    while (vertices.hasNext()) {
        HugeVertex vertex = (HugeVertex) vertices.next();
        Node node = sorted ? new WeightNode(vertex.id(), null, 0) : new Node(vertex.id(), null);
        sources.add(vertex.id(), node);
    }
    int stepNum = steps.size();
    int pathCount = 0;
    long access = 0;
    MultivaluedMap<Id, Node> newVertices = null;
    root: for (WeightedEdgeStep step : steps) {
        stepNum--;
        newVertices = newMultivalueMap();
        Iterator<Edge> edges;
        // Traversal vertices of previous level
        for (Map.Entry<Id, List<Node>> entry : sources.entrySet()) {
            List<Node> adjacency = newList();
            edges = this.edgesOfVertex(entry.getKey(), step.step());
            while (edges.hasNext()) {
                HugeEdge edge = (HugeEdge) edges.next();
                Id target = edge.id().otherVertexId();
                for (Node n : entry.getValue()) {
                    // If have loop, skip target
                    if (n.contains(target)) {
                        continue;
                    }
                    Node newNode;
                    if (sorted) {
                        double w = step.weightBy() != null ? edge.value(step.weightBy().name()) : step.defaultWeight();
                        newNode = new WeightNode(target, n, w);
                    } else {
                        newNode = new Node(target, n);
                    }
                    adjacency.add(newNode);
                    checkCapacity(capacity, ++access, "customized paths");
                }
            }
            if (step.sample() > 0) {
                // Sample current node's adjacent nodes
                adjacency = sample(adjacency, step.sample());
            }
            // Add current node's adjacent nodes
            for (Node node : adjacency) {
                newVertices.add(node.id(), node);
                // Avoid exceeding limit
                if (stepNum == 0) {
                    if (limit != NO_LIMIT && !sorted && ++pathCount >= limit) {
                        break root;
                    }
                }
            }
        }
        // Re-init sources
        sources = newVertices;
    }
    if (stepNum != 0) {
        return ImmutableList.of();
    }
    List<Path> paths = newList();
    for (List<Node> nodes : newVertices.values()) {
        for (Node n : nodes) {
            if (sorted) {
                WeightNode wn = (WeightNode) n;
                paths.add(new WeightPath(wn.path(), wn.weights()));
            } else {
                paths.add(new Path(n.path()));
            }
        }
    }
    return paths;
}
Also used : HugeEdge(com.baidu.hugegraph.structure.HugeEdge) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) WeightedEdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.WeightedEdgeStep) Iterator(java.util.Iterator) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Id(com.baidu.hugegraph.backend.id.Id)

Example 2 with WeightedEdgeStep

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

the class CustomizedPathsAPI method step.

private static List<WeightedEdgeStep> step(HugeGraph graph, PathRequest req) {
    int stepSize = req.steps.size();
    List<WeightedEdgeStep> steps = new ArrayList<>(stepSize);
    for (Step step : req.steps) {
        steps.add(step.jsonToStep(graph));
    }
    return steps;
}
Also used : ArrayList(java.util.ArrayList) WeightedEdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.WeightedEdgeStep) WeightedEdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.WeightedEdgeStep)

Example 3 with WeightedEdgeStep

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

the class CustomizedPathsAPI method post.

@POST
@Timed
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String post(@Context GraphManager manager, @PathParam("graph") String graph, PathRequest request) {
    E.checkArgumentNotNull(request, "The path request body can't be null");
    E.checkArgumentNotNull(request.sources, "The sources of path request can't be null");
    E.checkArgument(request.steps != null && !request.steps.isEmpty(), "The steps of path request can't be empty");
    if (request.sortBy == null) {
        request.sortBy = SortBy.NONE;
    }
    LOG.debug("Graph [{}] get customized paths from source vertex '{}', " + "with steps '{}', sort by '{}', capacity '{}', limit '{}' " + "and with_vertex '{}'", graph, request.sources, request.steps, request.sortBy, request.capacity, request.limit, request.withVertex);
    HugeGraph g = graph(manager, graph);
    Iterator<Vertex> sources = request.sources.vertices(g);
    List<WeightedEdgeStep> steps = step(g, request);
    boolean sorted = request.sortBy != SortBy.NONE;
    CustomizePathsTraverser traverser = new CustomizePathsTraverser(g);
    List<HugeTraverser.Path> paths;
    paths = traverser.customizedPaths(sources, steps, sorted, request.capacity, request.limit);
    if (sorted) {
        boolean incr = request.sortBy == SortBy.INCR;
        paths = CustomizePathsTraverser.topNPath(paths, incr, 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) CustomizePathsTraverser(com.baidu.hugegraph.traversal.algorithm.CustomizePathsTraverser) HugeGraph(com.baidu.hugegraph.HugeGraph) HugeTraverser(com.baidu.hugegraph.traversal.algorithm.HugeTraverser) WeightedEdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.WeightedEdgeStep) 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)

Aggregations

WeightedEdgeStep (com.baidu.hugegraph.traversal.algorithm.steps.WeightedEdgeStep)3 Id (com.baidu.hugegraph.backend.id.Id)2 HugeGraph (com.baidu.hugegraph.HugeGraph)1 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)1 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)1 CustomizePathsTraverser (com.baidu.hugegraph.traversal.algorithm.CustomizePathsTraverser)1 HugeTraverser (com.baidu.hugegraph.traversal.algorithm.HugeTraverser)1 Timed (com.codahale.metrics.annotation.Timed)1 ImmutableList (com.google.common.collect.ImmutableList)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 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1