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 @@ -34,6 +34,7 @@ sealed interface Action {
description.type,
description.mode,
buildVariables(description.input),
description.output,
buildEvents(description.raises),
)

Expand Down Expand Up @@ -88,6 +89,7 @@ internal constructor(
val type: String,
val mode: InvocationMode,
val input: List<ContextVariable>,
val output: List<ContextVariableReferenceDescription>,
val raises: List<Event>,
) : EventRaisingAction {
override fun raises(): List<Event> = raises
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 { raiseEvents(it) }
.onSuccess {
assignServiceOutput(it, commandExecutionContext.scope.extent)
raiseEvents(it)
}
.onFailure { logger.error(it) { "service invocation failed" } }
}

Expand All @@ -96,6 +99,24 @@ internal constructor(
private fun prepareInput(extent: Extent): List<ContextVariable> =
invokeAction.input.map { it.evaluate(extent) }

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}'"
}
}
}

private fun raiseEvents(output: List<ContextVariable>) {
invokeAction.raises.forEach { eventTemplate ->
val event = eventTemplate.copy(data = output)
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 @@ -99,6 +99,7 @@ class InvokeDescription extends ActionDescription {
type: InvocationType
mode: InvocationMode = "remote"
input: Context
output: Listing<ContextVariableReferenceDescription>
raises: Listing<EventDescription>
}

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

class ContextVariableReferenceDescription {
reference: String
}

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

typealias EventChannel = "internal"|"external"|"global"|"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 @@ -29,7 +29,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 @@ -45,6 +48,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
7 changes: 6 additions & 1 deletion src/test/resources/pkl/complete/completeStateMachine.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ sm = new csml.StateMachine {
type = "increment"
mode = "local"
input { ["v"] = "x" }
output { new csml.ContextVariableReferenceDescription { reference = "s" } }
raises { new csml.Internal { topic = "e2" } }
}
}
exit {
new csml.Eval { expression = "e = true" }
new csml.Eval { expression = "f = f + s" }
}
on {
["e2"] = new csml.Transition { to = "c"; do { new csml.Eval { expression = "x = $v" } } }
Expand Down Expand Up @@ -52,5 +54,8 @@ sm = new csml.StateMachine {
}
["e"] = new csml.Terminal { entry { new csml.Eval { expression = "b = true" } } }
}
transient { ["x"] = "0" }
transient {
["x"] = "0"
["s"] = "0"
}
}
2 changes: 1 addition & 1 deletion src/test/resources/pkl/complete/main.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ collaborativeStateMachine {
stateMachines {
["completeStateMachine"] = completeStateMachine.sm
}
persistent { ["b"] = "false"; ["v"] = "0"; ["e"] = "false" }
persistent { ["b"] = "false"; ["v"] = "0"; ["e"] = "false"; ["f"] = "0" }
}
instances {
["complete"] = "completeStateMachine"
Expand Down