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 link type that does not allow any vehicles, or pedestrians. */
37      public static final LinkType NONE;
38  
39      /** Two-directional road, accessible to all road GTU types (including PEDESTRIAN). */
40      public static final LinkType ROAD;
41  
42      /** One-directional road, accessible to all road GTU types (excluding PEDESTRIAN and BICYCLE). */
43      public static final LinkType FREEWAY;
44  
45      /** Two-directional water way. */
46      public static final LinkType WATER_WAY;
47  
48      /** Two-directional rail link. */
49      public static final LinkType RAIL_WAY;
50  
51      /** Virtual connection between nodes, e.g. to distribute demand. */
52      public static final LinkType CONNECTOR;
53  
54      static
55      {
56          GTUCompatibility<LinkType> compatibility = new GTUCompatibility<>((LinkType) null);
57          NONE = new LinkType("NONE", null, compatibility);
58          //
59          compatibility = new GTUCompatibility<>((LinkType) null);
60          compatibility.addAllowedGTUType(GTUType.ROAD_USER, LongitudinalDirectionality.DIR_BOTH);
61          ROAD = new LinkType("ROAD", null, compatibility);
62          //
63          compatibility = new GTUCompatibility<>((LinkType) null);
64          compatibility.addAllowedGTUType(GTUType.ROAD_USER, LongitudinalDirectionality.DIR_PLUS);
65          compatibility.addAllowedGTUType(GTUType.PEDESTRIAN, LongitudinalDirectionality.DIR_NONE);
66          compatibility.addAllowedGTUType(GTUType.BICYCLE, LongitudinalDirectionality.DIR_NONE);
67          FREEWAY = new LinkType("FREEWAY", ROAD, compatibility);
68          //
69          compatibility = new GTUCompatibility<>((LinkType) null);
70          compatibility.addAllowedGTUType(GTUType.WATER_WAY_USER, LongitudinalDirectionality.DIR_BOTH);
71          WATER_WAY = new LinkType("WATER_WAY", null, compatibility);
72          //
73          compatibility = new GTUCompatibility<>((LinkType) null);
74          compatibility.addAllowedGTUType(GTUType.RAIL_WAY_USER, LongitudinalDirectionality.DIR_BOTH);
75          RAIL_WAY = new LinkType("WATER_WAY", null, compatibility);
76          //
77          compatibility = new GTUCompatibility<>((LinkType) null);
78          compatibility.addAllowedGTUType(GTUType.ROAD_USER, LongitudinalDirectionality.DIR_PLUS);
79          compatibility.addAllowedGTUType(GTUType.WATER_WAY_USER, LongitudinalDirectionality.DIR_PLUS);
80          compatibility.addAllowedGTUType(GTUType.RAIL_WAY_USER, LongitudinalDirectionality.DIR_PLUS);
81          CONNECTOR = new LinkType("CONNECTOR", null, compatibility);
82      }
83  
84      /**
85       * Create a new Link type with compatibility set.
86       * @param id String; the id of the lane type (may not be null)
87       * @param parent LinkType; the parent type (may be null)
88       * @param compatibility the collection of compatible GTUTypes for this LinkType; can be null (resulting in a LinkType that
89       *            is inaccessible to all GTU types). This constructor makes a deep copy of the <code>compatibility</code>.
90       */
91      public LinkType(final String id, final LinkType parent, final GTUCompatibility<LinkType> compatibility)
92      {
93          super(id, parent);
94          this.compatibility = new GTUCompatibility<>(compatibility);
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public Boolean isCompatible(final GTUType gtuType, final GTUDirectionality directionality)
100     {
101         return this.compatibility.isCompatible(gtuType, directionality);
102     }
103 
104     /**
105      * Returns a link type with directionality reversed.
106      * @return LinkType; link type with directionality reversed
107      */
108     public final LinkType reverse()
109     {
110         if (this.reversed == null)
111         {
112             this.reversed = new ReversedLinkType(this);
113         }
114         return this.reversed;
115     }
116 
117     /**
118      * @return whether this is {@code NONE}
119      */
120     public final boolean isNone()
121     {
122         return this.equals(NONE);
123     }
124 
125     /**
126      * @return whether this is {@code ROAD}
127      */
128     public final boolean isRoad()
129     {
130         return this.equals(ROAD);
131     }
132 
133     /**
134      * @return whether this is {@code WATER_WAY}
135      */
136     public final boolean isWaterWay()
137     {
138         return this.equals(WATER_WAY);
139     }
140 
141     /**
142      * @return whether this is {@code RAIL_WAY}
143      */
144     public final boolean isRailWay()
145     {
146         return this.equals(RAIL_WAY);
147     }
148 
149     /**
150      * @return whether this is {@code CONNECTOR}
151      */
152     public final boolean isConnector()
153     {
154         return this.equals(CONNECTOR);
155     }
156 
157     /** {@inheritDoc} */
158     @Override
159     @SuppressWarnings("checkstyle:designforextension")
160     public String toString()
161     {
162         return "LinkType [id=" + getId() + ", compatibilitySet=" + this.compatibility + "]";
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     public final LongitudinalDirectionality getDirectionality(final GTUType gtuType, final boolean tryParentsOfGTUType)
168     {
169         return this.compatibility.getDirectionality(gtuType, tryParentsOfGTUType);
170     }
171 
172     /**
173      * Reversed version of an original and wrapped link type.
174      * <p>
175      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
176      * <br>
177      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
178      * <p>
179      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 24 aug. 2018 <br>
180      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
181      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
182      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
183      */
184     private class ReversedLinkType extends LinkType
185     {
186 
187         /** */
188         private static final long serialVersionUID = 20180824L;
189 
190         /** Original link type. */
191         private final LinkType original;
192 
193         /**
194          * Constructor.
195          * @param original LinkType; the original type (may not be null)
196          */
197         ReversedLinkType(final LinkType original)
198         {
199             super(original.getId() + "_rev", original.getParent().reverse(), new GTUCompatibility<>((LinkType) null));
200             this.original = original;
201         }
202 
203         /** {@inheritDoc} */
204         @Override
205         public Boolean isCompatible(final GTUType gtuType, final GTUDirectionality directionality)
206         {
207             return this.original.isCompatible(gtuType, directionality.flip());
208         }
209 
210         /** {@inheritDoc} */
211         @Override
212         public String toString()
213         {
214             return "ReversedLinkType [original=" + this.original + "]";
215         }
216 
217     }
218 
219 }