Runic.Workflow.Condition (Runic v0.1.0-alpha.7)

Copy Markdown View Source

Condition nodes are predicate checks that gate flow in a workflow.

A Condition receives a fact and evaluates to true/false. If true, downstream nodes become runnable; if false, the fact is consumed without propagation.

Meta Expression Support

Conditions can reference workflow state through meta expressions like state_of(:component). When a Condition has meta references, the :meta_refs field is populated during macro compilation, and :meta_ref edges are drawn during Component.connect/3.

During the prepare phase, these edges are traversed to populate meta_context in the CausalContext, making the referenced state available during execution.

Runtime Context

Conditions can also reference external runtime values via context/1 expressions, commonly used in rule where clauses:

Runic.rule name: :gated do
  given(val: v)
  where(v > context(:threshold))
  then(fn %{val: v} -> {:ok, v} end)
end

The condition's work function is rewritten to arity-2 when context/1 is detected, and values are resolved from the workflow's run_context during the prepare phase.

Summary

Functions

Checks a condition with meta context available.

Returns whether this condition has meta references that need to be resolved during the prepare phase.

Types

meta_ref()

@type meta_ref() :: %{
  kind: atom(),
  target: atom() | integer() | {atom(), atom()},
  field_path: [atom()],
  context_key: atom()
}

t()

@type t() :: %Runic.Workflow.Condition{
  arity: non_neg_integer(),
  closure: Runic.Closure.t() | nil,
  hash: integer() | nil,
  meta_refs: [meta_ref()],
  name: String.t() | atom() | nil,
  work: function(),
  work_hash: integer() | nil
}

Functions

check(condition, fact)

check_with_meta_context(condition, fact, meta_context)

@spec check_with_meta_context(t(), term(), map()) :: boolean()

Checks a condition with meta context available.

When a condition has meta references, its work function is arity 2, receiving (input, meta_context).

has_meta_refs?(condition)

@spec has_meta_refs?(t()) :: boolean()

Returns whether this condition has meta references that need to be resolved during the prepare phase.

new(work)

new(work, arity)

try_to_run_work(work, fact_value, arity)