Break.java

  1. package org.opentrafficsim.road.gtu.lane;

  2. import org.djutils.exceptions.Try;
  3. import org.opentrafficsim.core.gtu.Gtu;
  4. import org.opentrafficsim.core.gtu.perception.Perception;

  5. /**
  6.  * Utility to make debugging on a specific GTU more convenient. There is a method {@code on()} for a GTU and for perception.
  7.  * Should neither be available within the context of a method that needs to be debugged, {@code onSub()} can be used in
  8.  * combination with {@code onSuper()} at a higher-level method with a GTU or perception. <i>This class requires the user to set
  9.  * a break point in the method {@code trigger()}.</i>
  10.  * <p>
  11.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  13.  * </p>
  14.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  15.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  16.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  17.  */
  18. public final class Break
  19. {

  20.     /** Condition to allow or prevent breaking in lower-level functionality. */
  21.     private static boolean superCondition = false;

  22.     /**
  23.      * Constructor.
  24.      */
  25.     private Break()
  26.     {
  27.         //
  28.     }

  29.     /**
  30.      * Sets a break condition to true which is triggered by {@code onSub()} at a lower level where context is insufficient to
  31.      * determine the break condition.
  32.      * @param perception perception to obtain gtu from
  33.      * @param id GTU id to break on
  34.      * @param time time to break at (or after)
  35.      * @param additionalCondition additional condition
  36.      */
  37.     public static void onSuper(final Perception<?> perception, final String id, final double time,
  38.             final boolean additionalCondition)
  39.     {
  40.         Try.execute(() -> onSuper(perception.getGtu(), id, time, additionalCondition),
  41.                 "Trying to break on gtu, but gtu could not be obtained from perception.");
  42.     }

  43.     /**
  44.      * Sets a break condition to true which is triggered by {@code onSub()} at a lower level where context is insufficient to
  45.      * determine the break condition.
  46.      * @param gtu GTU
  47.      * @param id GTU id to break on
  48.      * @param time time to break at (or after)
  49.      * @param additionalCondition additional condition
  50.      */
  51.     public static void onSuper(final Gtu gtu, final String id, final double time, final boolean additionalCondition)
  52.     {
  53.         superCondition = gtu.getId().equals(id) && gtu.getSimulator().getSimulatorTime().si >= time && additionalCondition;
  54.     }

  55.     /**
  56.      * This method can be used if the context of a lower-level function does not contain the information on which to break. This
  57.      * method will only trigger a break if at a higher-level function where the context was sufficient, the break condition was
  58.      * set to true using {@code onSuper()}.
  59.      */
  60.     public static void onSub()
  61.     {
  62.         if (superCondition)
  63.         {
  64.             trigger();
  65.         }
  66.     }

  67.     /**
  68.      * This method can be used if the context of a lower-level function does not contain the information on which to break. This
  69.      * method will only trigger a break if at a higher-level function where the context was sufficient, the break condition was
  70.      * set to true using {@code onSuper()}.
  71.      * @param additionalCondition additional condition
  72.      */
  73.     public static void onSub(final boolean additionalCondition)
  74.     {
  75.         if (superCondition && additionalCondition)
  76.         {
  77.             trigger();
  78.         }
  79.     }

  80.     /**
  81.      * @param perception perception to obtain gtu from
  82.      * @param id GTU id to break on
  83.      * @param time time to break at (or after), in format ss, mm:ss or hh:mm:ss
  84.      * @param additionalCondition additional condition
  85.      */
  86.     public static void on(final Perception<?> perception, final String id, final String time, final boolean additionalCondition)
  87.     {
  88.         on(perception, id, timeFromString(time), additionalCondition);
  89.     }

  90.     /**
  91.      * Returns a double representation of a String time.
  92.      * @param time string format, ss, mm:ss or hh:mm:ss
  93.      * @return double representation of a String time
  94.      */
  95.     private static double timeFromString(final String time)
  96.     {
  97.         int index = time.indexOf(":");
  98.         if (index < 0)
  99.         {
  100.             return Double.valueOf(time) - 1e-6;
  101.         }
  102.         int index2 = time.indexOf(":", index + 1);
  103.         double h;
  104.         double m;
  105.         double s;
  106.         if (index2 < 0)
  107.         {
  108.             h = 0;
  109.             m = Double.valueOf(time.substring(0, index));
  110.             s = Double.valueOf(time.substring(index + 1));
  111.         }
  112.         else
  113.         {
  114.             h = Double.valueOf(time.substring(0, index));
  115.             m = Double.valueOf(time.substring(index + 1, index2));
  116.             s = Double.valueOf(time.substring(index2 + 1));
  117.         }
  118.         return h * 3600.0 + m * 60.0 + s - 1e-6;
  119.     }

  120.     /**
  121.      * @param perception perception to obtain gtu from
  122.      * @param id GTU id to break on
  123.      * @param time time to break at (or after)
  124.      * @param additionalCondition additional condition
  125.      */
  126.     public static void on(final Perception<?> perception, final String id, final double time, final boolean additionalCondition)
  127.     {
  128.         Try.execute(() -> on(perception.getGtu(), id, time, additionalCondition),
  129.                 "Trying to break on gtu, but gtu could not be obtained from perception.");
  130.     }

  131.     /**
  132.      * @param gtu GTU
  133.      * @param id GTU id to break on
  134.      * @param time time to break at (or after), in format ss, mm:ss or hh:mm:ss
  135.      * @param additionalCondition additional condition
  136.      */
  137.     public static void on(final Gtu gtu, final String id, final String time, final boolean additionalCondition)
  138.     {
  139.         on(gtu, id, timeFromString(time), additionalCondition);
  140.     }

  141.     /**
  142.      * @param gtu GTU
  143.      * @param id GTU id to break on
  144.      * @param time time to break at (or after)
  145.      * @param additionalCondition additional condition
  146.      */
  147.     public static void on(final Gtu gtu, final String id, final double time, final boolean additionalCondition)
  148.     {
  149.         if (gtu.getId().equals(id) && gtu.getSimulator().getSimulatorTime().si >= time && additionalCondition)
  150.         {
  151.             trigger();
  152.         }
  153.     }

  154.     /**
  155.      * Method that is invoked on a break condition. A break-point here allows the user to debug specific situations.
  156.      */
  157.     private static void trigger()
  158.     {
  159.         System.err.println("Break condition for debugging is true."); // SET BREAK POINT ON THIS LINE
  160.     }

  161. }