DefinitionsParser.java

  1. package org.opentrafficsim.road.network.factory.xml.parser;

  2. import java.util.LinkedHashMap;
  3. import java.util.Map;

  4. import org.djunits.value.vdouble.scalar.Speed;
  5. import org.djutils.logger.CategoryLogger;
  6. import org.opentrafficsim.base.logger.Cat;
  7. import org.opentrafficsim.base.parameters.ParameterType;
  8. import org.opentrafficsim.core.compatibility.GTUCompatibility;
  9. import org.opentrafficsim.core.gtu.GTUType;
  10. import org.opentrafficsim.core.network.LinkType;
  11. import org.opentrafficsim.core.network.LongitudinalDirectionality;
  12. import org.opentrafficsim.road.network.OTSRoadNetwork;
  13. import org.opentrafficsim.road.network.factory.xml.XmlParserException;
  14. import org.opentrafficsim.road.network.factory.xml.utils.ParseUtil;
  15. import org.opentrafficsim.road.network.lane.LaneType;
  16. import org.opentrafficsim.xml.generated.COMPATIBILITY;
  17. import org.opentrafficsim.xml.generated.DEFINITIONS;
  18. import org.opentrafficsim.xml.generated.GTUTEMPLATE;
  19. import org.opentrafficsim.xml.generated.GTUTEMPLATES;
  20. import org.opentrafficsim.xml.generated.GTUTYPE;
  21. import org.opentrafficsim.xml.generated.GTUTYPES;
  22. import org.opentrafficsim.xml.generated.LANETYPE;
  23. import org.opentrafficsim.xml.generated.LANETYPES;
  24. import org.opentrafficsim.xml.generated.LINKTYPE;
  25. import org.opentrafficsim.xml.generated.LINKTYPES;
  26. import org.opentrafficsim.xml.generated.PARAMETERTYPE;
  27. import org.opentrafficsim.xml.generated.ROADLAYOUT;
  28. import org.opentrafficsim.xml.generated.ROADLAYOUTS;
  29. import org.opentrafficsim.xml.generated.SPEEDLIMIT;

  30. import nl.tudelft.simulation.dsol.experiment.StreamInformation;

  31. /**
  32.  * DefinitionParser parses the XML nodes of the DEFINITIONS tag: GTUTYPE, GTUTEMPLATE, LINKTYPE, LANETYPE and ROADLAYOUT. <br>
  33.  * <br>
  34.  * Copyright (c) 2003-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
  35.  * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
  36.  * source code and binary code of this software is proprietary information of Delft University of Technology.
  37.  * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
  38.  */
  39. public final class DefinitionsParser
  40. {
  41.     /** */
  42.     private DefinitionsParser()
  43.     {
  44.         // utility class
  45.     }

  46.     /**
  47.      * Parse the DEFINITIONS tag in the OTS XML file.
  48.      * @param otsNetwork the network
  49.      * @param definitions the DEFINTIONS tag
  50.      * @param overwriteDefaults overwrite default definitions in otsNetwork or not
  51.      * @param roadLayoutMap temporary storage for the road layouts
  52.      * @param gtuTemplates map of GTU templates for the OD and/or Generators
  53.      * @param streamInformation map with stream information
  54.      * @param linkTypeSpeedLimitMap map with speed limit information per link type
  55.      * @throws XmlParserException on parsing error
  56.      */
  57.     public static void parseDefinitions(final OTSRoadNetwork otsNetwork, final DEFINITIONS definitions,
  58.             final boolean overwriteDefaults, final Map<String, ROADLAYOUT> roadLayoutMap,
  59.             final Map<String, GTUTEMPLATE> gtuTemplates, final StreamInformation streamInformation,
  60.             final Map<LinkType, Map<GTUType, Speed>> linkTypeSpeedLimitMap) throws XmlParserException
  61.     {
  62.         parseGtuTypes(definitions, otsNetwork, overwriteDefaults);
  63.         parseLinkTypes(definitions, otsNetwork, overwriteDefaults, linkTypeSpeedLimitMap);
  64.         parseLaneTypes(definitions, otsNetwork, overwriteDefaults);
  65.         parseGtuTemplates(definitions, otsNetwork, overwriteDefaults, gtuTemplates, streamInformation);
  66.         parseRoadLayouts(definitions, otsNetwork, roadLayoutMap);
  67.     }

  68.     /**
  69.      * Parse the GTUTYPES tag in the OTS XML file.
  70.      * @param definitions the DEFINTIONS tag
  71.      * @param otsNetwork the network
  72.      * @param overwriteDefaults overwrite default definitions in otsNetwork or not
  73.      * @throws XmlParserException on parsing error
  74.      */
  75.     public static void parseGtuTypes(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
  76.             final boolean overwriteDefaults) throws XmlParserException
  77.     {
  78.         for (GTUTYPES gtuTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(), GTUTYPES.class))
  79.         {
  80.             for (GTUTYPE gtuTag : gtuTypes.getGTUTYPE())
  81.             {
  82.                 GTUType networkGtuType = otsNetwork.getGtuTypes().get(gtuTag.getID());
  83.                 if (networkGtuType == null || (networkGtuType != null && !gtuTag.isDEFAULT())
  84.                         || (networkGtuType != null && gtuTag.isDEFAULT() && overwriteDefaults))
  85.                 {
  86.                     if (gtuTag.getPARENT() != null)
  87.                     {
  88.                         GTUType parent = otsNetwork.getGtuType(gtuTag.getPARENT());
  89.                         if (parent == null)
  90.                         {
  91.                             throw new XmlParserException(
  92.                                     "GTUType " + gtuTag.getID() + " parent " + gtuTag.getPARENT() + " not found");
  93.                         }
  94.                         GTUType gtuType = new GTUType(gtuTag.getID(), parent);
  95.                         CategoryLogger.filter(Cat.PARSER).trace("Added GTUType {}", gtuType);
  96.                     }
  97.                     else
  98.                     {
  99.                         GTUType gtuType = new GTUType(gtuTag.getID(), otsNetwork);
  100.                         CategoryLogger.filter(Cat.PARSER).trace("Added GTUType {}", gtuType);
  101.                     }
  102.                 }
  103.                 else
  104.                     CategoryLogger.filter(Cat.PARSER).trace("Did NOT add GTUType {}", gtuTag.getID());
  105.             }
  106.         }
  107.     }

  108.     /**
  109.      * Parse the LINKTYPES tag in the OTS XML file.
  110.      * @param definitions the DEFINTIONS tag
  111.      * @param otsNetwork the network
  112.      * @param overwriteDefaults overwrite default definitions in otsNetwork or not
  113.      * @param linkTypeSpeedLimitMap map with speed limit information per link type
  114.      * @throws XmlParserException on parsing error
  115.      */
  116.     public static void parseLinkTypes(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
  117.             final boolean overwriteDefaults, final Map<LinkType, Map<GTUType, Speed>> linkTypeSpeedLimitMap)
  118.             throws XmlParserException
  119.     {
  120.         for (LINKTYPES linkTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
  121.                 LINKTYPES.class))
  122.         {
  123.             for (LINKTYPE linkTag : linkTypes.getLINKTYPE())
  124.             {
  125.                 LinkType networkLinkType = otsNetwork.getLinkTypes().get(linkTag.getID());
  126.                 if (networkLinkType == null || (networkLinkType != null && !linkTag.isDEFAULT())
  127.                         || (networkLinkType != null && linkTag.isDEFAULT() && overwriteDefaults))
  128.                 {
  129.                     GTUCompatibility<LinkType> compatibility = new GTUCompatibility<>((LinkType) null);
  130.                     for (COMPATIBILITY compTag : linkTag.getCOMPATIBILITY())
  131.                     {
  132.                         GTUType gtuType = otsNetwork.getGtuType(compTag.getGTUTYPE());
  133.                         if (gtuType == null)
  134.                         {
  135.                             throw new XmlParserException("LinkType " + linkTag.getID() + ".compatibility: GTUType "
  136.                                     + compTag.getGTUTYPE() + " not found");
  137.                         }
  138.                         compatibility.addAllowedGTUType(gtuType,
  139.                                 LongitudinalDirectionality.valueOf(compTag.getDIRECTION().toString()));
  140.                     }
  141.                     LinkType parent = otsNetwork.getLinkType(linkTag.getPARENT());
  142.                     LinkType linkType = new LinkType(linkTag.getID(), parent, compatibility, otsNetwork);
  143.                     networkLinkType = linkType;
  144.                     CategoryLogger.filter(Cat.PARSER).trace("Added LinkType {}", linkType);
  145.                 }
  146.                 else
  147.                 {
  148.                     CategoryLogger.filter(Cat.PARSER).trace("Did NOT add LinkType {}", linkTag.getID());
  149.                 }

  150.                 linkTypeSpeedLimitMap.put(networkLinkType, new LinkedHashMap<>());
  151.                 for (SPEEDLIMIT speedLimitTag : linkTag.getSPEEDLIMIT())
  152.                 {
  153.                     GTUType gtuType = otsNetwork.getGtuType(speedLimitTag.getGTUTYPE());
  154.                     linkTypeSpeedLimitMap.get(networkLinkType).put(gtuType, speedLimitTag.getLEGALSPEEDLIMIT());
  155.                 }
  156.             }
  157.         }
  158.     }

  159.     /**
  160.      * Parse the LANETYPES tag in the OTS XML file.
  161.      * @param definitions the DEFINTIONS tag
  162.      * @param otsNetwork the network
  163.      * @param overwriteDefaults overwrite default definitions in otsNetwork or not
  164.      * @throws XmlParserException on parsing error
  165.      */
  166.     public static void parseLaneTypes(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
  167.             final boolean overwriteDefaults) throws XmlParserException
  168.     {
  169.         for (LANETYPES laneTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
  170.                 LANETYPES.class))
  171.         {
  172.             for (LANETYPE laneTag : laneTypes.getLANETYPE())
  173.             {
  174.                 LaneType networkLaneType = otsNetwork.getLaneTypes().get(laneTag.getID());
  175.                 if (networkLaneType == null || (networkLaneType != null && !laneTag.isDEFAULT())
  176.                         || (networkLaneType != null && laneTag.isDEFAULT() && overwriteDefaults))
  177.                 {
  178.                     GTUCompatibility<LaneType> compatibility = new GTUCompatibility<>((LaneType) null);
  179.                     for (COMPATIBILITY compTag : laneTag.getCOMPATIBILITY())
  180.                     {
  181.                         GTUType gtuType = otsNetwork.getGtuType(compTag.getGTUTYPE());
  182.                         if (gtuType == null)
  183.                         {
  184.                             throw new XmlParserException("LaneType " + laneTag.getID() + ".compatibility: GTUType "
  185.                                     + compTag.getGTUTYPE() + " not found");
  186.                         }
  187.                         compatibility.addAllowedGTUType(gtuType,
  188.                                 LongitudinalDirectionality.valueOf(compTag.getDIRECTION().toString()));
  189.                     }
  190.                     if (laneTag.getPARENT() != null)
  191.                     {
  192.                         LaneType parent = otsNetwork.getLaneType(laneTag.getPARENT());
  193.                         if (parent == null)
  194.                         {
  195.                             throw new XmlParserException(
  196.                                     "LaneType " + laneTag.getID() + " parent " + laneTag.getPARENT() + " not found");
  197.                         }
  198.                         LaneType laneType = new LaneType(laneTag.getID(), parent, compatibility, otsNetwork);
  199.                         CategoryLogger.filter(Cat.PARSER).trace("Added LaneType {}", laneType);
  200.                     }
  201.                     else
  202.                     {
  203.                         LaneType laneType = new LaneType(laneTag.getID(), compatibility, otsNetwork);
  204.                         CategoryLogger.filter(Cat.PARSER).trace("Added LaneType {}", laneType);
  205.                     }
  206.                 }
  207.                 else
  208.                     CategoryLogger.filter(Cat.PARSER).trace("Did NOT add LaneType {}", laneTag.getID());
  209.             }
  210.         }
  211.     }

  212.     /**
  213.      * Store the GTUTEMPLATE tags in the OTS XML file.
  214.      * @param definitions the DEFINTIONS tag
  215.      * @param otsNetwork the network
  216.      * @param overwriteDefaults overwrite default definitions in otsNetwork or not
  217.      * @param gtuTemplates the templates to be used in the OD/Generators
  218.      * @param streamInformation map with stream information
  219.      * @throws XmlParserException on parsing error
  220.      */
  221.     public static void parseGtuTemplates(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
  222.             final boolean overwriteDefaults, final Map<String, GTUTEMPLATE> gtuTemplates,
  223.             final StreamInformation streamInformation)
  224.             throws XmlParserException
  225.     {
  226.         for (GTUTEMPLATES templateTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
  227.                 GTUTEMPLATES.class))
  228.         {
  229.             for (GTUTEMPLATE templateTag : templateTypes.getGTUTEMPLATE())
  230.             {
  231.                 GTUType gtuType = otsNetwork.getGtuType(templateTag.getGTUTYPE());
  232.                 if (gtuType == null)
  233.                 {
  234.                     throw new XmlParserException(
  235.                             "GTUTemplate " + templateTag.getID() + " GTUType " + templateTag.getGTUTYPE() + " not found");
  236.                 }
  237.                 gtuTemplates.put(templateTag.getID(), templateTag);
  238.             }
  239.         }
  240.     }

  241.     /**
  242.      * Parse the ROADLAYOUTS tag in the OTS XML file.
  243.      * @param definitions the DEFINTIONS tag
  244.      * @param otsNetwork the network
  245.      * @param roadLayoutMap temporary storage for the road layouts
  246.      * @throws XmlParserException on parsing error
  247.      */
  248.     public static void parseRoadLayouts(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
  249.             final Map<String, ROADLAYOUT> roadLayoutMap) throws XmlParserException
  250.     {
  251.         for (ROADLAYOUTS roadLayoutTypes : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
  252.                 ROADLAYOUTS.class))
  253.         {
  254.             for (ROADLAYOUT layoutTag : roadLayoutTypes.getROADLAYOUT())
  255.             {
  256.                 roadLayoutMap.put(layoutTag.getID(), layoutTag);
  257.             }
  258.         }
  259.     }

  260.     /**
  261.      * Parse the PARAMETERTYPE tags in the OTS XML file.
  262.      * @param definitions the DEFINTIONS tag
  263.      * @param otsNetwork the network
  264.      * @param parameterMap map to store parameter type by id
  265.      * @throws XmlParserException if the field in a PARAMETERTYPE does not refer to a ParameterType&lt;?&gt;
  266.      */
  267.     public static void parseParameterTypes(final DEFINITIONS definitions, final OTSRoadNetwork otsNetwork,
  268.             final Map<String, ParameterType<?>> parameterMap) throws XmlParserException
  269.     {
  270.         for (PARAMETERTYPE parameterType : ParseUtil.getObjectsOfType(definitions.getIncludeAndGTUTYPESAndGTUTEMPLATES(),
  271.                 PARAMETERTYPE.class))
  272.         {
  273.             try
  274.             {
  275.                 parameterMap.put(parameterType.getID(), (ParameterType<?>) parameterType.getFIELD());
  276.             }
  277.             catch (ClassCastException exception)
  278.             {
  279.                 throw new XmlParserException("Parameter type with id " + parameterType.getID()
  280.                         + " refers to a static field that is not a ParameterType<?>.");
  281.             }
  282.         }
  283.     }

  284. }