Jido.BehaviorTree.Blackboard (Jido Behavior Tree v1.0.0)

View Source

A shared data structure for communication between behavior tree nodes.

The blackboard pattern allows nodes in a behavior tree to share data without tight coupling. Nodes can read from and write to the blackboard to coordinate their behavior and share results.

The blackboard is essentially a map with convenience functions for common operations like getting, setting, and updating values.

Summary

Functions

Deletes a key from the blackboard.

Checks if the blackboard is empty.

Gets a value from the blackboard by key.

Checks if the blackboard contains a key.

Gets all keys from the blackboard.

Merges another map or blackboard into this blackboard.

Creates a new blackboard with optional initial data.

Sets a value in the blackboard.

Returns the Zoi schema for this module

Returns the size (number of keys) in the blackboard.

Converts the blackboard to a plain map.

Updates a value in the blackboard using a function.

Gets all values from the blackboard.

Types

t()

@type t() :: %Jido.BehaviorTree.Blackboard{data: map()}

Functions

delete(bb, key)

@spec delete(t(), term()) :: t()

Deletes a key from the blackboard.

Examples

iex> bb = Jido.BehaviorTree.Blackboard.new(%{user_id: 123, temp: "delete_me"})
iex> bb = Jido.BehaviorTree.Blackboard.delete(bb, :temp)
iex> Jido.BehaviorTree.Blackboard.get(bb, :temp)
nil

empty?(blackboard)

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

Checks if the blackboard is empty.

Examples

iex> bb = Jido.BehaviorTree.Blackboard.new()
iex> Jido.BehaviorTree.Blackboard.empty?(bb)
true

iex> bb = Jido.BehaviorTree.Blackboard.new(%{a: 1})
iex> Jido.BehaviorTree.Blackboard.empty?(bb)
false

get(blackboard, key, default \\ nil)

@spec get(t(), term(), term()) :: term()

Gets a value from the blackboard by key.

Examples

iex> bb = Jido.BehaviorTree.Blackboard.new(%{user_id: 123})
iex> Jido.BehaviorTree.Blackboard.get(bb, :user_id)
123

iex> Jido.BehaviorTree.Blackboard.get(bb, :missing_key, "default")
"default"

has_key?(blackboard, key)

@spec has_key?(t(), term()) :: boolean()

Checks if the blackboard contains a key.

Examples

iex> bb = Jido.BehaviorTree.Blackboard.new(%{user_id: 123})
iex> Jido.BehaviorTree.Blackboard.has_key?(bb, :user_id)
true

iex> Jido.BehaviorTree.Blackboard.has_key?(bb, :missing)
false

keys(blackboard)

@spec keys(t()) :: [term()]

Gets all keys from the blackboard.

Examples

iex> bb = Jido.BehaviorTree.Blackboard.new(%{a: 1, b: 2})
iex> Jido.BehaviorTree.Blackboard.keys(bb)
[:a, :b]

merge(bb, data2)

@spec merge(t(), t() | map()) :: t()

Merges another map or blackboard into this blackboard.

Examples

iex> bb1 = Jido.BehaviorTree.Blackboard.new(%{a: 1, b: 2})
iex> bb2 = Jido.BehaviorTree.Blackboard.new(%{b: 3, c: 4})
iex> bb = Jido.BehaviorTree.Blackboard.merge(bb1, bb2)
iex> Jido.BehaviorTree.Blackboard.get(bb, :b)
3

new(initial_data \\ %{})

@spec new(map()) :: t()

Creates a new blackboard with optional initial data.

Examples

iex> bb = Jido.BehaviorTree.Blackboard.new()
%Jido.BehaviorTree.Blackboard{data: %{}}

iex> bb = Jido.BehaviorTree.Blackboard.new(%{user_id: 123, status: "active"})
%Jido.BehaviorTree.Blackboard{data: %{user_id: 123, status: "active"}}

put(bb, key, value)

@spec put(t(), term(), term()) :: t()

Sets a value in the blackboard.

Examples

iex> bb = Jido.BehaviorTree.Blackboard.new()
iex> bb = Jido.BehaviorTree.Blackboard.put(bb, :user_id, 123)
iex> Jido.BehaviorTree.Blackboard.get(bb, :user_id)
123

schema()

Returns the Zoi schema for this module

size(blackboard)

@spec size(t()) :: non_neg_integer()

Returns the size (number of keys) in the blackboard.

Examples

iex> bb = Jido.BehaviorTree.Blackboard.new(%{a: 1, b: 2})
iex> Jido.BehaviorTree.Blackboard.size(bb)
2

to_map(blackboard)

@spec to_map(t()) :: map()

Converts the blackboard to a plain map.

Examples

iex> bb = Jido.BehaviorTree.Blackboard.new(%{user_id: 123})
iex> Jido.BehaviorTree.Blackboard.to_map(bb)
%{user_id: 123}

update(bb, key, initial, fun)

@spec update(t(), term(), term(), (term() -> term())) :: t()

Updates a value in the blackboard using a function.

If the key doesn't exist, the initial value is used as input to the function.

Examples

iex> bb = Jido.BehaviorTree.Blackboard.new(%{counter: 5})
iex> bb = Jido.BehaviorTree.Blackboard.update(bb, :counter, 0, &(&1 + 1))
iex> Jido.BehaviorTree.Blackboard.get(bb, :counter)
6

iex> bb = Jido.BehaviorTree.Blackboard.new()
iex> bb = Jido.BehaviorTree.Blackboard.update(bb, :counter, 0, &(&1 + 1))
iex> Jido.BehaviorTree.Blackboard.get(bb, :counter)
1

values(blackboard)

@spec values(t()) :: [term()]

Gets all values from the blackboard.

Examples

iex> bb = Jido.BehaviorTree.Blackboard.new(%{a: 1, b: 2})
iex> Jido.BehaviorTree.Blackboard.values(bb)
[1, 2]