Statifier.Datamodel (statifier v1.5.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'"}
]
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
@type t() :: map()
Functions
@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.
@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.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 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. 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 a variable value in the datamodel.
Returns the updated datamodel.