Search in sources :

Example 6 with EdgeStep

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

the class HugeTraverser method edgesOfVertex.

private Iterator<Edge> edgesOfVertex(Id source, EdgeStep edgeStep, boolean mustAllSK) {
    Id[] edgeLabels = edgeStep.edgeLabels();
    Query query = GraphTransaction.constructEdgesQuery(source, edgeStep.direction(), edgeLabels);
    ConditionQuery filter = null;
    if (mustAllSK) {
        this.fillFilterBySortKeys(query, edgeLabels, edgeStep.properties());
    } else {
        filter = (ConditionQuery) query.copy();
        this.fillFilterByProperties(filter, edgeStep.properties());
    }
    query.capacity(Query.NO_CAPACITY);
    if (edgeStep.limit() != NO_LIMIT) {
        query.limit(edgeStep.limit());
    }
    Iterator<Edge> edges = this.graph().edges(query);
    if (filter != null) {
        ConditionQuery finalFilter = filter;
        edges = new FilterIterator<>(edges, (e) -> {
            return finalFilter.test((HugeEdge) e);
        });
    }
    return edgeStep.skipSuperNodeIfNeeded(edges);
}
Also used : QueryResults(com.baidu.hugegraph.backend.query.QueryResults) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) CollectionUtil(com.baidu.hugegraph.util.CollectionUtil) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) ImmutableList(com.google.common.collect.ImmutableList) CollectionUtils(org.apache.commons.collections.CollectionUtils) HugeGraph(com.baidu.hugegraph.HugeGraph) Map(java.util.Map) Query(com.baidu.hugegraph.backend.query.Query) CollectionFactory(com.baidu.hugegraph.util.collection.CollectionFactory) E(com.baidu.hugegraph.util.E) CoreOptions(com.baidu.hugegraph.config.CoreOptions) Edge(org.apache.tinkerpop.gremlin.structure.Edge) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) CollectionType(com.baidu.hugegraph.type.define.CollectionType) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) InsertionOrderUtil(com.baidu.hugegraph.util.InsertionOrderUtil) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched) Objects(java.util.Objects) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) HugeException(com.baidu.hugegraph.HugeException) List(java.util.List) Log(com.baidu.hugegraph.util.Log) Directions(com.baidu.hugegraph.type.define.Directions) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) Id(com.baidu.hugegraph.backend.id.Id) TraversalUtil(com.baidu.hugegraph.traversal.optimize.TraversalUtil) Collections(java.util.Collections) HugeType(com.baidu.hugegraph.type.HugeType) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Id(com.baidu.hugegraph.backend.id.Id) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 7 with EdgeStep

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

the class KneighborTraverser method customizedKneighbor.

public KneighborRecords customizedKneighbor(Id source, EdgeStep step, int maxDepth, long limit) {
    E.checkNotNull(source, "source vertex id");
    this.checkVertexExist(source, "source vertex");
    checkPositive(maxDepth, "k-neighbor max_depth");
    checkLimit(limit);
    boolean concurrent = maxDepth >= this.concurrentDepth();
    KneighborRecords records = new KneighborRecords(concurrent, source, true);
    Consumer<Id> consumer = v -> {
        if (this.reachLimit(limit, records.size())) {
            return;
        }
        Iterator<Edge> edges = edgesOfVertex(v, step);
        while (!this.reachLimit(limit, records.size()) && edges.hasNext()) {
            Id target = ((HugeEdge) edges.next()).id().otherVertexId();
            records.addPath(v, target);
        }
    };
    while (maxDepth-- > 0) {
        records.startOneLayer(true);
        traverseIds(records.keys(), consumer, concurrent);
        records.finishOneLayer();
    }
    return records;
}
Also used : Consumer(java.util.function.Consumer) Iterator(java.util.Iterator) KneighborRecords(com.baidu.hugegraph.traversal.algorithm.records.KneighborRecords) Directions(com.baidu.hugegraph.type.define.Directions) HugeGraph(com.baidu.hugegraph.HugeGraph) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Id(com.baidu.hugegraph.backend.id.Id) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) Set(java.util.Set) E(com.baidu.hugegraph.util.E) Edge(org.apache.tinkerpop.gremlin.structure.Edge) KneighborRecords(com.baidu.hugegraph.traversal.algorithm.records.KneighborRecords) Iterator(java.util.Iterator) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Id(com.baidu.hugegraph.backend.id.Id)

Example 8 with EdgeStep

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

the class PredictionTraverser method resourceAllocation.

public double resourceAllocation(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 / currentDegree;
        }
    }
    return sum;
}
Also used : EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) Id(com.baidu.hugegraph.backend.id.Id)

Example 9 with EdgeStep

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

the class CountAPI method steps.

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

Example 10 with EdgeStep

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

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