ParseUtil.java

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

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. import org.djunits.value.vdouble.scalar.Length;
  5. import org.djutils.eval.Eval;
  6. import org.opentrafficsim.road.gtu.generator.CfBaRoomChecker;
  7. import org.opentrafficsim.road.gtu.generator.CfRoomChecker;
  8. import org.opentrafficsim.road.gtu.generator.LaneBasedGtuGenerator.RoomChecker;
  9. import org.opentrafficsim.road.gtu.generator.TtcRoomChecker;
  10. import org.opentrafficsim.road.network.factory.xml.XmlParserException;
  11. import org.opentrafficsim.xml.bindings.types.LengthBeginEndType.LengthBeginEnd;
  12. import org.opentrafficsim.xml.generated.RandomStreamSource;
  13. import org.opentrafficsim.xml.generated.RoomCheckerType;

  14. import nl.tudelft.simulation.dsol.experiment.StreamInformation;
  15. import nl.tudelft.simulation.jstats.streams.StreamInterface;

  16. /**
  17.  * Parser - Utility class for parsing using JAXB generated classes.
  18.  * <p>
  19.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  20.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  21.  * </p>
  22.  * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
  23.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  24.  */
  25. public final class ParseUtil
  26. {
  27.     /** */
  28.     private ParseUtil()
  29.     {
  30.         // utility class
  31.     }

  32.     /**
  33.      * Returns all objects of given type from the list of all objects. The returned list may be altered as it is not backed by
  34.      * the input list.
  35.      * @param objectList List&lt;?&gt;; list of objects
  36.      * @param clazz Class&lt;T&gt;; class of type of objects to return
  37.      * @param <T> type
  38.      * @return list of all objects of given type from the list of all objects
  39.      */
  40.     @SuppressWarnings("unchecked")
  41.     public static <T> List<T> getObjectsOfType(final List<?> objectList, final Class<T> clazz)
  42.     {
  43.         List<T> list = new ArrayList<>();
  44.         for (Object object : objectList)
  45.         {
  46.             if (clazz.isAssignableFrom(object.getClass()))
  47.             {
  48.                 list.add((T) object);
  49.             }
  50.         }
  51.         return list;
  52.     }

  53.     /**
  54.      * Find and return the stream belonging to te streamId.
  55.      * @param streamInformation the map with streams from the RUN tag
  56.      * @param streamSource the stream source
  57.      * @param eval Eval; expression evaluator.
  58.      * @return the stream belonging to te streamId
  59.      * @throws XmlParserException when the stream could not be found
  60.      */
  61.     public static StreamInterface findStream(final StreamInformation streamInformation, final RandomStreamSource streamSource,
  62.             final Eval eval) throws XmlParserException
  63.     {
  64.         String streamId;
  65.         if (streamSource == null || streamSource.getDefault() == null)
  66.         {
  67.             streamId = "default";
  68.         }
  69.         else if (streamSource.getGeneration() == null)
  70.         {
  71.             streamId = "generation";
  72.         }
  73.         else
  74.         {
  75.             streamId = streamSource.getDefined().get(eval);
  76.         }
  77.         if (streamInformation.getStream(streamId) == null)
  78.         {
  79.             throw new XmlParserException("Could not find stream with Id=" + streamId);
  80.         }
  81.         return streamInformation.getStream(streamId);
  82.     }
  83.    
  84.     /**
  85.      * Parse LengthBeginEnd for a Lane.
  86.      * @param lbe LengthBeginEnd; the begin, end, fraction, or offset from begin or end on the lane
  87.      * @param laneLength Length; the length of the lane
  88.      * @return the offset on the lane
  89.      */
  90.     public static Length parseLengthBeginEnd(final LengthBeginEnd lbe, final Length laneLength)
  91.     {
  92.         if (lbe.isAbsolute())
  93.         {
  94.             if (lbe.isBegin())
  95.                 return lbe.getOffset();
  96.             else
  97.                 return laneLength.minus(lbe.getOffset());
  98.         }
  99.         else
  100.         {
  101.             return laneLength.times(lbe.getFraction());
  102.         }
  103.     }

  104.     /**
  105.      * Parse room checker.
  106.      * @param roomChecker RoomCheckerType; room checker type
  107.      * @param eval Eval; expression evaluator.
  108.      * @return RoomChecker; parsed room checker
  109.      */
  110.     public static RoomChecker parseRoomChecker(final RoomCheckerType roomChecker, final Eval eval)
  111.     {
  112.         if (roomChecker == null || roomChecker.getCf() != null)
  113.         {
  114.             return new CfRoomChecker();
  115.         }
  116.         else if (roomChecker.getCfBa() != null)
  117.         {
  118.             return new CfBaRoomChecker();
  119.         }
  120.         return new TtcRoomChecker(roomChecker.getTtc().get(eval));
  121.     }
  122. }