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

第110章 完整类关系

第110章 完整类关系

第109章建立了 SAI 的完整目录架构。

第110章进一步解决一个更具体的问题:

这些目录最终应该由哪些 Class 组成?Class 之间谁创建谁、谁调用谁、谁管理谁、谁依赖谁?

目录解决的是:

代码放在哪里?

类关系解决的是:

代码之间如何协作?

本章重点不是增加新的认知概念,而是把前面已经建立的 Application、Container、Individual、Central、Engine、Manager、Object、Memory、Decision、Behavior、Template、Renderer、Adapter、Device、Extension 连接起来。


1. Application

1.1 Application 定义

Application 是 SAI 应用程序的运行入口。

它负责启动整个 Framework。

基本关系:

Application
     ↓
Kernel
     ↓
Container
     ↓
Manager
     ↓
Individual

Application 不负责直接完成:

Cognition
Reasoning
Decision

而是启动运行环境。


1.2 Application Class

class Application
{
    protected $container;

    protected $kernel;

    public function __construct($container, $kernel)
    {
        $this->container = $container;
        $this->kernel = $kernel;
    }

    public function run($information)
    {
        $this->kernel->start();

        return $this->kernel->process($information);
    }

    public function shutdown()
    {
        return $this->kernel->shutdown();
    }
}

关系:

Application
    │
    ├── Container
    │
    └── Kernel

2. Container

2.1 Container 定义

Container 是运行时对象容器。

它保存已经创建的对象:

Individual
Manager
Engine
Memory
Object
Renderer
Adapter
Extension

例如:

Container
│
├── individual
├── memory_manager
├── engine_manager
├── object_manager
├── renderer_manager
├── adapter_manager
└── extension_manager

2.2 Container Class

class Container
{
    protected $objects = array();

    public function set($id, $object)
    {
        $this->objects[$id] = $object;
    }

    public function get($id)
    {
        if (!isset($this->objects[$id])) {
            return null;
        }

        return $this->objects[$id];
    }

    public function has($id)
    {
        return isset($this->objects[$id]);
    }

    public function remove($id)
    {
        if (isset($this->objects[$id])) {
            unset($this->objects[$id]);
        }
    }
}

2.3 Container 与 Registry

必须严格区分:

Registry
=
系统登记了什么
Container
=
当前运行中有什么对象

例如:

ExtensionRegistry
    ↓
RobotExtension 已登记

加载后:

Container
    ↓
RobotExtension 实例

所以:

Registry ≠ Container

3. Individual

Individual 是 SAI 的运行主体。

Individual
│
├── Identity
├── State
├── Ability
├── Memory
├── Central
└── InformationReceiver

类关系:

Individual
    │
    ├── Central
    ├── Memory
    └── Ability
          │
          ├── Perception
          ├── Cognition
          ├── Reasoning
          ├── Decision
          ├── Behavior
          └── Learning

3.1 Individual 不直接实现所有功能

例如:

$individual->perceive();
$individual->reason();
$individual->decide();

从架构上并不意味着 Individual 自己包含全部代码。

更合理的是:

Individual
     ↓
Central
     ↓
各功能组件

Individual 是:

功能组织主体。


4. Central

Central 是 Individual 内部的协调中心。

Individual
     ↓
Central
     ↓
Perception
     ↓
Cognition
     ↓
Memory
     ↓
Reasoning
     ↓
Decision
     ↓
Behavior

Central 可以持有这些组件:

class Central
{
    protected $perception;

    protected $cognition;

    protected $memory;

    protected $reasoning;

    protected $decision;

    protected $behavior;
}

但是 Central 的职责仍然是:

协调,而不是替代这些组件。


5. Engine

Engine 是具体功能执行类。

例如:

PerceptionEngine
CognitionEngine
ReasoningEngine
DecisionEngine
LearningEngine
DetectionEngine
RepairEngine

统一 Interface:

interface EngineInterface
{
    public function getId();

    public function getName();

    public function getVersion();

    public function initialize();

    public function execute($input);

    public function getState();

    public function shutdown();
}

关系:

Module
   ↓
Engine
   ↓
Input
   ↓
Processing
   ↓
Result

5.1 Engine 不负责管理其他 Engine

例如:

TemperatureEngine
DistanceEngine
PositionEngine

不应该互相承担管理职责。

统一交给:

EngineManager

因此:

Engine = 执行功能
Manager = 管理 Engine

6. Manager

Manager 是组件管理类。

可以有:

IndividualManager
ObjectManager
EngineManager
MemoryManager
DecisionManager
BehaviorManager
RendererManager
AdapterManager
ExtensionManager

