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