jeeflow workflow engine core (MoonBit port, zero registry deps)
moon new my-flow && cd my-flow
moon add mldong/jeeflow-core # this module: engine + SPI + memory repository
moon add moonbitlang/async # engine methods are async; async main needs it// cmd/main/moon.pkg
import {
"mldong/jeeflow-core/spi" @spi,
"mldong/jeeflow-core/memory" @memory,
"mldong/jeeflow-core/model" @model,
"mldong/jeeflow-core/json" @json,
"mldong/jeeflow-core/engine" @engine,
"mldong/jeeflow-core/id_gen" @id_gen,
"mldong/jeeflow-core/metadata" @metadata,
"moonbitlang/async",
}
pkgtype(kind: "executable")// cmd/main/main.mbt
async fn main raise {
// the repository is a generic parameter and the small SPIs are closure fields
let repo = @memory.MemoryRepository::new()
let gen = @id_gen.AtomicIdGenerator::new(1L)
let ctx = @spi.Ctx::new(repo, repo) // (process repository, ext repository)
.with_id_generator(fn() { gen.next_id() })
let engine = @engine.Engine::make(ctx)
// a process definition is the LogicFlow document the designer produces
let flow = $|{
$| "name": "leave", "displayName": "请假", "type": "approval",
$| "nodes": [
$| { "id": "start", "type": "snaker:start", "text": { "value": "开始" } },
$| { "id": "apply", "type": "snaker:task", "text": { "value": "申请" },
$| "properties": { "assignee": "applicant" } },
$| { "id": "end", "type": "snaker:end", "text": { "value": "结束" } }
$| ],
$| "edges": [
$| { "id": "e1", "sourceNodeId": "start", "targetNodeId": "apply" },
$| { "id": "e2", "sourceNodeId": "apply", "targetNodeId": "end" }
$| ]}
$|
let draft : @model.ProcessDefine = {
id: 0L, // 0 ⇒ the store assigns the id
name: "leave",
display_name: "请假",
define_type: "approval",
state: 1,
version: 1,
content: flow,
create_time: None,
create_user: None,
update_time: None,
update_user: None,
}
repo.save_define(draft)
let instance = engine.start_async(draft.id, "u1", @json.FlowData::new())
println("start instance=\{instance.instance_id} state=\{instance.state}")
let todos = repo.find_doing_tasks(instance.instance_id, [])
println("todo \{todos.length()} task at node '\{todos[0].task_name}'")
let next = engine.execute_task_async(todos[0].task_id, "u1", @json.FlowData::new())
let handled = repo.find_task_by_id(todos[0].task_id).unwrap()
println("execute task=\{handled.task_id} state=\{handled.task_state} next=\{next.length()}")
let ended = repo.find_instance_by_id(instance.instance_id).unwrap()
println("end instance=\{ended.instance_id} state=\{ended.state}")
// the shipped dictionaries, so submitType / state codes never get hardcoded by hand
let dicts = @metadata.EnumDictRegistry::new()
println("dicts \{dicts.list_dict_keys().join(" ")}")
}start instance=1 state=10
todo 1 task at node 'apply'
execute task=2 state=20 next=0
end instance=1 state=20
dicts wf_countersign_type wf_process_define_state wf_process_instance_state wf_process_submit_type wf_process_task_perform_type wf_process_task_state wf_process_task_type