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 defsm macro.
  • Define state transitions and guard conditions using the for_state macro.
  • Support for nested state machines using the defsm_module macro.
  • Define initial states and scenarios using the scenario_initial_state macro.
  • Integration with the Noizu.StateMachine.Records module 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
end

To 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

Link to this section Summary

Link to this section Functions

Link to this macro

blowitup(name, meta, prefix, args, guards, block)

View Source (macro)
Link to this function

check_header(name, args)

View Source
Link to this macro

defsm(signature)

View Source (macro)
Link to this macro

defsm(signature, list)

View Source (macro)
Link to this macro

defsm_module(name, list)

View Source (macro)
Link to this macro

for_state(signature, list)

View Source (macro)
Link to this macro

for_state(name, state, list)

View Source (macro)
Link to this function

merge_nsm_ast(inner, outer)

View Source
Link to this macro

nsm_global_state()

View Source (macro)
Link to this macro

nsm_local_state()

View Source (macro)
Link to this macro

nsm_set_global_state(path, value)

View Source (macro)
Link to this macro

nsm_set_local_state(path, value)

View Source (macro)
Link to this macro

nsm_set_module_state(module, path, value)

View Source (macro)
Link to this macro

scenario_initial_state(scenario, list)

View Source (macro)
Link to this macro

unpackme(block)

View Source (macro)
Link to this macro

unpackme(name, args, guards, block)

View Source (macro)