Conditional Flow Control
This document demonstrates how to use conditional flow features in the Kuetix workflow system, including on success when, if, else, continue on fail, and skip to.
Overview
The workflow system supports several conditional and control flow features:
- on success when: Conditional transitions that execute only when a step succeeds AND a condition is true
- if: Pre-condition that must be met before a state executes
- else: Alternate path when conditions fail
- continue on fail: Flag to continue execution even if a step fails
- skip to: Flag to skip to a specific state
WSL Syntax Examples
Example 1: Basic if Condition
workflow conditional_example {
start: ConditionalCheck
state ConditionalCheck {
if $constants.enabled == true
action services/common/process.Execute(config: "main") as Result
on success -> ProcessResult
on error -> HandleError
}
state ProcessResult {
action services/common/response.ResponseValue(message: "Processed successfully")
end ok
}
state HandleError {
action services/common/response.ResponseValue(message: "Processing failed")
end error
}
}
Example 2: Continue on Fail
workflow resilient_workflow {
start: RiskyOperation
state RiskyOperation {
continue on fail
action services/common/risky.Execute() as Result
on success -> Success
on error -> ContinueAnyway
}
state Success {
action services/common/response.ResponseValue(message: "Operation succeeded")
end ok
}
state ContinueAnyway {
action services/common/response.ResponseValue(message: "Operation failed but continuing")
end ok
}
}
Example 3: Skip To
workflow skip_example {
start: OptionalStep
state OptionalStep {
skip to
action services/common/optional.Process() as OptResult
on success -> FinalStep
}
state FinalStep {
action services/common/response.ResponseValue(message: "Done")
end ok
}
}
Example 4: On Success When
workflow quota_check {
start: CheckQuota
state CheckQuota {
action services/quota/check.UserQuota(userId: $request.userId) as QuotaResult
on success when $QuotaResult.remaining > 0 -> AllowOperation
on success when $QuotaResult.remaining == 0 -> QuotaExceeded
}
state AllowOperation {
action services/operation/execute.Process(userId: $request.userId)
end ok
}
state QuotaExceeded {
action services/response/error.QuotaExceeded(message: "No quota remaining")
end error
}
}
Example 5: Combined Attributes
const {
enabled: true,
threshold: 10
}
workflow combined_example {
start: ComplexState
state ComplexState {
if $constants.enabled == true
continue on fail
action services/common/complex.Process(data: $request.data) as ProcessResult
on success when $ProcessResult.score > $constants.threshold -> HighScorePath
on success when $ProcessResult.score <= $constants.threshold -> LowScorePath
on error -> ErrorPath
}
state HighScorePath {
action services/common/response.ResponseValue(message: "High score achieved")
end ok
}
state LowScorePath {
action services/common/response.ResponseValue(message: "Low score")
end ok
}
state ErrorPath {
action services/common/response.ResponseValue(message: "Error but continuing due to continue on fail")
end ok
}
}
Advanced Usage
Combining if with on success when
state ConditionalStep {
if $feature.enabled == true
action services/process/execute.Process() as Result
on success when $Result.score > 80 -> HighScorePath
on success -> LowScorePath
on else -> FeatureDisabled
}
Execution flow:
- First,
ifcondition is evaluated - If false, flow goes to
elsepath - If true, the step executes
- After successful execution,
on success whenis evaluated - Based on the result, flow goes to appropriate path
Key Features
- Backward Compatible: Existing flows without new attributes work unchanged
- Expression Support: Uses the same expression parser as existing conditions
- Context Access: Can access workflow context, parent context, and result values
- Flexible: Attributes can be combined for complex flow control
- WSL Native: First-class syntax support in WSL for all attributes
Common Use Cases
- if: Pre-execution validation, feature flags, environment checks
- on success when: Data validation, quota checks, business rules
- continue on fail: Resilient processing, optional operations
- skip to: Optional steps, conditional execution paths
- else: Fallback paths, error handling
JSON Equivalent (Legacy)
For reference, the WSL features map to the following JSON structure:
Example 1: on_success_when (Simple Condition)
{
"name": "validate_and_process",
"transitions": [
{
"name": "validate_data",
"to": "validate",
"on_success_when": "<<result.isValid>> == true",
"true": "process_data",
"false": "handle_invalid_data"
}
]
}
Example 2: if Condition
{
"name": "conditional_execution",
"transitions": [
{
"name": "check_enabled",
"to": "process",
"if": "<<feature.enabled>> == true",
"true": "execute_feature",
"else": "skip_feature"
}
]
}
Example 3: continue_on_fail
{
"name": "resilient_processing",
"transitions": [
{
"name": "may_fail_step",
"to": "risky_operation",
"continue_on_fail": true,
"true": "success_path",
"false": "failure_path"
}
]
}
Example 4: skipTo
{
"name": "skip_processing",
"transitions": [
{
"name": "skip_step",
"to": "optional_step",
"skipTo": true,
"true": "next_step"
}
]
}