第七篇 Central 中央协调
第26章 Central
本章大纲
- Central 定义
- Central 与 Individual
- Central 与 Application
- Central 接收
- Central 分发
- Central 协调
- Central 状态
- Central 生命周期
- Central 不负责什么
- Central 完整代码结构
26.1 Central 定义
Central(中央协调器)是 SAI Framework 中负责协调 Individual 内部各个核心模块运行的中央组件。
Central 的核心作用不是“思考”,而是:
接收进入 Individual 的信息,判断应该交给哪个内部模块处理,并协调各模块按照规定的运行顺序完成一次 SAI 生命周期。
可以把 Central 理解为:
Central
=
内部协调中心
而不是:
Central
≠ Cognition
≠ Reasoning
≠ Decision
≠ Intelligence
26.1.1 Central 在整体架构中的位置
前面的 Information 与 Scene 已经建立了外部信息进入系统的路径:
外部世界
↓
InformationSource
↓
InformationReceiver
↓
Information
↓
SceneCollector
↓
Scene
↓
Central
↓
SAI内部处理
Central 再向内部各模块协调:
Central
│
┌────────────────┼────────────────┐
↓ ↓ ↓
Perception Memory Cognition
│ │ │
└────────────────┼────────────────┘
↓
Reasoning
↓
Decision
↓
Behavior
↓
Action
因此,Central 位于:
外部输入
↓
Information / Scene
↓
Central
↓
内部认知处理
↓
外部行为
26.2 Central 与 Individual
Central 与 Individual 是两个不同层次。
Individual
Individual 是整个模拟人工个体。
Individual
├── Identity
├── State
├── Ability
├── Memory
├── Cognition
├── Learning
└── Behavior
Central
Central 是 Individual 内部的协调中心。
Individual
│
├── Identity
├── State
├── Ability
├── Memory
├── Cognition
├── Learning
├── Behavior
│
└── Central
│
├── Perception
├── Cognition
├── Memory
├── Reasoning
├── Decision
└── Behavior
因此:
Individual = 谁在运行
Central = 谁负责协调运行
26.2.1 为什么不能把 Central 等同于 Individual?
因为 Individual 包含的不只是运行协调。
例如:
Individual
├── Identity
│
├── State
│
├── Ability
│
├── Memory
│
├── Cognition
│
├── Learning
│
└── Behavior
Central 只是让这些能力按照生命周期运行。
例如:
Central
↓
调用 Perception
↓
调用 Cognition
↓
调用 Memory
↓
调用 Reasoning
↓
调用 Decision
↓
调用 Behavior
所以 Central 是:
Individual 的运行协调机制。
而不是:
Individual 本身。
26.3 Central 与 Application
第8章已经定义过 Application。
两者也不能混淆。
Application
↓
负责整个 Framework Runtime
而:
Central
↓
负责 Individual 内部协调
可以表示为:
Application
│
├── Bootstrap
├── Container
├── Application Lifecycle
│
└── Individual
│
└── Central
│
├── Perception
├── Cognition
├── Memory
├── Reasoning
├── Decision
└── Behavior
因此:
| 组件 | 主要职责 |
|---|---|
| Application | Framework 运行环境 |
| Individual | 模拟人工个体 |
| Central | Individual 内部协调 |
| Engine | 执行具体处理 |
| Manager | 管理对象/资源 |
| Controller | 外部入口控制 |
最重要的关系:
Application
↓
运行 Individual
Individual
↓
拥有 Central
Central
↓
协调 Engines
26.4 Central 接收
Central 首先需要接收进入 Individual 的内部输入。
它可以接收:
Information
Scene
Event
Feedback
Task
Action Result
但这里需要特别区分:
InformationReceiver 负责外部信息接收,Central 负责接收已经进入运行链路的内部输入。
例如:
Sensor
↓
InformationSource
↓
InformationReceiver
↓
Information
↓
Central
或者:
SceneCollector
↓
Scene
↓
Central
Central 不应该直接代替 Sensor 或 InformationReceiver。
26.4.1 Central 接收接口
可以设计为:
public function receive($input)
{
$this->input = $input;
}
也可以进一步区分:
public function receiveInformation($information)
{
$this->input = $information;
}
public function receiveScene($scene)
{
$this->scene = $scene;
}
public function receiveEvent($event)
{
$this->event = $event;
}
public function receiveFeedback($feedback)
{
$this->feedback = $feedback;
}
这样 Central 就成为内部运行入口。
26.5 Central 分发
Central 接收到信息以后,并不是自己处理全部内容。
它需要把任务交给对应的 Engine。
例如:
Central
│
├── Information
│ ↓
│ PerceptionEngine
│
├── Memory Request
│ ↓
│ MemoryEngine
│
├── Cognition Request
│ ↓
│ CognitionEngine
│
├── Reasoning Request
│ ↓
│ ReasoningEngine
│
├── Decision Request
│ ↓
│ DecisionEngine
│
└── Behavior Request
↓
BehaviorEngine
这就是:
Central 分发。
26.5.1 Central 为什么需要分发?
假设一个场景:
Temperature = 32°C
Person = PRESENT
Fan = OFF
Central 不应该自己完成:
读取温度
判断对象
理解场景
查询记忆
执行推理
产生决策
控制风扇
而应该:
Central
↓
PerceptionEngine
↓
CognitionEngine
↓
MemoryEngine
↓
ReasoningEngine
↓
DecisionEngine
↓
BehaviorEngine
每个 Engine 负责自己的工作。
Central 负责把这些工作连接起来。
26.6 Central 协调
Central 最核心的职责就是:
协调。
协调包括:
① 顺序协调
确定:
先感知
再认知
再记忆
再推理
再决策
再行为
而不是:
Decision
↓
Perception
② 数据协调
前一个 Engine 的结果成为后一个 Engine 的输入。
例如:
Information
↓
Perception
↓
Elements
↓
Objects
↓
Cognition
↓
Understanding
↓
Reasoning
↓
Conclusion
↓
Decision
↓
Behavior
Central 负责传递这些结果。
③ 状态协调
例如:
PerceptionEngine = running
CognitionEngine = completed
ReasoningEngine = running
DecisionEngine = waiting
Central 需要知道各模块目前处于什么状态。
④ 异常协调
如果:
PerceptionEngine
↓
failed
Central 不应该继续无条件执行:
Reasoning
↓
Decision
↓
Behavior
而应该根据系统规则:
Perception Failed
↓
Central
↓
停止当前流程
↓
记录异常
↓
等待恢复 / 重试 / 进入安全状态
这也为后面的:
Detection
Risk
Conflict
Diagnosis
Repair
Verification
提供基础。
26.6.1 Central 的协调模型
可以把 Central 简化为:
Input
↓
Receive
↓
Dispatch
↓
Coordinate
↓
Engine
↓
Result
↓
Next Engine
↓
Action
↓
Feedback
因此:
Central
=
Receive
+
Dispatch
+
Coordinate
+
State Management
26.7 Central 状态
Central 自身也需要状态。
例如:
created
initialized
ready
running
waiting
paused
error
stopping
stopped
可以表示:
Central State
│
├── created
├── initialized
├── ready
├── running
├── waiting
├── paused
├── error
├── stopping
└── stopped
26.7.1 Central 当前运行状态
例如:
$central->getState();
返回:
running
说明 Central 当前正在协调一次 Individual 运行过程。
如果:
error
说明 Central 当前协调过程出现异常。
26.7.2 Central 内部处理状态
除了自身状态,还可以记录当前流程:
Central
├── state = running
├── currentStage = reasoning
├── currentEngine = ReasoningEngine
├── input
├── result
└── error
例如:
array(
'state' => 'running',
'stage' => 'reasoning',
'engine' => 'ReasoningEngine'
);
这样就能够知道:
Individual 当前运行到哪里。
26.8 Central 生命周期
Central 也具有自己的生命周期。
Created
↓
Initialized
↓
Ready
↓
Running
↓
Waiting / Paused
↓
Running
↓
Stopping
↓
Stopped
26.8.1 Created
Central 对象刚刚创建。
Central
state = created
此时可能还没有:
- Engine
- Dispatcher
- Coordinator
- Individual Context
26.8.2 Initialized
初始化依赖:
Central
↓
Dispatcher
↓
Coordinator
↓
Engines
26.8.3 Ready
所有必要组件准备完成:
Central
state = ready
表示:
可以接受运行输入。
26.8.4 Running
开始协调 Individual。
例如:
Information
↓
Perception
↓
Cognition
↓
Reasoning
↓
Decision
↓
Behavior
Central:
state = running
26.8.5 Waiting
某个流程需要等待:
等待 Information
等待 Sensor
等待 Device
等待 Feedback
Central 可以进入:
waiting
26.8.6 Error
出现异常:
Engine Error
Input Error
State Error
Device Error
Central:
state = error
后续再由维护机制处理。
26.8.7 Stopping / Stopped
Individual 停止运行:
Running
↓
Stopping
↓
Stopped
Central 也需要同步结束。
26.9 Central 不负责什么
这一部分非常重要。
Central 很容易在设计过程中变成“万能类”。
这是错误的。
26.9.1 Central 不负责感知
错误:
Central
↓
识别摄像头
↓
识别温度
↓
识别人
正确:
Central
↓
PerceptionEngine
26.9.2 Central 不负责认知
Central 不负责:
理解对象
理解关系
理解状态
这些属于:
Cognition
CognitionEngine
26.9.3 Central 不负责推理
例如:
Temperature > 30
AND Person = PRESENT
如何得到:
需要降温
这是:
ReasoningEngine
而不是 Central。
26.9.4 Central 不负责决策
例如:
TurnOnFan
OpenWindow
DoNothing
哪个优先,需要由:
DecisionEngine
处理。
26.9.5 Central 不负责执行行为
Central 不应该直接:
$fan->turnOn();
而应该:
Central
↓
BehaviorEngine
↓
Behavior
↓
Action
↓
Adapter
↓
Device
26.9.6 Central 不负责学习
学习属于:
Learning
LearningEngine
Experience
Memory
Central 只负责协调学习流程。
例如:
Feedback
↓
Experience
↓
LearningEngine
↓
Memory Update
Central 负责连接这个过程,而不是自己学习。
26.9.7 Central 不应该成为“万能类”
错误结构:
Central
├── 感知
├── 认知
├── 记忆
├── 推理
├── 决策
├── 行为
├── 学习
├── 设备控制
├── 数据库
└── 网络
这种设计会形成一个巨大的:
God Class
最终导致:
- 职责混乱
- 难以维护
- 模块无法独立
- 测试困难
- 扩展困难
正确结构:
Central
│
├── Dispatcher
├── Coordinator
│
├── PerceptionEngine
├── CognitionEngine
├── MemoryEngine
├── ReasoningEngine
├── DecisionEngine
├── BehaviorEngine
└── LearningEngine
Central 负责协调,而不是吞并这些模块。
26.10 Central 完整代码结构
下面建立一个基础的 Central OOP 结构。
以下 PHP 代码是本章的设计示例,用于说明 SAI Framework 的 OOP 结构,未作为实际项目代码执行验证。
26.10.1 Central 基础类
<?php
namespace SAI\Central;
class Central
{
protected $state = 'created';
protected $input = null;
protected $result = null;
protected $currentStage = null;
protected $engines = array();
public function initialize()
{
$this->state = 'initialized';
}
public function ready()
{
$this->state = 'ready';
}
public function start()
{
$this->state = 'running';
}
public function stop()
{
$this->state = 'stopped';
}
public function getState()
{
return $this->state;
}
public function receive($input)
{
$this->input = $input;
}
public function registerEngine($name, $engine)
{
$this->engines[$name] = $engine;
}
public function getEngine($name)
{
if (!isset($this->engines[$name])) {
return null;
}
return $this->engines[$name];
}
public function setStage($stage)
{
$this->currentStage = $stage;
}
public function getStage()
{
return $this->currentStage;
}
public function setResult($result)
{
$this->result = $result;
}
public function getResult()
{
return $this->result;
}
}
这个类目前只负责:
状态
输入
结果
阶段
Engine 注册
Engine 获取
生命周期
没有把 Cognition、Reasoning、Decision 等逻辑直接写进去。
26.10.2 Central 与 Engine
可以进一步建立:
public function process($input)
{
$this->receive($input);
$this->setStage('perception');
$engine = $this->getEngine('perception');
if ($engine === null) {
return null;
}
$result = $engine->process($input);
$this->setResult($result);
return $result;
}
这里体现:
Central
↓
找到 Engine
↓
调用 Engine
↓
获得结果
↓
保存结果
Central 自己没有实现 Perception。
26.10.3 Central 协调完整流程
如果已经有多个 Engine,可以形成:
public function run($input)
{
$this->state = 'running';
$this->receive($input);
/*
* 1. Perception
*/
$this->setStage('perception');
$perception = $this->getEngine('perception');
if ($perception === null) {
$this->state = 'error';
return null;
}
$perceptionResult = $perception->process($input);
/*
* 2. Cognition
*/
$this->setStage('cognition');
$cognition = $this->getEngine('cognition');
if ($cognition === null) {
$this->state = 'error';
return null;
}
$cognitionResult = $cognition->process(
$perceptionResult
);
/*
* 3. Reasoning
*/
$this->setStage('reasoning');
$reasoning = $this->getEngine('reasoning');
if ($reasoning === null) {
$this->state = 'error';
return null;
}
$reasoningResult = $reasoning->process(
$cognitionResult
);
/*
* 4. Decision
*/
$this->setStage('decision');
$decision = $this->getEngine('decision');
if ($decision === null) {
$this->state = 'error';
return null;
}
$decisionResult = $decision->process(
$reasoningResult
);
/*
* 5. Behavior
*/
$this->setStage('behavior');
$behavior = $this->getEngine('behavior');
if ($behavior === null) {
$this->state = 'error';
return null;
}
$behaviorResult = $behavior->process(
$decisionResult
);
$this->setStage('completed');
$this->result = $behaviorResult;
return $behaviorResult;
}
这个例子体现 Central 的核心价值:
Central
│
├── 接收 Input
│
├── 调度 Perception
│
├── 调度 Cognition
│
├── 调度 Reasoning
│
├── 调度 Decision
│
├── 调度 Behavior
│
└── 返回最终结果
26.10.4 Central 完整结构关系
经过本章,可以得到:
Application
│
▼
Individual
│
▼
Central
│
┌──────────────┼──────────────┐
│ │ │
▼ ▼ ▼
Dispatcher Coordinator State
│ │
└───────┬──────┘
│
▼
Engine
│
┌────────────┼────────────┐
▼ ▼ ▼
Perception Cognition Memory
│ │ │
└────────────┼────────────┘
▼
Reasoning
│
▼
Decision
│
▼
Behavior
│
▼
Action
26.10.5 Central 在 SAI 完整数据流中的位置
现在结合前面第19~25章:
External World
│
▼
InformationSource
│
▼
Sensor / Human / Device / System
│
▼
InformationReceiver
│
▼
Information
│
▼
SceneCollector
│
▼
Scene
│
▼
Central
│
▼
Perception
│
▼
Element / Object
│
▼
Cognition
│
▼
Understanding
│
▼
Memory
│
▼
Reasoning
│
▼
Conclusion
│
▼
Decision
│
▼
Behavior
│
▼
Action
│
▼
External World
Central 位于整个 Individual 的内部协调枢纽位置。
本章核心认识
Central 最核心的一句话是:
Central 不负责“智能处理本身”,而负责让 Individual 内部的智能处理组件按照正确的顺序、状态和数据关系协同运行。
可以最终归纳成:
InformationReceiver
↓
接收外部信息
SceneCollector
↓
组织场景
Central
↓
协调内部运行
Engine
↓
执行具体处理
Individual
↓
作为完整模拟人工个体运行
进一步压缩:
Application
↓
Individual
↓
Central
↓
Engine
↓
Processing
↓
Behavior
而 Central 自己的职责只有几个核心动作:
Receive
↓
Dispatch
↓
Coordinate
↓
Monitor State
↓
Continue / Stop / Error
这也为下一章 Dispatcher(中央分发器) 做准备:
Central
│
├── Dispatcher
│ ↓
│ 分发任务
│
└── Coordinator
↓
协调流程
也就是说,Central 是中央协调层,Dispatcher 解决“交给谁”,Coordinator 解决“怎么协同运行”。