Skip to content

Commit 53e48e7

Browse files
angelacorteDanySK
authored andcommitted
refactor: move reset params outside
1 parent ca200b7 commit 53e48e7

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

alchemist-implementationbase/src/main/kotlin/it/unibo/alchemist/model/terminators/MetricsStableForTime.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ constructor(
1818
private val timeIntervalToCheck: Time = ZERO,
1919
private val equalTimes: Long,
2020
private val metricsToCheck: (Environment<T, Position<*>>) -> Map<String, T>,
21-
): TerminationPredicate<T, Position<*>> {
21+
) : TerminationPredicate<T, Position<*>> {
2222
private var timeStabilitySuccess: Time = ZERO
2323
private var lastChecked: Time = ZERO
2424
private var equalSuccess: Long = 0
@@ -35,28 +35,29 @@ constructor(
3535
val checkedInterval = simulationTime - lastChecked
3636
return when {
3737
checkedInterval >= timeIntervalToCheck -> {
38-
val metrics: Map<String, T> = metricsToCheck(environment)
39-
require(metrics.isNotEmpty()) {
40-
"There should be at least one metric to check."
38+
val metrics: Map<String, T> = metricsToCheck(environment).also {
39+
require(it.isNotEmpty()) { "There should be at least one metric to check." }
4140
}
4241
lastChecked = simulationTime
4342
when {
4443
lastUpdatedMetrics == metrics -> {
4544
timeStabilitySuccess += checkedInterval
46-
if(timeStabilitySuccess >= stableForTime) {
47-
timeStabilitySuccess = ZERO
48-
++equalSuccess >= equalTimes
49-
} else false
45+
if (timeStabilitySuccess >= stableForTime) timeStabilitySuccess = ZERO
46+
++equalSuccess >= equalTimes
5047
}
5148
else -> {
52-
timeStabilitySuccess = ZERO
53-
equalSuccess = 0
54-
lastUpdatedMetrics = metrics
49+
reset(metrics)
5550
false
5651
}
5752
}
5853
}
5954
else -> false
6055
}
6156
}
57+
58+
private fun reset(metrics: Map<String, T>) {
59+
timeStabilitySuccess = ZERO
60+
equalSuccess = 0
61+
lastUpdatedMetrics = metrics
62+
}
6263
}

0 commit comments

Comments
 (0)