Skip to content

Commit abfc0de

Browse files
committed
examples and readme
Signed-off-by: Pedro Escaleira <pedroescaleira@hotmail.com>
1 parent 7f4bc10 commit abfc0de

File tree

4 files changed

+334
-1
lines changed

4 files changed

+334
-1
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,33 @@ WorkflowValidator(Workflow(workflow)).validate()
132132
```
133133
The `validate` method will raise an exception if the provided workflow does not complaint specification.
134134

135-
You can see a full example in the [test_workflow_validator](tests/serverlessworkflow/sdk/test_workflow_validator.py) file
135+
You can see a full example in the [test_workflow_validator](tests/serverlessworkflow/sdk/test_workflow_validator.py) file
136+
137+
## Generate workflow state machine and graph
138+
139+
To generate the workflow graph diagram:
140+
141+
```python
142+
from serverlessworkflow.sdk.workflow import Workflow
143+
from serverlessworkflow.sdk.state_machine_helper import StateMachineHelper
144+
145+
def main():
146+
subflows = []
147+
with open("../tests/examples/graph.json") as f:
148+
workflow = Workflow.from_source(f.read())
149+
with open("../tests/examples/advertise-listing.json") as f:
150+
subflows.append(Workflow.from_source(f.read()))
151+
with open("../tests/examples/second-subgraph.json") as f:
152+
subflows.append(Workflow.from_source(f.read()))
153+
machine_helper = StateMachineHelper(workflow=workflow, get_actions=True, subflows=subflows)
154+
machine_helper.draw('diagram.svg')
155+
156+
157+
if __name__ == "__main__":
158+
main()
159+
```
160+
161+
The `StateMachineHelper` can be set with `get_actions` as `False` and the produced diagram will not represent the actions inside each state (it will only create a diagram with the states and their transitions). Moreover, the developer may not give any `subflows`, and they simply will not be generated.
162+
As for the `draw` method, the developer can also specify `graph_engine='mermaid'`. In that case, the method will not generate a figure, but rather the Mermaid code that can be executed, for instance, in the [Mermaid Live Editor](https://mermaid.live).
163+
164+
It is also possible to only generate the workflow state machine. An example on how to do so can be analyzed in the [state_machine_helper](serverlessworkflow/sdk/state_machine_helper.py) source code.

tests/examples/advertise-listing.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"id": "advertise-listing",
3+
"version": "0.1.0",
4+
"specVersion": "0.8",
5+
"start": "f8-advertise-listing",
6+
"states": [
7+
{
8+
"name": "f8-advertise-listing",
9+
"type": "operation",
10+
"actions": [
11+
{
12+
"functionRef": "f8"
13+
}
14+
],
15+
"transition": "advertise-parallel"
16+
},
17+
{
18+
"name": "advertise-parallel",
19+
"type": "parallel",
20+
"branches": [
21+
{
22+
"name": "send-sms",
23+
"actions": [
24+
{
25+
"functionRef": "f9"
26+
}
27+
]
28+
},
29+
{
30+
"name": "tweet",
31+
"actions": [
32+
{
33+
"functionRef": "f10"
34+
}
35+
]
36+
},
37+
{
38+
"name": "messenger-chatbot",
39+
"actions": [
40+
{
41+
"functionRef": "f11"
42+
}
43+
]
44+
},
45+
{
46+
"name": "test",
47+
"actions": [
48+
{
49+
"subFlowRef": "second-subgraph"
50+
}
51+
]
52+
}
53+
],
54+
"end": true
55+
}
56+
]
57+
}

tests/examples/graph.json

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
{
2+
"id": "graph",
3+
"version": "0.1.0",
4+
"specVersion": "0.8",
5+
"description": "Real estate listing website",
6+
"start": "entry-event",
7+
"states": [
8+
{
9+
"name": "entry-event",
10+
"type": "event",
11+
"onEvents": [
12+
{
13+
"eventRefs": [
14+
"triggerEvent"
15+
],
16+
"actions": [
17+
{
18+
"functionRef": "process-event"
19+
}
20+
]
21+
}
22+
],
23+
"stateDataFilter": {
24+
"output": "${ .transformed }"
25+
},
26+
"transition": "entry-decision"
27+
},
28+
{
29+
"name": "entry-decision",
30+
"type": "switch",
31+
"dataConditions": [
32+
{
33+
"condition": "${ .\"postListing\" == true }",
34+
"transition": "f1-upload-listing"
35+
},
36+
{
37+
"condition": "${ .\"submitDoc\" == true }",
38+
"transition": "f2-upload-verification"
39+
},
40+
{
41+
"condition": "${ .\"submitClientInfo\" == true }",
42+
"transition": "f3-upload-client"
43+
}
44+
],
45+
"defaultCondition": {
46+
"end": true
47+
}
48+
},
49+
{
50+
"name": "f1-upload-listing",
51+
"type": "operation",
52+
"actionMode": "parallel",
53+
"actions": [
54+
{
55+
"functionRef": "f1"
56+
},
57+
{
58+
"functionRef": "f2"
59+
},
60+
{
61+
"functionRef": {
62+
"refName": "f5"
63+
}
64+
}
65+
],
66+
"transition": "d1"
67+
},
68+
{
69+
"name": "d1",
70+
"type": "operation",
71+
"actions": [
72+
{
73+
"eventRef": {
74+
"triggerEventRef": "uploadPhoto",
75+
"resultEventRef": "newPhoto"
76+
}
77+
}
78+
],
79+
"transition": "f4-photo-verification"
80+
},
81+
{
82+
"name": "f4-photo-verification",
83+
"type": "operation",
84+
"actions": [
85+
{
86+
"functionRef": "f4"
87+
}
88+
],
89+
"transition": "f5-image-correction"
90+
},
91+
{
92+
"name": "f5-image-correction",
93+
"type": "operation",
94+
"actions": [
95+
{
96+
"functionRef": "f5"
97+
}
98+
],
99+
"transition": "f6-post-website"
100+
},
101+
{
102+
"name": "f6-post-website",
103+
"type": "operation",
104+
"actions": [
105+
{
106+
"functionRef": "f6"
107+
}
108+
],
109+
"transition": "advertise-listing"
110+
},
111+
{
112+
"name": "advertise-listing",
113+
"type": "operation",
114+
"actions": [
115+
{
116+
"subFlowRef": "advertise-listing"
117+
}
118+
],
119+
"end": true
120+
},
121+
{
122+
"name": "f2-upload-verification",
123+
"type": "operation",
124+
"actions": [
125+
{
126+
"functionRef": "f2"
127+
}
128+
],
129+
"transition": "d2"
130+
},
131+
{
132+
"name": "f3-upload-client",
133+
"type": "operation",
134+
"actions": [
135+
{
136+
"functionRef": "f3"
137+
}
138+
],
139+
"transition": "d2"
140+
},
141+
{
142+
"name": "d2",
143+
"type": "operation",
144+
"actions": [
145+
{
146+
"eventRef": {
147+
"triggerEventRef": "uploadInfo",
148+
"resultEventRef": "newInfo"
149+
}
150+
}
151+
],
152+
"transition": "new-info"
153+
},
154+
{
155+
"name": "new-info",
156+
"type": "parallel",
157+
"branches": [
158+
{
159+
"name": "post-website",
160+
"actions": [
161+
{
162+
"functionRef": "f7"
163+
},
164+
{
165+
"functionRef": "f6"
166+
}
167+
]
168+
},
169+
{
170+
"name": "advertise",
171+
"actions": [
172+
{
173+
"subFlowRef": "advertise-listing"
174+
}
175+
]
176+
}
177+
],
178+
"end": true
179+
}
180+
]
181+
}

tests/examples/second-subgraph.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"id": "second-subgraph",
3+
"version": "0.1.0",
4+
"specVersion": "0.8",
5+
"start": "test-switch",
6+
"states": [
7+
{
8+
"name": "test-switch",
9+
"type": "switch",
10+
"dataConditions": [
11+
{
12+
"condition": "condition1",
13+
"transition": "test"
14+
},
15+
{
16+
"condition": "condition2",
17+
"transition": "test2"
18+
}
19+
],
20+
"defaultCondition": {
21+
"end": true
22+
}
23+
},
24+
{
25+
"name": "test",
26+
"type": "parallel",
27+
"branches": [
28+
{
29+
"name": "send-sms",
30+
"actions": [
31+
{
32+
"functionRef": "f9"
33+
}
34+
]
35+
},
36+
{
37+
"name": "tweet",
38+
"actions": [
39+
{
40+
"functionRef": "f10"
41+
}
42+
]
43+
},
44+
{
45+
"name": "messenger-chatbot",
46+
"actions": [
47+
{
48+
"functionRef": "f11"
49+
}
50+
]
51+
}
52+
],
53+
"end": true
54+
},
55+
{
56+
"name": "test2",
57+
"type": "operation",
58+
"actions": [
59+
{
60+
"functionRef": "functionref"
61+
}
62+
],
63+
"end": true
64+
}
65+
]
66+
}

0 commit comments

Comments
 (0)