第3章 SAI Framework 核心设计思想
本章大纲
- OOP
- Object
- Property
- Method
- Element
- Relation
- Rule
- Fact
- State
- Experience
- Memory
- Ability
- Engine
- Manager
- Central
- Individual
- 模块化思想
- 开放扩展思想
- 本章完整对象示例
3.1 OOP
SAI Framework 的工程基础之一是 OOP(Object-Oriented Programming,面向对象编程)。
SAI 需要模拟的不是单纯的数据,而是:
对象
属性
方法
状态
关系
行为
这些概念天然适合使用对象进行组织。
例如一个机器人:
Robot
├── Property
│ ├── position
│ ├── speed
│ ├── battery
│ └── state
│
└── Method
├── move()
├── stop()
├── charge()
└── inspect()
PHP 中可以表示为:
class Robot
{
protected $name;
protected $state;
protected $battery;
public function move()
{
// 移动
}
public function stop()
{
// 停止
}
}
这里最重要的不是 PHP 语法,而是:
SAI 用对象组织 Individual 所认识和操作的世界。
3.2 Object
Object 是 SAI Framework 中非常重要的基础概念。
可以简单定义:
Object 是被 SAI 识别、描述、关联、操作或管理的具体对象。
例如:
人
汽车
机器人
房间
门
风扇
温度传感器
道路
城市
任务
都可以成为 Object。
一个 Object 可以拥有:
Object
├── Identity
├── Property
├── State
├── Relation
└── Method
例如:
Fan
├── id = fan_001
├── state = OFF
├── speed = 0
├── location = room_01
└── turnOn()
因此:
Object
↓
Property
↓
State
↓
Relation
↓
Method
共同构成一个可描述、可操作的对象。
3.3 Property
Property 是 Object 的属性。
例如:
Person
├── name
├── age
├── position
└── state
其中:
name
age
position
state
都是 Property。
例如:
$person->name = 'John';
$person->age = 30;
可以抽象成:
Object
Person
Property
name = John
age = 30
Property 本身描述的是:
对象具有什么特征。
例如:
Car
├── color = black
├── speed = 60
├── fuel = 70
└── state = running
SAI 进一步可以根据 Property 形成:
条件
状态
关系
规则
推理依据
所以 Property 并不是简单的数据字段。
它是 Individual 描述 Object 的基础。
3.4 Method
Method 描述对象能够执行什么操作。
例如:
Fan
├── state
├── speed
├── turnOn()
├── turnOff()
└── setSpeed()
其中:
state
speed
属于 Property。
而:
turnOn()
turnOff()
setSpeed()
属于 Method。
因此可以形成:
Object
├── Property
└── Method
例如:
class Fan
{
protected $state = 'OFF';
public function turnOn()
{
$this->state = 'ON';
}
public function turnOff()
{
$this->state = 'OFF';
}
}
SAI 的一个重要思想是:
对象不仅需要被描述,还需要具备可执行的方法。
这样 Individual 才能够通过对象的方法影响外部世界。
3.5 Element
Element 是 SAI 中比 Object 更基础的一层。
一个完整的信息进入 SAI 后,并不一定马上形成完整 Object。
例如:
“深圳市”
最初可能只是一个信息元素。
经过识别后:
Element
{
value: 深圳市
type: location
}
随后可以形成:
Object
{
name: 深圳市
type: city
}
所以可以表示为:
Information
↓
Element
↓
Object
Element 可以包括:
名称元素
数值元素
时间元素
位置元素
状态元素
事件元素
对象元素
属性元素
关系元素
例如:
temperature
32
room
01
都是可以被进一步组织的元素。
因此:
Element 是信息进入认知结构后形成的基础组成单元。
3.6 Relation
单独的 Object 很难构成完整的世界。
例如:
Person
Car
Road
如果只有三个对象,Individual 还不知道它们之间有什么关系。
因此需要 Relation。
例如:
Person
↓
驾驶
↓
Car
或者:
Car
↓
行驶于
↓
Road
或者:
Room
↓
包含
↓
Fan
可以统一表示为:
Subject
Relation
Object
例如:
Person
└── drives ──> Car
Car
└── located_on ──> Road
Room
└── contains ──> Fan
Relation 是 Individual 理解世界的重要基础。
因此:
Object
+
Relation
=
Object Relationship Structure
3.7 Rule
Rule 是规则。
规则描述:
在什么条件下应该产生什么结果。
最简单的形式:
IF Condition
THEN Action
例如:
IF temperature > 30
THEN fan = ON
也可以更加复杂:
IF
temperature > 30
AND
person_present = true
AND
fan = OFF
THEN
turn_on_fan
规则可以处理:
条件
判断
状态
关系
优先级
行为
风险
决策
因此 Rule 是 Reasoning 的重要基础。
基本结构:
Rule
├── Condition
├── Priority
└── Action
3.8 Fact
Fact 是事实。
它表示 Individual 当前认为已经成立的信息。
例如:
temperature = 32
可以形成:
Fact
{
object: room_01,
property: temperature,
value: 32
}
再例如:
Fan.state = OFF
也是一个 Fact。
多个 Fact 可以形成事实集合:
Facts
├── Room.temperature = 32
├── Fan.state = OFF
├── Person.state = PRESENT
└── Time = 14:30
然后 Rule 可以使用这些事实进行判断:
Facts
↓
Rule
↓
Reasoning
因此:
Fact 是推理所依据的已知事实,Rule 是处理事实关系和条件的规则。
3.9 State
State 表示对象或 Individual 在某个时间点的状态。
例如机器人:
Robot
├── position = A
├── speed = 10
├── direction = north
├── battery = 60%
└── state = moving
这里:
moving
就是当前状态。
状态还可以发生变化:
moving
↓
stopping
↓
stopped
或者:
battery = 60%
↓
battery = 20%
↓
battery = 5%
因此 State 是动态的。
可以表示:
State(t1)
↓
Action
↓
State(t2)
这对于模拟人工个体非常重要。
因为 Individual 不是静态对象,而是:
处于持续变化状态中的对象。
3.10 Experience
Experience 是 Individual 在运行过程中形成的经验。
例如:
Scene
↓
Decision
↓
Action
↓
Result
假设:
场景:
温度 > 30
行为:
打开风扇
结果:
温度下降
可以形成:
Experience
{
condition: temperature > 30,
action: turn_on_fan,
result: temperature_decreased
}
Experience 与 Fact 不完全相同。
Fact
表示:
当前或已知事实
例如:
temperature = 32
Experience
表示:
过去行为及其结果形成的经验
例如:
temperature > 30
→ turn_on_fan
→ temperature decreases
因此:
Fact
↓
Reasoning
↓
Decision
↓
Behavior
↓
Result
↓
Experience
Experience 可以反过来影响以后:
Reasoning
Decision
Priority
Risk
3.11 Memory
Memory 是 Individual 保存信息、事实、状态和经验的机制。
可以设计为:
Memory
├── ShortMemory
├── LongMemory
├── FactMemory
├── ExperienceMemory
└── StateMemory
例如:
ShortMemory
保存当前运行过程中暂时需要的信息。
LongMemory
保存长期需要的信息。
FactMemory
保存事实。
ExperienceMemory
保存经验。
StateMemory
保存状态历史。
因此:
Information
↓
Perception
↓
Memory
以及:
Experience
↓
Memory
最终形成 Individual 的持续记忆。
3.12 Ability
Ability 是 Individual 可以应用的能力。
需要注意:
Knowledge 是能力资源,Ability 是对资源的应用能力。
例如一个机器人可能拥有:
Ability
├── Move
├── Detect
├── Measure
├── Navigate
└── Control
一个 Web Individual 可以拥有:
Ability
├── ReceiveInformation
├── Analyze
├── SearchObject
├── Reason
└── Respond
Ability 可以与 Method、Rule、Memory 等共同工作。
例如:
Ability
Navigate
内部可能使用:
Object
Relation
Map
Rule
Memory
Reasoning
Decision
因此:
Ability
↓
应用已有知识、对象、方法和规则
↓
完成任务
3.13 Engine
Engine 是 SAI Framework 中用于执行某类核心处理逻辑的组件。
例如:
PerceptionEngine
CognitionEngine
MemoryEngine
ReasoningEngine
DecisionEngine
BehaviorEngine
LearningEngine
Engine 的特点是:
Engine 负责某一类机制的运行。
例如:
ReasoningEngine
负责:
Fact
+
Rule
+
Relation
+
Experience
↓
Reasoning
↓
Conclusion
而:
DecisionEngine
负责:
Candidate
+
State
+
Risk
+
Priority
↓
Decision
Engine 不应该变成一个“万能类”。
正确结构应该是:
CognitionEngine
负责认知
ReasoningEngine
负责推理
DecisionEngine
负责决策
BehaviorEngine
负责行为
这样可以保持职责清晰。
3.14 Manager
Manager 主要负责管理某一类对象或资源。
例如:
ObjectManager
MemoryManager
RelationManager
TemplateManager
DeviceManager
ExtensionManager
例如:
ObjectManager
可以负责:
create()
get()
update()
remove()
find()
而不是直接承担复杂的认知和推理。
因此:
Manager
=
管理
而:
Engine
=
运行机制
两者职责不同。
可以简单理解为:
Manager
↓
管理对象和资源
Engine
↓
执行核心处理机制
3.15 Central
Central 是 Individual 内部的协调中心。
它不是 Cognition。
也不是 Reasoning。
也不是 Decision。
它负责的是:
接收
调度
协调
状态管理
生命周期
结果组织
异常协调
例如:
Central
↓
InformationReceiver
↓
PerceptionEngine
↓
CognitionEngine
↓
ReasoningEngine
↓
DecisionEngine
↓
BehaviorEngine
因此 Central 更接近:
Coordinator
Dispatcher
Lifecycle Controller
而不是:
Intelligence Engine
这是 SAI Framework 中一个非常重要的设计边界。
3.16 Individual
Individual 是 SAI Framework 最终要构建的目标对象。
可以抽象为:
Individual
├── Identity
├── State
├── Ability
├── Memory
├── Cognition
├── Learning
└── Behavior
Individual 不是一个普通 Object 的简单集合。
它具有自己的:
身份
状态
能力
记忆
认知过程
行为
学习过程
因此:
SAI Framework
↓
Individual
Framework 提供基础结构。
Individual 则成为一个具体的模拟人工个体。
例如:
Individual
ID = robot_001
Type = service_robot
它可以拥有:
Ability:
Move
Detect
Navigate
Memory:
FactMemory
ExperienceMemory
Behavior:
Move
Stop
Return
3.17 模块化思想
SAI Framework 不应该把所有功能写进一个巨大类中。
例如不应该设计成:
class SAI
{
// 信息
// 感知
// 认知
// 记忆
// 推理
// 决策
// 行为
// 学习
// 设备
// 模板
// Web
}
这种结构很快就会变得难以维护。
SAI 更适合模块化:
SAI
├── Information
├── Scene
├── Perception
├── Cognition
├── Memory
├── Reasoning
├── Decision
├── Behavior
├── Expression
├── Template
├── Renderer
├── Adapter
├── Device
├── Feedback
├── Experience
└── Learning
每个模块有明确职责。
例如:
Perception
不负责:
Decision
Learning
Device Control
而:
Device
也不负责:
Cognition
Reasoning
这样可以使系统形成清晰的边界。
3.18 开放扩展思想
SAI Framework 还需要具备开放扩展能力。
核心 Framework 提供基础能力:
Core
↓
Module
↓
Extension
开发者可以增加:
CustomObject
CustomEngine
CustomAdapter
CustomRenderer
CustomTemplate
CustomDevice
例如 Framework 原本只有:
DeviceAdapter
开发者可以增加:
RobotAdapter
再增加:
DroneAdapter
甚至:
IndustrialMachineAdapter
核心系统不需要因此改变整体结构。
可以形成:
SAI Core
│
├── Standard Modules
│
└── Extensions
├── Robot
├── Drone
├── Vehicle
└── Industrial
开放扩展的目标不是无限增加功能,而是:
保持核心稳定,同时允许不同 Individual 根据实际需要增加自己的能力。
3.19 本章完整对象示例
下面把本章的核心概念放到一个完整案例中。
假设构建一个:
室内环境模拟人工个体 Indoor-001
它的任务是观察房间温度,根据当前状态决定是否启动风扇。
3.19.1 Individual
Individual
ID = Indoor-001
Type = IndoorEnvironmentIndividual
3.19.2 Object
环境中存在:
Room
Fan
TemperatureSensor
Person
对象关系:
Room
├── contains → Fan
├── contains → TemperatureSensor
└── contains → Person
3.19.3 Property
Room:
temperature = 32
humidity = 60
Fan:
state = OFF
speed = 0
Person:
state = PRESENT
3.19.4 Method
Fan:
turnOn()
turnOff()
setSpeed()
TemperatureSensor:
readTemperature()
3.19.5 Element
传感器产生:
temperature
32
room_01
这些首先作为 Element 进入 SAI。
3.19.6 Relation
建立关系:
Room
└── contains ──> Fan
Room
└── contains ──> TemperatureSensor
TemperatureSensor
└── measures ──> Room.temperature
3.19.7 Fact
系统当前获得:
Fact 1:
Room.temperature = 32
Fact 2:
Fan.state = OFF
Fact 3:
Person.state = PRESENT
3.19.8 State
Individual 当前状态:
Individual.state = observing
Room:
temperature = 32
Fan:
state = OFF
3.19.9 Rule
定义:
Rule R001
IF
Room.temperature > 30
AND
Person.state = PRESENT
AND
Fan.state = OFF
THEN
Fan.turnOn()
3.19.10 Memory
保存:
FactMemory
以及过去经验:
ExperienceMemory
例如:
temperature > 30
→ turnOnFan
→ temperature decreased
3.19.11 Ability
Indoor-001:
Ability
├── TemperatureDetection
├── EnvironmentAnalysis
├── CoolingDecision
└── FanControl
3.19.12 Engine
运行:
PerceptionEngine
↓
CognitionEngine
↓
ReasoningEngine
↓
DecisionEngine
↓
BehaviorEngine
3.19.13 Central
Central 负责把这些模块组织起来:
Central
↓
接收传感器信息
↓
调度感知
↓
调度认知
↓
调度推理
↓
调度决策
↓
调度行为
3.19.14 最终运行
完整过程:
TemperatureSensor
↓
Information
↓
Perception
↓
Element
↓
Object
↓
Property
↓
Relation
↓
Fact
↓
Cognition
↓
Memory
↓
Rule
↓
Reasoning
↓
Decision
↓
Behavior
↓
Action
↓
Fan.turnOn()
↓
Fan.state = ON
↓
Environment changes
↓
Feedback
↓
Experience
↓
Memory Update
本章核心结构
本章所有核心概念可以组织成下面的关系:
Individual
│
Central
│
┌────────────────┼────────────────┐
│ │ │
Object Memory Ability
│ │
┌──────┼──────┐ │
│ │ │ │
Property Method Relation Experience
│ │
└──────┬──────┘
│
Element
│
Fact
│
Rule
│
Engine
│
┌──────┼────────┐
│ │ │
Cognition Reasoning Decision
│
Behavior
│
Action
从工程角度看,则可以进一步归纳为:
OOP
│
├── Object
│ ├── Property
│ └── Method
│
├── Element
├── Relation
├── Fact
├── Rule
├── State
├── Experience
├── Memory
└── Ability
│
↓
Engine
│
↓
Manager
│
↓
Central
│
↓
Individual
本章小结
SAI Framework 的核心设计思想,可以归纳为六个层次。
第一层:对象基础
Object
Property
Method
解决:
世界中的对象是什么,以及对象具有什么属性、能够做什么。
第二层:认知基础
Element
Relation
Fact
解决:
信息如何形成对象、属性、关系和事实。
第三层:运行基础
State
Memory
Experience
Ability
解决:
Individual 当前是什么状态、记得什么、经历过什么以及能够做什么。
第四层:智能机制
Rule
Engine
解决:
如何利用事实、规则、关系、状态和经验进行处理。
第五层:系统组织
Manager
Central
解决:
如何管理模块、调度模块并维持 Individual 的整体运行。
第六层:最终目标
Individual
最终将:
Object
Element
Relation
Rule
Fact
State
Memory
Experience
Ability
Engine
Central
组织成为一个完整的:
模拟人工个体。
因此,SAI Framework 的核心并不是某一个 Engine,也不是某一种算法,而是一套从 对象建模 → 信息组织 → 状态管理 → 记忆经验 → 规则推理 → 决策行为 → Individual 运行 的统一工程体系。