Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ sealed interface Action {
description.type,
description.mode,
buildVariables(description.input),
description.output,
buildEvents(description.emits),
)

Expand Down Expand Up @@ -67,12 +68,13 @@ internal constructor(
val type: String,
val mode: InvocationMode,
val input: List<ContextVariable>,
val output: List<ContextVariableReferenceDescription>,
val emits: List<Event>,
) : EventRaisingAction {
override fun raises(): List<Event> = emits

override fun toString() =
"InvokeAction(type='$type', mode='$mode', input='$input', emits='$emits')"
"InvokeAction(type='$type', mode='$mode', input='$input', output='$output', emits='$emits')"
}

class MatchAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ internal constructor(

commandExecutionContext.coroutineScope.launch {
runCatching { service.invoke(input) }
.onSuccess { emitEvents(it) }
.onSuccess {
assignServiceOutput(it, commandExecutionContext.scope.extent)
emitEvents(it)
}
.onFailure { logger.error(it) { "service invocation failed" } }
}

Expand Down Expand Up @@ -108,6 +111,24 @@ internal constructor(
handler(event)
}
}

private fun assignServiceOutput(output: List<ContextVariable>, extent: Extent) {
invokeAction.output.forEach { variable ->
output
.firstOrNull { it.name == variable.reference }
?.let {
runCatching { extent.set(variable.reference, it.value) }
.onFailure { e ->
logger.warn(e) {
"failed to assign service output to variable '${variable.reference}'"
}
}
}
?: logger.warn {
"service output does not contain expected variable '${variable.reference}'"
}
}
}
}

class MatchActionCommand
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/pkl/csm/csml.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class InvokeDescription extends ActionDescription {
type: InvocationType
mode: InvocationMode = "remote"
input: Context
output: Listing<ContextVariableReferenceDescription>
emits: Listing<EventDescription>
}

Expand Down Expand Up @@ -131,6 +132,10 @@ class LogDescription extends ActionDescription {
message: Expression
}

class ContextVariableReferenceDescription {
reference: String
}

typealias EventTopic = String(matches(Regex(#"^[a-zA-Z_]\w*$"#)))

typealias EventChannel = "internal"|"external"|"peripheral"
Expand Down
6 changes: 5 additions & 1 deletion src/test/kotlin/at/ac/uibk/dps/cirrina/CompleteTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ class CompleteTest {
val context = ContextInMemory()
val server = mockHttpServer { input ->
val v = input.firstOrNull { it.name == "v" } ?: error("variable 'v' not found")
listOf(ContextVariable("v", (v.value as Int) + 1))
listOf(
ContextVariable("v", (v.value as Int) + 1),
ContextVariable("s", (v.value + 1) * 10),
)
}

try {
Expand All @@ -43,6 +46,7 @@ class CompleteTest {
assertEquals(100, context.get("v"))
assertEquals(true, context.get("b"))
assertEquals(true, context.get("e"))
assertEquals(50500, context.get("f"))
} finally {
server.stop(1)
}
Expand Down
13 changes: 10 additions & 3 deletions src/test/resources/pkl/complete/main.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ collaborativeStateMachine {
type = "increment"
mode = "local"
input { ["v"] = "x" }
output { new ContextVariableReferenceDescription { reference = "s" } }
emits { new Internal { topic = "e2" } }
}
}
exit { new Assign { expression = "e = true" } }
exit {
new Assign { expression = "e = true" }
new Assign { expression = "f = f + s" }
}
on {
["e2"] = new Transition {
to = "c"
Expand Down Expand Up @@ -70,10 +74,13 @@ collaborativeStateMachine {
}
["e"] = new Terminal { entry { new Assign { expression = "b = true" } } }
}
transient { ["x"] = "0" }
transient {
["x"] = "0"
["s"] = "0"
}
}
}
persistent { ["b"] = "false"; ["v"] = "0"; ["e"] = "false" }
persistent { ["b"] = "false"; ["v"] = "0"; ["e"] = "false"; ["f"] = "0" }
}
instances {
["complete"] = new Instance { stateMachineName = "completeStateMachine" }
Expand Down