首页 理论 架构 工程 文档 白皮书 著作 研究 案例 下载 博客 关于 开始使用 →

第27章 Dispatcher

第七篇 Central 中央协调

第27章 Dispatcher

本章大纲

  1. Dispatcher
  2. 信息分发
  3. 事件分发
  4. 模块分发
  5. 行为分发
  6. 反馈分发
  7. Dispatcher 规则
  8. Dispatcher 示例

27.1 Dispatcher

Dispatcher(分发器)是 SAI Framework 中负责根据输入类型、事件类型、处理阶段和分发规则,将信息或任务发送给指定模块的组件。

简单来说:

Dispatcher 解决的是“这个东西应该交给谁处理”。

上一章的 Central 解决:

“整个 Individual 如何协调运行?”

Dispatcher 解决:

“当前输入应该发送到哪个模块?”

因此:

Central
   ↓
Dispatcher
   ↓
确定目标
   ↓
指定模块

27.1.1 Dispatcher 在 Central 中的位置

                    Individual
                         │
                         ▼
                      Central
                         │
                    Dispatcher
                         │
       ┌─────────────────┼─────────────────┐
       ▼                 ▼                 ▼
 Information          Event             Feedback
       │                 │                 │
       ▼                 ▼                 ▼
 Perception          Manager /          Learning /
 Engine              Coordinator        Memory

Dispatcher 是 Central 的一个重要内部组件。

可以理解为:

Central = 中央协调
Dispatcher = 中央分发

27.1.2 Dispatcher 与 Controller 的区别

两者都存在“分发/控制”行为,但所在层次完全不同。

Controller
   ↓
外部请求
   ↓
进入 SAI

而:

Central
   ↓
Dispatcher
   ↓
内部模块

所以:

Controller = 外部入口控制

Dispatcher = 内部分发控制

例如:

HTTP Request
     ↓
Controller
     ↓
Information
     ↓
Central
     ↓
Dispatcher
     ↓
PerceptionEngine

27.2 信息分发

Dispatcher 最基础的用途是Information Distribution(信息分发)

前面已经建立:

InformationSource
       ↓
InformationReceiver
       ↓
Information
       ↓
Central

Central 收到 Information 后,需要判断:

这个 Information 应该交给谁?

例如:

Temperature Information
        ↓
PerceptionEngine
Device State Information
        ↓
State / Memory
Feedback Information
        ↓
Feedback / Experience / Learning

27.2.1 信息分发规则

可以建立最基础的规则:

IF information.type = scene
THEN Perception

IF information.type = state
THEN State / Memory

IF information.type = event
THEN Event Handler

IF information.type = feedback
THEN Feedback

IF information.type = instruction
THEN Task / Behavior Processing

注意:

Dispatcher 只是判断和发送,不负责真正理解信息。

例如:

temperature = 32

Dispatcher 不负责判断:

32°C 是否很热?

它只负责:

type = temperature
→ 发送给对应处理模块

27.2.2 信息分发流程

Information
     ↓
Dispatcher
     ↓
识别 type
     ↓
查找分发规则
     ↓
确定 Target
     ↓
发送

例如:

Temperature Information
        ↓
Dispatcher
        ↓
type = sensor
subtype = temperature
        ↓
PerceptionEngine

27.3 事件分发

除了 Information,SAI Framework 还会产生各种 Event。

例如:

Event
├── InformationReceived
├── SceneUpdated
├── StateChanged
├── ActionCompleted
├── DeviceChanged
├── FeedbackReceived
├── ErrorOccurred
└── LearningCompleted

Dispatcher 可以根据事件类型进行分发。


27.3.1 Event 的结构

可以设计:

Event
{
    id
    type
    source
    data
    time
}

例如:

{
    id: "event-001",
    type: "state_changed",
    source: "fan-001",
    data: {
        old: "off",
        new: "on"
    },
    time: "..."
}

Dispatcher 接收到以后:

state_changed
      ↓
Dispatcher
      ↓
StateManager
Memory
Feedback

27.3.2 事件分发与信息分发的区别

Information
→ 描述“获得了什么信息”

Event
→ 描述“发生了什么事情”

例如:

Temperature = 32

属于 Information。

而:

TemperatureChanged

属于 Event。

两者可以发生联系:

Sensor
 ↓
Information
 ↓
Temperature = 32
 ↓
State Changed
 ↓
Event
 ↓
Dispatcher

27.4 模块分发

Dispatcher 不只是根据 Information 类型分发,还可以直接根据任务或处理阶段把数据交给不同模块。

例如:

Central
   ↓
Dispatcher
   │
   ├── PerceptionEngine
   ├── CognitionEngine
   ├── MemoryEngine
   ├── ReasoningEngine
   ├── DecisionEngine
   ├── BehaviorEngine
   └── LearningEngine

27.4.1 模块分发的基本形式

例如:

$dispatcher->dispatch(
    'perception',
    $information
);

Dispatcher 找到:

perception
     ↓
PerceptionEngine

然后:

$engine->process($information);

这样 Central 不需要知道每个 Engine 的具体内部实现。