统一关系:

Manager
   │
   ├── Register
   ├── Get
   ├── Remove
   ├── Start
   ├── Stop
   └── Execute / Manage

例如:

class EngineManager
{
    protected $engines = array();

    public function register(EngineInterface $engine)
    {
        $this->engines[$engine->getId()] = $engine;
    }

    public function get($id)
    {
        if (!isset($this->engines[$id])) {
            return null;
        }

        return $this->engines[$id];
    }
}

7. Object

Object 是现实实体的结构化表示。

例如:

Robot_A
Machine_A
Vehicle_A
Obstacle_A
Sensor_A

Class:

Object
│
├── Property
├── Method
├── Relation
└── State

例如:

class Object
{
    protected $id;

    protected $properties = array();

    protected $methods = array();

    protected $relations = array();

    protected $state;
}

ObjectManager 管理 Object:

ObjectManager
     ↓
Object

8. Memory

Memory 负责保存和读取认知资源。

结构:

Memory
│
├── ShortMemory
├── LongMemory
├── FactMemory
├── RelationMemory
├── StateMemory
└── ExperienceMemory

类关系:

Individual
     │
     ↓
Memory
     │
     ├── ShortMemory
     └── LongMemory
            ├── FactMemory
            ├── RelationMemory
            ├── StateMemory
            └── ExperienceMemory

MemoryManager:

MemoryManager
      ↓
Memory

8.1 Memory 与 Database

不能简单认为:

Memory = Database

Database 可以作为一种存储实现。

Memory 的职责更高:

Read
Write
Update
Validate
Retrieve
Archive

它为:

Cognition
Reasoning
Decision
Learning

提供历史资源。


9. Decision

Decision 负责选择方案。

结构:

Decision
│
├── Conclusion
├── Candidate
├── Risk
├── Priority
├── Conflict
└── Choice

关系:

Reasoning
    ↓
Conclusion
    ↓
Decision
    ├── Candidate
    ├── Risk
    ├── Priority
    └── Conflict
    ↓
Choice

Decision 不直接执行具体动作。


10. Behavior

Behavior 位于 Decision 和 Action 之间。

Decision
    ↓
Behavior
    ↓
Action

例如:

Decision = STOP

可以形成:

Behavior = SAFE_STOP

包含:

CHECK_STATE
REDUCE_SPEED
STOP
READ_STATE
VERIFY

因此:

Decision = 选择什么
Behavior = 怎么组织执行过程
Action = 当前执行什么

11. Template

Template 属于 SATE 表现层。

关系:

Expression
     ↓
Template
     ↓
Renderer

Template Class:

class Template
{
    protected $id;

    protected $name;

    protected $content;

    protected $variables = array();
}

TemplateEngine:

Template
     ↓
TemplateEngine
     ↓
Rendered Structure

Template 不应该调用:

Cognition
Reasoning
Decision
Learning

它只处理表现结构。


12. Renderer

Renderer 将结构化结果转换成具体表现形式。

例如:

WebRenderer
ApiRenderer
DeviceRenderer
CustomRenderer

关系:

Expression
     ↓
Template
     ↓
Renderer
     ↓
Rendered Result

统一 Interface:

interface RendererInterface
{
    public function getId();

    public function getName();

    public function getVersion();

    public function supports($type);

    public function render($expression, $template = null);

    public function getState();
}

RendererManager:

RendererManager
      ↓
Renderer

13. Adapter

Adapter 负责连接外部环境。

Action
   ↓
Adapter
   ↓
External Environment

例如:

WebAdapter
ApiAdapter
RobotAdapter
VehicleAdapter
DeviceAdapter
CustomAdapter

统一 Interface:

interface AdapterInterface
{
    public function getId();

    public function getName();

    public function getVersion();

    public function connect($target);

    public function send($action);

    public function receive();

    public function disconnect();

    public function getState();
}

AdapterManager:

AdapterManager
      ↓
Adapter

14. Device

Device 是设备的结构化表示。

例如:

Robot_A
Motor_A
Sensor_A
Machine_A

结构:

Device
│
├── Property
├── Method
├── Ability
├── State
├── Command
└── Response

类关系:

Device
   ↕
DeviceAdapter

但是二者不能混淆。

Device
=
设备对象
DeviceAdapter
=
连接设备的接口
DeviceCommand
=
具体设备命令
DeviceResponse
=
设备返回结果

15. Extension

Extension 是功能扩展载体。

Extension
│
├── Module
├── Engine
├── Object
├── Rule
├── Adapter
└── Renderer

ExtensionManager:

ExtensionManager
       ↓
Extension

ExtensionRegistry:

