forked from orbisgis/java-network-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphAnalyzerTest.java
More file actions
391 lines (366 loc) · 12.4 KB
/
GraphAnalyzerTest.java
File metadata and controls
391 lines (366 loc) · 12.4 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* Java Network Analyzer provides a collection of graph theory and social
* network analysis algorithms implemented on mathematical graphs using the
* <a href="http://www.jgrapht.org/">JGraphT</a> library.
*
* Java Network Analyzer is developed by the GIS group of the DECIDE team of the
* Lab-STICC CNRS laboratory, see <http://www.lab-sticc.fr/>.
* It is part of the OrbisGIS tool ecosystem.
*
* The GIS group of the DECIDE team is located at :
*
* Laboratoire Lab-STICC – CNRS UMR 6285
* Equipe DECIDE
* UNIVERSITÉ DE BRETAGNE-SUD
* Institut Universitaire de Technologie de Vannes
* 8, Rue Montaigne - BP 561 56017 Vannes Cedex
*
* Java Network Analyzer is distributed under LGPL 3 license.
*
* Copyright (C) 2012-2014 CNRS (IRSTV CNRS FR 2488)
* Copyright (C) 2015-2018 CNRS (Lab-STICC CNRS UMR 6285)
*
* Java Network Analyzer is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Java Network Analyzer 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* Java Network Analyzer. If not, see <http://www.gnu.org/licenses/>.
*
* For more information, please consult: <http://www.orbisgis.org/>
* or contact directly:
* info_at_ orbisgis.org
*/
package org.javanetworkanalyzer.analyzers;
import org.javanetworkanalyzer.data.VCent;
import org.javanetworkanalyzer.data.VUCent;
import org.javanetworkanalyzer.data.VWCent;
import org.javanetworkanalyzer.model.*;
import org.javanetworkanalyzer.graphcreators.GraphCreator;
import org.javanetworkanalyzer.graphcreators.WeightedGraphCreator;
import org.javanetworkanalyzer.progress.ProgressMonitor;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Tests weighted and unweighted graph analysis.
*
* @author Adam Gouge
*/
public abstract class GraphAnalyzerTest
extends CentralityTest {
private static final String WEIGHTED = "Weighted";
private static final String UNWEIGHTED = "Unweighted";
private static final String DIRECTED = "Directed";
private static final String REVERSED = "Reversed";
private static final String UNDIRECTED = "Undirected";
/**
* A logger.
*/
private static final Logger LOGGER =
LoggerFactory.getLogger(GraphAnalyzerTest.class);
/**
* Does unweighted graph analysis on the given graph.
*
* @param graph The graph.
*
* @param orientation The orientation.
*
* @return The graph.
*/
protected KeyedGraph<VUCent, EdgeCent> doUnweightedAnalysis(
KeyedGraph<VUCent, EdgeCent> graph,
String orientation) {
try {
doAnalysis(new UnweightedGraphAnalyzer(graph, progressMonitor()),
UNWEIGHTED + " " + orientation);
} catch (Exception ex) {
}
if (printsResults()) {
printResults(graph);
System.out.println("");
}
return graph;
}
/**
* Does weighted graph analysis on the given graph.
*
* @param graph The graph.
*
* @param orientation The orientation.
*
* @return The graph.
*/
protected WeightedKeyedGraph<VWCent, EdgeCent> doWeightedAnalysis(
WeightedKeyedGraph<VWCent, EdgeCent> graph,
String orientation) {
try {
doAnalysis(new WeightedGraphAnalyzer(graph, progressMonitor()),
WEIGHTED + " " + orientation);
} catch (Exception ex) {
}
if (printsResults()) {
printResults(graph);
System.out.println("");
}
return graph;
}
/**
* Executes {@link GraphAnalyzer#computeAll()}.
*
* @param analyzer The {@link GraphAnalyzer}.
* @param analysisType Describes the type of analysis.
*/
private void doAnalysis(
GraphAnalyzer<?, ?, ?> analyzer,
String analysisType) {
long start = System.currentTimeMillis();
try {
analyzer.computeAll();
} catch (Exception ex) {
}
printTime(System.currentTimeMillis() - start, analysisType);
}
/**
* Does unweighted graph analysis on the directed graph loaded by
* {@link GraphAnalyzerTest#unweightedGraph(int)}.
*
* @return The graph.
*/
public DirectedG<VUCent, EdgeCent> unweightedDirectedAnalysis() {
try {
return (DirectedG) doUnweightedAnalysis(unweightedGraph(
GraphCreator.DIRECTED), DIRECTED);
} catch (Exception ex) {
}
return null;
}
/**
* Does unweighted graph analysis on the edge reversed graph loaded by
* {@link GraphAnalyzerTest#unweightedGraph(int)}.
*
* @return The graph.
*/
public DirectedG<VUCent, EdgeCent> unweightedReversedAnalysis() {
try {
return (DirectedG) doUnweightedAnalysis(unweightedGraph(
GraphCreator.REVERSED), REVERSED);
} catch (Exception ex) {
}
return null;
}
/**
* Does unweighted graph analysis on the undirected graph loaded by
* {@link GraphAnalyzerTest#unweightedGraph(int)}.
*
* @return The graph.
*/
public UndirectedG<VUCent, EdgeCent> unweightedUndirectedAnalysis() {
try {
return (UndirectedG) doUnweightedAnalysis(unweightedGraph(
GraphCreator.UNDIRECTED), UNDIRECTED);
} catch (Exception ex) {
}
return null;
}
/**
* Does weighted graph analysis on the directed graph loaded by
* {@link GraphAnalyzerTest#weightedGraph(int)}.
*
* @return The graph.
*/
public DirectedG<VWCent, EdgeCent> weightedDirectedAnalysis() {
try {
return (DirectedG) doWeightedAnalysis(weightedGraph(
GraphCreator.DIRECTED), DIRECTED);
} catch (Exception ex) {
}
return null;
}
/**
* Does weighted graph analysis on the edge reversed graph loaded by
* {@link GraphAnalyzerTest#weightedGraph(int)}.
*
* @return The graph.
*/
public DirectedG<VWCent, EdgeCent> weightedReversedAnalysis() {
try {
return (DirectedG) doWeightedAnalysis(weightedGraph(
GraphCreator.REVERSED), REVERSED);
} catch (Exception ex) {
}
return null;
}
/**
* Does weighted graph analysis on the undirected graph loaded by
* {@link GraphAnalyzerTest#weightedGraph(int)}.
*
* @return The graph.
*/
public UndirectedG<VWCent, EdgeCent> weightedUndirectedAnalysis() {
try {
return (UndirectedG) doWeightedAnalysis(weightedGraph(
GraphCreator.UNDIRECTED), UNDIRECTED);
} catch (Exception ex) {
}
return null;
}
/**
* Does unweighted analysis on an unweighted graph (loaded by
* {@link GraphAnalyzerTest#unweightedGraph(int)} and weighted analysis on a
* weighted graph WITH ALL WEIGHTS EQUAL TO 1.0 (loaded by
* {@link GraphAnalyzerTest#weightedGraph(int)}) and makes sure the results
* are the same.
*/
// TODO: Implement this method.
@Test
public void undirectedComparison() {
// int orientation = GraphCreator.UNDIRECTED;
//
// Map<UnweightedNodeBetweennessInfo, Edge> unweightedResults =
// doUnweightedAnalysis(unweightedGraph(orientation));
// Map<WeightedNodeBetweennessInfo, Ege> weightedResults =
// doWeightedAnalysis(weightedGraph(orientation));
}
/**
* Loads an unweighted graph with the given orientation from
* {@link GraphAnalyzerTest#getFilename()}.
*
* @param orientation The orientation.
*
* @return The graph.
*
*/
protected KeyedGraph<VUCent, EdgeCent> unweightedGraph(
int orientation) {
try {
return new GraphCreator(getFilename(),
orientation,
VUCent.class,
EdgeCent.class).loadGraph();
} catch (Exception ex) {
}
return null;
}
/**
* Loads a weighted graph with the given orientation from
* {@link GraphAnalyzerTest#getFilename()}.
*
* @param orientation The orientation.
*
* @return The graph.
*
*/
protected WeightedKeyedGraph<VWCent, EdgeCent> weightedGraph(
int orientation) {
try {
return new WeightedGraphCreator(getFilename(),
orientation,
VWCent.class,
EdgeCent.class,
getWeightColumnName()).loadGraph();
} catch (Exception ex) {
}
return null;
}
/**
* Checks the betweenness values of the given vertices against the given
* expected betweenness values.
*
* @param graph The graph
* @param expectedBetweenness The expected betweenness values
*/
protected void checkBetweenness(
double[] expectedBetweenness,
KeyedGraph<? extends VCent, ? extends DefaultWeightedEdge> graph) {
if (graph != null) {
VCent vertex;
for (int i = 0; i < getNumberOfNodes(); i++) {
vertex = graph.getVertex(i + 1);
if (vertex != null) {
assertEquals(expectedBetweenness[i],
vertex.getBetweenness(),
TOLERANCE);
} else {
throw new IllegalStateException("Null vertex " + (i + 1));
}
}
} else {
throw new IllegalStateException("Null graph");
}
}
/**
* Checks the closeness values of the given vertices against the given
* expected closeness values.
*
* @param graph The graph
* @param expectedCloseness The expected closeness values
*/
protected void checkCloseness(
double[] expectedCloseness,
KeyedGraph<? extends VCent, ? extends DefaultWeightedEdge> graph) {
if (graph != null) {
VCent vertex;
for (int i = 0; i < getNumberOfNodes(); i++) {
vertex = graph.getVertex(i + 1);
if (vertex != null) {
assertEquals(expectedCloseness[i],
vertex.getCloseness(),
TOLERANCE);
} else {
throw new IllegalStateException("Null vertex " + (i + 1));
}
}
} else {
throw new IllegalStateException("Null graph");
}
}
/**
* Returns a boolean indicating whether the results should be printed.
*
* @return A boolean indicating whether the results should be printed.
*/
protected abstract boolean printsResults();
/**
* Returns a {@link ProgressMonitor} to be used during graph analysis.
*
* @return
*/
protected abstract ProgressMonitor progressMonitor();
/**
* Returns the filename from which the graph edges should be loaded.
*
* @return The filename from which the graph edges should be loaded.
*/
protected abstract String getFilename();
/**
* Returns the weight column name.
*
* @return The weight column name.
*/
protected abstract String getWeightColumnName();
/**
* Returns the number of nodes in this graph.
*
* @return The number of nodes in this graph.
*/
protected abstract int getNumberOfNodes();
/**
* Prints the amount of time graph analysis took.
*
* @param time The time.
* @param analysisType analysis type
*/
protected void printTime(double time, String analysisType) {
LOGGER.info("({} ms) {} {} Graph Analysis",
time, getName(), analysisType);
}
}