View Javadoc
1   package org.opentrafficsim.core.network.route;
2   
3   import java.util.List;
4   
5   import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
6   import org.opentrafficsim.core.gtu.GTUType;
7   import org.opentrafficsim.core.network.Link;
8   import org.opentrafficsim.core.network.Network;
9   import org.opentrafficsim.core.network.NetworkException;
10  import org.opentrafficsim.core.network.Node;
11  
12  /**
13   * A CompleteRoute is a Route with directly connected Nodes.
14   * <p>
15   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
17   * <p>
18   * $LastChangedDate: 2015-07-16 10:20:53 +0200 (Thu, 16 Jul 2015) $, @version $Revision: 1124 $, by $Author: pknoppers $,
19   * initial version Jul 22, 2015 <br>
20   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   */
23  public class CompleteRoute extends Route
24  {
25      /** */
26      private static final long serialVersionUID = 20150722L;
27  
28      /** The GTUType for which this is a route. */
29      private final GTUType gtuType;
30  
31      /**
32       * Create an empty route for the given GTUType.
33       * @param id the name of the route
34       * @param gtuType the GTUType for which this is a route
35       */
36      public CompleteRoute(final String id, final GTUType gtuType)
37      {
38          super(id);
39          this.gtuType = gtuType;
40      }
41  
42      /**
43       * Create a route based on an initial list of nodes. <br>
44       * This constructor makes a defensive copy of the provided List.
45       * @param id the name of the route.
46       * @param gtuType the GTUType for which this is a route
47       * @param nodes the initial list of nodes.
48       * @throws NetworkException if intermediate nodes are missing in the route.
49       */
50      public CompleteRoute(final String id, final GTUType gtuType, final List<Node> nodes) throws NetworkException
51      {
52          super(id, nodes);
53          this.gtuType = gtuType;
54          Node fromNode = null;
55          for (Node toNode : getNodes())
56          {
57              if (null != fromNode)
58              {
59                  if (!fromNode.isDirectionallyConnectedTo(this.gtuType, toNode))
60                  {
61                      throw new NetworkException("CompleteRoute: node " + fromNode
62                              + " not directly or not directionally connected to node " + toNode);
63                  }
64              }
65              fromNode = toNode;
66          }
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public final void addNode(final Node node) throws NetworkException
72      {
73          if (getNodes().size() > 0)
74          {
75              Node lastNode = getNodes().get(getNodes().size() - 1);
76              if (!lastNode.isDirectionallyConnectedTo(this.gtuType, node))
77              {
78                  throw new NetworkException("CompleteRoute: last node " + lastNode
79                          + " not directly or not directionally connected to node " + node);
80              }
81          }
82          super.addNode(node);
83      }
84  
85      /**
86       * Determine if this Route contains the specified Link.
87       * @param link the link to check in the route.
88       * @return whether the link is part of the route or not.
89       */
90      public final boolean containsLink(final Link link)
91      {
92          Node sn = link.getStartNode();
93          Node en = link.getEndNode();
94          for (int index = 1; index < size(); index++)
95          {
96              if (getNodes().get(index) == en && getNodes().get(index - 1) == sn)
97              {
98                  return true;
99              }
100         }
101         return false;
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public final String toString()
107     {
108         return "CompleteRoute [gtuType=" + this.gtuType + ", nodes=" + super.getNodes() + "]";
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     @SuppressWarnings("checkstyle:designforextension")
114     public CompleteRoute clone(final Network newNetwork, final OTSSimulatorInterface newSimulator, final boolean animation)
115             throws NetworkException
116     {
117         CompleteRoute newRoute = new CompleteRoute(getId(), this.gtuType);
118         for (Node node : getNodes())
119         {
120             newRoute.addNode(newNetwork.getNode(node.getId()));
121         }
122         return newRoute;
123     }
124 
125 }