ExtensionRegistry
       ↓
Extension Definition

ExtensionLoader:

Registry
 ↓
Loader
 ↓
Class
 ↓
Extension Instance

因此:

ExtensionRegistry
        ↓
ExtensionLoader
        ↓
ExtensionManager
        ↓
Extension Runtime

16. Application 到 Individual

现在开始建立第一条完整依赖链。

Application
     ↓
Kernel
     ↓
Container
     ↓
Individual
     ↓
Central

解释:

Application
=
启动应用
Kernel
=
管理 Framework 生命周期
Container
=
提供运行对象
Individual
=
运行 SAI 个体
Central
=
协调 Individual 内部组件

17. Central 到 Cognition

Central 继续组织认知流程:

Central
   ↓
Perception
   ↓
Cognition
   ↓
Memory
   ↓
Reasoning
   ↓
Decision

完整:

Information
     ↓
Central
     ↓
Perception
     ↓
Cognition
     ↓
Memory
     ↓
Reasoning
     ↓
Decision

这里需要注意:

Memory 不是简单的“Reasoning 后面一步”。

实际上:

Cognition ← Memory
Reasoning ← Memory
Decision  ← Memory
Learning  ← Memory

Memory 是多个认知组件共享的资源。


18. Decision 到外部世界

Decision 之后:

Decision
   ↓
Behavior
   ↓
Action
   ↓
Expression
   ↓
Template
   ↓
Renderer
   ↓
Adapter
   ↓
Device / External Environment

但是对于设备控制,Expression/Template/Renderer 并不是每一次内部设备 Action 都必须经过。

因此可以存在:

外部表现路径

Decision
 ↓
Behavior
 ↓
Action
 ↓
Expression
 ↓
Template
 ↓
Renderer
 ↓
Adapter
 ↓
External System

设备执行路径

Decision
 ↓
Behavior
 ↓
Action
 ↓
DeviceAdapter
 ↓
Device

两条路径可以共存。


19. Feedback 到 Memory

外部执行产生:

Device
   ↓
DeviceResponse
   ↓
Adapter
   ↓
Feedback

Feedback 可以进入:

Feedback
   ├── Information
   ├── Memory
   ├── Experience
   ├── Detection
   └── Learning

因此形成闭环:

Decision
 ↓
Behavior
 ↓
Action
 ↓
External World
 ↓
Feedback
 ↓
Experience
 ↓
Learning
 ↓
Memory Update
 ↓
下一轮 Cognition

20. Engine 与 Manager 的依赖关系

一个典型关系:

EngineManager
      ↓
TemperatureEngine

Manager 不负责实现 TemperatureEngine 的具体计算。

例如:

EngineManager
      ↓
execute()
      ↓
TemperatureEngine
      ↓
temperature > limit
      ↓
Result

因此:

Manager → Engine

而不是:

Engine → Manager

Engine 可以依赖 Configuration、Object、Memory 等资源,但不应该反过来管理自己的 Manager。


21. Object 与 Manager

同样:

ObjectManager
      ↓
Object

例如:

ObjectManager
│
├── Robot_A
├── Sensor_A
└── Obstacle_A

Object 自己负责:

Property
Method
Relation
State

ObjectManager 负责:

Register
Get
Remove
Find

22. Renderer 与 Manager

RendererManager
      ↓
Renderer

例如:

RendererManager
│
├── WebRenderer
├── ApiRenderer
└── DeviceRenderer

调用:

Expression
    ↓
RendererManager
    ↓
ApiRenderer
    ↓
JSON Result

23. Adapter 与 Manager

AdapterManager
      ↓
Adapter

例如:

AdapterManager
│
├── RobotAdapter
├── VehicleAdapter
└── DeviceAdapter

执行:

Action
 ↓
AdapterManager
 ↓
RobotAdapter
 ↓
Robot

24. Extension 与 Manager

ExtensionRegistry
       ↓
ExtensionLoader
       ↓
ExtensionManager
       ↓
Extension

完整:

Registry
   ↓
检查注册信息
   ↓
Loader
   ↓
加载 Class
   ↓
ExtensionManager
   ↓
Extension
   ↓
Runtime

25. 完整类关系图

把本章主要 Class 放在一起:

                         Application
                              │
                              ↓
                           Kernel
                              │
                              ↓
                         Container
                              │
          ┌───────────────────┼───────────────────┐
          ↓                   ↓                   ↓
   IndividualManager     EngineManager      ExtensionManager
          │                   │                   │
          ↓                   ↓                   ↓
     Individual            Engine             Extension
          │
          ↓
        Central
          │
    ┌─────┼─────────────┐
    ↓     ↓             ↓
