View Javadoc
1   package org.opentrafficsim.road.network.factory.xml.old;
2   
3   import java.io.Serializable;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import org.djunits.unit.DurationUnit;
8   import org.djunits.unit.SpeedUnit;
9   import org.djunits.unit.TimeUnit;
10  import org.djunits.value.vdouble.scalar.Duration;
11  import org.djunits.value.vdouble.scalar.Length;
12  import org.djunits.value.vdouble.scalar.Speed;
13  import org.djunits.value.vdouble.scalar.Time;
14  import org.opentrafficsim.core.animation.gtu.colorer.GTUColorer;
15  import org.opentrafficsim.core.distributions.Distribution.FrequencyAndObject;
16  import org.opentrafficsim.core.distributions.Generator;
17  import org.opentrafficsim.core.distributions.ProbabilityException;
18  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
19  import org.opentrafficsim.core.gtu.GTUDirectionality;
20  import org.opentrafficsim.core.gtu.GTUException;
21  import org.opentrafficsim.core.network.NetworkException;
22  import org.opentrafficsim.core.network.factory.xml.units.Distributions;
23  import org.opentrafficsim.core.network.factory.xml.units.TimeUnits;
24  import org.opentrafficsim.core.network.route.FixedRouteGenerator;
25  import org.opentrafficsim.core.network.route.ProbabilisticRouteGenerator;
26  import org.opentrafficsim.core.network.route.Route;
27  import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
28  import org.opentrafficsim.road.gtu.generator.GTUGeneratorIndividualOld;
29  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
30  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlannerFactory;
31  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingDirectedChangeTacticalPlannerFactory;
32  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingTacticalPlannerFactory;
33  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedTacticalPlannerFactory;
34  import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusFactory;
35  import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusOld;
36  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
37  import org.opentrafficsim.road.gtu.lane.tactical.lmrs.DefaultLMRSPerceptionFactory;
38  import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LMRSFactory;
39  import org.opentrafficsim.road.gtu.lane.tactical.toledo.ToledoFactory;
40  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
41  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
42  import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlannerFactory;
43  import org.opentrafficsim.road.network.factory.xml.old.CrossSectionElementTag.ElementType;
44  import org.opentrafficsim.road.network.lane.Lane;
45  import org.w3c.dom.NamedNodeMap;
46  import org.w3c.dom.Node;
47  import org.xml.sax.SAXException;
48  
49  import nl.tudelft.simulation.dsol.SimRuntimeException;
50  import nl.tudelft.simulation.jstats.streams.MersenneTwister;
51  import nl.tudelft.simulation.jstats.streams.StreamInterface;
52  
53  /**
54   * <p>
55   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
56   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
57   * <p>
58   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
59   * initial version Jul 23, 2015 <br>
60   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
61   */
62  class GeneratorTag implements Serializable
63  {
64      /** */
65      private static final long serialVersionUID = 20150723L;
66  
67      /** Lane name. */
68      @SuppressWarnings("checkstyle:visibilitymodifier")
69      String laneName = null;
70  
71      /** Position of the sink on the link, relative to the design line, stored as a string to parse when the length is known. */
72      @SuppressWarnings("checkstyle:visibilitymodifier")
73      String positionStr = null;
74  
75      /** Direction in which to generate the GTU, relative to the design line of the Link. */
76      // TOO parse direction, and add to XML formal
77      GTUDirectionality gtuDirection = GTUDirectionality.DIR_PLUS;
78  
79      /** GTU tag. */
80      @SuppressWarnings("checkstyle:visibilitymodifier")
81      GTUTag gtuTag = null;
82  
83      /** GTU mix tag. */
84      @SuppressWarnings("checkstyle:visibilitymodifier")
85      GTUMixTag gtuMixTag = null;
86  
87      /** Tactical planner name. */
88      @SuppressWarnings("checkstyle:visibilitymodifier")
89      String tacticalPlannerName = null;
90  
91      /** Interarrival time. */
92      @SuppressWarnings("checkstyle:visibilitymodifier")
93      ContinuousDistDoubleScalar.Rel<Duration, DurationUnit> iatDist = null;
94  
95      /** Initial speed. */
96      @SuppressWarnings("checkstyle:visibilitymodifier")
97      ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> initialSpeedDist = null;
98  
99      /** Maximimum number of generated GTUs. */
100     @SuppressWarnings("checkstyle:visibilitymodifier")
101     int maxGTUs = Integer.MAX_VALUE;
102 
103     /** Start time of generation. */
104     @SuppressWarnings("checkstyle:visibilitymodifier")
105     Time startTime = null;
106 
107     /** End time of generation. */
108     @SuppressWarnings("checkstyle:visibilitymodifier")
109     Time endTime = null;
110 
111     /** Route tag. */
112     @SuppressWarnings("checkstyle:visibilitymodifier")
113     RouteTag routeTag = null;
114 
115     /** Route mix tag. */
116     @SuppressWarnings("checkstyle:visibilitymodifier")
117     RouteMixTag routeMixTag = null;
118 
119     /** Shortest route tag. */
120     @SuppressWarnings("checkstyle:visibilitymodifier")
121     ShortestRouteTag shortestRouteTag = null;
122 
123     /** Shortest route mix tag. */
124     @SuppressWarnings("checkstyle:visibilitymodifier")
125     ShortestRouteMixTag shortestRouteMixTag = null;
126 
127     /** GTU colorer. */
128     @SuppressWarnings("checkstyle:visibilitymodifier")
129     GTUColorer gtuColorer;
130 
131     /**
132      * Parse the GENERATOR tag.
133      * @param node Node; the GENERATOR node to parse
134      * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
135      * @param linkTag LinkTag; the parent LINK tag
136      * @throws SAXException when parsing of the tag fails
137      * @throws NetworkException when parsing of the tag fails
138      */
139     @SuppressWarnings("checkstyle:needbraces")
140     static void parseGenerator(final Node node, final XmlNetworkLaneParserOld parser, final LinkTag linkTag)
141             throws SAXException, NetworkException
142     {
143         NamedNodeMap attributes = node.getAttributes();
144         GeneratorTag generatorTag = new GeneratorTag();
145 
146         if (attributes.getNamedItem("LANE") == null)
147             throw new SAXException("GENERATOR: missing attribute LANE" + " for link " + linkTag.name);
148         String laneName = attributes.getNamedItem("LANE").getNodeValue().trim();
149         if (linkTag.roadLayoutTag == null)
150             throw new NetworkException("GENERATOR: LANE " + laneName + " no ROADTYPE for link " + linkTag.name);
151         CrossSectionElementTag cseTag = linkTag.roadLayoutTag.cseTags.get(laneName);
152         if (cseTag == null)
153             throw new NetworkException("GENERATOR: LANE " + laneName + " not found in elements of link " + linkTag.name
154                     + " - roadtype " + linkTag.roadLayoutTag.name);
155         if (cseTag.elementType != ElementType.LANE)
156             throw new NetworkException("GENERATOR: LANE " + laneName + " not a real GTU lane for link " + linkTag.name
157                     + " - roadtype " + linkTag.roadLayoutTag.name);
158         if (linkTag.generatorTags.containsKey(laneName))
159             throw new SAXException("GENERATOR for LANE with NAME " + laneName + " defined twice");
160         generatorTag.laneName = laneName;
161 
162         Node position = attributes.getNamedItem("POSITION");
163         if (position == null)
164             throw new NetworkException("GENERATOR: POSITION element not found in elements of link " + linkTag.name
165                     + " - roadtype " + linkTag.roadLayoutTag.name);
166         generatorTag.positionStr = position.getNodeValue().trim();
167 
168         /*-
169         // TODO parse direction
170         Node directionStr = attributes.getNamedItem("DIRECTION");
171         if (directionStr == null)
172             throw new NetworkException("GENERATOR: DIRECTION element not found in elements of link " + linkTag.name
173                 + " - roadtype " + linkTag.roadLayoutTag.name);
174         generatorTag.gtuDirection = parseDirection(directionStr.getNodeValue().trim());
175          */
176 
177         if (attributes.getNamedItem("GTU") != null)
178         {
179             String gtuName = attributes.getNamedItem("GTU").getNodeValue().trim();
180             if (!parser.gtuTags.containsKey(gtuName))
181                 throw new NetworkException(
182                         "GENERATOR: LANE " + laneName + " GTU " + gtuName + " in link " + linkTag.name + " not defined");
183             generatorTag.gtuTag = parser.gtuTags.get(gtuName);
184         }
185 
186         if (attributes.getNamedItem("GTUMIX") != null)
187         {
188             String gtuMixName = attributes.getNamedItem("GTUMIX").getNodeValue().trim();
189             if (!parser.gtuMixTags.containsKey(gtuMixName))
190                 throw new NetworkException(
191                         "GENERATOR: LANE " + laneName + " GTUMIX " + gtuMixName + " in link " + linkTag.name + " not defined");
192             generatorTag.gtuMixTag = parser.gtuMixTags.get(gtuMixName);
193         }
194 
195         if (generatorTag.gtuTag == null && generatorTag.gtuMixTag == null)
196             throw new SAXException(
197                     "GENERATOR: missing attribute GTU or GTUMIX for Lane with NAME " + laneName + " of link " + linkTag.name);
198 
199         if (generatorTag.gtuTag != null && generatorTag.gtuMixTag != null)
200             throw new SAXException("GENERATOR: both attribute GTU and GTUMIX defined for Lane with NAME " + laneName
201                     + " of link " + linkTag.name);
202 
203         if (attributes.getNamedItem("TACTICALPLANNER") != null)
204         {
205             generatorTag.tacticalPlannerName = attributes.getNamedItem("TACTICALPLANNER").getNodeValue().trim();
206         }
207 
208         Node iat = attributes.getNamedItem("IAT");
209         if (iat == null)
210             throw new SAXException("GENERATOR: missing attribute IAT");
211         generatorTag.iatDist = Distributions.parseDurationDist(iat.getNodeValue());
212 
213         Node initialSpeed = attributes.getNamedItem("INITIALSPEED");
214         if (initialSpeed == null)
215             throw new SAXException("GENERATOR: missing attribute INITIALSPEED");
216         generatorTag.initialSpeedDist = Distributions.parseSpeedDist(initialSpeed.getNodeValue());
217 
218         Node maxGTU = attributes.getNamedItem("MAXGTU");
219         generatorTag.maxGTUs = maxGTU == null ? Integer.MAX_VALUE : Integer.parseInt(maxGTU.getNodeValue().trim());
220 
221         if (attributes.getNamedItem("STARTTIME") != null)
222             generatorTag.startTime = TimeUnits.parseTime(attributes.getNamedItem("STARTTIME").getNodeValue());
223 
224         if (attributes.getNamedItem("ENDTIME") != null)
225             generatorTag.endTime = TimeUnits.parseTime(attributes.getNamedItem("ENDTIME").getNodeValue());
226 
227         int numberRouteTags = 0;
228 
229         if (attributes.getNamedItem("ROUTE") != null)
230         {
231             String routeName = attributes.getNamedItem("ROUTE").getNodeValue().trim();
232             if (!parser.routeTags.containsKey(routeName))
233                 throw new NetworkException(
234                         "GENERATOR: LANE " + laneName + " ROUTE " + routeName + " in link " + linkTag.name + " not defined");
235             generatorTag.routeTag = parser.routeTags.get(routeName);
236             numberRouteTags++;
237         }
238 
239         if (attributes.getNamedItem("ROUTEMIX") != null)
240         {
241             String routeMixName = attributes.getNamedItem("ROUTEMIX").getNodeValue().trim();
242             if (!parser.routeMixTags.containsKey(routeMixName))
243                 throw new NetworkException("GENERATOR: LANE " + laneName + " ROUTEMIX " + routeMixName + " in link "
244                         + linkTag.name + " not defined");
245             generatorTag.routeMixTag = parser.routeMixTags.get(routeMixName);
246             numberRouteTags++;
247         }
248 
249         if (attributes.getNamedItem("SHORTESTROUTE") != null)
250         {
251             String shortestRouteName = attributes.getNamedItem("SHORTESTROUTE").getNodeValue().trim();
252             if (!parser.shortestRouteTags.containsKey(shortestRouteName))
253                 throw new NetworkException("GENERATOR: LANE " + laneName + " SHORTESTROUTE " + shortestRouteName + " in link "
254                         + linkTag.name + " not defined");
255             generatorTag.shortestRouteTag = parser.shortestRouteTags.get(shortestRouteName);
256             numberRouteTags++;
257         }
258 
259         if (attributes.getNamedItem("SHORTESTROUTEMIX") != null)
260         {
261             String shortestRouteMixName = attributes.getNamedItem("SHORTESTROUTEMIX").getNodeValue().trim();
262             if (!parser.shortestRouteMixTags.containsKey(shortestRouteMixName))
263                 throw new NetworkException("GENERATOR: LANE " + laneName + " SHORTESTROUTEMIX " + shortestRouteMixName
264                         + " in link " + linkTag.name + " not defined");
265             generatorTag.shortestRouteMixTag = parser.shortestRouteMixTags.get(shortestRouteMixName);
266             numberRouteTags++;
267         }
268 
269         if (numberRouteTags > 1)
270             throw new SAXException(
271                     "GENERATOR: multiple ROUTE tags defined for Lane with NAME " + laneName + " of link " + linkTag.name);
272 
273         if (numberRouteTags == 0)
274             throw new SAXException(
275                     "GENERATOR: no ROUTE tags defined for Lane with NAME " + laneName + " of link " + linkTag.name);
276 
277         Node gtuColorerNode = attributes.getNamedItem("GTUCOLORER");
278         if (gtuColorerNode == null)
279             throw new SAXException("GENERATOR: missing attribute GTUCOLORER");
280         generatorTag.gtuColorer = GTUColorerTag.parseGTUColorer(gtuColorerNode.getNodeValue().trim(), parser.globalTag);
281 
282         linkTag.generatorTags.put(generatorTag.laneName, generatorTag);
283     }
284 
285     /**
286      * Make the generators for this link, if available.
287      * @param linkTag LinkTag; the parent LINK tag
288      * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
289      * @param simulator OTSSimulatorInterface; the simulator to schedule GTU generation
290      * @throws SimRuntimeException in case of simulation problems building the car generator
291      * @throws NetworkException when route generator cannot be instantiated
292      * @throws GTUException when construction of the Strategical Planner failed
293      */
294     static void makeGenerators(final LinkTag linkTag, final XmlNetworkLaneParserOld parser,
295             final OTSSimulatorInterface simulator) throws SimRuntimeException, NetworkException, GTUException
296     {
297         for (GeneratorTag generatorTag : linkTag.generatorTags.values())
298         {
299             makeGenerator(generatorTag, parser, linkTag, simulator);
300         }
301     }
302 
303     /**
304      * Make a generator.
305      * @param generatorTag GeneratorTag; XML tag for the generator to build
306      * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
307      * @param linkTag LinkTag; the parent LINK tag
308      * @param simulator OTSSimulatorInterface; the simulator to schedule GTU generation
309      * @throws SimRuntimeException in case of simulation problems building the car generator
310      * @throws NetworkException when route generator cannot be instantiated
311      * @throws GTUException when construction of the Strategical Planner failed
312      */
313     static void makeGenerator(final GeneratorTag generatorTag, final XmlNetworkLaneParserOld parser, final LinkTag linkTag,
314             final OTSSimulatorInterface simulator) throws SimRuntimeException, NetworkException, GTUException
315     {
316         Lane lane = linkTag.lanes.get(generatorTag.laneName);
317         Class<?> gtuClass = LaneBasedIndividualGTU.class;
318 
319         Generator<Route> routeGenerator;
320         if (generatorTag.routeMixTag == null)
321         {
322             List<org.opentrafficsim.core.network.Node> nodeList = new ArrayList<>();
323             for (NodeTag nodeTag : generatorTag.routeTag.routeNodeTags)
324             {
325                 nodeList.add(parser.nodeTags.get(nodeTag.name).node);
326             }
327             routeGenerator = new FixedRouteGenerator(new Route(generatorTag.laneName, nodeList));
328         }
329         else
330         {
331             List<FrequencyAndObject<Route>> probRoutes = new ArrayList<>();
332             for (int i = 0; i < generatorTag.routeMixTag.weights.size(); i++)
333             {
334                 List<org.opentrafficsim.core.network.Node> nodeList = new ArrayList<>();
335                 for (NodeTag nodeTag : generatorTag.routeMixTag.routes.get(i).routeNodeTags)
336                 {
337                     nodeList.add(parser.nodeTags.get(nodeTag.name).node);
338                 }
339                 probRoutes.add(new FrequencyAndObject<>(generatorTag.routeMixTag.weights.get(i),
340                         new Route(generatorTag.routeMixTag.routes.get(i).name, nodeList)));
341             }
342             try
343             {
344                 if (simulator.getReplication().getStream("GENERAL") == null)
345                 {
346                     simulator.getReplication().getStreams().put("GENERAL", new MersenneTwister(1L));
347                 }
348                 routeGenerator = new ProbabilisticRouteGenerator(probRoutes, simulator.getReplication().getStream("GENERAL"));
349             }
350             catch (ProbabilityException exception)
351             {
352                 throw new RuntimeException("Could not generate route mix.");
353             }
354         }
355         Time startTime = generatorTag.startTime != null ? generatorTag.startTime : Time.ZERO;
356         Time endTime = generatorTag.endTime != null ? generatorTag.endTime : new Time(Double.MAX_VALUE, TimeUnit.BASE_SECOND);
357         Length position = LinkTag.parseBeginEndPosition(generatorTag.positionStr, lane);
358         LaneBasedTacticalPlannerFactory<?> tacticalPlannerFactory =
359                 makeTacticalPlannerFactory(generatorTag, simulator.getReplication().getStream("GENERAL"));
360         LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> strategicalPlannerFactory =
361                 new LaneBasedStrategicalRoutePlannerFactory(tacticalPlannerFactory);
362         GTUGeneratorIndividualOld generator = new GTUGeneratorIndividualOld(linkTag.name + "." + generatorTag.laneName,
363                 simulator, generatorTag.gtuTag.gtuType, gtuClass, generatorTag.initialSpeedDist, generatorTag.iatDist,
364                 generatorTag.gtuTag.lengthDist, generatorTag.gtuTag.widthDist, generatorTag.gtuTag.maxSpeedDist,
365                 generatorTag.maxGTUs, startTime, endTime, lane, position, generatorTag.gtuDirection, strategicalPlannerFactory,
366                 routeGenerator, parser.network);
367 
368         // TODO GTUMix
369         // TODO RouteMix
370         // TODO ShortestRoute
371         // TODO ShortestRouteMix
372     }
373 
374     /**
375      * Factories are: IDM|MOBIL/IDM|DIRECTION/IDM|LMRS|TOLEDO.
376      * @param generatorTag GeneratorTag; the tag to parse
377      * @param stream StreamInterface; random number stream
378      * @return a LaneBasedTacticalPlannerFactory according to the tag
379      */
380     static LaneBasedTacticalPlannerFactory<?> makeTacticalPlannerFactory(final GeneratorTag generatorTag,
381             final StreamInterface stream)
382     {
383         if (generatorTag.tacticalPlannerName == null || generatorTag.tacticalPlannerName.equals("IDM"))
384         {
385             return new LaneBasedGTUFollowingTacticalPlannerFactory(new IDMPlusOld());
386         }
387         if (generatorTag.tacticalPlannerName.equals("MOBIL/IDM"))
388         {
389             return new LaneBasedCFLCTacticalPlannerFactory(new IDMPlusOld(), new Egoistic());
390         }
391         if (generatorTag.tacticalPlannerName.equals("DIRECTION/IDM"))
392         {
393             return new LaneBasedGTUFollowingDirectedChangeTacticalPlannerFactory(new IDMPlusOld());
394         }
395         if (generatorTag.tacticalPlannerName.equals("LMRS"))
396         {
397             try
398             {
399                 return new LMRSFactory(new IDMPlusFactory(stream), new DefaultLMRSPerceptionFactory());
400             }
401             catch (GTUException exception)
402             {
403                 exception.printStackTrace();
404             }
405         }
406         if (generatorTag.tacticalPlannerName.equals("TOLEDO"))
407         {
408             return new ToledoFactory();
409         }
410         System.err.println("Unknown generatorTag.tacticalPlannerName: " + generatorTag.tacticalPlannerName
411                 + ", not one of: IDM|MOBIL/IDM|DIRECTION/IDM|LMRS|TOLEDO");
412         return new LaneBasedGTUFollowingTacticalPlannerFactory(new IDMPlusOld());
413     }
414 
415     /** {@inheritDoc} */
416     @Override
417     public final String toString()
418     {
419         return "GeneratorTag [laneName=" + this.laneName + ", positionStr=" + this.positionStr + ", gtuDirection="
420                 + this.gtuDirection + ", gtuTag=" + this.gtuTag + ", gtuMixTag=" + this.gtuMixTag + ", iatDist=" + this.iatDist
421                 + ", initialSpeedDist=" + this.initialSpeedDist + ", maxGTUs=" + this.maxGTUs + ", startTime=" + this.startTime
422                 + ", endTime=" + this.endTime + ", routeTag=" + this.routeTag + ", routeMixTag=" + this.routeMixTag
423                 + ", shortestRouteTag=" + this.shortestRouteTag + ", shortestRouteMixTag=" + this.shortestRouteMixTag
424                 + ", gtuColorer=" + this.gtuColorer + "]";
425     }
426 
427 }