@@ -11,6 +11,8 @@ package cz.vutbr.fit.interlockSim.sim
1111
1212import cz.vutbr.fit.interlockSim.context.SimulationContext
1313import cz.vutbr.fit.interlockSim.context.SimulationContext.ReportType
14+ import cz.vutbr.fit.interlockSim.exceptions.requireSimulation
15+ import cz.vutbr.fit.interlockSim.exceptions.requireSimulationNotNull
1416import cz.vutbr.fit.interlockSim.objects.cells.InOut
1517import cz.vutbr.fit.interlockSim.objects.cells.RailSemaphore
1618import cz.vutbr.fit.interlockSim.objects.cells.RailSemaphore.Signal
@@ -87,7 +89,7 @@ class Train :
8789
8890 final override fun actions () {
8991 var where: PathSeparator = timetable.getIn()
90- assert (where != null )
92+ requireSimulationNotNull (where) { " PathSeparator from timetable.getIn() must not be null " }
9193 // out se muze rovnat in => bude vyreseno "prepojenim lokomotivy"
9294
9395 while (true ) {
@@ -102,7 +104,9 @@ class Train :
102104 separatorAction(where, current, next)
103105
104106 onNext = true
105- assert (position.isActive && pv.isActive)
107+ requireSimulation(position.isActive && pv.isActive) {
108+ " Position and velocity integration must be active"
109+ }
106110 waitUntil {
107111 // dtmin - horni odhad zmeny pri poslednim kroku numericke metody behem dobrzdovani k uzlu
108112 position.state + dtMin >= nextLength
@@ -111,7 +115,7 @@ class Train :
111115 position.state - = nextLength
112116 totalLenghtOfPreviousBlocks + = nextLength
113117 where = next!! .getSecondEnd(where)
114- assert (where != null )
118+ requireSimulationNotNull (where) { " PathSeparator from getSecondEnd() must not be null " }
115119 current = next
116120 onNext = false
117121 }
@@ -173,8 +177,10 @@ class Train :
173177 next : TrackSection ?
174178 ) {
175179 // isSeparatorInDirection accepts nullable Track parameters
176- assert (context.isSeparatorInDirection(separator as OrientedPathSeparator , next, current)) { semaphore }
177- assert (semaphore.getSignal() != null )
180+ requireSimulation(context.isSeparatorInDirection(separator as OrientedPathSeparator , next, current)) {
181+ " Separator must be in direction, semaphore: $semaphore "
182+ }
183+ requireSimulationNotNull(semaphore.getSignal()) { " Semaphore signal must not be null" }
178184 logger.info {
179185 " ${jDisco.Process .time()} SENSOR: Train $number detected at semaphore " +
180186 " ${if (semaphore.getName() != null ) semaphore.getName() else semaphore.hashCode()} , " +
@@ -185,7 +191,7 @@ class Train :
185191 // EXTENSION stanice
186192
187193 if (semaphore.getSignal() == RailSemaphore .Signal .STOP ) {
188- assert (getVelocity() >= 0 )
194+ requireSimulation (getVelocity() >= 0 ) { " Velocity must be non-negative when approaching semaphore " }
189195 logger.debug { " Train $number approaching semaphore with STOP signal, halting" }
190196 fireStop()
191197 context.report(semaphore.getSignal().toString(), this @Train, ReportType .TRAIN_EVENTS )
@@ -247,7 +253,7 @@ class Train :
247253 }
248254
249255 private fun fireStop () {
250- assert (getVelocity() >= 0 )
256+ requireSimulation (getVelocity() >= 0 ) { " Velocity must be non-negative when stopping " }
251257 front.stop()
252258 tail.stop()
253259 this @Train.stop()
@@ -271,9 +277,9 @@ class Train :
271277 semaphore : RailSemaphore ,
272278 path : Path ?
273279 ) {
274- assert (path != null )
280+ requireSimulationNotNull (path) { " Path must not be null in accelerate method " }
275281 val thisSignal: Signal = semaphore.getSignal()
276- assert (thisSignal.isAllowing()) { thisSignal }
282+ requireSimulation (thisSignal.isAllowing()) { " Signal must be allowing: $ thisSignal" }
277283 @Suppress(" RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS" )
278284 val nextSemaphore: RailSemaphore = path!! .getLastPathSemaphore()
279285 val nextSignal: Signal = nextSemaphore.getSignal()
@@ -313,15 +319,17 @@ class Train :
313319 val semaphore: RailSemaphore = where
314320 semaphoreAction(semaphore, semaphore, current, next)
315321 } else if (where == timetable.getIn() && next != null ) {
316- assert (getAcceleration() != null )
322+ requireSimulationNotNull (getAcceleration()) { " Acceleration must not be null at timetable entry " }
317323 semaphoreAction((where as InOut ).getInSemaphore(), where, current, next)
318324 } else {
319325 @Suppress(" RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS" )
320326 pathToSemaphore?.removeFirst()
321327 @Suppress(" RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS" )
322328 pathToSemaphore?.removeFirst()
323329 }
324- assert (pathToSemaphore?.getFirst() == where) { pathToSemaphore ? : " null" }
330+ requireSimulation(pathToSemaphore?.getFirst() == where) {
331+ " Path to semaphore first element must match current position: ${pathToSemaphore ? : " null" } "
332+ }
325333 if (next != null ) next.enter(this @Train)
326334 }
327335 }
@@ -358,7 +366,7 @@ class Train :
358366 Math .abs(front.getTotalDistance() - tail.getTotalDistance() - getLength()) <= maxAbsError
359367
360368 override fun report (reportObj : StringBuilder ): StringBuilder {
361- assert (reportObj != null )
369+ requireSimulationNotNull (reportObj) { " Report object must not be null" }
362370 reportObj.append(front.getTotalDistance()).append(' ' ).append(tail.getTotalDistance())
363371 return reportObj.append(' ' ).append(getLength())
364372 }
@@ -409,7 +417,7 @@ class Train :
409417 }
410418
411419 override fun iteration () {
412- assert (currentCondition != null )
420+ requireSimulationNotNull (currentCondition) { " Current condition must not be null during iteration " }
413421 accelerate = true
414422 logger.trace {
415423 " Train $number motor iteration: target speed $targetSpeed , " +
@@ -433,7 +441,7 @@ class Train :
433441 speed : Double ,
434442 test : AccelerationStopTest
435443 ) {
436- assert (speed >= 0 )
444+ requireSimulation (speed >= 0 ) { " Speed must be non-negative: $speed " }
437445 targetSpeed = speed
438446 currentCondition = AccelerationStopCondition (test)
439447 cancelAccelerating()
@@ -469,7 +477,7 @@ class Train :
469477 }
470478 context.report(" in on warning $normalSpeed " , this @Train, ReportType ._DEBUG )
471479
472- assert (getVelocity() >= 0 )
480+ requireSimulation (getVelocity() >= 0 ) { " Velocity must be non-negative in onWarning " }
473481 privateAccelerateTo(normalSpeed, AccelerationStopTest .TO_HALF_SPEED )
474482 }
475483
@@ -526,14 +534,8 @@ class Train :
526534 * @param timetable
527535 */
528536 constructor (context: SimulationContext ? , timetable: Timetable ? ) {
529- if (context == null ) {
530- throw NullPointerException (" context must not be null" )
531- }
532- if (timetable == null ) {
533- throw NullPointerException (" timetable must not be null" )
534- }
535- this .context = context
536- this .timetable = timetable
537+ this .context = requireSimulationNotNull(context) { " context must not be null" }
538+ this .timetable = requireSimulationNotNull(timetable) { " timetable must not be null" }
537539 this .length = timetable.getLength()
538540 number = ++ count
539541 trainPrefix = " Train #$number "
0 commit comments