27.4.2 Dispatcher 与 Engine

二者职责不同:

Dispatcher
    ↓
决定交给谁
Engine
    ↓
真正处理

例如:

Information
    ↓
Dispatcher
    ↓
PerceptionEngine
    ↓
Perception Result

因此:

Dispatcher 不等于 Engine。


27.5 行为分发

Dispatcher 还可以用于行为输出。

例如 DecisionEngine 得到:

Decision
{
    action: "turn_on_fan"
}

Dispatcher 不应该自己打开风扇。

它应该:

Decision
   ↓
Dispatcher
   ↓
BehaviorEngine
   ↓
Behavior
   ↓
Action
   ↓
DeviceAdapter
   ↓
Fan

27.5.1 行为目标分发

假设存在:

turn_on_fan
open_window
send_message
stop_machine
move_forward

可以建立:

Behavior
      ↓
Dispatcher
      │
      ├── DeviceAdapter
      ├── RobotAdapter
      ├── WebRenderer
      └── ApiRenderer

例如:

turn_on_fan
      ↓
FanAdapter
      ↓
Fan Device

而:

send_message
      ↓
Web/API Adapter

27.5.2 Dispatcher 不负责决定行为

这是一个重要边界。

错误:

Dispatcher
 ↓
判断温度
 ↓
决定开风扇
 ↓
执行风扇

正确:

Perception
 ↓
Cognition
 ↓
Reasoning
 ↓
Decision
 ↓
Behavior
 ↓
Dispatcher
 ↓
目标 Adapter / Device

因此:

Reasoning = 得出结论

Decision = 决定做什么

Behavior = 表达准备执行什么

Dispatcher = 把行为发送到哪里

Adapter = 转换成外部设备能够执行的形式

27.6 反馈分发

行为执行以后,外部世界会产生反馈。

例如:

Action
 ↓
Fan ON
 ↓
Temperature changes
 ↓
Sensor
 ↓
Information
 ↓
Feedback

Dispatcher 可以将 Feedback 分发给:

Feedback
   ↓
Dispatcher
   │
   ├── Memory
   ├── Experience
   ├── Learning
   ├── State
   └── Diagnosis

27.6.1 为什么反馈需要分发?

因为同一个 Feedback 可能影响多个内部模块。

例如:

Fan ON
Temperature 32 → 29

这个结果可能产生:

State Update
Experience Record
Memory Update
Learning Input

因此:

Feedback
    ↓
Dispatcher
    ├── State
    ├── Memory
    ├── Experience
    └── Learning

Dispatcher 负责:

让同一反馈进入需要它的模块。

但 Dispatcher 不负责判断这个反馈意味着什么。


27.7 Dispatcher 规则

Dispatcher 的核心不是大量代码,而是:

分发规则。


27.7.1 基础规则

最简单的规则:

IF Type
THEN Target

例如:

scene → perception
state → memory
feedback → learning
event → event_handler
behavior → adapter

27.7.2 条件分发

进一步可以:

IF type = state
AND source = device
THEN device_state_manager

或者:

IF type = feedback
AND result = success
THEN experience
IF type = feedback
AND result = failure
THEN diagnosis

27.7.3 优先级

不同信息可能具有不同优先级:

Emergency
   ↓
Safety
   ↓
Control
   ↓
Task
   ↓
Normal Information

例如:

Fire Alarm

应该优先于:

Room Temperature

Dispatcher 可以根据:

priority

决定分发顺序。


27.7.4 状态限制

Dispatcher 还应该考虑目标模块状态。

例如:

PerceptionEngine = stopped

此时:

Scene Information

不能正常分发给它。

因此:

IF target.state != ready
THEN reject / wait / retry

这使 Dispatcher 与 Central 的状态管理产生联系。


27.7.5 Dispatcher 基本规则模型

可以抽象为:

DispatchRule
{
    type
    subtype
    source
    condition
    priority
    target
}

例如:

{
    type: "scene",
    subtype: "environment",
    priority: 10,
    target: "perception"
}

或者:

{
    type: "feedback",
    subtype: "failure",
    priority: 100,
    target: "diagnosis"
}

27.8 Dispatcher 示例

下面建立一个基础的 PHP OOP Dispatcher。

以下 PHP 代码是本章的设计示例,用于说明 SAI Framework 的 OOP 结构,未作为实际项目代码执行验证。


27.8.1 Dispatcher 基础类

<?php

namespace SAI\Central;

class Dispatcher
{
    protected $targets = array();

    protected $rules = array();


    public function registerTarget($name, $target)
    {
        $this->targets[$name] = $target;
    }


    public function addRule($type, $target)
    {
        $this->rules[$type] = $target;
    }


    public function dispatch($type, $data)
    {
        if (!isset($this->rules[$type])) {
            return null;
        }

        $targetName = $this->rules[$type];

        if (!isset($this->targets[$targetName])) {
            return null;
        }

        $target = $this->targets[$targetName];

        return $target->process($data);
    }
}

这个 Dispatcher 完成:

注册目标
 ↓
注册规则
 ↓
接收类型
 ↓
