|
1
|
|
package baseCode.dataStructure.graph;
|
|
2
|
|
|
|
3
|
|
import java.util.Iterator;
|
|
4
|
|
import java.util.LinkedHashSet;
|
|
5
|
|
import java.util.Set;
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
public class DirectedGraphNode extends AbstractGraphNode implements Comparable {
|
|
16
|
|
|
|
17
|
|
protected Set parents;
|
|
18
|
|
|
|
19
|
|
protected Set children;
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
protected int topoSortOrder = 0;
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
117
|
public DirectedGraphNode( Object key, Object value, Graph graph ) {
|
|
30
|
117
|
super( key, value, graph );
|
|
31
|
117
|
parents = new LinkedHashSet();
|
|
32
|
117
|
children = new LinkedHashSet();
|
|
33
|
|
}
|
|
34
|
|
|
|
35
|
|
|
|
36
|
|
|
|
37
|
|
|
|
38
|
93
|
public void setTopoSortOrder( int i ) {
|
|
39
|
93
|
topoSortOrder = i;
|
|
40
|
|
}
|
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
|
44
|
|
|
|
45
|
359
|
public int getTopoSortOrder() {
|
|
46
|
359
|
return topoSortOrder;
|
|
47
|
|
}
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
126
|
public void addChild( Object newChildKey ) {
|
|
53
|
126
|
children.add( newChildKey );
|
|
54
|
|
}
|
|
55
|
|
|
|
56
|
|
|
|
57
|
|
|
|
58
|
|
|
|
59
|
127
|
public void addParent( Object newParentKey ) {
|
|
60
|
127
|
parents.add( newParentKey );
|
|
61
|
|
}
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
0
|
public Object getParentKeys() {
|
|
67
|
0
|
return parents;
|
|
68
|
|
}
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
|
73
|
0
|
public Object getChildKeys() {
|
|
74
|
0
|
return children;
|
|
75
|
|
}
|
|
76
|
|
|
|
77
|
|
|
|
78
|
|
|
|
79
|
|
|
|
80
|
|
|
|
81
|
|
|
|
82
|
169
|
public Set getChildNodes() {
|
|
83
|
169
|
Set f = new LinkedHashSet();
|
|
84
|
169
|
for ( Iterator i = this.getChildIterator(); i.hasNext(); ) {
|
|
85
|
164
|
Object k = i.next();
|
|
86
|
164
|
f.add( getGraph().get( k ) );
|
|
87
|
|
}
|
|
88
|
169
|
return f;
|
|
89
|
|
}
|
|
90
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
|
94
|
|
|
|
95
|
|
|
|
96
|
0
|
public Set getParentNodes() {
|
|
97
|
0
|
Set f = new LinkedHashSet();
|
|
98
|
0
|
for ( Iterator i = this.getParentIterator(); i.hasNext(); ) {
|
|
99
|
0
|
Object k = i.next();
|
|
100
|
0
|
f.add( getGraph().get( k ) );
|
|
101
|
|
}
|
|
102
|
0
|
return f;
|
|
103
|
|
}
|
|
104
|
|
|
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
|
|
|
110
|
1
|
public Graph getChildGraph() {
|
|
111
|
1
|
Set k = this.getAllChildNodes();
|
|
112
|
1
|
k.add( this );
|
|
113
|
|
|
|
114
|
1
|
DirectedGraph returnVal = new DirectedGraph();
|
|
115
|
1
|
for ( Iterator it = k.iterator(); it.hasNext(); ) {
|
|
116
|
4
|
DirectedGraphNode m = ( DirectedGraphNode ) it.next();
|
|
117
|
4
|
returnVal.addNode( ( DirectedGraphNode ) m.clone() );
|
|
118
|
|
}
|
|
119
|
1
|
returnVal.prune();
|
|
120
|
1
|
return returnVal;
|
|
121
|
|
}
|
|
122
|
|
|
|
123
|
|
|
|
124
|
|
|
|
125
|
|
|
|
126
|
|
|
|
127
|
|
|
|
128
|
0
|
public boolean isLeaf() {
|
|
129
|
0
|
return children.size() == 0;
|
|
130
|
|
}
|
|
131
|
|
|
|
132
|
|
|
|
133
|
|
|
|
134
|
|
|
|
135
|
0
|
public int outDegree() {
|
|
136
|
0
|
return children.size();
|
|
137
|
|
}
|
|
138
|
|
|
|
139
|
|
|
|
140
|
|
|
|
141
|
|
|
|
142
|
93
|
public int inDegree() {
|
|
143
|
93
|
return parents.size();
|
|
144
|
|
}
|
|
145
|
|
|
|
146
|
|
|
|
147
|
|
|
|
148
|
|
|
|
149
|
0
|
public int numChildren() {
|
|
150
|
0
|
return getAllChildNodes( null ).size();
|
|
151
|
|
}
|
|
152
|
|
|
|
153
|
|
|
|
154
|
|
|
|
155
|
|
|
|
156
|
0
|
public int numParents() {
|
|
157
|
0
|
return getAllParentNodes( null ).size();
|
|
158
|
|
}
|
|
159
|
|
|
|
160
|
|
|
|
161
|
|
|
|
162
|
|
|
|
163
|
1
|
public Set getAllChildNodes() {
|
|
164
|
1
|
return this.getAllChildNodes( null );
|
|
165
|
|
}
|
|
166
|
|
|
|
167
|
|
|
|
168
|
|
|
|
169
|
|
|
|
170
|
|
|
|
171
|
|
|
|
172
|
0
|
public Set getAllParentNodes() {
|
|
173
|
0
|
return this.getAllParentNodes( null );
|
|
174
|
|
}
|
|
175
|
|
|
|
176
|
|
|
|
177
|
|
|
|
178
|
|
|
|
179
|
|
|
|
180
|
|
|
|
181
|
|
|
|
182
|
0
|
public boolean hasChild( Object j ) {
|
|
183
|
0
|
return children.contains( j );
|
|
184
|
|
}
|
|
185
|
|
|
|
186
|
|
|
|
187
|
|
|
|
188
|
|
|
|
189
|
|
|
|
190
|
|
|
|
191
|
|
|
|
192
|
0
|
public boolean hasParent( Object j ) {
|
|
193
|
0
|
return parents.contains( j );
|
|
194
|
|
}
|
|
195
|
|
|
|
196
|
83
|
public String toString() {
|
|
197
|
83
|
return this.getItem().toString();
|
|
198
|
|
}
|
|
199
|
|
|
|
200
|
|
|
|
201
|
|
|
|
202
|
|
|
|
203
|
91
|
public void prune() {
|
|
204
|
91
|
for ( Iterator it = this.getChildIterator(); it.hasNext(); ) {
|
|
205
|
89
|
Object j = it.next();
|
|
206
|
89
|
DirectedGraphNode k = ( DirectedGraphNode ) getGraph().get( j );
|
|
207
|
89
|
if ( k == null ) {
|
|
208
|
0
|
if ( log.isDebugEnabled() ) {
|
|
209
|
0
|
log.debug( "Pruned child " + j + " from " + this );
|
|
210
|
|
}
|
|
211
|
0
|
children.remove( j );
|
|
212
|
|
}
|
|
213
|
|
|
|
214
|
|
}
|
|
215
|
|
|
|
216
|
91
|
for ( Iterator it = this.getParentIterator(); it.hasNext(); ) {
|
|
217
|
90
|
Object j = it.next();
|
|
218
|
90
|
DirectedGraphNode k = ( DirectedGraphNode ) getGraph().get( j );
|
|
219
|
90
|
if ( k == null ) {
|
|
220
|
1
|
if ( log.isDebugEnabled() ) {
|
|
221
|
1
|
log.debug( "Pruned parent " + j + " from " + this );
|
|
222
|
|
}
|
|
223
|
1
|
parents.remove( j );
|
|
224
|
|
}
|
|
225
|
|
|
|
226
|
|
}
|
|
227
|
|
|
|
228
|
|
}
|
|
229
|
|
|
|
230
|
|
|
|
231
|
|
|
|
232
|
4
|
private Set getAllChildNodes( Set list ) {
|
|
233
|
4
|
if ( list == null ) {
|
|
234
|
1
|
list = new LinkedHashSet();
|
|
235
|
|
}
|
|
236
|
|
|
|
237
|
4
|
for ( Iterator it = this.getChildIterator(); it.hasNext(); ) {
|
|
238
|
3
|
Object j = it.next();
|
|
239
|
3
|
list.add( getGraph().get( j ) );
|
|
240
|
3
|
( ( DirectedGraphNode ) getGraph().get( j ) ).getAllChildNodes( list );
|
|
241
|
|
}
|
|
242
|
4
|
return list;
|
|
243
|
|
}
|
|
244
|
|
|
|
245
|
0
|
private Set getAllParentNodes( Set list ) {
|
|
246
|
0
|
if ( list == null ) {
|
|
247
|
0
|
list = new LinkedHashSet();
|
|
248
|
|
}
|
|
249
|
|
|
|
250
|
0
|
for ( Iterator it = this.getParentIterator(); it.hasNext(); ) {
|
|
251
|
0
|
Object j = it.next();
|
|
252
|
0
|
list.add( getGraph().get( j ) );
|
|
253
|
0
|
( ( DirectedGraphNode ) getGraph().get( j ) ).getAllParentNodes( list );
|
|
254
|
|
}
|
|
255
|
0
|
return list;
|
|
256
|
|
}
|
|
257
|
|
|
|
258
|
268
|
private Iterator getChildIterator() {
|
|
259
|
268
|
return children.iterator();
|
|
260
|
|
}
|
|
261
|
|
|
|
262
|
95
|
private Iterator getParentIterator() {
|
|
263
|
95
|
return parents.iterator();
|
|
264
|
|
}
|
|
265
|
|
|
|
266
|
|
|
|
267
|
|
|
|
268
|
|
|
|
269
|
|
|
|
270
|
|
|
|
271
|
|
|
|
272
|
359
|
public int compareTo( Object o ) {
|
|
273
|
359
|
DirectedGraphNode k = ( DirectedGraphNode ) o;
|
|
274
|
359
|
int ord = k.getTopoSortOrder();
|
|
275
|
359
|
if ( ord < this.topoSortOrder ) {
|
|
276
|
168
|
return 1;
|
|
277
|
191
|
} else if ( ord > this.topoSortOrder ) {
|
|
278
|
191
|
return -1;
|
|
279
|
|
}
|
|
280
|
0
|
return 0;
|
|
281
|
|
}
|
|
282
|
|
|
|
283
|
|
|
|
284
|
|
|
|
285
|
|
|
|
286
|
|
|
|
287
|
|
|
|
288
|
|
|
|
289
|
4
|
public Object clone() {
|
|
290
|
4
|
DirectedGraphNode r = new DirectedGraphNode( key, item, graph );
|
|
291
|
4
|
for ( Iterator it = this.getParentIterator(); it.hasNext(); ) {
|
|
292
|
4
|
Object j = it.next();
|
|
293
|
4
|
r.addParent( j );
|
|
294
|
|
}
|
|
295
|
|
|
|
296
|
4
|
for ( Iterator it = this.getChildIterator(); it.hasNext(); ) {
|
|
297
|
3
|
Object j = it.next();
|
|
298
|
3
|
r.addChild( j );
|
|
299
|
|
}
|
|
300
|
4
|
return r;
|
|
301
|
|
}
|
|
302
|
|
|
|
303
|
|
}
|