1 package org.opentrafficsim.road.gtu.lane; 2 3 import org.djutils.exceptions.Try; 4 import org.opentrafficsim.core.gtu.GTU; 5 import org.opentrafficsim.core.gtu.perception.Perception; 6 7 /** 8 * Utility to make debugging on a specific GTU more convenient. There is a method {@code on()} for a GTU and for perception. 9 * Should neither be available within the context of a method that needs to be debugged, {@code onSub()} can be used in 10 * combination with {@code onSuper()} at a higher-level method with a GTU or perception. <i>This class requires the user to set 11 * a break point in the method {@code trigger()}.</i> 12 * <p> 13 * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br> 14 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>. 15 * <p> 16 * @version $Revision$, $LastChangedDate$, by $Author$, initial version 10 apr. 2017 <br> 17 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a> 18 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 19 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a> 20 */ 21 public final class Break 22 { 23 24 /** Condition to allow or prevent breaking in lower-level functionality. */ 25 private static boolean superCondition = true; 26 27 /** 28 * Constructor. 29 */ 30 private Break() 31 { 32 // 33 } 34 35 /** 36 * Sets a break condition to true which is triggered by {@code onSub()} at a lower level where context is insufficient to 37 * determine the break condition. 38 * @param perception Perception<?>; perception to obtain gtu from 39 * @param id String; GTU id to break on 40 * @param time double; time to break at (or after) 41 * @param additionalCondition boolean; additional condition 42 */ 43 public static void onSuper(final Perception<?> perception, final String id, final double time, 44 final boolean additionalCondition) 45 { 46 Try.execute(() -> onSuper(perception.getGtu(), id, time, additionalCondition), 47 "Trying to break on gtu, but gtu could not be obtained from perception."); 48 } 49 50 /** 51 * Sets a break condition to true which is triggered by {@code onSub()} at a lower level where context is insufficient to 52 * determine the break condition. 53 * @param gtu GTU; GTU 54 * @param id String; GTU id to break on 55 * @param time double; time to break at (or after) 56 * @param additionalCondition boolean; additional condition 57 */ 58 public static void onSuper(final GTU gtu, final String id, final double time, final boolean additionalCondition) 59 { 60 superCondition = gtu.getId().equals(id) && gtu.getSimulator().getSimulatorTime().si >= time && additionalCondition; 61 } 62 63 /** 64 * This method can be used if the context of a lower-level function does not contain the information on which to break. This 65 * method will only trigger a break if at a higher-level function where the context was sufficient, the break condition was 66 * set to true using {@code onSuper()}. 67 */ 68 public static void onSub() 69 { 70 if (superCondition) 71 { 72 trigger(); 73 } 74 } 75 76 /** 77 * This method can be used if the context of a lower-level function does not contain the information on which to break. This 78 * method will only trigger a break if at a higher-level function where the context was sufficient, the break condition was 79 * set to true using {@code onSuper()}. 80 * @param additionalCondition boolean; additional condition 81 */ 82 public static void onSub(final boolean additionalCondition) 83 { 84 if (superCondition && additionalCondition) 85 { 86 trigger(); 87 } 88 } 89 90 /** 91 * @param perception Perception<?>; perception to obtain gtu from 92 * @param id String; GTU id to break on 93 * @param time String; time to break at (or after), in format ss, mm:ss or hh:mm:ss 94 * @param additionalCondition boolean; additional condition 95 */ 96 public static void on(final Perception<?> perception, final String id, final String time, final boolean additionalCondition) 97 { 98 on(perception, id, timeFromString(time), additionalCondition); 99 } 100 101 /** 102 * Returns a double representation of a String time. 103 * @param time String; string format, ss, mm:ss or hh:mm:ss 104 * @return double representation of a String time 105 */ 106 private static double timeFromString(final String time) 107 { 108 int index = time.indexOf(":"); 109 if (index < 0) 110 { 111 return Double.valueOf(time) - 1e-6; 112 } 113 int index2 = time.indexOf(":", index + 1); 114 double h; 115 double m; 116 double s; 117 if (index2 < 0) 118 { 119 h = 0; 120 m = Double.valueOf(time.substring(0, index)); 121 s = Double.valueOf(time.substring(index + 1)); 122 } 123 else 124 { 125 h = Double.valueOf(time.substring(0, index)); 126 m = Double.valueOf(time.substring(index + 1, index2)); 127 s = Double.valueOf(time.substring(index2 + 1)); 128 } 129 return h * 3600.0 + m * 60.0 + s - 1e-6; 130 } 131 132 /** 133 * @param perception Perception<?>; perception to obtain gtu from 134 * @param id String; GTU id to break on 135 * @param time double; time to break at (or after) 136 * @param additionalCondition boolean; additional condition 137 */ 138 public static void on(final Perception<?> perception, final String id, final double time, final boolean additionalCondition) 139 { 140 Try.execute(() -> on(perception.getGtu(), id, time, additionalCondition), 141 "Trying to break on gtu, but gtu could not be obtained from perception."); 142 } 143 144 /** 145 * @param gtu GTU; GTU 146 * @param id String; GTU id to break on 147 * @param time String; time to break at (or after), in format ss, mm:ss or hh:mm:ss 148 * @param additionalCondition boolean; additional condition 149 */ 150 public static void on(final GTU gtu, final String id, final String time, final boolean additionalCondition) 151 { 152 on(gtu, id, timeFromString(time), additionalCondition); 153 } 154 155 /** 156 * @param gtu GTU; GTU 157 * @param id String; GTU id to break on 158 * @param time double; time to break at (or after) 159 * @param additionalCondition boolean; additional condition 160 */ 161 public static void on(final GTU gtu, final String id, final double time, final boolean additionalCondition) 162 { 163 if (gtu.getId().equals(id) && gtu.getSimulator().getSimulatorTime().si >= time && additionalCondition) 164 { 165 trigger(); 166 } 167 } 168 169 /** 170 * Method that is invoked on a break condition. A break-point here allows the user to debug specific situations. 171 */ 172 private static void trigger() 173 { 174 System.err.println("Break condition for debugging is true."); // SET BREAK POINT ON THIS LINE 175 } 176 177 }