Search in sources :

Example 1 with CCResult

use of apoc.result.CCResult in project neo4j-apoc-procedures by neo4j-contrib.

the class WeaklyConnectedComponents method wcc.

@Deprecated
@Procedure("apoc.algo.wcc")
@Description("CALL apoc.algo.wcc() YIELD number of weakly connected components")
public Stream<CCResult> wcc() {
    List<List<CCVar>> results = new LinkedList<List<CCVar>>();
    ResourceIterator<Node> nodes = db.getAllNodes().iterator();
    PrimitiveLongSet allNodes = Primitive.longSet(0);
    while (nodes.hasNext()) {
        Node node = nodes.next();
        if (node.getDegree() == 0) {
            List<CCVar> result = new LinkedList<CCVar>();
            result.add(new CCVar(node.getId() + "", node.getLabels().iterator().next().name()));
            results.add(result);
        } else {
            allNodes.add(node.getId());
        }
    }
    nodes.close();
    PrimitiveLongIterator it = allNodes.iterator();
    while (it.hasNext()) {
        try {
            long n = it.next();
            List<CCVar> result = new LinkedList<CCVar>();
            PrimitiveLongIterator reachableIDs = go(db.getNodeById(n), Direction.BOTH, result).iterator();
            while (reachableIDs.hasNext()) {
                long id = (long) reachableIDs.next();
                allNodes.remove(id);
            }
            results.add(result);
        } catch (NoSuchElementException e) {
            break;
        }
        it = allNodes.iterator();
    }
    allNodes.close();
    return results.stream().map((x) -> new CCResult(x.stream().map((z) -> new Long(z.getId())).collect(Collectors.toList()), x.stream().collect(Collectors.groupingBy(CCVar::getType)).entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().size()))));
}
Also used : CCVar(apoc.algo.wcc.CCVar) Primitive(org.neo4j.collection.primitive.Primitive) java.util(java.util) Log(org.neo4j.logging.Log) Context(org.neo4j.procedure.Context) Description(org.neo4j.procedure.Description) Collectors(java.util.stream.Collectors) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) Stream(java.util.stream.Stream) org.neo4j.graphdb(org.neo4j.graphdb) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) CCResult(apoc.result.CCResult) Procedure(org.neo4j.procedure.Procedure) CCVar(apoc.algo.wcc.CCVar) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) CCResult(apoc.result.CCResult) Description(org.neo4j.procedure.Description) Procedure(org.neo4j.procedure.Procedure)

Aggregations

CCVar (apoc.algo.wcc.CCVar)1 CCResult (apoc.result.CCResult)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Primitive (org.neo4j.collection.primitive.Primitive)1 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)1 PrimitiveLongSet (org.neo4j.collection.primitive.PrimitiveLongSet)1 org.neo4j.graphdb (org.neo4j.graphdb)1 Log (org.neo4j.logging.Log)1 Context (org.neo4j.procedure.Context)1 Description (org.neo4j.procedure.Description)1 Procedure (org.neo4j.procedure.Procedure)1