forked from CAIDA/walrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
H3GraphLoader.java
371 lines (314 loc) · 9.92 KB
/
H3GraphLoader.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
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
//
// The Walrus Graph Visualization Tool.
// Copyright (C) 2000,2001,2002 The Regents of the University of California.
//
// This program 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// ######END_HEADER######
//
import java.io.*;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import org.caida.libsea.*;
public class H3GraphLoader
{
///////////////////////////////////////////////////////////////////////
// PUBLIC INTERFACES
///////////////////////////////////////////////////////////////////////
public interface AttributeTypeMatcher
{
boolean match(ValueType type);
}
////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
////////////////////////////////////////////////////////////////////////
public H3GraphLoader() {}
////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
////////////////////////////////////////////////////////////////////////
public H3Graph load(Graph graph, String spanningTree)
throws InvalidGraphDataException
{
int numNodes = graph.getNumNodes();
int numLinks = graph.getNumLinks();
H3Graph retval = new H3Graph(numNodes, numLinks);
IDMap map = populateNodeIDs(retval, graph);
findSpanningTreeQualifierAttributes(graph, spanningTree);
int rootID = findSpanningTreeRootNodeID(graph, m_rootAttribute);
retval.setRootNode(map.map(rootID));
populateLinks(retval, graph, map, m_treeLinkAttribute);
return retval;
}
// Returns List<String>.
public List loadSpanningTreeQualifiers(Graph graph)
{
List<String> retval = new ArrayList<String>();
QualifierIterator iterator =
graph.getQualifiersByType(SPANNING_TREE_QUALIFIER);
while (!iterator.atEnd())
{
retval.add(iterator.getName());
iterator.advance();
}
Collections.sort(retval);
return retval;
}
// Returns List<String>.
public List loadAttributes(Graph graph, AttributeTypeMatcher matcher)
{
List<String> retval = new ArrayList<String>();
AttributeDefinitionIterator iterator = graph.getAttributeDefinitions();
while (!iterator.atEnd())
{
if (matcher.match(iterator.getType()))
{
retval.add(iterator.getName());
}
iterator.advance();
}
Collections.sort(retval);
return retval;
}
////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
////////////////////////////////////////////////////////////////////////
private void findSpanningTreeQualifierAttributes
(Graph graph, String spanningTree)
throws InvalidGraphDataException
{
QualifierIterator qualifierIterator = graph.getQualifier(spanningTree);
if (qualifierIterator.atEnd())
{
String msg =
"spanning tree qualifier `" + spanningTree + "' not found";
throw new IllegalArgumentException(msg);
}
boolean foundRootAttribute = false;
boolean foundTreeLinkAttribute = false;
QualifierAttributeIterator qualifierAttributeIterator =
qualifierIterator.getAttributes();
while (!qualifierAttributeIterator.atEnd())
{
String name = qualifierAttributeIterator.getName();
if (name.equals(ROOT_ATTRIBUTE))
{
foundRootAttribute = true;
m_rootAttribute =
qualifierAttributeIterator.getAttributeID();
checkAttributeType
(graph, SPANNING_TREE_QUALIFIER, ROOT_ATTRIBUTE,
m_rootAttribute, ValueType.BOOLEAN);
}
else if (name.equals(TREE_LINK_ATTRIBUTE))
{
foundTreeLinkAttribute = true;
m_treeLinkAttribute =
qualifierAttributeIterator.getAttributeID();
checkAttributeType
(graph, SPANNING_TREE_QUALIFIER, TREE_LINK_ATTRIBUTE,
m_treeLinkAttribute, ValueType.BOOLEAN);
}
qualifierAttributeIterator.advance();
}
if (!foundRootAttribute)
{
String msg = "missing attribute `" + ROOT_ATTRIBUTE
+ "' of qualifier type `" + SPANNING_TREE_QUALIFIER + "'";
throw new InvalidGraphDataException(msg);
}
if (!foundTreeLinkAttribute)
{
String msg = "missing attribute `" + TREE_LINK_ATTRIBUTE
+ "' of qualifier type `" + SPANNING_TREE_QUALIFIER + "'";
throw new InvalidGraphDataException(msg);
}
}
///////////////////////////////////////////////////////////////////////
private int findSpanningTreeRootNodeID(Graph graph, int attribute)
throws InvalidGraphDataException
{
AttributesByAttributeIterator iterator =
graph.getAttributeDefinition(attribute).getNodeAttributes();
while (!iterator.atEnd()
&& !iterator.getAttributeValues().getBooleanValue())
{
iterator.advance();
}
if (iterator.atEnd())
{
String msg = "no root node found for spanning tree";
throw new InvalidGraphDataException(msg);
}
return iterator.getObjectID();
}
///////////////////////////////////////////////////////////////////////
private void checkAttributeType
(Graph graph, String qualifierType, String attributeName,
int attribute, ValueType type)
throws InvalidGraphDataException
{
AttributeDefinitionIterator iterator =
graph.getAttributeDefinition(attribute);
if (iterator.getType() != type)
{
String msg = "attribute `" + attributeName
+ "' of qualifier type `" + qualifierType
+ "' must have type " + type.getName()
+ "; found " + iterator.getType().getName();
throw new InvalidGraphDataException(msg);
}
}
///////////////////////////////////////////////////////////////////////
private void populateLinks(H3Graph retval, Graph graph, IDMap map,
int treeLinkAttribute)
{
BitSet treeLinksMap = createTreeLinksMap(graph, treeLinkAttribute);
NodeIterator nodeIterator = graph.getNodes();
while (!nodeIterator.atEnd())
{
int node = map.map(nodeIterator.getObjectID());
retval.startChildLinks(node);
{
LinkIterator linkIterator =
nodeIterator.getOutgoingLinks();
while (!linkIterator.atEnd())
{
int link = linkIterator.getObjectID();
if (treeLinksMap.get(link))
{
int destination =
map.map(linkIterator.getDestination());
retval.addChildLink(node, destination, link);
}
linkIterator.advance();
}
}
retval.startNontreeLinks(node);
{
LinkIterator linkIterator =
nodeIterator.getOutgoingLinks();
while (!linkIterator.atEnd())
{
int link = linkIterator.getObjectID();
if (!treeLinksMap.get(link))
{
int destination =
map.map(linkIterator.getDestination());
retval.addNontreeLink(node, destination, link);
}
linkIterator.advance();
}
}
retval.endNodeLinks(node);
nodeIterator.advance();
}
}
///////////////////////////////////////////////////////////////////////
private BitSet createTreeLinksMap(Graph graph, int treeLinkAttribute)
{
BitSet retval = new BitSet(graph.getLinkIDRange());
AttributesByAttributeIterator iterator =
graph.getAttributeDefinition(treeLinkAttribute)
.getLinkAttributes();
while (!iterator.atEnd())
{
if (iterator.getAttributeValues().getBooleanValue())
{
retval.set(iterator.getObjectID());
}
iterator.advance();
}
return retval;
}
///////////////////////////////////////////////////////////////////////
private IDMap populateNodeIDs(H3Graph retval, Graph graph)
{
int[] mapping = extractSortedNodeIDs(graph);
for (int i = 0; i < mapping.length; i++)
{
retval.setNodeID(i, mapping[i]);
}
return new IDMap(mapping);
}
private int[] extractSortedNodeIDs(Graph graph)
{
int numNodes = graph.getNumNodes();
int[] retval = new int[numNodes];
boolean isSorted = true;
int previousID = -1; // Must be less than all valid IDs.
int i = 0;
NodeIterator iterator = graph.getNodes();
while (!iterator.atEnd())
{
int id = iterator.getObjectID();
retval[i++] = id;
isSorted = isSorted && (previousID <= id);
previousID = id;
iterator.advance();
}
if (!isSorted)
{
Arrays.sort(retval);
}
return retval;
}
////////////////////////////////////////////////////////////////////////
// PRIVATE FIELDS
////////////////////////////////////////////////////////////////////////
private static final boolean DEBUG_PRINT = true;
private static final String SPANNING_TREE_QUALIFIER = "spanning_tree";
private static final String ROOT_ATTRIBUTE = "root";
private static final String TREE_LINK_ATTRIBUTE = "tree_link";
private int m_rootAttribute;
private int m_treeLinkAttribute;
////////////////////////////////////////////////////////////////////////
// PUBLIC CLASSES
////////////////////////////////////////////////////////////////////////
public static class InvalidGraphDataException extends Exception
{
public InvalidGraphDataException()
{
super();
}
public InvalidGraphDataException(String s)
{
super(s);
}
}
////////////////////////////////////////////////////////////////////////
// PRIVATE CLASSES
////////////////////////////////////////////////////////////////////////
private static class IDMap
{
public IDMap(int[] mapping)
{
m_mapping = mapping;
}
public int map(int id)
{
int retval = Arrays.binarySearch(m_mapping, id);
if (retval < 0)
{
String msg = "id[" + id + "] not found";
throw new RuntimeException(msg);
}
return retval;
}
private int[] m_mapping;
}
}