Statifier.Datamodel (statifier v1.5.0)

View Source

Manages the data model for SCXML state machines.

The datamodel provides variable storage and initialization for state machines, supporting the SCXML <datamodel> and <data> elements. It handles:

  • Variable initialization from <data> elements
  • Expression evaluation for initial values
  • Context building for condition evaluation
  • Variable access and updates during execution

Examples

# Initialize from data elements
data_elements = [
  %Statifier.Data{id: "counter", expr: "0"},
  %Statifier.Data{id: "name", expr: "'John'"}
]
datamodel = Statifier.Datamodel.initialize(data_elements, state_chart)

# Access variables
value = Statifier.Datamodel.get(datamodel, "counter")
# => 0

# Update variables
datamodel = Statifier.Datamodel.set(datamodel, "counter", 1)

Summary

Functions

Build evaluation context for Predicator expressions.

Prepare Predicator functions for evaluation (In() function for state checks). Returns the functions map needed for Predicator.evaluate/3.

Get a variable value from the datamodel.

Check if a variable exists in the datamodel.

Initialize a datamodel from a list of data elements.

Merge another map into the datamodel.

Create a new empty datamodel.

Set a value at a nested path in the datamodel.

Set a variable value in the datamodel.

Types

t()

@type t() :: map()

Functions

build_evaluation_context(datamodel, state_chart)

@spec build_evaluation_context(t(), Statifier.StateChart.t()) :: map()

Build evaluation context for Predicator expressions.

Takes the datamodel and state_chart, returns a context ready for evaluation. This is the single source of truth for context preparation.

build_predicator_functions(configuration)

@spec build_predicator_functions(Statifier.Configuration.t()) :: map()

Prepare Predicator functions for evaluation (In() function for state checks). Returns the functions map needed for Predicator.evaluate/3.

get(datamodel, variable_name)

@spec get(t(), String.t()) :: any()

Get a variable value from the datamodel.

Returns the value if found, nil otherwise.

has?(datamodel, variable_name)

@spec has?(t(), String.t()) :: boolean()

Check if a variable exists in the datamodel.

initialize(data_elements, state_chart)

@spec initialize([Statifier.Data.t()], Statifier.StateChart.t()) :: t()

Initialize a datamodel from a list of data elements.

Processes each <data> element, evaluates its expression (if any), and stores the result in the datamodel.

merge(datamodel, updates)

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

Merge another map into the datamodel.

Useful for bulk updates or combining datamodels.

new()

@spec new() :: t()

Create a new empty datamodel.

put_in_path(datamodel, path_components, value)

@spec put_in_path(t(), [String.t()], any()) :: {:ok, t()} | {:error, String.t()}

Set a value at a nested path in the datamodel.

Takes a datamodel, a list of path components (keys), and a value. Creates intermediate maps as needed for nested assignment.

Examples

iex> datamodel = %{}
iex> Statifier.Datamodel.put_in_path(datamodel, ["user", "name"], "John")
{:ok, %{"user" => %{"name" => "John"}}}

iex> datamodel = %{"user" => %{"age" => 30}}
iex> Statifier.Datamodel.put_in_path(datamodel, ["user", "name"], "Jane")
{:ok, %{"user" => %{"age" => 30, "name" => "Jane"}}}

set(datamodel, variable_name, value)

@spec set(t(), String.t(), any()) :: t()

Set a variable value in the datamodel.

Returns the updated datamodel.