1 package org.opentrafficsim.core.network.route;
2
3 import java.util.List;
4
5 import org.opentrafficsim.core.gtu.GTUType;
6 import org.opentrafficsim.core.network.Link;
7 import org.opentrafficsim.core.network.Network;
8 import org.opentrafficsim.core.network.NetworkException;
9 import org.opentrafficsim.core.network.Node;
10
11 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
12
13
14
15
16
17
18
19
20
21
22
23
24 public class CompleteRoute extends Route
25 {
26
27 private static final long serialVersionUID = 20150722L;
28
29
30 private final GTUType gtuType;
31
32
33
34
35
36
37 public CompleteRoute(final String id, final GTUType gtuType)
38 {
39 super(id);
40 this.gtuType = gtuType;
41 }
42
43
44
45
46
47
48
49
50
51 public CompleteRoute(final String id, final GTUType gtuType, final List<Node> nodes) throws NetworkException
52 {
53 super(id, nodes);
54 this.gtuType = gtuType;
55 Node fromNode = null;
56 for (Node toNode : getNodes())
57 {
58 if (null != fromNode)
59 {
60 if (!fromNode.isDirectionallyConnectedTo(this.gtuType, toNode))
61 {
62 throw new NetworkException("CompleteRoute: node " + fromNode
63 + " not directly or not directionally connected to node " + toNode);
64 }
65 }
66 fromNode = toNode;
67 }
68 }
69
70
71 @Override
72 public final CompleteRoute addNode(final Node node) throws NetworkException
73 {
74 if (getNodes().size() > 0)
75 {
76 Node lastNode = getNodes().get(getNodes().size() - 1);
77 if (!lastNode.isDirectionallyConnectedTo(this.gtuType, node))
78 {
79 throw new NetworkException("CompleteRoute: last node " + lastNode
80 + " not directly or not directionally connected to node " + node);
81 }
82 }
83 super.addNode(node);
84 return this;
85 }
86
87
88
89
90
91
92 public final boolean containsLink(final Link link)
93 {
94 int index1 = getNodes().indexOf(link.getStartNode());
95 int index2 = getNodes().indexOf(link.getEndNode());
96 return index1 >= 0 && index2 >= 0 && Math.abs(index2 - index1) == 1;
97 }
98
99
100 @Override
101 public final String toString()
102 {
103 return "CompleteRoute [gtuType=" + this.gtuType + ", nodes=" + super.getNodes() + "]";
104 }
105
106
107 @Override
108 @SuppressWarnings("checkstyle:designforextension")
109 public CompleteRoute clone(final Network newNetwork, final SimulatorInterface.TimeDoubleUnit newSimulator)
110 throws NetworkException
111 {
112 CompleteRoute newRoute = new CompleteRoute(getId(), this.gtuType);
113 for (Node node : getNodes())
114 {
115 newRoute.addNode(newNetwork.getNode(node.getId()));
116 }
117 return newRoute;
118 }
119
120 }