jeeflow 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jeeflow-1.0.0/LICENSE +201 -0
- jeeflow-1.0.0/PKG-INFO +15 -0
- jeeflow-1.0.0/README.md +118 -0
- jeeflow-1.0.0/jeeflow/__init__.py +6 -0
- jeeflow-1.0.0/jeeflow/engine.py +324 -0
- jeeflow-1.0.0/jeeflow/extensions.py +88 -0
- jeeflow-1.0.0/jeeflow/memory.py +70 -0
- jeeflow-1.0.0/jeeflow/model.py +246 -0
- jeeflow-1.0.0/jeeflow/repository/__init__.py +14 -0
- jeeflow-1.0.0/jeeflow/repository/base.py +326 -0
- jeeflow-1.0.0/jeeflow/repository/mysql.py +57 -0
- jeeflow-1.0.0/jeeflow/repository/postgres.py +57 -0
- jeeflow-1.0.0/jeeflow/spi.py +48 -0
- jeeflow-1.0.0/jeeflow.egg-info/PKG-INFO +15 -0
- jeeflow-1.0.0/jeeflow.egg-info/SOURCES.txt +18 -0
- jeeflow-1.0.0/jeeflow.egg-info/dependency_links.txt +1 -0
- jeeflow-1.0.0/jeeflow.egg-info/requires.txt +11 -0
- jeeflow-1.0.0/jeeflow.egg-info/top_level.txt +1 -0
- jeeflow-1.0.0/pyproject.toml +21 -0
- jeeflow-1.0.0/setup.cfg +4 -0
jeeflow-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
jeeflow-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jeeflow
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Lightweight async workflow engine - Python implementation
|
|
5
|
+
Requires-Python: >=3.10
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Provides-Extra: mysql
|
|
8
|
+
Requires-Dist: aiomysql>=0.2.0; extra == "mysql"
|
|
9
|
+
Provides-Extra: postgres
|
|
10
|
+
Requires-Dist: asyncpg>=0.29.0; extra == "postgres"
|
|
11
|
+
Provides-Extra: dev
|
|
12
|
+
Requires-Dist: pytest; extra == "dev"
|
|
13
|
+
Requires-Dist: pytest-asyncio; extra == "dev"
|
|
14
|
+
Requires-Dist: aiomysql; extra == "dev"
|
|
15
|
+
Dynamic: license-file
|
jeeflow-1.0.0/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# jeeflow · Python
|
|
2
|
+
|
|
3
|
+
轻量级异步工作流引擎 — Python 实现,对齐 [jeeflow SPEC](https://jeeflow-doc.mldong.com/spec/)。
|
|
4
|
+
|
|
5
|
+
## 快速开始
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install jeeflow
|
|
9
|
+
|
|
10
|
+
# 启动 demo
|
|
11
|
+
cd demo
|
|
12
|
+
python main.py
|
|
13
|
+
# → http://localhost:8100
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## 项目结构
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
jeeflow/
|
|
20
|
+
├── model.py # 域类型 + LogicFlow JSON 解析
|
|
21
|
+
├── spi.py # SPI 接口(ProcessRepository / UserProvider / IDGenerator / ExpressionEvaluator)
|
|
22
|
+
├── engine.py # 引擎核心(EngineImpl)
|
|
23
|
+
├── extensions.py # 扩展体系(拦截器 / 事件 / HandlerRegistry)
|
|
24
|
+
├── memory.py # 内存仓储(MemoryRepository)
|
|
25
|
+
├── repository/ # JDBC 多库实现(base 共享核心 + mysql/postgres 适配器)
|
|
26
|
+
├── __init__.py
|
|
27
|
+
tests/
|
|
28
|
+
├── spec_test.py # 10 项 SPEC 合规测试
|
|
29
|
+
demo/
|
|
30
|
+
├── main.py # FastAPI 演示站
|
|
31
|
+
├── web/
|
|
32
|
+
│ └── index.html # 前端仪表盘
|
|
33
|
+
pyproject.toml # 项目配置
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## 引擎使用
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from jeeflow import EngineImpl, MemoryRepository
|
|
40
|
+
from jeeflow.model import parse_flow_model
|
|
41
|
+
import json
|
|
42
|
+
|
|
43
|
+
repo = MemoryRepository()
|
|
44
|
+
engine = EngineImpl(repo)
|
|
45
|
+
|
|
46
|
+
# 加载流程定义(LogicFlow JSON)
|
|
47
|
+
with open("flow.json") as f:
|
|
48
|
+
flow = parse_flow_model(json.load(f))
|
|
49
|
+
|
|
50
|
+
# 启动流程
|
|
51
|
+
inst = await engine.start_process_instance_by_id(define_id, "user1", {"amount": 3000})
|
|
52
|
+
|
|
53
|
+
# 完成任务
|
|
54
|
+
inst = await engine.execute_process_task(task_id, "user1")
|
|
55
|
+
|
|
56
|
+
# 驳回
|
|
57
|
+
inst = await engine.execute_and_jump_to_end(task_id, "user1")
|
|
58
|
+
|
|
59
|
+
# 跳转
|
|
60
|
+
inst = await engine.execute_and_jump_task(task_id, "user1", target_task_name="previous")
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## 扩展
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from jeeflow import EngineExtensions, FlowInterceptor, HandlerRegistry, EventType
|
|
67
|
+
|
|
68
|
+
class MyInterceptor(FlowInterceptor):
|
|
69
|
+
async def pre_handle(self, node, inst): return True
|
|
70
|
+
async def post_handle(self, node, inst): pass
|
|
71
|
+
@property
|
|
72
|
+
def order(self): return 10
|
|
73
|
+
|
|
74
|
+
async def on_event(evt):
|
|
75
|
+
print(f"[{evt.type.value}] instance={evt.instanceId}")
|
|
76
|
+
|
|
77
|
+
engine.set_extensions(EngineExtensions(
|
|
78
|
+
interceptors=[MyInterceptor()],
|
|
79
|
+
event_listener=on_event,
|
|
80
|
+
registry=HandlerRegistry(),
|
|
81
|
+
))
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## SPI 接口
|
|
85
|
+
|
|
86
|
+
| 接口 | 说明 | 必须 |
|
|
87
|
+
|------|------|------|
|
|
88
|
+
| `ProcessRepository` | 流程数据持久化 | ✅ |
|
|
89
|
+
| `UserProvider` | 用户信息获取 | 可选 |
|
|
90
|
+
| `IDGenerator` | ID 生成(默认时间戳) | 可选 |
|
|
91
|
+
| `ExpressionEvaluator` | 决策表达式求值 | 可选 |
|
|
92
|
+
|
|
93
|
+
## 运行测试
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
pip install pytest pytest-asyncio
|
|
97
|
+
pytest tests/ -v
|
|
98
|
+
# 10 passed
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## 与其他语言版本对比
|
|
102
|
+
|
|
103
|
+
| 特性 | Java | Go | Node.js | Python |
|
|
104
|
+
|------|------|-----|---------|--------|
|
|
105
|
+
| 引擎核心 | ✅ | ✅ | ✅ | ✅ |
|
|
106
|
+
| 拦截器 | ✅ | ✅ | ✅ | ✅ |
|
|
107
|
+
| 事件监听 | ✅ | ✅ | ✅ | ✅ |
|
|
108
|
+
| HandlerRegistry | ✅ | ✅ | ✅ | ✅ |
|
|
109
|
+
| 会签 | ✅ | ✅ | ✅ | ✅ |
|
|
110
|
+
| 决策表达式 | ✅ | ✅ | ✅ | ✅ |
|
|
111
|
+
| Fork/Join | ✅ | ✅ | ✅ | ✅ |
|
|
112
|
+
| 驳回/跳转 | ✅ | ✅ | ✅ | ✅ |
|
|
113
|
+
| Demo + 前端 | ✅ | ✅ | ✅ | ✅ |
|
|
114
|
+
| SPEC 合规测试 | ✅ | ✅ | ✅ | ✅ |
|
|
115
|
+
|
|
116
|
+
## 许可
|
|
117
|
+
|
|
118
|
+
Apache 2.0
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"""jeeflow — lightweight async workflow engine (Python)"""
|
|
2
|
+
from .engine import Engine, EngineImpl
|
|
3
|
+
from .extensions import EngineExtensions, FlowInterceptor, HandlerRegistry, EventType, ProcessEvent
|
|
4
|
+
from .memory import MemoryRepository
|
|
5
|
+
from .repository import JdbcRepository, TsIDGenerator, MySqlAdapter, PostgresAdapter
|
|
6
|
+
from .spi import ProcessRepository, UserProvider, IDGenerator, ExpressionEvaluator
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
"""引擎核心——对标 Java EngineImpl"""
|
|
2
|
+
import json, time, random
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from typing import Any, Optional
|
|
5
|
+
from .model import (
|
|
6
|
+
FlowModel, FlowNode, FlowEdge,
|
|
7
|
+
TYPE_START, TYPE_END, TYPE_TASK, TYPE_DECISION, TYPE_FORK, TYPE_JOIN, TYPE_CUSTOM,
|
|
8
|
+
ProcessInstance, ProcessTask, ProcessDefine,
|
|
9
|
+
InstanceState, TaskState,
|
|
10
|
+
parse_flow_model,
|
|
11
|
+
)
|
|
12
|
+
from .spi import ProcessRepository, UserProvider, IDGenerator, ExpressionEvaluator
|
|
13
|
+
from .extensions import EngineExtensions, EventType, ProcessEvent
|
|
14
|
+
|
|
15
|
+
KEY_SUBMIT_TYPE = "submitType"
|
|
16
|
+
KEY_BUSINESS_NO = "BUSINESS_NO"
|
|
17
|
+
KEY_USER_ID = "u_userId"
|
|
18
|
+
KEY_REAL_NAME = "u_realName"
|
|
19
|
+
KEY_DEPT_ID = "u_deptId"
|
|
20
|
+
KEY_DEPT_NAME = "u_deptName"
|
|
21
|
+
KEY_POST_ID = "u_postId"
|
|
22
|
+
KEY_POST_NAME = "u_postName"
|
|
23
|
+
|
|
24
|
+
class Engine:
|
|
25
|
+
"""引擎接口"""
|
|
26
|
+
|
|
27
|
+
async def start_process_instance_by_id(self, define_id: int, operator: str, args: dict[str, Any] = None) -> ProcessInstance: ...
|
|
28
|
+
async def execute_process_task(self, task_id: int, operator: str, args: dict[str, Any] = None) -> ProcessInstance: ...
|
|
29
|
+
async def execute_and_jump_to_end(self, task_id: int, operator: str, args: dict[str, Any] = None) -> ProcessInstance: ...
|
|
30
|
+
async def execute_and_jump_task(self, task_id: int, operator: str, args: dict[str, Any] = None, target_task_name: str = None) -> ProcessInstance: ...
|
|
31
|
+
async def execute_and_jump_to_first_task_node(self, task_id: int, operator: str, args: dict[str, Any] = None) -> ProcessInstance: ...
|
|
32
|
+
|
|
33
|
+
class EngineImpl(Engine):
|
|
34
|
+
def __init__(self, repo: ProcessRepository, user_prov: UserProvider = None,
|
|
35
|
+
id_gen: IDGenerator = None, expr_eval: ExpressionEvaluator = None):
|
|
36
|
+
self.repo = repo
|
|
37
|
+
self.user_prov = user_prov
|
|
38
|
+
self.id_gen = id_gen
|
|
39
|
+
self.expr_eval = expr_eval
|
|
40
|
+
self.ext: Optional[EngineExtensions] = None
|
|
41
|
+
|
|
42
|
+
def set_extensions(self, ext: EngineExtensions): self.ext = ext
|
|
43
|
+
|
|
44
|
+
# ─── Start ────────────────────────────────────────────────────────────────
|
|
45
|
+
|
|
46
|
+
async def start_process_instance_by_id(self, define_id: int, operator: str, args: dict[str, Any] = None) -> ProcessInstance:
|
|
47
|
+
def_ = await self.repo.find_define_by_id(define_id)
|
|
48
|
+
if not def_: raise ValueError(f"define not found: {define_id}")
|
|
49
|
+
flow = parse_flow_model(json.loads(def_.content))
|
|
50
|
+
vars_ = {**(args or {})}
|
|
51
|
+
await self._add_user_info(operator, vars_)
|
|
52
|
+
inst = ProcessInstance(id=self._next_id(), defineId=define_id, operator=operator,
|
|
53
|
+
variables=vars_, createTime=datetime.now(), updateTime=datetime.now(),
|
|
54
|
+
createUser=operator, updateUser=operator,
|
|
55
|
+
businessNo=str(vars_.get(KEY_BUSINESS_NO, "")))
|
|
56
|
+
await self.repo.save_instance(inst)
|
|
57
|
+
await self._fire_event(ProcessEvent(type=EventType.PROCESS_START, instanceId=inst.id, operator=operator))
|
|
58
|
+
start_node = _find_by_type(flow, TYPE_START)
|
|
59
|
+
if not start_node: raise ValueError("no start node")
|
|
60
|
+
for node in _follow_edges(flow, start_node.id):
|
|
61
|
+
await self._execute_node(flow, inst, node, operator, vars_)
|
|
62
|
+
return await self.repo.find_instance_by_id(inst.id)
|
|
63
|
+
|
|
64
|
+
# ─── Execute ──────────────────────────────────────────────────────────────
|
|
65
|
+
|
|
66
|
+
async def execute_process_task(self, task_id: int, operator: str, args: dict[str, Any] = None) -> ProcessInstance:
|
|
67
|
+
task, inst = await self._load_and_check(task_id, operator)
|
|
68
|
+
vars_ = {**inst.variables, **task.variables, **(args or {})}
|
|
69
|
+
await self._add_user_info(operator, vars_)
|
|
70
|
+
now = datetime.now()
|
|
71
|
+
# 聚合根:完成任务(子实体状态转换 + 实例变量合并)
|
|
72
|
+
inst.complete_task(task, operator, vars_, now)
|
|
73
|
+
await self.repo.update_task(task)
|
|
74
|
+
await self._fire_event(ProcessEvent(EventType.TASK_COMPLETE, inst.id, task.id, task.taskName, operator))
|
|
75
|
+
|
|
76
|
+
def_ = await self.repo.find_define_by_id(inst.defineId)
|
|
77
|
+
flow = parse_flow_model(json.loads(def_.content))
|
|
78
|
+
inst.variables = vars_
|
|
79
|
+
await self.repo.update_instance(inst)
|
|
80
|
+
|
|
81
|
+
cur_node = _find_node(flow, task.taskName)
|
|
82
|
+
if cur_node:
|
|
83
|
+
ct = cur_node.properties.get("countersignType", "")
|
|
84
|
+
if ct == "SEQUENTIAL":
|
|
85
|
+
doing = await self.repo.find_doing_tasks(inst.id)
|
|
86
|
+
if not doing:
|
|
87
|
+
actors, lc = _get_cs_state(vars_, cur_node.id)
|
|
88
|
+
if actors and lc + 1 < len(actors):
|
|
89
|
+
# 聚合根:创建串行会签下一步任务
|
|
90
|
+
nt = inst.create_task(self._next_id(), cur_node.id, cur_node.text.get("value", ""),
|
|
91
|
+
actors[lc + 1], operator, cur_node.properties.get("form", ""), now)
|
|
92
|
+
nt.variables = {f"operatorList_{cur_node.id}": actors, f"loopCounter_{cur_node.id}": lc + 1,
|
|
93
|
+
f"nrOfInstances_{cur_node.id}": len(actors)}
|
|
94
|
+
await self.repo.save_task(nt)
|
|
95
|
+
return await self.repo.find_instance_by_id(inst.id)
|
|
96
|
+
else:
|
|
97
|
+
return await self.repo.find_instance_by_id(inst.id)
|
|
98
|
+
if ct in ("PARALLEL",) or ct.startswith("RATIO"):
|
|
99
|
+
doing = await self.repo.find_doing_tasks(inst.id)
|
|
100
|
+
if doing: return await self.repo.find_instance_by_id(inst.id)
|
|
101
|
+
|
|
102
|
+
for node in _follow_edges(flow, cur_node.id):
|
|
103
|
+
if node.type == TYPE_END:
|
|
104
|
+
# 聚合根:流程完成
|
|
105
|
+
inst.finish(datetime.now())
|
|
106
|
+
inst.variables = vars_
|
|
107
|
+
await self.repo.update_instance(inst)
|
|
108
|
+
await self._fire_event(ProcessEvent(EventType.PROCESS_FINISH, inst.id, operator=operator))
|
|
109
|
+
else:
|
|
110
|
+
await self._execute_node(flow, inst, node, operator, vars_)
|
|
111
|
+
return await self.repo.find_instance_by_id(inst.id)
|
|
112
|
+
|
|
113
|
+
# ─── Reject ───────────────────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
async def execute_and_jump_to_end(self, task_id: int, operator: str, args: dict[str, Any] = None) -> ProcessInstance:
|
|
116
|
+
task, inst = await self._load_and_check(task_id, operator)
|
|
117
|
+
now = datetime.now()
|
|
118
|
+
# 聚合根:废弃所有进行中任务
|
|
119
|
+
for t in inst.abandon_all_doing(now):
|
|
120
|
+
await self.repo.update_task(t)
|
|
121
|
+
# 子实体:完成任务
|
|
122
|
+
task.finish(operator, task.variables, now)
|
|
123
|
+
await self.repo.update_task(task)
|
|
124
|
+
# 聚合根:驳回
|
|
125
|
+
inst.reject(now)
|
|
126
|
+
await self.repo.update_instance(inst)
|
|
127
|
+
await self._fire_event(ProcessEvent(EventType.PROCESS_REJECT, inst.id, task_id, operator=operator))
|
|
128
|
+
return inst
|
|
129
|
+
|
|
130
|
+
# ─── Jump ────────────────────────────────────────────────────────────────
|
|
131
|
+
|
|
132
|
+
async def execute_and_jump_task(self, task_id: int, operator: str, args: dict[str, Any] = None,
|
|
133
|
+
target_task_name: str = None) -> ProcessInstance:
|
|
134
|
+
task, inst = await self._load_and_check(task_id, operator)
|
|
135
|
+
now = datetime.now()
|
|
136
|
+
# 聚合根:废弃所有进行中任务
|
|
137
|
+
for t in inst.abandon_all_doing(now):
|
|
138
|
+
await self.repo.update_task(t)
|
|
139
|
+
# 子实体:完成任务
|
|
140
|
+
task.finish(operator, task.variables, now)
|
|
141
|
+
await self.repo.update_task(task)
|
|
142
|
+
if target_task_name:
|
|
143
|
+
def_ = await self.repo.find_define_by_id(inst.defineId)
|
|
144
|
+
flow = parse_flow_model(json.loads(def_.content))
|
|
145
|
+
target = _find_node(flow, target_task_name)
|
|
146
|
+
if target: await self._execute_node(flow, inst, target, operator, inst.variables)
|
|
147
|
+
return inst
|
|
148
|
+
|
|
149
|
+
# ─── Jump To First Task(退回发起人,boot2 ROLLBACK_TO_OPERATOR=6)───────
|
|
150
|
+
|
|
151
|
+
async def execute_and_jump_to_first_task_node(self, task_id: int, operator: str,
|
|
152
|
+
args: dict[str, Any] = None) -> ProcessInstance:
|
|
153
|
+
task, inst = await self._load_and_check(task_id, operator)
|
|
154
|
+
now = datetime.now()
|
|
155
|
+
# 聚合根:废弃所有进行中任务
|
|
156
|
+
for t in inst.abandon_all_doing(now):
|
|
157
|
+
await self.repo.update_task(t)
|
|
158
|
+
# 子实体:完成任务
|
|
159
|
+
task.finish(operator, task.variables, now)
|
|
160
|
+
await self.repo.update_task(task)
|
|
161
|
+
# 找到第一个任务节点,强制参与者为发起人,重新执行
|
|
162
|
+
def_ = await self.repo.find_define_by_id(inst.defineId)
|
|
163
|
+
flow = parse_flow_model(json.loads(def_.content))
|
|
164
|
+
start_node = _find_by_type(flow, TYPE_START)
|
|
165
|
+
if start_node:
|
|
166
|
+
for node in _follow_edges(flow, start_node.id):
|
|
167
|
+
if node.type in (TYPE_TASK, TYPE_CUSTOM):
|
|
168
|
+
node.properties["assignee"] = inst.operator
|
|
169
|
+
await self._execute_node(flow, inst, node, operator, inst.variables)
|
|
170
|
+
break
|
|
171
|
+
return inst
|
|
172
|
+
|
|
173
|
+
# ─── Helpers ──────────────────────────────────────────────────────────────
|
|
174
|
+
|
|
175
|
+
async def _load_and_check(self, task_id: int, operator: str):
|
|
176
|
+
task = await self.repo.find_task_by_id(task_id)
|
|
177
|
+
if not task: raise ValueError(f"task not found: {task_id}")
|
|
178
|
+
if task.taskState != TaskState.DOING: raise ValueError("task not doing")
|
|
179
|
+
if not self._is_allowed(task, operator): raise ValueError(f"operator {operator} not allowed")
|
|
180
|
+
inst = await self.repo.find_instance_by_id(task.processInstanceId)
|
|
181
|
+
if not inst: raise ValueError("instance not found")
|
|
182
|
+
return task, inst
|
|
183
|
+
|
|
184
|
+
async def _execute_node(self, flow: FlowModel, inst: ProcessInstance, node: FlowNode, operator: str, vars_: dict):
|
|
185
|
+
if not await self._fire_pre(node, inst): return
|
|
186
|
+
try:
|
|
187
|
+
if node.type in (TYPE_TASK, TYPE_CUSTOM):
|
|
188
|
+
await self._create_task(node, inst, operator, vars_)
|
|
189
|
+
elif node.type == TYPE_DECISION:
|
|
190
|
+
await self._evaluate_decision(flow, inst, node, operator, vars_)
|
|
191
|
+
elif node.type == TYPE_FORK:
|
|
192
|
+
for n in _follow_edges(flow, node.id): await self._execute_node(flow, inst, n, operator, vars_)
|
|
193
|
+
elif node.type == TYPE_JOIN:
|
|
194
|
+
if not await self.repo.find_doing_tasks(inst.id):
|
|
195
|
+
for n in _follow_edges(flow, node.id): await self._execute_node(flow, inst, n, operator, vars_)
|
|
196
|
+
elif node.type == TYPE_END:
|
|
197
|
+
inst.finish(datetime.now())
|
|
198
|
+
inst.variables = vars_
|
|
199
|
+
await self.repo.update_instance(inst)
|
|
200
|
+
await self._fire_event(ProcessEvent(EventType.PROCESS_FINISH, inst.id, operator=operator))
|
|
201
|
+
finally:
|
|
202
|
+
await self._fire_post(node, inst)
|
|
203
|
+
|
|
204
|
+
async def _evaluate_decision(self, flow, inst, node, operator, vars_):
|
|
205
|
+
# 收集所有出边
|
|
206
|
+
edges = [e for e in flow.edges if e.sourceNodeId == node.id]
|
|
207
|
+
if not edges: return
|
|
208
|
+
# 先尝试表达式求值
|
|
209
|
+
if self.expr_eval:
|
|
210
|
+
for edge in edges:
|
|
211
|
+
expr = edge.properties.get("expr", "")
|
|
212
|
+
if not expr: continue
|
|
213
|
+
result = await self.expr_eval.eval(expr, vars_)
|
|
214
|
+
if _is_truthy(result):
|
|
215
|
+
target = _find_node(flow, edge.targetNodeId)
|
|
216
|
+
if target: return await self._execute_node(flow, inst, target, operator, vars_)
|
|
217
|
+
# 回退:取第一条没有 expr 的边作为默认路径
|
|
218
|
+
for edge in edges:
|
|
219
|
+
expr = edge.properties.get("expr", "")
|
|
220
|
+
if not expr:
|
|
221
|
+
target = _find_node(flow, edge.targetNodeId)
|
|
222
|
+
if target: return await self._execute_node(flow, inst, target, operator, vars_)
|
|
223
|
+
# 最后的回退:取第一条边
|
|
224
|
+
if edges:
|
|
225
|
+
target = _find_node(flow, edges[0].targetNodeId)
|
|
226
|
+
if target: return await self._execute_node(flow, inst, target, operator, vars_)
|
|
227
|
+
|
|
228
|
+
async def _create_task(self, node: FlowNode, inst: ProcessInstance, operator: str, vars_: dict):
|
|
229
|
+
actors = await self._resolve_actors(node, inst)
|
|
230
|
+
if not actors: return
|
|
231
|
+
perform_type = int(node.properties.get("performType", 0))
|
|
232
|
+
ct = node.properties.get("countersignType", "")
|
|
233
|
+
now = datetime.now()
|
|
234
|
+
form = node.properties.get("form", "")
|
|
235
|
+
if perform_type == 1 and ct:
|
|
236
|
+
if ct == "PARALLEL":
|
|
237
|
+
for a in actors: await self.repo.save_task(inst.create_task(self._next_id(), node.id, node.text.get("value", ""), a, operator, form, now))
|
|
238
|
+
elif ct == "SEQUENTIAL":
|
|
239
|
+
nt = inst.create_task(self._next_id(), node.id, node.text.get("value", ""), actors[0], operator, form, now)
|
|
240
|
+
nt.variables = {f"operatorList_{node.id}": actors, f"loopCounter_{node.id}": 0, f"nrOfInstances_{node.id}": len(actors)}
|
|
241
|
+
await self.repo.save_task(nt)
|
|
242
|
+
else:
|
|
243
|
+
for a in actors: await self.repo.save_task(inst.create_task(self._next_id(), node.id, node.text.get("value", ""), a, operator, form, now))
|
|
244
|
+
else:
|
|
245
|
+
await self.repo.save_task(inst.create_task(self._next_id(), node.id, node.text.get("value", ""), actors[0], operator, form, now))
|
|
246
|
+
|
|
247
|
+
async def _resolve_actors(self, node: FlowNode, inst: ProcessInstance) -> list[str]:
|
|
248
|
+
assignee = node.properties.get("assignee", "")
|
|
249
|
+
if assignee:
|
|
250
|
+
actors = [a.strip() for a in assignee.split(",") if a.strip()]
|
|
251
|
+
# boot2 约定:"applicant" → 解析为流程发起人
|
|
252
|
+
if "applicant" in actors:
|
|
253
|
+
actors = [inst.operator if a == "applicant" else a for a in actors]
|
|
254
|
+
return actors
|
|
255
|
+
handler_name = node.properties.get("assignmentHandler", "")
|
|
256
|
+
if handler_name and self.ext and self.ext.registry:
|
|
257
|
+
h = self.ext.registry.resolve_assignment(handler_name)
|
|
258
|
+
if h: return await h.assign(node, inst)
|
|
259
|
+
if self.ext and self.ext.assignment_handler:
|
|
260
|
+
result = self.ext.assignment_handler(handler_name, node, inst)
|
|
261
|
+
if hasattr(result, '__await__'): return await result
|
|
262
|
+
return result
|
|
263
|
+
return []
|
|
264
|
+
|
|
265
|
+
def _is_allowed(self, task: ProcessTask, operator: str) -> bool:
|
|
266
|
+
# 子实体:actorIds 权限判断
|
|
267
|
+
return task.is_allowed(operator)
|
|
268
|
+
|
|
269
|
+
async def _add_user_info(self, operator: str, vars_: dict):
|
|
270
|
+
if not self.user_prov: return
|
|
271
|
+
u = await self.user_prov.get_user(operator)
|
|
272
|
+
if not u: return
|
|
273
|
+
vars_[KEY_USER_ID] = u.userId
|
|
274
|
+
if u.realName: vars_[KEY_REAL_NAME] = u.realName
|
|
275
|
+
if u.deptId: vars_[KEY_DEPT_ID] = u.deptId
|
|
276
|
+
if u.deptName: vars_[KEY_DEPT_NAME] = u.deptName
|
|
277
|
+
if u.postId: vars_[KEY_POST_ID] = u.postId
|
|
278
|
+
if u.postName: vars_[KEY_POST_NAME] = u.postName
|
|
279
|
+
|
|
280
|
+
def _next_id(self) -> int:
|
|
281
|
+
if self.id_gen: return self.id_gen.next_id()
|
|
282
|
+
return int(time.time() * 1000) + random.randint(0, 999)
|
|
283
|
+
|
|
284
|
+
# ─── Extensions ───────────────────────────────────────────────────────────
|
|
285
|
+
|
|
286
|
+
async def _fire_pre(self, node, inst) -> bool:
|
|
287
|
+
if not self.ext: return True
|
|
288
|
+
for ic in sorted(self.ext.interceptors, key=lambda x: x.order):
|
|
289
|
+
if not await ic.pre_handle(node, inst): return False
|
|
290
|
+
return True
|
|
291
|
+
|
|
292
|
+
async def _fire_post(self, node, inst):
|
|
293
|
+
if not self.ext: return
|
|
294
|
+
for ic in sorted(self.ext.interceptors, key=lambda x: x.order, reverse=True):
|
|
295
|
+
await ic.post_handle(node, inst)
|
|
296
|
+
|
|
297
|
+
async def _fire_event(self, evt: ProcessEvent):
|
|
298
|
+
if self.ext and self.ext.event_listener:
|
|
299
|
+
result = self.ext.event_listener(evt)
|
|
300
|
+
if hasattr(result, '__await__'):
|
|
301
|
+
await result
|
|
302
|
+
|
|
303
|
+
# ─── Pure Functions ─────────────────────────────────────────────────────────────
|
|
304
|
+
|
|
305
|
+
def _find_node(flow: FlowModel, id: str) -> Optional[FlowNode]:
|
|
306
|
+
return next((n for n in flow.nodes if n.id == id), None)
|
|
307
|
+
|
|
308
|
+
def _find_by_type(flow: FlowModel, typ: str) -> Optional[FlowNode]:
|
|
309
|
+
return next((n for n in flow.nodes if n.type == typ), None)
|
|
310
|
+
|
|
311
|
+
def _follow_edges(flow: FlowModel, source_id: str) -> list[FlowNode]:
|
|
312
|
+
return [_find_node(flow, e.targetNodeId) for e in flow.edges if e.sourceNodeId == source_id and _find_node(flow, e.targetNodeId)]
|
|
313
|
+
|
|
314
|
+
def _get_cs_state(vars_: dict, node_id: str):
|
|
315
|
+
actors = vars_.get(f"operatorList_{node_id}")
|
|
316
|
+
lc = int(vars_.get(f"loopCounter_{node_id}", 0))
|
|
317
|
+
return actors, lc
|
|
318
|
+
|
|
319
|
+
def _is_truthy(v) -> bool:
|
|
320
|
+
if isinstance(v, bool): return v
|
|
321
|
+
if isinstance(v, str): return v not in ("", "false")
|
|
322
|
+
if v is None: return False
|
|
323
|
+
if isinstance(v, (int, float)): return v != 0
|
|
324
|
+
return True
|