Perception Cognition   Memory
                       │
                       ↓
                  MemoryManager
                       │
                       ↓
                  Memory Objects
                       │
                       ↓
                    Reasoning
                       │
                       ↓
                    Decision
                       │
                       ↓
                    Behavior
                       │
                       ↓
                     Action
                       │
             ┌─────────┴─────────┐
             ↓                   ↓
        Expression          DeviceAdapter
             ↓                   ↓
         Template              Device
             ↓                   ↓
         Renderer          DeviceResponse
             ↓                   │
          Adapter                 │
             ↓                   │
      External System             │
             └─────────┬─────────┘
                       ↓
                    Feedback
                       ↓
                   Experience
                       ↓
                    Learning
                       ↓
                 Memory Update

26. Manager 层完整关系

Manager 层可以独立看成:

Manager
│
├── IndividualManager
├── EngineManager
├── ObjectManager
├── MemoryManager
├── DecisionManager
├── BehaviorManager
├── RendererManager
├── AdapterManager
└── ExtensionManager

它们不是一个巨大的 Manager。

而是:

一个 Manager 基类思想 + 多个职责明确的具体 Manager。


27. Interface 层

为了降低 Class 之间的直接耦合,可以使用 Interface。

例如:

IndividualInterface
EngineInterface
ObjectInterface
MemoryInterface
DecisionInterface
BehaviorInterface
RendererInterface
AdapterInterface
ExtensionInterface

关系:

Interface
    ↑
Concrete Class

例如:

EngineInterface
      ↑
TemperatureEngine
RendererInterface
      ↑
WebRenderer
AdapterInterface
      ↑
RobotAdapter
ExtensionInterface
      ↑
RobotExtension

这样 Manager 依赖 Interface,而不是依赖某个具体 Class。


28. 依赖倒置

例如不应该设计成:

class EngineManager
{
    protected $temperatureEngine;
}

因为这样 Manager 被绑定到一个具体 Engine。

更合理:

class EngineManager
{
    protected $engines = array();

    public function register(EngineInterface $engine)
    {
        $this->engines[$engine->getId()] = $engine;
    }
}

这样:

EngineManager
      ↓
EngineInterface
      ↑
      ├── TemperatureEngine
      ├── DistanceEngine
      ├── PositionEngine
      └── DetectionEngine

形成标准接口依赖。


29. 主要类之间的依赖关系

可以进一步归纳:

主要依赖 主要被谁使用
Application Kernel、Container 系统入口
Kernel Container、Manager Application
Container Runtime Objects Kernel
Individual Central、Memory Kernel/Central
Central Cognitive Components Individual
Engine Input、Configuration Module/Manager
Manager Interface Kernel/Container
Object Property、Method、Relation、State Perception/Cognition
Memory Memory Records Cognition/Reasoning/Learning
Decision Conclusion、Risk、Priority Central
Behavior Decision、Action Central
Template Expression/Data SATE
Renderer Template/Expression RendererManager
Adapter Action/Rendered Result AdapterManager
Device Property/Method/State Adapter
Extension Module/Engine/Object等 ExtensionManager

30. 谁创建谁

这是类关系中非常重要的一部分。

基本原则:

Application
    ↓ creates
Kernel

Kernel
    ↓ creates / obtains
Container

Container
    ↓ provides
Individual / Manager / Engine / Object

Individual
    ↓ owns / uses
Central

Central
    ↓ uses
Perception / Cognition / Memory / Reasoning / Decision / Behavior

Behavior
    ↓ creates / organizes
Action

Adapter
    ↓ connects
Device / External System

但是:

“依赖”不一定等于“创建”。

例如 Central 可以通过 Container 获取 Memory,而不是自己 new Memory()


31. Container 注入关系

更完整的方式是:

Application
      ↓
Kernel
      ↓
Container
      │
      ├── Individual
      ├── Central
      ├── MemoryManager
      ├── EngineManager
      ├── ObjectManager
      ├── RendererManager
      ├── AdapterManager
      └── ExtensionManager

然后:

Individual
     ↓
Central
     ↓
Container 获取需要的组件

这样可以降低:

Central → Concrete Class

之间的强耦合。


32. 完整 SAI 类依赖主链

最终最重要的一条主链:

Application
    ↓
Kernel
    ↓
Container
    ↓
Individual
    ↓
Central
    ↓
Perception
    ↓
Cognition
    ↓
Memory
    ↓
Reasoning
    ↓
Decision
    ↓
Behavior
    ↓
Action
    ↓
Adapter
    ↓
Device / External Environment
    ↓
Feedback
    ↓
Experience
    ↓
