|
| 1 | +/** |
| 2 | + * Copyright (c) 2017 "Neo4j, Inc." <http://neo4j.com> |
| 3 | + * |
| 4 | + * This file is part of Neo4j Graph Algorithms <http://github.com/neo4j-contrib/neo4j-graph-algorithms>. |
| 5 | + * |
| 6 | + * Neo4j Graph Algorithms is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +package org.neo4j.graphalgo; |
| 20 | + |
| 21 | +import org.neo4j.graphalgo.api.Graph; |
| 22 | +import org.neo4j.graphalgo.core.GraphLoader; |
| 23 | +import org.neo4j.graphalgo.core.ProcedureConfiguration; |
| 24 | +import org.neo4j.graphalgo.core.utils.Pools; |
| 25 | +import org.neo4j.graphalgo.core.utils.ProgressLogger; |
| 26 | +import org.neo4j.graphalgo.core.utils.ProgressTimer; |
| 27 | +import org.neo4j.graphalgo.core.utils.TerminationFlag; |
| 28 | +import org.neo4j.graphalgo.core.utils.paged.AllocationTracker; |
| 29 | +import org.neo4j.graphalgo.core.write.Exporter; |
| 30 | +import org.neo4j.graphalgo.impl.harmonic.HarmonicCentrality; |
| 31 | +import org.neo4j.graphalgo.impl.DangalchevClosenessCentrality; |
| 32 | +import org.neo4j.graphalgo.results.CentralityProcResult; |
| 33 | +import org.neo4j.graphdb.Direction; |
| 34 | +import org.neo4j.kernel.api.KernelTransaction; |
| 35 | +import org.neo4j.kernel.internal.GraphDatabaseAPI; |
| 36 | +import org.neo4j.logging.Log; |
| 37 | +import org.neo4j.procedure.*; |
| 38 | + |
| 39 | +import java.util.Map; |
| 40 | +import java.util.stream.Stream; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author mknblch |
| 44 | + */ |
| 45 | +public class DangalchevCentralityProc { |
| 46 | + |
| 47 | + public static final String DEFAULT_TARGET_PROPERTY = "centrality"; |
| 48 | + |
| 49 | + @Context |
| 50 | + public GraphDatabaseAPI api; |
| 51 | + |
| 52 | + @Context |
| 53 | + public Log log; |
| 54 | + |
| 55 | + @Context |
| 56 | + public KernelTransaction transaction; |
| 57 | + |
| 58 | + @Procedure(value = "algo.dangalchev.stream") |
| 59 | + @Description("CALL algo.dangalchev.stream(label:String, relationship:String{concurrency:4}) YIELD nodeId, centrality - yields centrality for each node") |
| 60 | + public Stream<DangalchevClosenessCentrality.Result> dangalchevStream( |
| 61 | + @Name(value = "label", defaultValue = "") String label, |
| 62 | + @Name(value = "relationship", defaultValue = "") String relationship, |
| 63 | + @Name(value = "config", defaultValue = "{}") Map<String, Object> config) { |
| 64 | + |
| 65 | + final ProcedureConfiguration configuration = ProcedureConfiguration.create(config) |
| 66 | + .overrideNodeLabelOrQuery(label) |
| 67 | + .overrideRelationshipTypeOrQuery(relationship); |
| 68 | + |
| 69 | + final AllocationTracker tracker = AllocationTracker.create(); |
| 70 | + |
| 71 | + final Graph graph = new GraphLoader(api, Pools.DEFAULT) |
| 72 | + .init(log, configuration.getNodeLabelOrQuery(), configuration.getRelationshipOrQuery(), configuration) |
| 73 | + .withoutNodeProperties() |
| 74 | + .withConcurrency(configuration.getConcurrency()) |
| 75 | + .withAllocationTracker(tracker) |
| 76 | + .asUndirected(true) |
| 77 | + .load(configuration.getGraphImpl("huge")); |
| 78 | + |
| 79 | + final DangalchevClosenessCentrality algo = new DangalchevClosenessCentrality(graph, configuration.getConcurrency(), Pools.DEFAULT) |
| 80 | + .withProgressLogger(ProgressLogger.wrap(log, "DangalchevCentrality")) |
| 81 | + .withTerminationFlag(TerminationFlag.wrap(transaction)) |
| 82 | + .compute(); |
| 83 | + |
| 84 | + graph.release(); |
| 85 | + |
| 86 | + return algo.resultStream(); |
| 87 | + } |
| 88 | + |
| 89 | + @Procedure(value = "algo.dangalchev", mode = Mode.WRITE) |
| 90 | + @Description("CALL algo.dangalchev(label:String, relationship:String, {write:true, writeProperty:'centrality, concurrency:4'}) YIELD " + |
| 91 | + "loadMillis, computeMillis, writeMillis, nodes] - yields evaluation details") |
| 92 | + public Stream<CentralityProcResult> dangalchev( |
| 93 | + @Name(value = "label", defaultValue = "") String label, |
| 94 | + @Name(value = "relationship", defaultValue = "") String relationship, |
| 95 | + @Name(value = "config", defaultValue = "{}") Map<String, Object> config) { |
| 96 | + |
| 97 | + final ProcedureConfiguration configuration = ProcedureConfiguration.create(config) |
| 98 | + .overrideNodeLabelOrQuery(label) |
| 99 | + .overrideRelationshipTypeOrQuery(relationship); |
| 100 | + |
| 101 | + final CentralityProcResult.Builder builder = CentralityProcResult.builder(); |
| 102 | + |
| 103 | + final AllocationTracker tracker = AllocationTracker.create(); |
| 104 | + final int concurrency = configuration.getConcurrency(); |
| 105 | + final TerminationFlag terminationFlag = TerminationFlag.wrap(transaction); |
| 106 | + |
| 107 | + final Graph graph; |
| 108 | + try (ProgressTimer timer = builder.timeLoad()) { |
| 109 | + graph = new GraphLoader(api, Pools.DEFAULT) |
| 110 | + .init(log, configuration.getNodeLabelOrQuery(), configuration.getRelationshipOrQuery(), configuration) |
| 111 | + .withoutNodeProperties() |
| 112 | + .withConcurrency(concurrency) |
| 113 | + .withAllocationTracker(tracker) |
| 114 | + .asUndirected(true) |
| 115 | + .load(configuration.getGraphImpl("huge")); |
| 116 | + } |
| 117 | + |
| 118 | + builder.withNodeCount(graph.nodeCount()); |
| 119 | + |
| 120 | + final DangalchevClosenessCentrality algo = new DangalchevClosenessCentrality(graph, concurrency, Pools.DEFAULT) |
| 121 | + .withProgressLogger(ProgressLogger.wrap(log, "DangalchevCentrality")) |
| 122 | + .withTerminationFlag(TerminationFlag.wrap(transaction)); |
| 123 | + |
| 124 | + builder.timeEval(algo::compute); |
| 125 | + |
| 126 | + if (configuration.isWriteFlag()) { |
| 127 | + graph.release(); |
| 128 | + final String writeProperty = configuration.getWriteProperty(DEFAULT_TARGET_PROPERTY); |
| 129 | + builder.timeWrite(() -> { |
| 130 | + Exporter exporter = Exporter.of(api, graph) |
| 131 | + .withLog(log) |
| 132 | + .parallel(Pools.DEFAULT, concurrency, terminationFlag) |
| 133 | + .build(); |
| 134 | + algo.export(writeProperty, exporter); |
| 135 | + }); |
| 136 | + algo.release(); |
| 137 | + } |
| 138 | + |
| 139 | + return Stream.of(builder.build()); |
| 140 | + } |
| 141 | +} |
0 commit comments