Jido. BehaviorTree. Agent
(Jido Behavior Tree v1.0.0)
View Source
A simple behavior tree executor using a GenServer.
This agent provides a stateful execution environment for behavior trees, maintaining the tree state and blackboard between ticks. It supports both manual and automatic execution modes.
Features
- Execute behavior trees with automatic state management
- Maintain shared blackboard between executions
- Support for manual or automatic tree progression
- Built-in telemetry and logging
Example Usage
# Create a simple tree
tree = Jido.BehaviorTree.Tree.new(simple_node)
# Start the agent
{:ok, agent} = Jido.BehaviorTree.Agent.start_link(
tree: tree,
blackboard: %{status: :ready},
mode: :manual
)
# Tick the tree manually
status = Jido.BehaviorTree.Agent.tick(agent)
# Get current blackboard
bb = Jido.BehaviorTree.Agent.blackboard(agent)
Summary
Functions
Gets the current blackboard.
Returns a specification to start this module under a supervisor.
Gets directives emitted on the most recent tick.
Gets a value from the blackboard.
Halts the agent and cleans up the tree.
Gets the current Jido agent context (if configured).
Gets the current execution mode.
Puts a value in the blackboard.
Replaces the root node of the tree.
Sets the execution mode.
Starts a behavior tree agent.
Gets the status from the most recent tick.
Executes a single tick of the behavior tree.
Types
@type mode() :: :manual | :auto
Agent execution mode
@type state() :: %{ tree: Jido.BehaviorTree.Tree.t(), blackboard: Jido.BehaviorTree.Blackboard.t(), jido_agent: Jido.Agent.t() | nil, last_directives: [Jido.Agent.directive()], last_status: :idle | Jido.BehaviorTree.Status.t(), mode: mode(), interval: non_neg_integer() | nil, timer_ref: reference() | nil, tick_count: non_neg_integer() }
Agent state
Functions
@spec blackboard(pid()) :: Jido.BehaviorTree.Blackboard.t()
Gets the current blackboard.
Examples
bb = Jido.BehaviorTree.Agent.blackboard(agent)
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec directives(pid()) :: [Jido.Agent.directive()]
Gets directives emitted on the most recent tick.
Gets a value from the blackboard.
Examples
value = Jido.BehaviorTree.Agent.get(agent, :key, "default")
@spec halt(pid()) :: :ok
Halts the agent and cleans up the tree.
Examples
:ok = Jido.BehaviorTree.Agent.halt(agent)
@spec jido_agent(pid()) :: Jido.Agent.t() | nil
Gets the current Jido agent context (if configured).
Gets the current execution mode.
Examples
mode = Jido.BehaviorTree.Agent.mode(agent)
Puts a value in the blackboard.
Examples
:ok = Jido.BehaviorTree.Agent.put(agent, :key, "value")
@spec replace_root(pid(), Jido.BehaviorTree.Node.t()) :: :ok
Replaces the root node of the tree.
Examples
:ok = Jido.BehaviorTree.Agent.replace_root(agent, new_root)
@spec set_mode(pid(), mode()) :: :ok | {:error, Jido.BehaviorTree.Error.BehaviorTreeError.t()}
Sets the execution mode.
When changing to :auto mode, automatic ticking will start.
When changing to :manual mode, automatic ticking will stop.
Returns a validation error when mode is not :manual or :auto.
Examples
:ok = Jido.BehaviorTree.Agent.set_mode(agent, :auto)
@spec start_link(keyword()) :: GenServer.on_start()
Starts a behavior tree agent.
Options
:tree- The behavior tree to execute (required):blackboard- Initial blackboard data (default: empty blackboard):jido_agent- Optional%Jido.Agent{}for Action-node state/directive integration:mode- Execution mode, either:manualor:auto(default::manual):interval- Tick interval in milliseconds for auto mode (default: 1000)
Examples
{:ok, agent} = Jido.BehaviorTree.Agent.start_link(
tree: tree,
blackboard: %{user_id: 123},
mode: :auto,
interval: 2000
)
@spec status(pid()) :: :idle | Jido.BehaviorTree.Status.t()
Gets the status from the most recent tick.
Returns :idle before the first tick.
@spec tick(pid()) :: Jido.BehaviorTree.Status.t()
Executes a single tick of the behavior tree.
Returns the status of the tree execution.
Examples
status = Jido.BehaviorTree.Agent.tick(agent)