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
19
20
21
22
23
24
25
26 public final class LaneAttributes
27 {
28
29 private LaneAttributes()
30 {
31
32 }
33
34
35
36
37
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
58
59
60
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
172
173
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
189
190
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
204
205
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 }