1 package org.opentrafficsim.core.network;
2
3 import java.util.Collection;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import org.opentrafficsim.core.unit.LengthUnit;
8 import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar;
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 public class ExpansionNetwork<ID, L extends Link<?, ?>> extends Network<ID, L>
76 {
77
78 private static final long serialVersionUID = 20150104L;
79
80
81 private Node<?, ?> expansionOfNode = null;
82
83
84
85
86
87 public ExpansionNetwork(final ID id)
88 {
89 super(id);
90 }
91
92
93
94
95
96
97 public ExpansionNetwork(final ID id, final Collection<? extends L> collection)
98 {
99 super(id, collection);
100 }
101
102
103
104
105
106
107
108
109 public ExpansionNetwork(final ID id, final Collection<? extends L> collection, final Node<?, ?> expansionNode)
110 throws NetworkException
111 {
112 super(id, collection);
113 if (collection.contains(expansionNode))
114 {
115 throw new NetworkException("Creating Network " + id + " with initial collection. Expansion node "
116 + expansionNode.toString() + " is part of the initial collection");
117 }
118 this.expansionOfNode = expansionNode;
119 }
120
121
122
123
124 public final Node<?, ?> getExpansionOfNode()
125 {
126 return this.expansionOfNode;
127 }
128
129
130
131
132
133 public final void setExpansionOfNode(final Node<?, ?> expansionOfNode) throws NetworkException
134 {
135 this.expansionOfNode = expansionOfNode;
136 }
137
138
139
140
141
142
143
144 public final boolean isInNetwork(final Node<?, ?> node, final boolean recurse)
145 {
146 if (isInNetwork(node))
147 {
148 return true;
149 }
150 else if (recurse)
151 {
152 for (Node<?, ?> n : getNodeSet())
153 {
154
155
156 {
157 return true;
158 }
159 }
160 }
161 return false;
162 }
163
164
165
166
167
168
169
170 public final ExpansionNetwork<?, ?> getSubNetworkConsistNode(final Node<?, ?> node) throws NetworkException
171 {
172 if (isInNetwork(node, true))
173 {
174
175 if (isInNetwork(node, false))
176 {
177 return this;
178 }
179 else
180 {
181 for (Node<?, ?> n : getNodeSet())
182 {
183
184
185 {
186 return getSubNetworkConsistNode(node);
187 }
188 }
189 }
190 }
191 throw new NetworkException("The network does not contain the Node" + node.getId().toString() + ".");
192 }
193
194
195
196
197
198
199
200 public final boolean deleteNode(final Node<?, ?> deleteThis) throws NetworkException
201 {
202
203 if (isInNetwork(deleteThis, true))
204 {
205
206 if (isInNetwork(deleteThis, false))
207 {
208 getNodeSet().remove(deleteThis);
209 return true;
210 }
211 else
212 {
213 ExpansionNetwork<?, ?> n = getSubNetworkConsistNode(deleteThis);
214 n.remove(deleteThis);
215 return true;
216 }
217 }
218 throw new NetworkException("Deleting" + deleteThis.getId().toString() + "is failed. Possible cause:"
219 + " node is not a member of the given Network");
220 }
221
222
223
224
225
226
227 public final boolean collapseToNode(final HashSet<Node<?, ?>> nodesOfSubNetwork)
228 {
229 Link<?, Node<?, ?>>[] setOfLinks = (Link<?, Node<?, ?>>[]) super.toArray();
230 Set<L> insideLinks = new HashSet<L>();
231 Set<L> neighbourLinks = new HashSet<L>();
232
233 for (Node<?, ?> node : nodesOfSubNetwork)
234 {
235 for (Link<?, Node<?, ?>> link : setOfLinks)
236 {
237 if (nodesOfSubNetwork.contains(link.getStartNode()) && nodesOfSubNetwork.contains(link.getEndNode()))
238
239 {
240
241 insideLinks.add((L) link);
242
243 super.remove(link);
244
245 getNodeSet().remove(node);
246 }
247 else if (node.equals(link.getStartNode()) || node.equals(link.getEndNode()))
248 {
249
250 neighbourLinks.add((L) link);
251
252 super.remove(link);
253 getNodeSet().remove(node);
254 }
255
256 }
257 }
258
259
260
261
262
263
264
265 return true;
266 }
267
268
269
270
271
272
273
274 public final boolean collapseLinks(final Node<?, ?> node1, final Node<?, ?> node2)
275 {
276 Link<?, Node<?, ?>>[] setOfLinks = (Link<?, Node<?, ?>>[]) super.toArray();
277 float forwardCapacity = 0.0f;
278 float reverseCapacity = 0.0f;
279 DoubleScalar.Rel<LengthUnit> shortestLengthFrom1 =
280 new DoubleScalar.Rel<LengthUnit>(Double.MAX_VALUE, LengthUnit.METER);
281 DoubleScalar.Rel<LengthUnit> shortestLengthFrom2 =
282 new DoubleScalar.Rel<LengthUnit>(Double.MAX_VALUE, LengthUnit.METER);
283
284 int forwardLinksFound = 0;
285 int reverseLinksFound = 0;
286 for (Link<?, Node<?, ?>> link : setOfLinks)
287 {
288 if (node1.equals(link.getStartNode()) && node2.equals(link.getEndNode()))
289 {
290 super.remove(link);
291
292 forwardCapacity += link.getCapacity().floatValue();
293
294 if (shortestLengthFrom1.floatValue() > link.getLength().floatValue())
295 {
296 shortestLengthFrom1 = link.getLength();
297 }
298 forwardLinksFound++;
299 }
300 else if (node2.equals(link.getStartNode()) && node1.equals(link.getEndNode()))
301 {
302 super.remove(link);
303
304 reverseCapacity += link.getCapacity().floatValue();
305
306 if (shortestLengthFrom2.floatValue() > link.getLength().floatValue())
307 {
308 shortestLengthFrom2 = link.getLength();
309 }
310 reverseLinksFound++;
311 }
312 }
313 if (0 == forwardLinksFound && 0 == reverseLinksFound)
314 {
315 return false;
316 }
317
318 if (forwardLinksFound > 0)
319 {
320
321
322
323 }
324 if (reverseLinksFound > 0)
325 {
326
327
328
329 }
330 return true;
331 }
332
333
334
335
336
337
338
339
340 public final Set<L> findLinkHierarchyBelow(final int hierarchyLevel) throws NetworkException
341 {
342 Link<?, Node<?, ?>>[] setOfLinks = (Link<?, Node<?, ?>>[]) super.toArray();
343 Set<L> linksAboveLevel = new HashSet<L>();
344
345 for (Link<?, Node<?, ?>> link : setOfLinks)
346 {
347
348
349
350
351 }
352 return linksAboveLevel;
353 }
354
355 }