1 package org.opentrafficsim.road.network.factory.xml.old;
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.lane.CrossSectionLink.Priority;
11 import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
12 import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
13
14
15
16
17
18
19
20
21
22
23 public final class LaneAttributes
24 {
25
26 private LaneAttributes()
27 {
28
29 }
30
31
32
33
34
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
55
56
57
58
59 public static OvertakingConditions parseOvertakingConditions(final String ocStr, final XmlNetworkLaneParserOld 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
169
170
171
172 private static Set<GTUType> parseGTUTypeSet(final String set, final XmlNetworkLaneParserOld 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
186
187
188
189 private static GTUType parseGTUType(final String typeName, final XmlNetworkLaneParserOld parser)
190 {
191 if (!parser.gtuTypes.containsKey(typeName))
192 {
193 GTUType gtuType = new GTUType(typeName, parser.network.getGtuType(GTUType.DEFAULTS.VEHICLE));
194 parser.gtuTypes.put(typeName, gtuType);
195 }
196 return parser.gtuTypes.get(typeName);
197 }
198
199
200
201
202
203
204 public static Priority parsePriority(final String priority) throws NetworkException
205 {
206 try
207 {
208 return Priority.valueOf(priority);
209 }
210 catch (IllegalArgumentException exception)
211 {
212 throw new NetworkException("Unknown priority string: " + priority, exception);
213 }
214 }
215
216 }