Examples¶
The following simplified examples demonstrate some of the key CloudSlang concepts.
- Example 1 - User-defined Navigation and Publishing Outputs
- Example 2 - Default Navigation
- Example 3 - Subflow
- Example 4 - Loops
- Example 5 - Parallel Loop
- Example 6 - Operation Paths
Each of the examples below can be run by doing the following:
- Create a new folder.
- Create new CloudSlang(.sl) files and copy the code into them.
- Use the CLI to run the flow.
For more information on getting set up to run flows, see the CloudSlang CLI and Hello World Example sections.
Example 3 - Subflow¶
This example uses the flow from Example 1 as a subflow. It takes in
four numbers (or uses default ones) to call division_flow
twice. If
either division returns the ILLEGAL
result, navigation is routed to
the on_failure
step and the flow ends with a result of FAILURE
.
If both divisions are successful, the on_failure
step is skipped and
the flow ends with a result of SUCCESS
.
Note
To run this flow, the files from Example 1 should be
placed in the same folder as this flow file or use the --cp
flag at
the command line.
Flow - master_divider.sl
namespace: examples.divide
flow:
name: master_divider
inputs:
- dividend1: "3"
- divisor1: "2"
- dividend2: "1"
- divisor2: "0"
workflow:
- division1:
do:
division:
- input1: ${dividend1}
- input2: ${divisor1}
publish:
- ans: ${quotient}
navigate:
- SUCCESS: division2
- ILLEGAL: failure_step
- division2:
do:
division:
- input1: ${dividend2}
- input2: ${divisor2}
publish:
- ans: ${quotient}
navigate:
- SUCCESS: SUCCESS
- ILLEGAL: failure_step
- on_failure:
- failure_step:
do:
print:
- text: ${ans}
Example 4 - Loops¶
This example demonstrates the different types of values that can be looped on and various methods for handling loop breaks.
Flow - loops.sl
namespace: examples.loops
flow:
name: loops
inputs:
- sum:
default: '0'
private: true
workflow:
- fail3a:
loop:
for: value in [1,2,3,4,5]
do:
fail3:
- text: ${str(value)}
navigate:
- SUCCESS: fail3b
- FAILURE: fail3b
- fail3b:
loop:
for: value in [1,2,3,4,5]
do:
fail3:
- text: ${str(value)}
break: []
- custom3:
loop:
for: value in "1,2,3,4,5"
do:
custom3:
- text: ${value}
break:
- CUSTOM
navigate:
- CUSTOM: aggregate
- SUCCESS: skip_this
- skip_this:
do:
print:
- text: "This will not run."
navigate:
- SUCCESS: aggregate
- aggregate:
loop:
for: value in range(1,6)
do:
print:
- text: ${str(value)}
- sum
publish:
- sum: ${str(int(sum) + int(out))}
break: []
navigate:
- SUCCESS: print
- print:
do:
print:
- text: ${sum}
navigate:
- SUCCESS: SUCCESS
Operation - fail3.sl
namespace: examples.loops
operation:
name: fail3
inputs:
- text
python_action:
script: print text
results:
- FAILURE: ${int(text) == 3}
- SUCCESS
Operation - custom3.sl
namespace: examples.loops
operation:
name: custom3
inputs:
- text
python_action:
script: print text
results:
- CUSTOM: ${int(text) == 3}
- SUCCESS
Operation - print.sl
namespace: examples.loops
operation:
name: print
inputs:
- text
python_action:
script: print text
outputs:
- out: ${text}
results:
- SUCCESS
Example 5 - Parallel Loop¶
This example demonstrates the usage of a parallel loop including aggregation.
Flow - parallel_loop_aggregate.sl
namespace: examples.parallel
flow:
name: parallel_loop_aggregate
inputs:
- values: "1 2 3 4"
workflow:
- print_values:
parallel_loop:
for: value in values.split()
do:
print_branch:
- ID: ${str(value)}
publish:
- name_list: "${', '.join(map(lambda x : str(x['name']), branches_context))}"
- first_name: ${branches_context[0]['name']}
- last_name: ${branches_context[-1]['name']}
- total: "${str(sum(map(lambda x : int(x['num']), branches_context)))}"
navigate:
- SUCCESS: SUCCESS
outputs:
- name_list
- first_name
- last_name
- total
results:
- SUCCESS
Operation - print_branch.sl
namespace: examples.parallel
operation:
name: print_branch
inputs:
- ID
python_action:
script: |
name = 'branch ' + str(ID)
print 'Hello from ' + name
outputs:
- name
- num: ${ID}
Example 6 - Operation Paths¶
This example demonstrates the various ways to reference an operation or subflow from a flow step.
This example uses the following folder structure:
- examples
- paths
- flow.sl
- op1.sl
- folder_a
- op2.sl
- folder_b
- op3.sl
- folder_c
- op4.sl
- paths
Flow - flow.sl
namespace: examples.paths
imports:
alias: examples.paths.folder_b
flow:
name: flow
workflow:
- default_path:
do:
op1:
- text: "default path"
navigate:
- SUCCESS: fully_qualified_path
- fully_qualified_path:
do:
examples.paths.folder_a.op2:
- text: "fully qualified path"
navigate:
- SUCCESS: using_alias
- using_alias:
do:
alias.op3:
- text: "using alias"
navigate:
- SUCCESS: alias_continuation
- alias_continuation:
do:
alias.folder_c.op4:
- text: "alias continuation"
navigate:
- SUCCESS: SUCCESS
results:
- SUCCESS
Operation - op1.sl
namespace: examples.paths
operation:
name: op1
inputs:
- text
python_action:
script: print text
Operation - op2.sl
namespace: examples.paths.folder_a
operation:
name: op2
inputs:
- text
python_action:
script: print text
Operation - op3.sl
namespace: examples.paths.folder_b
operation:
name: op3
inputs:
- text
python_action:
script: print text
Operation - op4.sl
namespace: examples.paths.folder_b.folder_c
operation:
name: op4
inputs:
- text
python_action:
script: print text