Search in sources :

Example 1 with JaccardSimilarTraverser

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

the class JaccardSimilarityAPI 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.vertex, "The source vertex of request can't be null");
    E.checkArgument(request.step != null, "The steps of request can't be null");
    E.checkArgument(request.top >= 0, "The top must be >= 0, but got: %s", request.top);
    LOG.debug("Graph [{}] get jaccard similars from source vertex '{}', " + "with step '{}', top '{}' and capacity '{}'", graph, request.vertex, request.step, request.top, request.capacity);
    HugeGraph g = graph(manager, graph);
    Id sourceId = HugeVertex.getIdValue(request.vertex);
    EdgeStep step = step(g, request.step);
    Map<Id, Double> results;
    try (JaccardSimilarTraverser traverser = new JaccardSimilarTraverser(g)) {
        results = traverser.jaccardSimilars(sourceId, step, request.top, request.capacity);
    }
    return manager.serializer(g).writeMap(results);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) Id(com.baidu.hugegraph.backend.id.Id) JaccardSimilarTraverser(com.baidu.hugegraph.traversal.algorithm.JaccardSimilarTraverser) POST(jakarta.ws.rs.POST) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 2 with JaccardSimilarTraverser

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

the class JaccardSimilarityAPI method get.

@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String get(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("vertex") String vertex, @QueryParam("other") String other, @QueryParam("direction") String direction, @QueryParam("label") String edgeLabel, @QueryParam("max_degree") @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree) {
    LOG.debug("Graph [{}] get jaccard similarity between '{}' and '{}' " + "with direction {}, edge label {} and max degree '{}'", graph, vertex, other, direction, edgeLabel, maxDegree);
    Id sourceId = VertexAPI.checkAndParseVertexId(vertex);
    Id targetId = VertexAPI.checkAndParseVertexId(other);
    Directions dir = Directions.convert(EdgeAPI.parseDirection(direction));
    HugeGraph g = graph(manager, graph);
    double similarity;
    try (JaccardSimilarTraverser traverser = new JaccardSimilarTraverser(g)) {
        similarity = traverser.jaccardSimilarity(sourceId, targetId, dir, edgeLabel, maxDegree);
    }
    return JsonUtil.toJson(ImmutableMap.of("jaccard_similarity", similarity));
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) Directions(com.baidu.hugegraph.type.define.Directions) Id(com.baidu.hugegraph.backend.id.Id) JaccardSimilarTraverser(com.baidu.hugegraph.traversal.algorithm.JaccardSimilarTraverser) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(jakarta.ws.rs.GET)

Aggregations

HugeGraph (com.baidu.hugegraph.HugeGraph)2 Id (com.baidu.hugegraph.backend.id.Id)2 JaccardSimilarTraverser (com.baidu.hugegraph.traversal.algorithm.JaccardSimilarTraverser)2 Timed (com.codahale.metrics.annotation.Timed)2 Produces (jakarta.ws.rs.Produces)2 EdgeStep (com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep)1 Directions (com.baidu.hugegraph.type.define.Directions)1 Consumes (jakarta.ws.rs.Consumes)1 GET (jakarta.ws.rs.GET)1 POST (jakarta.ws.rs.POST)1