Skip to content

Commit bb1258b

Browse files
committed
fix: removing in memory context creation for every event
1 parent f0c8d80 commit bb1258b

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

src/main/kotlin/at/ac/uibk/dps/cirrina/Runtime.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ constructor(
3232
@Main main: URI,
3333
persistentContext: Context?,
3434
stateMachineFactory: StateMachine.Factory,
35-
meterRegistry: MeterRegistry,
35+
val meterRegistry: MeterRegistry,
3636
) {
3737
val eventHandler = EventHandler()
3838
val extent = persistentContext?.let { Extent.of(it) } ?: Extent.of()

src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/ActionCommand.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package at.ac.uibk.dps.cirrina.execution.`object`
22

33
import at.ac.uibk.dps.cirrina.csm.Csml.EventChannel
44
import at.ac.uibk.dps.cirrina.execution.service.ServiceImplementationSelector
5+
import io.micrometer.core.instrument.MeterRegistry
6+
import io.micrometer.core.instrument.Tag
7+
import io.micrometer.core.instrument.Tags
58
import kotlinx.coroutines.CoroutineScope
69
import kotlinx.coroutines.launch
710
import mu.KotlinLogging
@@ -16,6 +19,7 @@ class ActionExecutor(
1619
private val selector: ServiceImplementationSelector,
1720
private val eventHandler: StateMachine.StateMachineEventHandler,
1821
private val coroutineScope: CoroutineScope,
22+
private val meterRegistry: MeterRegistry,
1923
) {
2024
fun execute(action: Action, scope: Scope): List<Action> =
2125
when (action) {
@@ -26,6 +30,7 @@ class ActionExecutor(
2630
is TimeoutAction -> listOf(action.triggers)
2731
is TimeoutResetAction -> emptyList()
2832
is LogAction -> executeLog(action, scope)
33+
is IncrCtrAction -> executeIncrCtr(action, scope)
2934
else -> error("unknown action type: ${action::class.simpleName}")
3035
}
3136

@@ -84,4 +89,13 @@ class ActionExecutor(
8489
action.message.execute(scope.extent).toString().also { logger.info(it) }
8590
return emptyList()
8691
}
92+
93+
private fun executeIncrCtr(action: IncrCtrAction, scope: Scope): List<Action> {
94+
val tags =
95+
Tags.of(
96+
action.tag.map { (key, value) -> Tag.of(key, value.execute(scope.extent).toString()) }
97+
)
98+
meterRegistry.counter(action.counter, tags).increment(action.by.toDouble())
99+
return emptyList()
100+
}
87101
}

src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/Extent.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package at.ac.uibk.dps.cirrina.execution.`object`
22

3-
class Extent private constructor(private val contexts: Array<Context>) {
3+
class Extent
4+
private constructor(
5+
private val contexts: Array<Context>,
6+
private val overlay: Map<String, Any?> = emptyMap(),
7+
) {
48
val high: Context? = contexts.lastOrNull()
59

610
fun setOrCreate(name: String, value: Any?): Int =
@@ -12,13 +16,16 @@ class Extent private constructor(private val contexts: Array<Context>) {
1216
?: error("variable '$name' not found in any context")
1317

1418
fun resolve(name: String): Any =
15-
contexts.lastOrNull { it.has(name) }?.get(name)
19+
overlay[name]
20+
?: contexts.lastOrNull { it.has(name) }?.get(name)
1621
?: error("variable '$name' not found in any context")
1722

18-
fun has(name: String): Boolean = contexts.any { it.has(name) }
23+
fun has(name: String): Boolean = name in overlay || contexts.any { it.has(name) }
1924

2025
fun extend(high: Context): Extent = Extent(contexts + high)
2126

27+
fun with(overlay: Map<String, Any?>): Extent = Extent(contexts, overlay)
28+
2229
companion object {
2330
fun empty() = Extent(emptyArray())
2431

src/main/kotlin/at/ac/uibk/dps/cirrina/execution/object/StateMachine.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import TimeoutActionManager
44
import at.ac.uibk.dps.cirrina.Runtime
55
import at.ac.uibk.dps.cirrina.csm.Csml.EventChannel
66
import at.ac.uibk.dps.cirrina.execution.`object`.StateMachine.Factory
7-
import at.ac.uibk.dps.cirrina.execution.provider.ContextInMemory
87
import at.ac.uibk.dps.cirrina.spec.Instance
98
import at.ac.uibk.dps.cirrina.spec.StateMachine as StateMachineSpec
109
import at.ac.uibk.dps.cirrina.spec.Transition as TransitionSpec
@@ -47,7 +46,12 @@ internal constructor(
4746
private val timeoutActionManager = TimeoutActionManager(coroutineScope)
4847
private val stateMachineEventHandler = StateMachineEventHandler(runtime.eventHandler)
4948
private val actionExecutor =
50-
ActionExecutor(runtime.selector, stateMachineEventHandler, coroutineScope)
49+
ActionExecutor(
50+
runtime.selector,
51+
stateMachineEventHandler,
52+
coroutineScope,
53+
runtime.meterRegistry,
54+
)
5155

5256
private val stateInstances =
5357
specification.vertexSet().associate { it.name to stateFactory.create(it, this) }
@@ -140,11 +144,7 @@ internal constructor(
140144
if (candidates.isEmpty()) return null
141145

142146
val evalExtent =
143-
activeState!!
144-
.extent
145-
.extend(
146-
ContextInMemory().apply { event.data.forEach { create(VAR_PREFIX + it.name, it.value) } }
147-
)
147+
activeState!!.extent.with(event.data.associate { VAR_PREFIX + it.name to it.value })
148148

149149
return trySelect(candidates, evalExtent)?.also {
150150
event.data.forEach { d -> extent.setOrCreate(VAR_PREFIX + d.name, d.value) }

0 commit comments

Comments
 (0)