View Javadoc
1   package org.opentrafficsim.road.network.factory.xml.units;
2   
3   import static org.opentrafficsim.core.gtu.GTUType.VEHICLE;
4   
5   import java.util.HashSet;
6   import java.util.Set;
7   
8   import org.djunits.value.vdouble.scalar.Speed;
9   import org.opentrafficsim.core.gtu.GTUType;
10  import org.opentrafficsim.core.network.NetworkException;
11  import org.opentrafficsim.core.network.factory.xml.units.SpeedUnits;
12  import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
13  import org.opentrafficsim.road.network.lane.CrossSectionLink.Priority;
14  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
15  import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
16  
17  /**
18   * <p>
19   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
21   * <p>
22   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
23   * initial version Jul 23, 2015 <br>
24   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   */
26  public final class LaneAttributes
27  {
28      /** Utility class. */
29      private LaneAttributes()
30      {
31          // do not instantiate
32      }
33  
34      /**
35       * @param lkpStr the lane keeping policy string.
36       * @return the lane keeping policy.
37       * @throws NetworkException in case of unknown policy.
38       */
39      public static LaneKeepingPolicy parseLaneKeepingPolicy(final String lkpStr) throws NetworkException
40      {
41          if (lkpStr.equals("KEEPRIGHT"))
42          {
43              return LaneKeepingPolicy.KEEP_RIGHT;
44          }
45          else if (lkpStr.equals("KEEPLEFT"))
46          {
47              return LaneKeepingPolicy.KEEP_LEFT;
48          }
49          else if (lkpStr.equals("KEEPLANE"))
50          {
51              return LaneKeepingPolicy.KEEP_LANE;
52          }
53          throw new NetworkException("Unknown lane keeping policy string: " + lkpStr);
54      }
55  
56      /**
57       * @param ocStr the overtaking conditions string.
58       * @return the overtaking conditions.
59       * @param parser the parser to get access to the defined GTUTypes.
60       * @throws NetworkException in case of unknown overtaking conditions.
61       */
62      public static OvertakingConditions parseOvertakingConditions(final String ocStr, final XmlNetworkLaneParser parser)
63              throws NetworkException
64      {
65          if (ocStr.equals("LEFTONLY"))
66          {
67              return new OvertakingConditions.LeftOnly();
68          }
69          else if (ocStr.equals("RIGHTONLY"))
70          {
71              return new OvertakingConditions.RightOnly();
72          }
73          else if (ocStr.equals("LEFTANDRIGHT"))
74          {
75              return new OvertakingConditions.LeftAndRight();
76          }
77          else if (ocStr.equals("NONE"))
78          {
79              return new OvertakingConditions.None();
80          }
81          else if (ocStr.equals("SAMELANERIGHT"))
82          {
83              return new OvertakingConditions.SameLaneRight();
84          }
85          else if (ocStr.equals("SAMELANELEFT"))
86          {
87              return new OvertakingConditions.SameLaneLeft();
88          }
89          else if (ocStr.equals("SAMELANEBOTH"))
90          {
91              return new OvertakingConditions.SameLaneBoth();
92          }
93          else if (ocStr.startsWith("LEFTALWAYS RIGHTSPEED"))
94          {
95              int lb = ocStr.indexOf('(');
96              int rb = ocStr.indexOf(')');
97              if (lb == -1 || rb == -1 || rb - lb < 3)
98              {
99                  throw new NetworkException("Speed in overtaking conditions string: '" + ocStr + "' not coded right");
100             }
101             Speed speed = SpeedUnits.parseSpeed(ocStr.substring(lb + 1, rb));
102             return new OvertakingConditions.LeftAlwaysRightSpeed(speed);
103         }
104         else if (ocStr.startsWith("RIGHTALWAYS LEFTSPEED"))
105         {
106             int lb = ocStr.indexOf('(');
107             int rb = ocStr.indexOf(')');
108             if (lb == -1 || rb == -1 || rb - lb < 3)
109             {
110                 throw new NetworkException("Speed in overtaking conditions string: '" + ocStr + "' not coded right");
111             }
112             Speed speed = SpeedUnits.parseSpeed(ocStr.substring(lb + 1, rb));
113             return new OvertakingConditions.RightAlwaysLeftSpeed(speed);
114         }
115         else if (ocStr.startsWith("LEFTSET"))
116         {
117             int lset1 = ocStr.indexOf('[') + 1;
118             int rset1 = ocStr.indexOf(']', lset1);
119             int lset2 = ocStr.indexOf('[', ocStr.indexOf("OVERTAKE")) + 1;
120             int rset2 = ocStr.indexOf(']', lset2);
121             if (lset1 == -1 || rset1 == -1 || rset1 - lset1 < 3 || lset2 == -1 || rset2 == -1 || rset2 - lset2 < 3)
122             {
123                 throw new NetworkException("Sets in overtaking conditions string: '" + ocStr + "' not coded right");
124             }
125             Set<GTUType> overtakingGTUs = parseGTUTypeSet(ocStr.substring(lset1, rset1), parser);
126             Set<GTUType> overtakenGTUs = parseGTUTypeSet(ocStr.substring(lset2, rset2), parser);
127             if (ocStr.contains("RIGHTSPEED"))
128             {
129                 int i = ocStr.indexOf("RIGHTSPEED");
130                 int lb = ocStr.indexOf('(', i);
131                 int rb = ocStr.indexOf(')', i);
132                 if (lb == -1 || rb == -1 || rb - lb < 3)
133                 {
134                     throw new NetworkException("Speed in overtaking conditions string: '" + ocStr + "' not coded right");
135                 }
136                 Speed speed = SpeedUnits.parseSpeed(ocStr.substring(lb + 1, rb));
137                 return new OvertakingConditions.LeftSetRightSpeed(overtakingGTUs, overtakenGTUs, speed);
138             }
139             return new OvertakingConditions.LeftSet(overtakingGTUs, overtakenGTUs);
140         }
141         else if (ocStr.startsWith("RIGHTSET"))
142         {
143             int lset1 = ocStr.indexOf('[') + 1;
144             int rset1 = ocStr.indexOf(']', lset1);
145             int lset2 = ocStr.indexOf('[', ocStr.indexOf("OVERTAKE")) + 1;
146             int rset2 = ocStr.indexOf(']', lset2);
147             if (lset1 == -1 || rset1 == -1 || rset1 - lset1 < 3 || lset2 == -1 || rset2 == -1 || rset2 - lset2 < 3)
148             {
149                 throw new NetworkException("Sets in overtaking conditions string: '" + ocStr + "' not coded right");
150             }
151             Set<GTUType> overtakingGTUs = parseGTUTypeSet(ocStr.substring(lset1, rset1), parser);
152             Set<GTUType> overtakenGTUs = parseGTUTypeSet(ocStr.substring(lset2, rset2), parser);
153             if (ocStr.contains("LEFTSPEED"))
154             {
155                 int i = ocStr.indexOf("LEFTSPEED");
156                 int lb = ocStr.indexOf('(', i);
157                 int rb = ocStr.indexOf(')', i);
158                 if (lb == -1 || rb == -1 || rb - lb < 3)
159                 {
160                     throw new NetworkException("Speed in overtaking conditions string: '" + ocStr + "' not coded right");
161                 }
162                 Speed speed = SpeedUnits.parseSpeed(ocStr.substring(lb + 1, rb));
163                 return new OvertakingConditions.RightSetLeftSpeed(overtakingGTUs, overtakenGTUs, speed);
164             }
165             return new OvertakingConditions.RightSet(overtakingGTUs, overtakenGTUs);
166         }
167         throw new NetworkException("Unknown overtaking conditions string: " + ocStr);
168     }
169 
170     /**
171      * @param set the string with the GTUTypes ike "CAR, TRUCK" or "ALL"
172      * @param parser the parser to get access to the defined GTUTypes.
173      * @return a parsed set of GTUTypes
174      */
175     private static Set<GTUType> parseGTUTypeSet(final String set, final XmlNetworkLaneParser parser)
176     {
177         Set<GTUType> gtuTypeSet = new HashSet<GTUType>();
178         String[] types = set.trim().split(",");
179         for (String type : types)
180         {
181             GTUType gtuType = parseGTUType(type.trim(), parser);
182             gtuTypeSet.add(gtuType);
183         }
184         return gtuTypeSet;
185     }
186 
187     /**
188      * @param typeName the name of the GTU type.
189      * @param parser the parser with the lists of information
190      * @return the GTUType that was retrieved or created.
191      */
192     private static GTUType parseGTUType(final String typeName, final XmlNetworkLaneParser parser)
193     {
194         if (!parser.gtuTypes.containsKey(typeName))
195         {
196             GTUType gtuType = new GTUType(typeName, VEHICLE);
197             parser.gtuTypes.put(typeName, gtuType);
198         }
199         return parser.gtuTypes.get(typeName);
200     }
201 
202     /**
203      * @param priority the priority string
204      * @return the priority
205      * @throws NetworkException in case of unknown priority
206      */
207     public static Priority parsePriority(final String priority) throws NetworkException
208     {
209         try
210         {
211             return Priority.valueOf(priority);
212         }
213         catch (IllegalArgumentException exception)
214         {
215             throw new NetworkException("Unknown priority string: " + priority, exception);
216         }
217     }
218 
219 }