Statifier.Datamodel (statifier v1.9.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'"}
]
state_chart = Statifier.Datamodel.initialize(state_chart, data_elements)

# Access variables
value = Statifier.Datamodel.get(state_chart.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(state_chart)

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

Build evaluation context for Predicator expressions.

Takes the state_chart, extracts its datamodel, and 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(state_chart, data_elements)

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. Returns the updated StateChart with the initialized datamodel and any error events that were generated.

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. Does not create intermediate maps as needed for nested assignment.

Examples

iex> datamodel = %{}
iex> Statifier.Datamodel.put_in_path(datamodel, ["user", "name"], "John")
{:error, "Cannot assign to nested path: 'user' does not exist"}

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.