View Javadoc
1   package org.opentrafficsim.road.network.factory.xml.parser;
2   
3   import java.util.LinkedHashMap;
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                 {
154                     CategoryLogger.filter(Cat.PARSER).trace("Did NOT add LinkType {}", linkTag.getID());
155                 }
156 
157                 linkTypeSpeedLimitMap.put(networkLinkType, new LinkedHashMap<>());
158                 for (SPEEDLIMIT speedLimitTag : linkTag.getSPEEDLIMIT())
159                 {
160                     GTUType gtuType = otsNetwork.getGtuType(speedLimitTag.getGTUTYPE());
161                     linkTypeSpeedLimitMap.get(networkLinkType).put(gtuType, speedLimitTag.getLEGALSPEEDLIMIT());
162                 }
163             }
164         }
165     }
166 
167     /**
168      * Parse the LANETYPES tag in the OTS XML file.
169      * @param definitions the DEFINTIONS tag
170      * @param otsNetwork the network
171      * @param overwriteDefaults overwrite default definitions in otsNetwork or not
172      * @throws XmlParserException on parsing error
173      */
174     public static void parseLaneTypes(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
175             final boolean overwriteDefaults) throws XmlParserException
176     {
177         for (LANETYPES laneTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
178                 LANETYPES.class))
179         {
180             for (LANETYPE laneTag : laneTypes.getLANETYPE())
181             {
182                 LaneType networkLaneType = otsNetwork.getLaneTypes().get(laneTag.getID());
183                 if (networkLaneType == null || (networkLaneType != null && !laneTag.isDEFAULT())
184                         || (networkLaneType != null && laneTag.isDEFAULT() && overwriteDefaults))
185                 {
186                     GTUCompatibility<LaneType> compatibility = new GTUCompatibility<>((LaneType) null);
187                     for (COMPATIBILITY compTag : laneTag.getCOMPATIBILITY())
188                     {
189                         GTUType gtuType = otsNetwork.getGtuType(compTag.getGTUTYPE());
190                         if (gtuType == null)
191                         {
192                             throw new XmlParserException("LaneType " + laneTag.getID() + ".compatibility: GTUType "
193                                     + compTag.getGTUTYPE() + " not found");
194                         }
195                         compatibility.addAllowedGTUType(gtuType,
196                                 LongitudinalDirectionality.valueOf(compTag.getDIRECTION().toString()));
197                     }
198                     if (laneTag.getPARENT() != null)
199                     {
200                         LaneType parent = otsNetwork.getLaneType(laneTag.getPARENT());
201                         if (parent == null)
202                         {
203                             throw new XmlParserException(
204                                     "LaneType " + laneTag.getID() + " parent " + laneTag.getPARENT() + " not found");
205                         }
206                         LaneType laneType = new LaneType(laneTag.getID(), parent, compatibility, otsNetwork);
207                         CategoryLogger.filter(Cat.PARSER).trace("Added LaneType {}", laneType);
208                     }
209                     else
210                     {
211                         LaneType laneType = new LaneType(laneTag.getID(), compatibility, otsNetwork);
212                         CategoryLogger.filter(Cat.PARSER).trace("Added LaneType {}", laneType);
213                     }
214                 }
215                 else
216                     CategoryLogger.filter(Cat.PARSER).trace("Did NOT add LaneType {}", laneTag.getID());
217             }
218         }
219     }
220 
221     /**
222      * Store the GTUTEMPLATE tags in the OTS XML file.
223      * @param definitions the DEFINTIONS tag
224      * @param otsNetwork the network
225      * @param overwriteDefaults overwrite default definitions in otsNetwork or not
226      * @param gtuTemplates the templates to be used in the OD/Generators
227      * @param streamMap map with stream information
228      * @throws XmlParserException on parsing error
229      */
230     public static void parseGtuTemplates(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
231             final boolean overwriteDefaults, Map<String, GTUTEMPLATE> gtuTemplates, Map<String, StreamInformation> streamMap)
232             throws XmlParserException
233     {
234         for (GTUTEMPLATES templateTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
235                 GTUTEMPLATES.class))
236         {
237             for (GTUTEMPLATE templateTag : templateTypes.getGTUTEMPLATE())
238             {
239                 GTUType gtuType = otsNetwork.getGtuType(templateTag.getGTUTYPE());
240                 if (gtuType == null)
241                 {
242                     throw new XmlParserException(
243                             "GTUTemplate " + templateTag.getID() + " GTUType " + templateTag.getGTUTYPE() + " not found");
244                 }
245                 gtuTemplates.put(templateTag.getID(), templateTag);
246             }
247         }
248     }
249 
250     /**
251      * Parse the ROADLAYOUTS tag in the OTS XML file.
252      * @param definitions the DEFINTIONS tag
253      * @param otsNetwork the network
254      * @param roadLayoutMap temporary storage for the road layouts
255      * @throws XmlParserException on parsing error
256      */
257     public static void parseRoadLayouts(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
258             final Map<String, ROADLAYOUT> roadLayoutMap) throws XmlParserException
259     {
260         for (ROADLAYOUTS roadLayoutTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
261                 ROADLAYOUTS.class))
262         {
263             for (ROADLAYOUT layoutTag : roadLayoutTypes.getROADLAYOUT())
264             {
265                 roadLayoutMap.put(layoutTag.getID(), layoutTag);
266             }
267         }
268     }
269 
270     /**
271      * Parse the PARAMETERTYPE tags in the OTS XML file.
272      * @param definitions the DEFINTIONS tag
273      * @param otsNetwork the network
274      * @param parameterMap map to store parameter type by id
275      * @throws XmlParserException if the field in a PARAMETERTYPE does not refer to a ParameterType&lt;?&gt;
276      */
277     public static void parseParameterTypes(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
278             final Map<String, ParameterType<?>> parameterMap) throws XmlParserException
279     {
280         for (PARAMETERTYPE parameterType : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
281                 PARAMETERTYPE.class))
282         {
283             try
284             {
285                 parameterMap.put(parameterType.getID(), (ParameterType<?>) parameterType.getFIELD());
286             }
287             catch (ClassCastException exception)
288             {
289                 throw new XmlParserException("Parameter type with id " + parameterType.getID()
290                         + " refers to a static field that is not a ParameterType<?>.");
291             }
292         }
293     }
294 
295 }