Statifier.Datamodel (statifier v1.9.0)
View SourceManages 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
@type t() :: map()
Functions
@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.
@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 a variable value from the datamodel.
Returns the value if found, nil otherwise.
Check if a variable exists in the datamodel.
@spec initialize(Statifier.StateChart.t(), [Statifier.Data.t()]) :: Statifier.StateChart.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. Returns the updated StateChart
with the initialized datamodel and any error events that were generated.
Merge another map into the datamodel.
Useful for bulk updates or combining datamodels.
@spec new() :: t()
Create a new empty datamodel.
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 a variable value in the datamodel.
Returns the updated datamodel.