wolflow 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE.txt +239 -0
  4. data/README.md +393 -0
  5. data/lib/wolflow/cycle.rb +67 -0
  6. data/lib/wolflow/dsl.rb +93 -0
  7. data/lib/wolflow/errors.rb +13 -0
  8. data/lib/wolflow/exclusive_choice.rb +106 -0
  9. data/lib/wolflow/extensions.rb +20 -0
  10. data/lib/wolflow/multi_choice.rb +127 -0
  11. data/lib/wolflow/multi_merge.rb +20 -0
  12. data/lib/wolflow/operators/attribute.rb +22 -0
  13. data/lib/wolflow/operators/base.rb +67 -0
  14. data/lib/wolflow/operators/literal.rb +22 -0
  15. data/lib/wolflow/operators/operation.rb +42 -0
  16. data/lib/wolflow/recursion.rb +21 -0
  17. data/lib/wolflow/simple.rb +134 -0
  18. data/lib/wolflow/simple_merge.rb +19 -0
  19. data/lib/wolflow/start.rb +9 -0
  20. data/lib/wolflow/structured_loop.rb +106 -0
  21. data/lib/wolflow/structured_synchronized_merge.rb +45 -0
  22. data/lib/wolflow/synchronization.rb +17 -0
  23. data/lib/wolflow/task.rb +195 -0
  24. data/lib/wolflow/task_spec.rb +114 -0
  25. data/lib/wolflow/version.rb +5 -0
  26. data/lib/wolflow/workflow.rb +82 -0
  27. data/lib/wolflow/workflow_spec.rb +82 -0
  28. data/lib/wolflow.rb +25 -0
  29. data/sig/cycle.rbs +7 -0
  30. data/sig/dsl.rbs +36 -0
  31. data/sig/exclusive_choice.rbs +12 -0
  32. data/sig/multi_choice.rbs +23 -0
  33. data/sig/multi_merge.rbs +6 -0
  34. data/sig/operators/attribute.rbs +11 -0
  35. data/sig/operators/base.rbs +21 -0
  36. data/sig/operators/literal.rbs +15 -0
  37. data/sig/operators/operation.rbs +10 -0
  38. data/sig/operators.rbs +7 -0
  39. data/sig/recursion.rbs +4 -0
  40. data/sig/simple.rbs +22 -0
  41. data/sig/simple_merge.rbs +6 -0
  42. data/sig/start.rbs +4 -0
  43. data/sig/structured_loop.rbs +8 -0
  44. data/sig/structured_synchronized_merge.rbs +6 -0
  45. data/sig/synchronization.rbs +6 -0
  46. data/sig/task.rbs +66 -0
  47. data/sig/task_spec.rbs +52 -0
  48. data/sig/wolflow.rbs +18 -0
  49. data/sig/workflow.rbs +33 -0
  50. data/sig/workflow_spec.rbs +20 -0
  51. metadata +115 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 341f216c39d06eda0e99156f6513335473821dbde3529496c59b49917b5bcb7d
