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 graph-based execution in phronomy.
== Basic usage
app = Phronomy::Workflow.define(MyContext) do initial :fetch
state :fetch, action: FETCH_NODE
state :process, action: PROCESS_NODE
after :fetch, to: :process
after :process, to: :__finish__
end
result = app.invoke({ url: "https://example.com" })
== Wait states
app = Phronomy::Workflow.define(MyContext) do initial :propose
state :propose, action: PROPOSE_NODE
wait_state :awaiting_approval
state :execute, action: EXECUTE_NODE
after :propose, to: :awaiting_approval
after :execute, to: :__finish__
event :approve, from: :awaiting_approval, to: :execute
event :reject, from: :awaiting_approval, to: :propose
end
halted = app.invoke({ ... }) final = app.send_event(state: halted, event: :approve)
== Conditional transitions
event :route, from: :decide, guard: ->(s) { s.score > 5 }, to: :high event :route, 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.
65 66 67 |
# File 'lib/phronomy/workflow.rb', line 65 def initialize(runner) @runner = runner end |
Class Method Details
.define(context_class) { ... } ⇒ Phronomy::Workflow
Defines a new Workflow.
58 59 60 61 62 |
# File 'lib/phronomy/workflow.rb', line 58 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.
73 74 75 |
# File 'lib/phronomy/workflow.rb', line 73 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.
81 82 83 |
# File 'lib/phronomy/workflow.rb', line 81 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.
90 91 92 |
# File 'lib/phronomy/workflow.rb', line 90 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 { node: Symbol, state: Object } after each node.
99 100 101 |
# File 'lib/phronomy/workflow.rb', line 99 def stream(input, config: {}, &block) @runner.stream(input, config: config, &block) end |