查找目标
 ↓
发送数据

27.8.2 注册 Engine

例如:

$dispatcher = new Dispatcher();

$dispatcher->registerTarget(
    'perception',
    $perceptionEngine
);

$dispatcher->registerTarget(
    'memory',
    $memoryEngine
);

$dispatcher->registerTarget(
    'learning',
    $learningEngine
);

建立规则:

$dispatcher->addRule(
    'scene',
    'perception'
);

$dispatcher->addRule(
    'state',
    'memory'
);

$dispatcher->addRule(
    'feedback',
    'learning'
);

形成:

scene
 ↓
perception

state
 ↓
memory

feedback
 ↓
learning

27.8.3 实际分发

例如:

$result = $dispatcher->dispatch(
    'scene',
    $scene
);

运行关系:

Scene
 ↓
Dispatcher
 ↓
查找 scene 规则
 ↓
perception
 ↓
PerceptionEngine
 ↓
process(Scene)

27.8.4 多目标分发

有些信息需要发送给多个模块。

例如 Feedback:

Feedback
   ↓
Memory
Experience
Learning

此时可以将规则设计成数组:

$this->rules['feedback'] = array(
    'memory',
    'experience',
    'learning'
);

Dispatcher:

public function dispatchMany($type, $data)
{
    if (!isset($this->rules[$type])) {
        return array();
    }

    $results = array();

    foreach ($this->rules[$type] as $targetName) {

        if (!isset($this->targets[$targetName])) {
            continue;
        }

        $target = $this->targets[$targetName];

        $results[$targetName] = $target->process($data);
    }

    return $results;
}

于是:

Feedback
    ↓
Dispatcher
    │
    ├── MemoryEngine
    ├── ExperienceEngine
    └── LearningEngine

27.8.5 Central + Dispatcher

把本章与上一章结合:

Individual
    ↓
Central
    ↓
Dispatcher
    ↓
Target

例如:

class Central
{
    protected $dispatcher;

    public function __construct(Dispatcher $dispatcher)
    {
        $this->dispatcher = $dispatcher;
    }

    public function receive($input)
    {
        return $this->dispatcher->dispatch(
            $input['type'],
            $input['data']
        );
    }
}

于是:

Information
      ↓
Central
      ↓
Dispatcher
      ↓
目标模块

27.8.6 完整示例:温度场景

假设系统收到:

Scene
{
    type: "scene",
    temperature: 32,
    person: "present",
    fan: "off"
}

运行:

Scene
 ↓
Central
 ↓
Dispatcher
 ↓
scene → perception
 ↓
PerceptionEngine
 ↓
Elements / Objects
 ↓
Cognition
 ↓
Reasoning
 ↓
Decision
 ↓
Behavior
 ↓
Action

执行以后:

Fan = ON
Temperature = 29

产生 Feedback:

Feedback
 ↓
Central
 ↓
Dispatcher
 ├── State
 ├── Memory
 ├── Experience
 └── Learning

于是形成:

                  Central
                     │
                 Dispatcher
                     │
       ┌─────────────┼─────────────┐
       ↓             ↓             ↓
 Information       Event       Feedback
       │             │             │
       ↓             ↓             ↓
 Perception       Event       Memory
                              Experience
                              Learning

27.8.7 Dispatcher 完整职责模型

经过本章,可以把 Dispatcher 定义为:

Dispatcher
│
├── Receive Dispatch Request
│
├── Match Dispatch Rule
│
├── Identify Target
│
├── Check Target
│
├── Dispatch
│
├── Multi-Dispatch
│
├── Priority
│
└── Return Result

核心过程:

输入
 ↓
类型判断
 ↓
规则匹配
 ↓
目标确定
 ↓
状态检查
 ↓
单目标 / 多目标分发
 ↓
获得结果

本章小结

Dispatcher 与 Central 的关系可以最终确定为:

Individual
    ↓
Central
    ↓
Dispatcher
    ↓
模块 / Engine / Adapter / Manager

二者职责分别是:

组件 核心问题
Individual 谁在运行
Central 如何协调整个运行
Dispatcher 当前数据/任务交给谁
Coordinator 多个模块如何协同
Engine 具体怎么处理
Adapter 如何连接外部对象/设备

因此:

Central 是中央协调中心,Dispatcher 是中央分发机制。

Dispatcher 不负责:

不负责感知
不负责认知
不负责推理
不负责决策
不负责学习
不直接执行设备

它只负责:

确定目标
匹配规则
发送数据
管理分发过程

最终形成:

Information
     ↓
Central
     ↓
Dispatcher
     ↓
┌────┼────────┬────────┐
↓    ↓        ↓        ↓
Perception Memory  Reasoning  Feedback
     ↓
  Cognition
     ↓
  Decision
     ↓
  Behavior
     ↓
  Action

下一章 第28章 Coordinator 将进一步解决一个不同的问题:

Dispatcher 解决“交给谁”,Coordinator 解决“多个模块如何按照顺序、依赖、状态和结果协同完成任务”。

Leave a Reply

Your email address will not be published. Required fields are marked *