View Javadoc
1   package org.opentrafficsim.core.network;
2   
3   import java.io.Serializable;
4   
5   import org.opentrafficsim.base.HierarchicalType;
6   import org.opentrafficsim.base.Identifiable;
7   import org.opentrafficsim.core.compatibility.Compatibility;
8   import org.opentrafficsim.core.compatibility.GTUCompatibility;
9   import org.opentrafficsim.core.gtu.GTUDirectionality;
10  import org.opentrafficsim.core.gtu.GTUType;
11  
12  /**
13   * Link type to indicate compatibility with GTU types. The id of a LinkType should be unique within a simulation. This is,
14   * however, not checked or enforced, as different simulations running in the same JVM can have different compatibilitySets for
15   * LinkTypes with the same id. Therefore, uniqueness is not enforced.
16   * <p>
17   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19   * <p>
20   * $LastChangedDate: 2015-08-30 00:16:51 +0200 (Sun, 30 Aug 2015) $, @version $Revision: 1329 $, by $Author: averbraeck $,
21   * initial version Aug 21, 2014 <br>
22   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
24   */
25  public class LinkType extends HierarchicalType<LinkType> implements Serializable, Identifiable, Compatibility<GTUType, LinkType>
26  {
27      /** */
28      private static final long serialVersionUID = 20140821L;
29  
30      /** The compatibility of GTU types with this link type. */
31      private final GTUCompatibility<LinkType> compatibility;
32  
33      /** Reversed link type. */
34      private LinkType reversed = null;
35  
36      /** the network to which the LinkType belongs. */
37      private final Network network;
38  
39      /** Default types with their name. */
40      public enum DEFAULTS
41      {
42          /** The link type that does not allow any vehicles, or pedestrians. */
43          NONE("NONE"),
44  
45          /** Two-directional road, accessible to all road GTU types (including PEDESTRIAN). */
46          ROAD("ROAD"),
47  
48          /** One-directional road, accessible to all road GTU types (excluding PEDESTRIAN and BICYCLE). */
49          FREEWAY("FREEWAY"),
50  
51          /** Two-directional water way. */
52          WATERWAY("WATERWAY"),
53  
54          /** Two-directional rail link. */
55          RAILWAY("RAILWAY"),
56  
57          /** Virtual connection between nodes, e.g. to distribute demand. */
58          CONNECTOR("CONNECTOR");
59  
60          /** The name. */
61          private final String id;
62  
63          /**
64           * Construct the enum.
65           * @param id String; the id
66           */
67          DEFAULTS(final String id)
68          {
69              this.id = id;
70          }
71  
72          /** @return the id */
73          public String getId()
74          {
75              return this.id;
76          }
77      }
78  
79      /**
80       * Create a new Link type with compatibility set.
81       * @param id String; the id of the lane type (may not be null)
82       * @param parent LinkType; the parent type (may be null)
83       * @param compatibility the collection of compatible GTUTypes for this LinkType; can be null (resulting in a LinkType that
84       *            is inaccessible to all GTU types). This constructor makes a deep copy of the <code>compatibility</code>.
85       * @param network Network; The network to which the LinkType belongs
86       */
87      public LinkType(final String id, final LinkType parent, final GTUCompatibility<LinkType> compatibility,
88              final Network network)
89      {
90          super(id, parent);
91          this.compatibility = new GTUCompatibility<>(compatibility);
92          this.network = network;
93          this.network.addLinkType(this);
94      }
95  
96      /**
97       * Whether this, or any of the parent types, equals the given type.
98       * @param type DEFAULTS; type
99       * @return whether this, or any of the parent types, equals the given type
100      */
101     public boolean isOfType(final DEFAULTS type)
102     {
103         if (this.getId().equals(type.getId()))
104         {
105             return true;
106         }
107         if (getParent() != null)
108         {
109             return getParent().isOfType(type);
110         }
111         return false;
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public Boolean isCompatible(final GTUType gtuType, final GTUDirectionality directionality)
117     {
118         return this.compatibility.isCompatible(gtuType, directionality);
119     }
120 
121     /**
122      * @return the gtu compatibility for this LinkType
123      */
124     public GTUCompatibility<LinkType> getCompatibility()
125     {
126         return this.compatibility;
127     }
128     
129     /**
130      * Returns a link type with directionality reversed.
131      * @return LinkType; link type with directionality reversed
132      */
133     public final LinkType reverse()
134     {
135         if (this.reversed == null)
136         {
137             this.reversed = new ReversedLinkType(this);
138         }
139         return this.reversed;
140     }
141 
142     /**
143      * @return whether this is {@code NONE}
144      */
145     public final boolean isNone()
146     {
147         return this.getId().equals(DEFAULTS.NONE.getId());
148     }
149 
150     /**
151      * @return whether this is {@code ROAD}
152      */
153     public final boolean isRoad()
154     {
155         return this.getId().equals(DEFAULTS.ROAD.getId());
156     }
157 
158     /**
159      * @return whether this is {@code WATER_WAY}
160      */
161     public final boolean isWaterWay()
162     {
163         return this.getId().equals(DEFAULTS.WATERWAY.getId());
164     }
165 
166     /**
167      * @return whether this is {@code RAIL_WAY}
168      */
169     public final boolean isRailWay()
170     {
171         return this.getId().equals(DEFAULTS.RAILWAY.getId());
172     }
173 
174     /**
175      * @return whether this is {@code CONNECTOR}
176      */
177     public final boolean isConnector()
178     {
179         return this.getId().equals(DEFAULTS.CONNECTOR.getId());
180     }
181 
182     /**
183      * @return the network to which the LinkType belongs
184      */
185     public Network getNetwork()
186     {
187         return this.network;
188     }
189 
190     /** {@inheritDoc} */
191     @Override
192     @SuppressWarnings("checkstyle:designforextension")
193     public String toString()
194     {
195         return "LinkType [id=" + getId() + ", compatibilitySet=" + this.compatibility + "]";
196     }
197 
198     /** {@inheritDoc} */
199     @Override
200     public final LongitudinalDirectionality getDirectionality(final GTUType gtuType, final boolean tryParentsOfGTUType)
201     {
202         return this.compatibility.getDirectionality(gtuType, tryParentsOfGTUType);
203     }
204 
205     /**
206      * Reversed version of an original and wrapped link type.
207      * <p>
208      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
209      * <br>
210      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
211      * <p>
212      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 24 aug. 2018 <br>
213      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
214      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
215      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
216      */
217     private class ReversedLinkType extends LinkType
218     {
219 
220         /** */
221         private static final long serialVersionUID = 20180824L;
222 
223         /** Original link type. */
224         private final LinkType original;
225 
226         /**
227          * Constructor.
228          * @param original LinkType; the original type (may not be null)
229          */
230         ReversedLinkType(final LinkType original)
231         {
232             super(original.getId() + "_rev", original.getParent().reverse(), new GTUCompatibility<>((LinkType) null),
233                     original.getNetwork());
234             this.original = original;
235         }
236 
237         /** {@inheritDoc} */
238         @Override
239         public Boolean isCompatible(final GTUType gtuType, final GTUDirectionality directionality)
240         {
241             return this.original.isCompatible(gtuType, directionality.flip());
242         }
243 
244         /** {@inheritDoc} */
245         @Override
246         public String toString()
247         {
248             return "ReversedLinkType [original=" + this.original + "]";
249         }
250 
251     }
252 
253 }