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