Learning
    ↓
Memory

这里形成真正的:

运行闭环。


33. 完整类关系总图

将管理关系、认知关系、执行关系和扩展关系同时放进去:

                              Application
                                   │
                                   ↓
                                Kernel
                                   │
                                   ↓
                               Container
                                   │
             ┌─────────────────────┼─────────────────────┐
             ↓                     ↓                     ↓
          Managers              Individual          ExtensionManager
             │                     │                     │
      ┌──────┼──────┐              ↓                     ↓
      ↓      ↓      ↓           Central              Extension
    Engine Object Memory            │
   Manager Manager Manager          │
                                    ↓
                     ┌──────────────┼──────────────┐
                     ↓              ↓              ↓
                 Perception      Cognition       Memory
                     │              │              │
                     ↓              ↓              ↓
                  Element        Understanding   Facts
                     ↓                             Relations
                  Object                           States
                     │                             Experience
                     ↓
                  Relation
                                    │
                                    ↓
                                Reasoning
                                    │
                                    ↓
                                 Decision
                                    │
                                    ↓
                                 Behavior
                                    │
                                    ↓
                                  Action
                                    │
                         ┌──────────┴──────────┐
                         ↓                     ↓
                    Expression            DeviceAdapter
                         ↓                     ↓
                     Template               Device
                         ↓                     ↓
                     Renderer           DeviceResponse
                         ↓                     │
                      Adapter                 │
                         ↓                     │
                 External System              │
                         └──────────┬──────────┘
                                    ↓
                                 Feedback
                                    ↓
                                Experience
                                    ↓
                                 Learning
                                    ↓
                              Memory Update

34. 第110章核心类关系原则

整个 SAI Framework 可以归纳成六条关系。

第一:Application 启动 Framework

Application → Kernel

第二:Container 提供运行对象

Container → Runtime Objects

第三:Individual 组织 SAI

Individual → Central → Cognitive Components

第四:Manager 管理组件

Manager → Component

第五:Interface 降低耦合

Manager
   ↓
Interface
   ↑
Concrete Class

第六:Extension 扩展 Framework

ExtensionManager
      ↓
Extension
      ↓
Module / Engine / Object / Adapter / Renderer

本章最终定义

Application

SAI 应用程序的启动与运行入口。

Container

保存并提供当前运行对象的依赖容器。

Individual

SAI 的独立运行主体。

Central

Individual 内部负责组件协调和运行流程组织的中心。

Engine

执行具体功能的执行类。

Manager

对同类组件进行注册、获取、生命周期和运行管理的类。

Object

对现实实体进行结构化表示的对象。

Memory

保存、读取、更新和组织认知历史资源的系统。

Decision

根据结论、候选、风险、优先级和条件选择行动方案的组件。

Behavior

对 Decision 选定方案进行执行过程组织的组件。

Template

定义结构化数据如何组织成外部表现结构的模板。

Renderer

把结构化表达转换成具体表现形式的组件。

Adapter

把 SAI 内部结果或 Action 连接到外部环境的组件。

Device

SAI 对外部设备进行结构化表示的对象。

Extension

在不直接改变 Core 基础结构的情况下扩展 Framework 功能的组件载体。


第110章最终类关系模型

                         Application
                              │
                              ↓
                           Kernel
                              │
                              ↓
                          Container
                              │
              ┌───────────────┼───────────────┐
              ↓               ↓               ↓
           Manager        Individual      Extension
              │               │               │
              ↓               ↓               ↓
          Components        Central        Extension
                              │
              ┌───────────────┼───────────────┐
              ↓               ↓               ↓
         Perception       Cognition         Memory
              │               │               │
              └───────────────┼───────────────┘
                              ↓
                          Reasoning
                              ↓
                           Decision
                              ↓
                           Behavior
                              ↓
                            Action
                              ↓
                           Adapter
                              ↓
                       Device / World
                              ↓
                          Feedback
                              ↓
                         Experience
                              ↓
                          Learning
                              ↓
                       Memory Update

而底层依赖原则是:

Application
    ↓
Kernel
    ↓
Container
    ↓
Interface
    ↓
Manager
    ↓
Concrete Component

核心思想最终可以浓缩成:

Application 启动,Kernel 组织,Container 提供,Manager 管理,Individual 运行,Central 协调,Engine 执行,Object 表示,Memory 保存,Decision 选择,Behavior 组织,Template 定义表现结构,Renderer 转换表现,Adapter 连接外部环境,Device 执行设备能力,Extension 扩展系统。

这就把第109章的目录架构进一步落实成了第110章的类架构与依赖架构

Leave a Reply

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