View Javadoc
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   * A CompleteRoute is a Route with directly connected Nodes.
15   * <p>
16   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18   * <p>
19   * $LastChangedDate: 2015-07-16 10:20:53 +0200 (Thu, 16 Jul 2015) $, @version $Revision: 1124 $, by $Author: pknoppers $,
20   * initial version Jul 22, 2015 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   */
24  public class CompleteRoute extends Route
25  {
26      /** */
27      private static final long serialVersionUID = 20150722L;
28  
29      /** The GTUType for which this is a route. */
30      private final GTUType gtuType;
31  
32      /**
33       * Create an empty route for the given GTUType.
34       * @param id String; the name of the route
35       * @param gtuType GTUType; the GTUType for which this is a route
36       */
37      public CompleteRoute(final String id, final GTUType gtuType)
38      {
39          super(id);
40          this.gtuType = gtuType;
41      }
42  
43      /**
44       * Create a route based on an initial list of nodes. <br>
45       * This constructor makes a defensive copy of the provided List.
46       * @param id String; the name of the route.
47       * @param gtuType GTUType; the GTUType for which this is a route
48       * @param nodes List&lt;Node&gt;; the initial list of nodes.
49       * @throws NetworkException if intermediate nodes are missing in the route.
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      /** {@inheritDoc} */
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       * Determine if this Route contains the specified Link.
89       * @param link Link; the link to check in the route.
90       * @return whether the link is part of the route or not.
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      /** {@inheritDoc} */
100     @Override
101     public final String toString()
102     {
103         return "CompleteRoute [gtuType=" + this.gtuType + ", nodes=" + super.getNodes() + "]";
104     }
105 
106     /** {@inheritDoc} */
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 }