View Javadoc
1   package org.opentrafficsim.road.network.factory.xml.parser;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.djunits.value.vdouble.scalar.Speed;
7   import org.djutils.logger.CategoryLogger;
8   import org.opentrafficsim.base.logger.Cat;
9   import org.opentrafficsim.base.parameters.ParameterType;
10  import org.opentrafficsim.core.compatibility.GTUCompatibility;
11  import org.opentrafficsim.core.gtu.GTUType;
12  import org.opentrafficsim.core.network.LinkType;
13  import org.opentrafficsim.core.network.LongitudinalDirectionality;
14  import org.opentrafficsim.road.network.OTSRoadNetwork;
15  import org.opentrafficsim.road.network.factory.xml.XmlParserException;
16  import org.opentrafficsim.road.network.factory.xml.utils.ParseUtil;
17  import org.opentrafficsim.road.network.factory.xml.utils.StreamInformation;
18  import org.opentrafficsim.road.network.lane.LaneType;
19  import org.opentrafficsim.xml.generated.COMPATIBILITY;
20  import org.opentrafficsim.xml.generated.DEFINITIONS;
21  import org.opentrafficsim.xml.generated.GTUTEMPLATE;
22  import org.opentrafficsim.xml.generated.GTUTEMPLATES;
23  import org.opentrafficsim.xml.generated.GTUTYPE;
24  import org.opentrafficsim.xml.generated.GTUTYPES;
25  import org.opentrafficsim.xml.generated.LANETYPE;
26  import org.opentrafficsim.xml.generated.LANETYPES;
27  import org.opentrafficsim.xml.generated.LINKTYPE;
28  import org.opentrafficsim.xml.generated.LINKTYPES;
29  import org.opentrafficsim.xml.generated.PARAMETERTYPE;
30  import org.opentrafficsim.xml.generated.ROADLAYOUT;
31  import org.opentrafficsim.xml.generated.ROADLAYOUTS;
32  import org.opentrafficsim.xml.generated.SPEEDLIMIT;
33  
34  /**
35   * DefinitionParser parses the XML nodes of the DEFINITIONS tag: GTUTYPE, GTUTEMPLATE, LINKTYPE, LANETYPE and ROADLAYOUT. <br>
36   * <br>
37   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
38   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
39   * source code and binary code of this software is proprietary information of Delft University of Technology.
40   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
41   */
42  public final class DefinitionsParser
43  {
44      /** */
45      private DefinitionsParser()
46      {
47          // utility class
48      }
49  
50      /**
51       * Parse the DEFINITIONS tag in the OTS XML file.
52       * @param otsNetwork the network
53       * @param definitions the DEFINTIONS tag
54       * @param overwriteDefaults overwrite default definitions in otsNetwork or not
55       * @param roadLayoutMap temporary storage for the road layouts
56       * @param gtuTemplates map of GTU templates for the OD and/or Generators
57       * @param streamMap map with stream information
58       * @param linkTypeSpeedLimitMap map with speed limit information per link type
59       * @throws XmlParserException on parsing error
60       */
61      public static void parseDefinitions(final OTSRoadNetwork otsNetwork, final DEFINITIONS definitions,
62              final boolean overwriteDefaults, final Map<String, ROADLAYOUT> roadLayoutMap,
63              final Map<String, GTUTEMPLATE> gtuTemplates, Map<String, StreamInformation> streamMap,
64              Map<LinkType, Map<GTUType, Speed>> linkTypeSpeedLimitMap) throws XmlParserException
65      {
66          parseGtuTypes(definitions, otsNetwork, overwriteDefaults);
67          parseLinkTypes(definitions, otsNetwork, overwriteDefaults, linkTypeSpeedLimitMap);
68          parseLaneTypes(definitions, otsNetwork, overwriteDefaults);
69          parseGtuTemplates(definitions, otsNetwork, overwriteDefaults, gtuTemplates, streamMap);
70          parseRoadLayouts(definitions, otsNetwork, roadLayoutMap);
71      }
72  
73      /**
74       * Parse the GTUTYPES tag in the OTS XML file.
75       * @param definitions the DEFINTIONS tag
76       * @param otsNetwork the network
77       * @param overwriteDefaults overwrite default definitions in otsNetwork or not
78       * @throws XmlParserException on parsing error
79       */
80      public static void parseGtuTypes(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
81              final boolean overwriteDefaults) throws XmlParserException
82      {
83          for (GTUTYPES gtuTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(), GTUTYPES.class))
84          {
85              for (GTUTYPE gtuTag : gtuTypes.getGTUTYPE())
86              {
87                  GTUType networkGtuType = otsNetwork.getGtuTypes().get(gtuTag.getID());
88                  if (networkGtuType == null || (networkGtuType != null && !gtuTag.isDEFAULT())
89                          || (networkGtuType != null && gtuTag.isDEFAULT() && overwriteDefaults))
90                  {
91                      if (gtuTag.getPARENT() != null)
92                      {
93                          GTUType parent = otsNetwork.getGtuType(gtuTag.getPARENT());
94                          if (parent == null)
95                          {
96                              throw new XmlParserException(
97                                      "GTUType " + gtuTag.getID() + " parent " + gtuTag.getPARENT() + " not found");
98                          }
99                          GTUType gtuType = new GTUType(gtuTag.getID(), parent);
100                         CategoryLogger.filter(Cat.PARSER).trace("Added GTUType {}", gtuType);
101                     }
102                     else
103                     {
104                         GTUType gtuType = new GTUType(gtuTag.getID(), otsNetwork);
105                         CategoryLogger.filter(Cat.PARSER).trace("Added GTUType {}", gtuType);
106                     }
107                 }
108                 else
109                     CategoryLogger.filter(Cat.PARSER).trace("Did NOT add GTUType {}", gtuTag.getID());
110             }
111         }
112     }
113 
114     /**
115      * Parse the LINKTYPES tag in the OTS XML file.
116      * @param definitions the DEFINTIONS tag
117      * @param otsNetwork the network
118      * @param overwriteDefaults overwrite default definitions in otsNetwork or not
119      * @param linkTypeSpeedLimitMap map with speed limit information per link type
120      * @throws XmlParserException on parsing error
121      */
122     public static void parseLinkTypes(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
123             final boolean overwriteDefaults, final Map<LinkType, Map<GTUType, Speed>> linkTypeSpeedLimitMap)
124             throws XmlParserException
125     {
126         for (LINKTYPES linkTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
127                 LINKTYPES.class))
128         {
129             for (LINKTYPE linkTag : linkTypes.getLINKTYPE())
130             {
131                 LinkType networkLinkType = otsNetwork.getLinkTypes().get(linkTag.getID());
132                 if (networkLinkType == null || (networkLinkType != null && !linkTag.isDEFAULT())
133                         || (networkLinkType != null && linkTag.isDEFAULT() && overwriteDefaults))
134                 {
135                     GTUCompatibility<LinkType> compatibility = new GTUCompatibility<>((LinkType) null);
136                     for (COMPATIBILITY compTag : linkTag.getCOMPATIBILITY())
137                     {
138                         GTUType gtuType = otsNetwork.getGtuType(compTag.getGTUTYPE());
139                         if (gtuType == null)
140                         {
141                             throw new XmlParserException("LinkType " + linkTag.getID() + ".compatibility: GTUType "
142                                     + compTag.getGTUTYPE() + " not found");
143                         }
144                         compatibility.addAllowedGTUType(gtuType,
145                                 LongitudinalDirectionality.valueOf(compTag.getDIRECTION().toString()));
146                     }
147                     LinkType parent = otsNetwork.getLinkType(linkTag.getPARENT());
148                     LinkType linkType = new LinkType(linkTag.getID(), parent, compatibility, otsNetwork);
149                     networkLinkType = linkType;
150                     CategoryLogger.filter(Cat.PARSER).trace("Added LinkType {}", linkType);
151                 }
152                 else
153                     CategoryLogger.filter(Cat.PARSER).trace("Did NOT add LinkType {}", linkTag.getID());
154 
155                 linkTypeSpeedLimitMap.put(networkLinkType, new HashMap<>());
156                 for (SPEEDLIMIT speedLimitTag : linkTag.getSPEEDLIMIT())
157                 {
158                     GTUType gtuType = otsNetwork.getGtuType(speedLimitTag.getGTUTYPE());
159                     linkTypeSpeedLimitMap.get(networkLinkType).put(gtuType, speedLimitTag.getLEGALSPEEDLIMIT());
160                 }
161             }
162         }
163     }
164 
165     /**
166      * Parse the LANETYPES tag in the OTS XML file.
167      * @param definitions the DEFINTIONS tag
168      * @param otsNetwork the network
169      * @param overwriteDefaults overwrite default definitions in otsNetwork or not
170      * @throws XmlParserException on parsing error
171      */
172     public static void parseLaneTypes(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
173             final boolean overwriteDefaults) throws XmlParserException
174     {
175         for (LANETYPES laneTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
176                 LANETYPES.class))
177         {
178             for (LANETYPE laneTag : laneTypes.getLANETYPE())
179             {
180                 LaneType networkLaneType = otsNetwork.getLaneTypes().get(laneTag.getID());
181                 if (networkLaneType == null || (networkLaneType != null && !laneTag.isDEFAULT())
182                         || (networkLaneType != null && laneTag.isDEFAULT() && overwriteDefaults))
183                 {
184                     GTUCompatibility<LaneType> compatibility = new GTUCompatibility<>((LaneType) null);
185                     for (COMPATIBILITY compTag : laneTag.getCOMPATIBILITY())
186                     {
187                         GTUType gtuType = otsNetwork.getGtuType(compTag.getGTUTYPE());
188                         if (gtuType == null)
189                         {
190                             throw new XmlParserException("LaneType " + laneTag.getID() + ".compatibility: GTUType "
191                                     + compTag.getGTUTYPE() + " not found");
192                         }
193                         compatibility.addAllowedGTUType(gtuType,
194                                 LongitudinalDirectionality.valueOf(compTag.getDIRECTION().toString()));
195                     }
196                     if (laneTag.getPARENT() != null)
197                     {
198                         LaneType parent = otsNetwork.getLaneType(laneTag.getPARENT());
199                         if (parent == null)
200                         {
201                             throw new XmlParserException(
202                                     "LaneType " + laneTag.getID() + " parent " + laneTag.getPARENT() + " not found");
203                         }
204                         LaneType laneType = new LaneType(laneTag.getID(), parent, compatibility, otsNetwork);
205                         CategoryLogger.filter(Cat.PARSER).trace("Added LaneType {}", laneType);
206                     }
207                     else
208                     {
209                         LaneType laneType = new LaneType(laneTag.getID(), compatibility, otsNetwork);
210                         CategoryLogger.filter(Cat.PARSER).trace("Added LaneType {}", laneType);
211                     }
212                 }
213                 else
214                     CategoryLogger.filter(Cat.PARSER).trace("Did NOT add LaneType {}", laneTag.getID());
215             }
216         }
217     }
218 
219     /**
220      * Store the GTUTEMPLATE tags in the OTS XML file.
221      * @param definitions the DEFINTIONS tag
222      * @param otsNetwork the network
223      * @param overwriteDefaults overwrite default definitions in otsNetwork or not
224      * @param gtuTemplates the templates to be used in the OD/Generators
225      * @param streamMap map with stream information
226      * @throws XmlParserException on parsing error
227      */
228     public static void parseGtuTemplates(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
229             final boolean overwriteDefaults, Map<String, GTUTEMPLATE> gtuTemplates, Map<String, StreamInformation> streamMap)
230             throws XmlParserException
231     {
232         for (GTUTEMPLATES templateTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
233                 GTUTEMPLATES.class))
234         {
235             for (GTUTEMPLATE templateTag : templateTypes.getGTUTEMPLATE())
236             {
237                 GTUType gtuType = otsNetwork.getGtuType(templateTag.getGTUTYPE());
238                 if (gtuType == null)
239                 {
240                     throw new XmlParserException(
241                             "GTUTemplate " + templateTag.getID() + " GTUType " + templateTag.getGTUTYPE() + " not found");
242                 }
243                 gtuTemplates.put(templateTag.getID(), templateTag);
244             }
245         }
246     }
247 
248     /**
249      * Parse the ROADLAYOUTS tag in the OTS XML file.
250      * @param definitions the DEFINTIONS tag
251      * @param otsNetwork the network
252      * @param roadLayoutMap temporary storage for the road layouts
253      * @throws XmlParserException on parsing error
254      */
255     public static void parseRoadLayouts(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
256             final Map<String, ROADLAYOUT> roadLayoutMap) throws XmlParserException
257     {
258         for (ROADLAYOUTS roadLayoutTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
259                 ROADLAYOUTS.class))
260         {
261             for (ROADLAYOUT layoutTag : roadLayoutTypes.getROADLAYOUT())
262             {
263                 roadLayoutMap.put(layoutTag.getID(), layoutTag);
264             }
265         }
266     }
267 
268     /**
269      * Parse the PARAMETERTYPE tags in the OTS XML file.
270      * @param definitions the DEFINTIONS tag
271      * @param otsNetwork the network
272      * @param parameterMap map to store parameter type by id
273      * @throws XmlParserException if the field in a PARAMETERTYPE does not refer to a ParameterType&lt;?&gt;
274      */
275     public static void parseParameterTypes(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
276             final Map<String, ParameterType<?>> parameterMap) throws XmlParserException
277     {
278         for (PARAMETERTYPE parameterType : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
279                 PARAMETERTYPE.class))
280         {
281             try
282             {
283                 parameterMap.put(parameterType.getID(), (ParameterType<?>) parameterType.getFIELD());
284             }
285             catch (ClassCastException exception)
286             {
287                 throw new XmlParserException("Parameter type with id " + parameterType.getID()
288                         + " refers to a static field that is not a ParameterType<?>.");
289             }
290         }
291     }
292 
293 }