Class: Phronomy::Workflow
- Inherits:
-
Object
- Object
- Phronomy::Workflow
- Includes:
- Runnable
- Defined in:
- lib/phronomy/workflow.rb
Overview
StateChart-style workflow definition DSL.
Defines agent workflows in terms of states and events backed by Phronomy::WorkflowRunner. This is the primary high-level API for workflow-based execution in phronomy.
== Basic usage
app = Phronomy::Workflow.define(MyContext) do initial :fetch
state :fetch
state :process
entry :fetch, FETCH_NODE
entry :process, PROCESS_NODE
transition from: :fetch, to: :process
transition from: :process, to: :__finish__
end
result = app.invoke({ url: "https://example.com" })
== Wait states
app = Phronomy::Workflow.define(MyContext) do initial :propose
state :propose
wait_state :awaiting_approval
state :execute
entry :propose, PROPOSE_NODE
entry :execute, EXECUTE_NODE
transition from: :propose, to: :awaiting_approval
transition from: :execute, to: :__finish__
transition from: :awaiting_approval, on: :approve, to: :execute
transition from: :awaiting_approval, on: :reject, to: :propose
end
halted = app.invoke({ ... }) final = app.send_event(state: halted, event: :approve)
== Conditional transitions
transition from: :decide, guard: ->(s) { s.score > 5 }, to: :high transition from: :decide, to: :low # fallback (no guard)
Defined Under Namespace
Classes: Builder
Class Method Summary collapse
-
.define(context_class) { ... } ⇒ Phronomy::Workflow
Defines a new Workflow.
Instance Method Summary collapse
-
#initialize(runner) ⇒ Workflow
constructor
A new instance of Workflow.
-
#invoke(input, config: {}) ⇒ Object
Executes the workflow from the initial state.
-
#resume(state:, input: nil) ⇒ Object
Resumes a halted workflow.
-
#send_event(state:, event:, input: nil) ⇒ Object
Fires a named event to advance a halted workflow.
-
#stream(input, config: {}) {|Hash| ... } ⇒ Object
Streaming execution.
Methods included from Runnable
Constructor Details
#initialize(runner) ⇒ Workflow
Returns a new instance of Workflow.
71 72 73 |
# File 'lib/phronomy/workflow.rb', line 71 def initialize(runner) @runner = runner end |
Class Method Details
.define(context_class) { ... } ⇒ Phronomy::Workflow
Defines a new Workflow.
64 65 66 67 68 |
# File 'lib/phronomy/workflow.rb', line 64 def self.define(context_class, &block) builder = Builder.new(context_class) builder.instance_eval(&block) builder.build end |
Instance Method Details
#invoke(input, config: {}) ⇒ Object
Executes the workflow from the initial state.
79 80 81 |
# File 'lib/phronomy/workflow.rb', line 79 def invoke(input, config: {}) @runner.invoke(input, config: config) end |
#resume(state:, input: nil) ⇒ Object
Resumes a halted workflow. Generic resume that works for all halt types.
87 88 89 |
# File 'lib/phronomy/workflow.rb', line 87 def resume(state:, input: nil) @runner.resume(state: state, input: input) end |
#send_event(state:, event:, input: nil) ⇒ Object
Fires a named event to advance a halted workflow.
96 97 98 |
# File 'lib/phronomy/workflow.rb', line 96 def send_event(state:, event:, input: nil) @runner.send_event(state: state, event: event, input: input) end |
#stream(input, config: {}) {|Hash| ... } ⇒ Object
Streaming execution. Yields { state: Symbol, context: Object } after each state action.
105 106 107 |
# File 'lib/phronomy/workflow.rb', line 105 def stream(input, config: {}, &block) @runner.stream(input, config: config, &block) end |