第96章 状态机
第94章建立了生命周期系统,第95章建立了事件系统。
第96章进一步把二者连接起来:
状态机负责根据当前 State、Condition 和 Event,判断是否允许发生 Transition,并执行对应 Action,使对象从一个状态进入另一个状态。
因此可以把三章关系理解为:
第94章 Lifecycle
↓
管理对象生命周期
第95章 Event
↓
描述发生了什么
第96章 State Machine
↓
根据 Event + Condition
控制 State Transition
核心模型:
Event
↓
Condition
↓
Transition
↓
Action
↓
New State
1. State Machine
1.1 State Machine 定义
State Machine(状态机)可以定义为:
State Machine 是由 State、Transition、Condition、Action 和 Event 组成的离散状态控制机制,根据输入事件和当前条件决定对象是否从当前状态转换到目标状态。
基本结构:
State Machine
├── State
├── Event
├── Condition
├── Transition
├── Action
└── State Event
例如一个 Device:
OFF
↓ START
INITIALIZING
↓ READY
READY
↓ RUN
RUNNING
↓ STOP
STOPPED
这里:
OFF = State
START = Event
READY条件 = Condition
OFF→INITIALIZING = Transition
启动设备 = Action
1.2 状态机不是决策系统
必须保持边界:
Decision
↓
选择做什么
而:
State Machine
↓
判断当前状态允许怎样变化
例如:
Decision = START DEVICE
状态机检查:
当前状态 = READY
是否允许 START?
如果允许:
READY
↓
RUNNING
因此:
Decision 负责选择,State Machine 负责状态转换。
2. State
State 是状态机的基本单位。
定义:
State 是对象在某一个时间点满足特定条件时所处的结构化运行状态。
例如 Individual:
CREATED
INITIALIZING
READY
RUNNING
WAITING
PAUSED
STOPPING
STOPPED
ERROR
UNKNOWN
Device:
OFF
INITIALIZING
READY
RUNNING
STOPPING
STOPPED
ERROR
MAINTENANCE
DISCONNECTED
UNKNOWN
2.1 State 结构
可以定义:
State
{
id
name
object_id
type
properties
entered_at
exited_at
}
例如:
{
id: "running",
name: "RUNNING",
object_id: "Machine_A",
type: "DEVICE_STATE"
}
2.2 State 与 Property
必须区分:
State = 对象当前处于什么运行状态
Property = 对象具有什么属性和值
例如:
Machine_A
可以同时拥有:
state = RUNNING
temperature = 92
speed = 1200
load = 82
所以:
temperature = 92
是 Property。
而:
RUNNING
是 State。
Property 可以影响 State Transition:
temperature > 95
↓
Condition
↓
RUNNING → STOPPING
3. Transition
Transition 是状态转换。
定义:
Transition 是 State Machine 根据 Event 和 Condition,将对象从一个 State 转换到另一个 State 的结构化过程。
基本形式:
Current State
↓
Event
↓
Condition
↓
Transition
↓
Action
↓
New State
例如:
READY
│
│ START
▼
RUNNING
3.1 Transition 结构
Transition
{
id
from_state
to_state
event
condition
action
priority
state
}
例如:
{
id: "device_start",
from_state: "READY",
to_state: "RUNNING",
event: "START",
condition: "device_available",
action: "START_DEVICE",
state: "ACTIVE"
}
3.2 Transition 不是 Event
例如:
START
是 Event。
而:
READY → RUNNING
是 Transition。
关系:
Event
↓
触发 Transition
↓
State Change
4. Condition
Condition 是状态转换条件。
定义:
Condition 是判断当前状态是否允许执行某一个 Transition 的结构化条件。
例如:
Current State = READY
Event = START
Condition = DeviceAvailable = TRUE
满足:
READY
↓
START
↓
DeviceAvailable = TRUE
才允许:
RUNNING
4.1 Condition 状态
Condition 可以具有:
TRUE
FALSE
UNKNOWN
CONFLICT
不能把:
UNKNOWN
直接当成:
TRUE
也不能简单当成:
FALSE
例如:
DeviceAvailable = UNKNOWN
则:
READY
↓
START
↓
UNKNOWN
可能导致:
Transition = BLOCKED
4.2 多条件
可以使用:
AND
OR
NOT
例如:
State = READY
AND
Power = TRUE
AND
Connection = TRUE
全部满足:
READY → RUNNING
如果:
Power = TRUE
Connection = FALSE
则:
Transition = REJECTED
5. Action
State Machine 中的 Action 是状态转换过程中实际执行的操作。
例如:
READY
↓
START
↓
Condition TRUE
↓
Action = START_DEVICE
↓
RUNNING
Action 可以包括:
SET_PROPERTY
SET_STATE
CALL_METHOD
SEND_COMMAND
CREATE_EVENT
CREATE_OBJECT
UPDATE_OBJECT
STOP_PROCESS
5.1 State Machine Action 与 Decision Action
这里需要继续保持概念边界。
Decision Action
前面的 Action 是:
Decision
↓
Behavior
↓
Action
表示:
Individual 决定执行的具体操作。
State Machine Action
状态机 Action 是:
状态转换过程中用于完成状态切换或同步对象状态的操作。
例如:
READY
↓
Transition
↓
Action: initialize_runtime
↓
RUNNING
因此:
State Machine Action
可以是普通 Action 的内部执行动作,也可以是状态转换专用动作。
状态机本身不应该取代 Decision。
6. State Event
State Event 是与状态变化直接相关的事件。
例如:
STATE_ENTERED
STATE_EXITED
STATE_CHANGED
TRANSITION_STARTED
TRANSITION_COMPLETED
TRANSITION_FAILED
6.1 State Enter
例如:
READY
↓
RUNNING
产生:
STATE_EXITED
target = READY
以及:
STATE_ENTERED
target = RUNNING
6.2 State Changed
可以产生:
STATE_CHANGED
结构:
{
object_id: "Machine_A",
previous_state: "READY",
current_state: "RUNNING",
event: "START",
timestamp: "..."
}
6.3 Transition Event
转换开始:
TRANSITION_STARTED
转换完成:
TRANSITION_COMPLETED
转换失败:
TRANSITION_FAILED
于是状态机本身也可以使用第95章建立的 Event System:
State Machine
↓
State Event
↓
Event Queue
↓
Event Dispatcher
↓
Listener
↓
Handler
7. Individual 状态机
Individual 是 SAI 的运行主体,因此需要拥有自己的状态机。
7.1 Individual State
CREATED
↓
INITIALIZING
↓
READY
↓
RUNNING
↓
WAITING
↓
RUNNING
↓
STOPPING
↓
STOPPED
异常:
ERROR
PAUSED
UNKNOWN
7.2 Individual 启动
CREATED
│
│ INITIALIZE
▼
INITIALIZING
│
│ INITIALIZATION_SUCCESS
▼
READY
对应:
Event = INITIALIZE
Condition = ConfigurationValid
Action = initialize()
7.3 Individual 开始运行
READY
│
│ START
▼
RUNNING
条件:
Kernel = RUNNING
Memory = READY
Required Engines = READY
InformationReceiver = READY
满足后:
Action = startRuntime
7.4 Individual 等待
当没有新的 Information:
RUNNING
│
│ NO_INPUT
▼
WAITING
收到新 Information:
WAITING
│
│ INFORMATION_RECEIVED
▼
RUNNING
这与第94章的生命周期直接对应。
7.5 Individual 暂停
例如系统检测到异常:
RUNNING
↓
ERROR_DETECTED
↓
PAUSE
↓
PAUSED
修复完成:
PAUSED
↓
RESUME
↓
RUNNING
7.6 Individual 停止
RUNNING
│
│ STOP
▼
STOPPING
│
│ STOP_COMPLETED
▼
STOPPED
Individual 状态机完整模型
┌──────────┐
│ CREATED │
└────┬─────┘
│ INITIALIZE
▼
┌──────────────┐
│ INITIALIZING │
└──────┬───────┘
│ SUCCESS
▼
┌────────┐
┌──────│ READY │──────┐
│ └───┬────┘ │
│ │ START │
│ ▼ │
│ ┌─────────┐ │
│ │ RUNNING │◄─────┘
│ └────┬────┘
│ │ NO_INPUT
│ ▼
│ ┌─────────┐
└──────│ WAITING │
└────┬────┘
│ INFORMATION
└──────────────→ RUNNING
RUNNING
│
│ STOP
▼
STOPPING
│
▼
STOPPED
RUNNING
│ ERROR
▼
ERROR
│
│ REPAIR + VERIFY
▼
READY
8. Device 状态机
Device 状态机主要负责设备运行状态。
例如:
OFF
INITIALIZING
READY
RUNNING
PAUSED
STOPPING
STOPPED
ERROR
MAINTENANCE
DISCONNECTED
UNKNOWN
8.1 Device 启动
OFF
↓ POWER_ON
INITIALIZING
↓ INITIALIZATION_SUCCESS
READY
8.2 Device 开始运行
READY
↓ START
RUNNING
条件:
Power = ON
Connection = CONNECTED
No Critical Error
Capability = AVAILABLE
8.3 Device 暂停
RUNNING
↓ PAUSE
PAUSED
恢复:
PAUSED
↓ RESUME
RUNNING
8.4 Device 停止
RUNNING
↓ STOP
STOPPING
↓ STOP_COMPLETED
STOPPED
8.5 Device 异常
例如温度超过安全限制:
RUNNING
↓
TEMPERATURE_HIGH
↓
Condition:
temperature > 95
↓
STOP
↓
STOPPING
↓
STOPPED
如果是严重异常:
RUNNING
↓
CRITICAL_ERROR
↓
EMERGENCY_STOP
↓
STOPPED
8.6 Device 断开
RUNNING
↓
CONNECTION_LOST
↓
DISCONNECTED
重新连接:
DISCONNECTED
↓ CONNECT
READY
是否能够直接进入 READY,需要根据设备实际状态和 Condition 判断,不能固定假设。
Device 状态机完整模型
┌──────┐
│ OFF │
└──┬───┘
│ POWER_ON
▼
┌──────────────┐
│ INITIALIZING │
└──────┬───────┘
│ SUCCESS
▼
┌───────┐
┌─────│ READY │─────┐
│ └───┬───┘ │
│ │ START │
│ ▼ │
│ ┌─────────┐ │
│ │ RUNNING │ │
│ └────┬────┘ │
│ │ │
│ │ PAUSE │
│ ▼ │
│ ┌─────────┐ │
│ │ PAUSED │───┘ RESUME
│ └─────────┘
│
│ STOP
▼
STOPPING
│
▼
STOPPED
RUNNING
│
│ ERROR
▼
ERROR
│
│ REPAIR + VERIFY
▼
READY
RUNNING
│
│ CONNECTION_LOST
▼
DISCONNECTED
Individual 与 Device 状态机的关系
这是 SAI 状态机体系中非常重要的一层。
Individual:
Individual_A
state = RUNNING
Device:
Machine_A
state = RUNNING
两者不是同一个状态。
例如:
Individual_A = RUNNING
Machine_A = ERROR
这是完全可能的。
Individual 可以发现:
Machine_A ERROR
然后:
Detection
↓
Risk
↓
Diagnosis
↓
Decision
↓
Repair
所以:
Individual State
≠
Device State
但是二者可以通过 Event、Information、Feedback 建立关系:
Device State
↓
State Event
↓
Event System
↓
Information
↓
Individual
↓
Cognition
↓
Decision
↓
Action
↓
Device State
状态机与生命周期、事件系统的统一关系
现在前三章可以正式连接:
Lifecycle
│
▼
State
▲
│
Transition
▲ │
│ ▼
Event → Condition
│
▼
Action
│
▼
New State
│
▼
State Event
│
└────→ Event System
也就是说:
第94章
Lifecycle
规定对象从哪里开始、如何运行、如何结束。
第95章
Event System
负责传递“发生了什么”。
第96章
State Machine
负责根据:
Current State
+
Event
+
Condition
决定:
Transition
+
Action
+
New State
SAI 状态机核心公式
可以把状态转换抽象成:
Transition =
(Current State + Event + Condition)
完整执行:
Current State
+
Event
+
Condition
↓
Transition
↓
Action
↓
New State
↓
State Event
例如:
Machine_A
state = RUNNING
Event:
TEMPERATURE_CHANGED
Property:
temperature = 97
Condition:
temperature > 95
Transition:
RUNNING → STOPPING
Action:
STOP_DEVICE
New State:
STOPPED
形成:
RUNNING
↓
TEMPERATURE_CHANGED
↓
temperature > 95
↓
Transition
↓
STOP_DEVICE
↓
STOPPED
↓
STATE_CHANGED
本章核心定义
State Machine
State Machine 是通过 State、Event、Condition、Transition 和 Action 管理对象状态变化的离散运行机制。
State
State 表示对象当前所处的结构化状态。
Transition
Transition 表示对象从一个状态向另一个状态的合法转换过程。
Condition
Condition 决定某个状态转换是否满足执行条件。
Action
Action 是状态转换过程中实际执行的操作。
State Event
State Event 是状态进入、退出、改变以及状态转换过程产生的事件。
最终形成:
Event
↓
Condition
↓
Transition
↓
Action
↓
State Change
↓
State Event
↓
Event System
而 SAI Framework 中已经形成三层基础机制:
第94章 Lifecycle
↓
管理“生命周期”
第95章 Event System
↓
管理“发生什么”
第96章 State Machine
↓
管理“状态如何变化”
最终统一为:
┌──────────────────────────────────────┐
│ SAI Framework │
│ │
│ Lifecycle │
│ ↓ │
│ State Machine │
│ ↑ │
│ Event System │
│ ↓ │
│ State → Event → Condition │
│ ↓ │
│ Transition → Action → New State │
│ │
└──────────────────────────────────────┘
这一步使 SAI Framework 中的 Individual、Device、Behavior、Information、Object、Learning 不再只是具有一个静态 state 属性,而是开始具备明确、可追踪、可验证的状态转换机制。