View Source Noizu.StateMachine.Module (noizu_core v1.0.28)
Noizu State Machine
The Noizu State Machine is a powerful tool for defining and managing state machines in Elixir applications. It provides a declarative way to define states, transitions, and behaviors using a DSL-like syntax.
key-features
Key Features
- Define states and their associated behaviors using the
defsmmacro. - Define state transitions and guard conditions using the
for_statemacro. - Support for nested state machines using the
defsm_modulemacro. - Define initial states and scenarios using the
scenario_initial_statemacro. - Integration with the
Noizu.StateMachine.Recordsmodule for managing state machine records. - Built-in support for global and local state management.
- Extensible architecture allowing for custom state machine configurations.
modules-and-protocols
Modules and Protocols
noizu-statemachine
Noizu.StateMachine
The main module that provides the entry point for defining state machines.
__using__/1: A macro that sets up the necessary module attributes and imports the required modules when used in a module definition.initialize/1: Initializes a new state machine instance with the specified scenario.reset/1: Resets the state machine to the specified scenario.
noizu-statemachine-module
Noizu.StateMachine.Module
A module that defines the macros and functions used for defining state machines.
__before_compile__/1: A macro that generates the necessary code for the state machine before compilation.__nsm_load__/1: Loads the state machine state from the agent.__nsm_init_module_state__/2: Initializes the state of a module within the state machine.nsm_set_global_state/2: Sets a value in the global state of the state machine.nsm_set_local_state/2: Sets a value in the local state of the state machine.nsm_set_module_state/3: Sets a value in the state of a specific module within the state machine.nsm_local_state/0: Retrieves the local state of the current module.nsm_global_state/0: Retrieves the global state of the state machine.defsm_module/2: Defines a nested state machine module.defsm/2: Defines a state behavior.scenario_initial_state/2: Defines an initial state for a scenario.for_state/2: Defines a state transition and its associated behavior.
noizu-statemachine-records
Noizu.StateMachine.Records
A module that defines the record structure used by the state machine.
nsm: A record representing the state machine state.
usage
Usage
To define a state machine, create a module and use the Noizu.StateMachine module:
defmodule MyStateMachine do
use Noizu.StateMachine
# Define initial states and scenarios
scenario_initial_state :default do
%{flag: false}
end
# Define state behaviors
defsm some_action(arg) do
# Default behavior
{:ok, arg}
end
# Define state transitions
for_state "state 1" do
defsm some_action(arg) do
{:state_1, arg}
end
end
for_state "state 2" when global.flag == true do
defsm some_action(arg) do
{:state_2, arg}
end
end
endTo use the state machine:
# Initialize the state machine with a scenario
handle = MyStateMachine.initialize(:default)
# Call state machine actions
result = MyStateMachine.some_action(handle, :arg)
testing
Testing
The provided test case (Noizu.StateMachineTest) demonstrates how to test the state machine functionality. It covers scenarios such as initializing the state machine, verifying the initial state, testing state transitions, and ensuring the correct behavior based on global and local state conditions.
defmodule Noizu.StateMachineTest do
use ExUnit.Case, async: false
describe "State Machine" do
test "initialization" do
handle = MyStateMachine.initialize(:default)
assert MyStateMachine.some_action(handle, :arg) == {:ok, :arg}
end
test "state transitions" do
handle = MyStateMachine.initialize(:default)
MyStateMachine.update_global_state(handle, :flag, true)
assert MyStateMachine.some_action(handle, :arg) == {:state_2, :arg}
end
# More test cases...
end
end