4
+ data.tar.gz: 32fc5ae80dca08b3c766cc220be6e26dae9ad1a3915d6e797dd33f069fdb5b01
5
+ SHA512:
6
+ metadata.gz: e172f8032758cf66ab3b8a4463b79c2b081bc12906c454111f560d74f6c625ebb36882622e1b880503df6cc0e989ab424dc50c1e1645952bc9aed9aa10c9434d
7
+ data.tar.gz: 16a23a7c0587d0ed3d4a9fd306625f4a53b72ea4b2088973ec4720fa09ae340a6859cab37f89445dae7225d9a22443733d9e2aece08d89a8381dbcc84d00c928
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.0.1] - 2024-12-18
4
+
5
+ - Initial release.
data/LICENSE.txt ADDED
@@ -0,0 +1,239 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ https://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ Copyright 2013-2017 Docker, Inc.
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License");
182
+ you may not use this file except in compliance with the License.
183
+ You may obtain a copy of the License at
184
+
185
+ https://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable law or agreed to in writing, software
188
+ distributed under the License is distributed on an "AS IS" BASIS,
189
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
+ See the License for the specific language governing permissions and
191
+ limitations under the License.
192
+
193
+
194
+ * lib/httpx/domain_name.rb
195
+
196
+ This file is derived from the implementation of punycode available at
197
+ here:
198
+
199
+ https://www.verisign.com/en_US/channel-resources/domain-registry-products/idn-sdks/index.xhtml
200
+
201
+ Copyright (C) 2000-2002 Verisign Inc., All rights reserved.
202
+
203
+ Redistribution and use in source and binary forms, with or
204
+ without modification, are permitted provided that the following
205
+ conditions are met:
206
+
207
+ 1) Redistributions of source code must retain the above copyright
208
+ notice, this list of conditions and the following disclaimer.
209
+
210
+ 2) Redistributions in binary form must reproduce the above copyright
211
+ notice, this list of conditions and the following disclaimer in
212
+ the documentation and/or other materials provided with the
213
+ distribution.
214
+
215
+ 3) Neither the name of the VeriSign Inc. nor the names of its
216
+ contributors may be used to endorse or promote products derived
217
+ from this software without specific prior written permission.
218
+
219
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
220
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
222
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
223
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
224
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
225
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
226
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
227
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
229
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
230
+ POSSIBILITY OF SUCH DAMAGE.
231
+
232
+ This software is licensed under the BSD open source license. For more
233
+ information visit www.opensource.org.
234
+
235
+ Authors:
236
+ John Colosi (VeriSign)
237
+ Srikanth Veeramachaneni (VeriSign)
238
+ Nagesh Chigurupati (Verisign)
239
+ Praveen Srinivasan(Verisign)
data/README.md ADDED
@@ -0,0 +1,393 @@
1
+ # Wolflow
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/wolflow.svg)](http://rubygems.org/gems/wolflow)
4
+ [![pipeline status](https://gitlab.com/os85/wolflow/badges/master/pipeline.svg)](https://gitlab.com/os85/wolflow/pipelines?page=1&scope=all&ref=master)
5
+ [![coverage report](https://gitlab.com/os85/wolflow/badges/master/coverage.svg?job=coverage)](https://os85.gitlab.io/wolflow/#_AllFiles)
6
+
7
+ Wolflow is a workflow engine which implements [workflow patterns](http://www.workflowpatterns.com/) in ruby.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'wolflow'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install wolflow
24
+
25
+ ## Usage
26
+
27
+ `wolflow` exposes the workflow patterns building blocks as objects. As a user, you first build a specification of your workflow (the "workflow spec") as a series of linked "task specs"; then you execute and monitor its progress using a "workflow":
28
+
29
+ ```ruby
30
+ require "wolflow"
31
+
32
+ include Wolflow
33
+
34
+ class MathSpec < Simple
35
+ def on_complete(task)
36
+ task.workflow.data[:tasks] += 1
37
+ puts("do math exercises")
38
+ super
39
+ end
40
+ end
41
+
42
+ class ScienceSpec < Simple
43
+ def on_complete(task)
44
+ task.workflow.data[:tasks] += 1
45
+ puts("do science exercises")
46
+ super
47
+ end
48
+ end
49
+
50
+ # build homework specification
51
+ wspec = WorkflowSpec.new(id: "My Homework")
52
+
53
+ # first do math, then do science homework
54
+ wspec.connect(MathSpec.new(id: "math")).connect(ScienceSpec.new(id: "science"))
55
+
56
+ # preare workflow instance
57
+ wflow = Workflow.new(id: "homework", workflow_spec: wspec)
58
+ wflow.data[:tasks] = 0
59
+
60
+ # run a task each second
61
+ while wflow.complete_one
62
+ sleep(1)
63
+ end
64
+
65
+ # or run all tasks at once
66
+ wflow.complete_all
67
+ #=> "do math exercises"
68
+ #=> "do science exercises"
69
+ puts wflow.data
70
+ #≈> {:tasks=>2}
71
+ ```
72
+
73
+ Workflow Specs, as well as Workflows, are serializable, which can be convenient for storage, or transfering representations across programs:
74
+
75
+ ```ruby
76
+ require "json"
77
+
78
+ # convert wspec to hash first, then dump to json
79
+ spec_dump = JSON.dump(Hash[wspec])
80
+ #=> => "{\"id\":\"My Homework\",\"task_specs\":[{\"id\":\"math\",\"name\":\"math_spec\",\"next_tasks\":[\"science\"]},{\"id\":\"science\",\"name\":\"science_spec\",\"next_tasks\":[]}]}"
81
+ loaded_spec = WorkflowSpec.from_hash(JSON.parse(spec_dump, symbolize_names: true))
82
+ #=> #<Wolflow::WorkflowSpec:0x000000012899e340 @id="My Homework", @....
83
+
84
+ wf_dump = JSON.dump(Hash[wflow])
85
+ #=> "{\"id\":\"homework\",\"workflow_spec\":{\"id\":\"My Homework\",\"task_specs\":[{\"id\":\"math\",\"name\":\"math_spec\",\"next_tasks\":[\"science\"]},{\"id\":\"science\",\"name\":\"science_spec\",...
86
+ loaded_wf = Workflow.from_hash(JSON.parse(wf_dump, symbolize_names: true))
87
+ ```
88
+
89
+ ## Patterns
90
+
91
+ Here are a few snippets on how to apply the workflow patterns supported by `wolflow`. For simplification purposes, assume that all snippets are preceded by:
92
+
93
+ ```ruby
94
+ require "wolflow"
95
+
96
+ include Wolflow
97
+ ```
98
+
99
+ and can be loaded into a worlfow using:
100
+
101
+ ```ruby
102
+ wf = Workflow.new(id: test, workflow_spec: spec)
103
+ # and, if applicable:
104
+ wf.complete_all
105
+ ```
106
+
107
+ ### Sequence
108
+
109
+ * http://www.workflowpatterns.com/patterns/control/basic/wcp1.php
110
+ * A task in a process in enabled after the completion of a preceding task in the same process.
111
+
112
+ ```ruby
113
+ spec = WorkflowSpec.new(id: "sequence")
114
+
115
+ spec.connect(Simple.new(id: "one")).connect(Simple.new(id: "two")) #=> returns the simple task with id "two"
116
+ ```
117
+
118
+ ### Parallel Split
119
+
120
+ * http://www.workflowpatterns.com/patterns/control/basic/wcp2.php
121
+ * The divergence of a branch into two or more parallel branches each of which execute concurrently.
122
+
123
+ ```ruby
124
+ spec = WorkflowSpec.new(id: "parallel_split")
125
+
126
+ task_one = spec.connect(Simple.new(id: "one"))
127
+
128
+ task_one.connect(Simple.new(id: "parallel_1"), Simple.new(id: "parallel_2"), Simple.new(id: "parallel_2")) #=> returns parallel tasks
129
+ ```
130
+
131
+ ### Synchronization
132
+
133
+ * http://www.workflowpatterns.com/patterns/control/basic/wcp3.php
134
+ * The convergence of two or more branches into a single subsequent branch such that the thread of control is passed to the subsequent branch when all input branches have been enabled.
135
+
136
+ ```ruby
137
+ spec = WorkflowSpec.new(id: "synchronization")
138
+
139
+ task_one = spec.connect(Simple.new(id: "one"))
140
+
141
+ parallel_tasks = task_one.connect(Simple.new(id: "parallel_1"), Simple.new(id: "parallel_2"), Simple.new(id: "parallel_2"))
142
+
143
+ sync_task = Synchronization.new(id: "sync_tasks")
144
+
145
+ sync_task.join(*parallel_tasks)
146
+ ```
147
+
148
+ ### Exclusive Choice
149
+
150
+ * http://www.workflowpatterns.com/patterns/control/basic/wcp4.php
151
+ * The divergence of a branch into two or more branches such that when the incoming branch is enabled, the thread of control is immediately passed to precisely one of the outgoing branches based on a mechanism that can select one of the outgoing branches.
152
+
153
+ ```ruby
154
+ spec = WorkflowSpec.new(id: "exclusive_choice")
155
+
156
+ task_one = spec.connect(Simple.new(id: "one"))
157
+ if_spec = Simple.new(id: "if")
158
+ else_spec = Simple.new(id: "else")
159
+
160
+ choice_spec, _ = task_one.choose(else_spec, id: "choice")
161
+ # equivalent to:
162
+ # choice_spec = ExclusiveChoice.new(id: "choice", else_tasks: [else_spec])
163
+
164
+ choice_spec.connect(->(_task) { Time.now.hour < 13 }, if_spec)
165
+ ```
166
+
167
+ ### Simple Merge
168
+
169
+ * http://www.workflowpatterns.com/patterns/control/basic/wcp5.php
170
+ * The convergence of two or more branches into a single subsequent branch such that each enablement of an incoming branch results in the thread of control being passed to the subsequent branch.
171
+
172
+ ```ruby
173
+ spec = WorkflowSpec.new(id: "simple_merge")
174
+
175
+ task_one = spec.connect(Simple.new(id: "one"))
176
+
177
+ parallel_tasks = task_one.connect(Simple.new(id: "parallel_1"), Simple.new(id: "parallel_2"), Simple.new(id: "parallel_2"))
178
+
179
+ merge_task = SimpleMerge.new(id: "merge_tasks")
180
+
181
+ merge_task.join(*parallel_tasks)
182
+ ```
183
+
184
+ ### Multi Choice
185
+
186
+ * http://www.workflowpatterns.com/patterns/control/advanced_branching/wcp6.php
187
+ * The divergence of a branch into two or more branches such that when the incoming branch is enabled, the thread of control is immediately passed to one or more of the outgoing branches based on a mechanism that selects one or more outgoing branches.
188
+
189
+ ```ruby
190
+ spec = WorkflowSpec.new(id: "multi_choice")
191
+
192
+ task_one = spec.connect(Simple.new(id: "one"))
193
+ if_spec = Simple.new(id: "if")
194
+ if2_spec = Simple.new(id: "if2")
195
+
196
+ choice_spec = MultiChoice.new(id: "choice")
197
+
198
+ choice_spec.connect(->(_task) { Time.now.hour < 13 }, if_spec)
199
+ choice_spec.connect(->(_task) { Time.now.hour < 7 }, if2_spec)
200
+ ```
201
+
202
+ ### Structured Synchronized Merge
203
+
204
+ * http://www.workflowpatterns.com/patterns/control/advanced_branching/wcp7.php
205
+ * The Structured Synchronizing Merge occurs in a structured context, i.e. there must be a single Multi-Choice construct earlier in the process model with which the Structured Synchronizing Merge is associated and it must merge all of the branches emanating from the Multi-Choice. These branches must either flow from the Structured Synchronizing Merge without any splits or joins or they must be structured in form (i.e. balanced splits and joins).
206
+
207
+ ```ruby
208
+ task_one = spec.connect(Simple.new(id: "structured_synchronized_merge"))
209
+ if_spec = Simple.new(id: "if")
210
+ if2_spec = Simple.new(id: "if2")
211
+
212
+ choice_spec = MultiChoice.new(id: "choice")
213
+
214
+ choice_spec.connect(->(_task) { Time.now.hour < 13 }, if_spec)
215
+ choice_spec.connect(->(_task) { Time.now.hour < 7 }, if2_spec)
216
+
217
+ merge_task = StructuredSynchronizingMerge.new(id: "merge_tasks")
218
+
219
+ merge_task.join(if_spec, if2_spec) #=> would raise error unless all arguments are branched from the same multi choice spec
220
+ ```
221
+
222
+ ### Multi Merge
223
+
224
+ * http://www.workflowpatterns.com/patterns/control/advanced_branching/wcp8.php
225
+ * The convergence of two or more branches into a single subsequent branch such that each enablement of an incoming branch results in the thread of control being passed to the subsequent branch.
226
+
227
+ ```ruby
228
+ spec = WorkflowSpec.new(id: "multi_merge")
229
+
230
+ task_one = spec.connect(Simple.new(id: "one"))
231
+
232
+ parallel_tasks = task_one.connect(Simple.new(id: "parallel_1"), Simple.new(id: "parallel_2"), Simple.new(id: "parallel_2"))
233
+
234
+ merge_task = MultiMerge.new(id: "merge_tasks")
235
+
236
+ merge_task.join(*parallel_tasks)
237
+ ```
238
+
239
+ ### Arbitrary Cycles
240
+
241
+ * http://www.workflowpatterns.com/patterns/control/structural/wcp10.php
242
+ * The ability to represent cycles in a process model that have more than one entry or exit point. It must be possible for individual entry and exit points to be associated with distinct branches.
243
+
244
+ ```ruby
245
+ spec = WorkflowSpec.new(id: "cycle")
246
+
247
+ task_one = spec.connect(Simple.new(id: "one"))
248
+ task_two = task_one.connect(Simple.new(id: "two"))
249
+ cycle = task_two.connect(Cycle.new(id: "cycle"))
250
+ cycle.connect(task_one)
251
+ ```
252
+
253
+ ### Structured Loop
254
+
255
+ * http://www.workflowpatterns.com/patterns/control/new/wcp21.php
256
+ * The ability to execute a task or sub-process repeatedly. The loop has either a pre-test or post-test condition associated with it that is either evaluated at the beginning or end of the loop to determine whether it should continue.
257
+
258
+ ```ruby
259
+ spec = WorkflowSpec.new(id: "structured_loop")
260
+
261
+ task_one = spec.connect(Simple.new(id: "one"))
262
+ task_two = task_one.connect(Simple.new(id: "two"))
263
+ structured_loop = task_two.connect(StructuredLoop.new(id: "structured_loop"))
264
+ structured_loop.connect(->(_) { Time.now.hour < 13 }, task_one)
265
+ structured_loop.connect_else(Simple.new(id: "three"))
266
+ ```
267
+
268
+ ### Recursion
269
+
270
+ * http://www.workflowpatterns.com/patterns/control/new/wcp22.php
271
+ * The ability of a task to invoke itself during its execution or an ancestor in terms of the overall decomposition structure with which it is associated.
272
+
273
+ ```ruby
274
+ spec = WorkflowSpec.new(id: "recursion")
275
+
276
+ task_one = spec.connect(Simple.new(id: "one"))
277
+ task_two = task_one.connect(Simple.new(id: "two"))
278
+ recursion = task_two.connect(Recursion.new(id: "recursion"))
279
+ recursion.connect(task_one)
280
+ ```
281
+
282
+ ## Operators
283
+
284
+ In the examples above, ruby lambdas are used to showcase the selection mechanism for deciding whether a given conditional branch is followed:
285
+
286
+ ```ruby
287
+ # ...
288
+ choice_spec.connect(->(_task) { Time.now.hour < 13 }, if_spec)
289
+ ```
290
+
291
+ This is not the only option though; the only requirement for these selection mechanism is that it responds to `#call(?Task)`:
292
+
293
+ ```ruby
294
+ class IsBeforeLunch
295
+ def self.call(_task)
296
+ Time.now.hour < 13
297
+ end
298
+ end
299
+ choice_spec.connect(IsBeforeLunch, if_spec)
300
+ ```
301
+
302
+ Both options wouldn't be enough though, if you need to serialize workflows (lambdas and singleton classes can't be marshalled into hashes).
303
+
304
+ `wolflow` ships with **operators**, which are low-level hash-serializable primitives which can describe the type of expression one would most commonly expect in conditionals.
305
+
306
+ There are 3 basic operators: "literals" (plain values), "attributes" (variables pointing at data in the workflow), "operations" (applies a reducer function on a sequence of arguments)
307
+ Here are a few examples:
308
+
309
+ ```ruby
310
+ require "wolflow"
311
+
312
+ include Wolflow
313
+
314
+ lit = Operators::Literal.new(value: 3) #=> 3, serializes to { type: "literal", value: 3 }"
315
+
316
+ lit2 = Operators::Literal.new(value: 4)
317
+ var = Operators::Attribute.new(name: "foo") #=> "foo", serializes to { type: "attribute", name: "foo" }
318
+ Operators::Operation.new(op: "+", members: [lit, lit2, var]) #=> "3 + 4 + foo", serializes to { type: "operation", op: "+", members: [{ type: "literal", name: "3" }, { type: "literal", name: "4" }, { type: "attribute", name: "foo" }]
319
+ ```
320
+
321
+ Then there is "base", where others extend from, and which can also be used for extending the operator language.
322
+
323
+ ```ruby
324
+ # example of custom class calculation levenshtein distance using the gem:
325
+
326
+ require "levenshtein"
327
+
328
+ class LevenshteinDistance < Operators::Base
329
+ # (strings: Array[Operators::Base], threshold: Float, **untyped) -> void
330
+ def initialize(strings:, threshold: 0.8, **args)
331
+ @strings = strings
332
+ @threshold = threshold
333
+ super(**args)
334
+ end
335
+
336
+ # (Task task) -> Float
337
+ def call(task)
338
+ string1, string2 = @strings.map { |s| s.call(task) }
339
+
340
+ Levenshtein.normalized_distance(string1, string2, @threhsold)
341
+ end
342
+
343
+ def to_hash
344
+ hs = super
345
+ hs[:threshold] = @threshold.to_hash
346
+ hs[:strings] = @strings.map(&:to_hash)
347
+ hs
348
+ end
349
+
350
+ def self.from_hash(hash)
351
+ case hash
352
+ in { threshold: Hash => threshold, strings: [*, Hash, *] => strings, ** }
353
+ super(hash.merge(threshold: Operators.from_hash(threshold), strings: strings.map{ |s| Operators.from_hash(s) }))
354
+ else
355
+ super
356
+ end
357
+ end
358
+ end
359
+
360
+ op = Operators.from_hash({ type: "levenshtein_distance", threshold: { type: "literal", value: 0.8}, strings: [{ type: "literal", value: "test_string"}, {type: "attribute", name: "foo"}]})
361
+ #=>
362
+ #<LevenshteinDistance:0x000000012a796960 ...
363
+
364
+ op.call(Task.new(data: { foo: "bar" })) #=> calculates levenshtein distance between "test_string" and "bar"
365
+ ```
366
+
367
+ ## Design Concepts
368
+
369
+ `wolflow` is designed to provide workflow engine primitives as plain ruby objects, with a simple yet extensive API which allows defining, extending, and executing workflows. It's therefore defined by what it explicitly does not support.
370
+
371
+ * **No custom business language** to build workflows. It's impossible to provide such a language which fits all use cases. It's however possible to design your own language of top of it.
372
+ * **Does not support standard workflow representations** (like BPMN) out-of-the-box. However, it's possible to build support on top of it.
373
+ * **No storage support**. There are too many options to consider in the space. However, every object encodes to / decodes from ruby hashes, so p.ex. storing them in a database with JSON support should be fairly easy.
374
+ * **No execution engine**. You may want to distribute task execution to thread pools, separate processes, background jobs.
375
+
376
+ ## Related Links/Projects
377
+
378
+ * [Workflow Patterns documentation](http://www.workflowpatterns.com/patterns/control/index.php)
379
+ * [flor](https://github.com/floraison/flor), a ruby workflow engine with its own custom workflow definition "language", with built-in storage and execution mechanisms.
380
+ * [Spiff Workflow](https://github.com/sartography/SpiffWorkflow), a python workflow engine allowing in-code workflow definitions, with BPMN and DMN support.
381
+ * [Camunda](https://camunda.com/), a workflow engine which is most commonly used "standalone", but can also be embedded in a Java (Spring Boot) application.
382
+
383
+ You can also find other interesting resources about workflow engines [here](https://github.com/meirwah/awesome-workflow-engines)
384
+
385
+ ## Development
386
+
387
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
388
+
389
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
390
+
391
+ ## Contributing
392
+
393
+ Bug reports and pull requests are welcome on GitHub at https://gitlab.com/os85/wolflow.