第100章 开发一个简单 SAI
第100章开始从前面的理论进入最小可运行 SAI Individual。
这里先明确边界:
本章的目标是建立一个最小的 SAI,不是完整 WSaiOS,也不是完整 ICAI,更不是生产系统。
这个简单 SAI 只实现前面已经定义的核心链路:
Individual
↓
Information
↓
Perception
↓
Cognition
↓
Memory
↓
Reasoning
↓
Decision
↓
Behavior
为了让结构清楚,本章使用 PHP OOP + 数组 + 规则判断 实现,不使用任何大模型、神经网络、Embedding、Vector Search 或生成式机制。
1. 创建项目
1.1 项目结构
首先建立一个最简单的项目:
simple-sai/
│
├── index.php
│
├── src/
│ ├── Individual.php
│ ├── Information.php
│ ├── Perception.php
│ ├── Cognition.php
│ ├── Memory.php
│ ├── Reasoning.php
│ ├── Decision.php
│ └── Behavior.php
│
└── data/
└── memory.json
这里暂时不加入:
Kernel
Container
Extension
EngineManager
AdapterManager
Renderer
Task System
Exception System
原因很简单:
第100章先把最核心的 Individual 运行起来。
后面的框架系统可以逐步加入。
1.2 项目运行关系
整个项目:
index.php
↓
Individual
↓
Information
↓
Perception
↓
Cognition
↓
Memory
↓
Reasoning
↓
Decision
↓
Behavior
2. 创建 Individual
2.1 Individual 定义
Individual 是这个简单 SAI 的运行主体。
class Individual
{
protected $id;
protected $name;
protected $perception;
protected $cognition;
protected $memory;
protected $reasoning;
protected $decision;
protected $behavior;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
public function setPerception($perception)
{
$this->perception = $perception;
}
public function setCognition($cognition)
{
$this->cognition = $cognition;
}
public function setMemory($memory)
{
$this->memory = $memory;
}
public function setReasoning($reasoning)
{
$this->reasoning = $reasoning;
}
public function setDecision($decision)
{
$this->decision = $decision;
}
public function setBehavior($behavior)
{
$this->behavior = $behavior;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
}
创建:
$individual = new Individual(
'Individual_A',
'Simple SAI'
);
得到:
Individual_A
│
├── Perception
├── Cognition
├── Memory
├── Reasoning
├── Decision
└── Behavior
3. 创建 Information
Information 是 Individual 接收到的信息。
本例使用一个非常简单的场景:
一台机器当前温度为 92℃,正常上限为 80℃。
Information:
class Information
{
protected $data;
public function __construct($data)
{
$this->data = $data;
}
public function getData()
{
return $this->data;
}
}
创建:
$information = new Information(array(
'object' => 'Machine_A',
'temperature' => 92,
'limit' => 80
));
结构:
Information
├── object = Machine_A
├── temperature = 92
└── limit = 80
这里的 Information 只是:
Individual 当前收到的数据。
它还不是认知结论。
4. 创建 Perception
4.1 Perception 定义
Perception 负责从 Information 中获取结构化元素。
class Perception
{
public function perceive(Information $information)
{
$data = $information->getData();
return array(
'object' => $data['object'],
'temperature' => $data['temperature'],
'limit' => $data['limit']
);
}
}
执行:
$perceptionResult = $perception->perceive($information);
结果:
{
object: Machine_A,
temperature: 92,
limit: 80
}
此时 SAI 已经完成:
Information
↓
Perception
↓
Elements
5. 创建 Cognition
5.1 Cognition 定义
Cognition 不只是获取数据,而是理解当前结构代表什么。
例如:
temperature = 92
limit = 80
Perception 只是获得这两个值。
Cognition 判断:
92 > 80
因此:
Machine_A
temperature state
=
HIGH
5.2 Cognition 类
class Cognition
{
public function understand($perception)
{
$temperature = $perception['temperature'];
$limit = $perception['limit'];
if ($temperature > $limit) {
$state = 'HIGH';
} else {
$state = 'NORMAL';
}
return array(
'object' => $perception['object'],
'temperature' => $temperature,
'limit' => $limit,
'state' => $state
);
}
}
执行:
$cognitionResult = $cognition->understand(
$perceptionResult
);
结果:
Machine_A
temperature = 92
limit = 80
state = HIGH
于是:
Information
↓
Perception
↓
Cognition
↓
Machine_A.temperature = HIGH
6. 创建 Memory
Memory 用来保存 Individual 已经获得并确认的结构化信息。
class Memory
{
protected $items = array();
public function store($item)
{
$this->items[] = $item;
}
public function all()
{
return $this->items;
}
}
保存 Cognition Result:
$memory->store($cognitionResult);
Memory:
Memory
└── Machine_A
├── temperature = 92
├── limit = 80
└── state = HIGH
6.1 Memory 的作用
Memory 不负责决定:
“应该停止机器”
它只保存:
Machine_A
temperature = 92
state = HIGH
后面的 Reasoning 使用这些信息。
因此:
Memory
↓
Reasoning
7. 创建 Reasoning
7.1 Reasoning 定义
Reasoning 根据:
Fact
+
Rule
+
Current State
产生 Conclusion。
本例规则:
IF temperature > limit
THEN machine requires cooling
结构:
Rule_A
IF
temperature > limit
THEN
action = COOL
7.2 Reasoning 类
class Reasoning
{
public function reason($memory)
{
$items = $memory->all();
if (empty($items)) {
return array(
'state' => 'UNKNOWN',
'conclusion' => null
);
}
$last = $items[count($items) - 1];
if ($last['temperature'] > $last['limit']) {
return array(
'state' => 'CONFIRMED',
'conclusion' => 'COOL_REQUIRED',
'reason' => 'temperature > limit'
);
}
return array(
'state' => 'CONFIRMED',
'conclusion' => 'COOL_NOT_REQUIRED',
'reason' => 'temperature <= limit'
);
}
}
执行:
$reasoningResult = $reasoning->reason($memory);
结果:
{
state: CONFIRMED,
conclusion: COOL_REQUIRED,
reason: temperature > limit
}
7.3 推理链
本例推理链:
Machine_A.temperature = 92
↓
Machine_A.limit = 80
↓
92 > 80
↓
Rule_A
↓
COOL_REQUIRED
这就是最简单的:
Fact → Condition → Rule → Conclusion
8. 创建 Decision
Reasoning 得到:
COOL_REQUIRED
但 Reasoning 还没有真正决定执行什么。
Decision 需要从候选方案中选择:
CONTINUE
COOL
STOP
8.1 Decision 类
class Decision
{
public function decide($reasoningResult)
{
if ($reasoningResult['state'] !== 'CONFIRMED') {
return array(
'state' => 'UNKNOWN',
'selected' => null
);
}
if ($reasoningResult['conclusion'] === 'COOL_REQUIRED') {
return array(
'state' => 'DECIDED',
'selected' => 'COOL',
'reason' => $reasoningResult['reason']
);
}
return array(
'state' => 'DECIDED',
'selected' => 'CONTINUE',
'reason' => $reasoningResult['reason']
);
}
}
执行:
$decisionResult = $decision->decide(
$reasoningResult
);
结果:
Decision
state = DECIDED
selected = COOL
注意:
Reasoning
=
COOL_REQUIRED
Decision
=
COOL
两者不同。
9. 创建 Behavior
Decision:
COOL
Behavior 负责组织实际行为过程。
class Behavior
{
public function execute($decision)
{
if ($decision['state'] !== 'DECIDED') {
return array(
'state' => 'FAILED',
'result' => null
);
}
if ($decision['selected'] === 'COOL') {
return array(
'state' => 'SUCCESS',
'behavior' => 'COOL',
'result' => 'Cooling started'
);
}
return array(
'state' => 'SUCCESS',
'behavior' => 'CONTINUE',
'result' => 'Machine continues'
);
}
}
执行:
$behaviorResult = $behavior->execute(
$decisionResult
);
得到:
Behavior
state = SUCCESS
behavior = COOL
result = Cooling started
10. 运行完整实例
现在把所有对象连接起来。
10.1 加载类
require_once 'src/Individual.php';
require_once 'src/Information.php';
require_once 'src/Perception.php';
require_once 'src/Cognition.php';
require_once 'src/Memory.php';
require_once 'src/Reasoning.php';
require_once 'src/Decision.php';
require_once 'src/Behavior.php';
10.2 创建 Individual
$individual = new Individual(
'Individual_A',
'Simple SAI'
);
10.3 创建各个组件
$perception = new Perception();
$cognition = new Cognition();
$memory = new Memory();
$reasoning = new Reasoning();
$decision = new Decision();
$behavior = new Behavior();
连接:
$individual->setPerception($perception);
$individual->setCognition($cognition);
$individual->setMemory($memory);
$individual->setReasoning($reasoning);
$individual->setDecision($decision);
$individual->setBehavior($behavior);
10.4 输入 Information
$information = new Information(array(
'object' => 'Machine_A',
'temperature' => 92,
'limit' => 80
));
10.5 Perception
$perceptionResult = $perception->perceive(
$information
);
结果:
Machine_A
temperature = 92
limit = 80
10.6 Cognition
$cognitionResult = $cognition->understand(
$perceptionResult
);
结果:
Machine_A
state = HIGH
10.7 Memory
$memory->store(
$cognitionResult
);
10.8 Reasoning
$reasoningResult = $reasoning->reason(
$memory
);
结果:
COOL_REQUIRED
10.9 Decision
$decisionResult = $decision->decide(
$reasoningResult
);
结果:
DECISION = COOL
10.10 Behavior
$behaviorResult = $behavior->execute(
$decisionResult
);
结果:
BEHAVIOR = COOL
STATE = SUCCESS
11. 完整运行链
现在这个最简单 SAI 已经完成了一次完整运行:
Individual_A
↓
Information
↓
Machine_A
temperature = 92
limit = 80
↓
Perception
↓
Elements
↓
Cognition
↓
temperature = HIGH
↓
Memory
↓
Reasoning
↓
COOL_REQUIRED
↓
Decision
↓
COOL
↓
Behavior
↓
Cooling started
12. 完整代码
将前面的逻辑暂时集中到 index.php,可以得到一个最小可运行版本:
<?php
class Information
{
protected $data;
public function __construct($data)
{
$this->data = $data;
}
public function getData()
{
return $this->data;
}
}
class Perception
{
public function perceive(Information $information)
{
$data = $information->getData();
return array(
'object' => $data['object'],
'temperature' => $data['temperature'],
'limit' => $data['limit']
);
}
}
class Cognition
{
public function understand($perception)
{
$state = 'NORMAL';
if ($perception['temperature'] >
$perception['limit']) {
$state = 'HIGH';
}
return array(
'object' => $perception['object'],
'temperature' => $perception['temperature'],
'limit' => $perception['limit'],
'state' => $state
);
}
}
class Memory
{
protected $items = array();
public function store($item)
{
$this->items[] = $item;
}
public function all()
{
return $this->items;
}
}
class Reasoning
{
public function reason($memory)
{
$items = $memory->all();
if (empty($items)) {
return array(
'state' => 'UNKNOWN',
'conclusion' => null
);
}
$last = $items[count($items) - 1];
if ($last['temperature'] >
$last['limit']) {
return array(
'state' => 'CONFIRMED',
'conclusion' => 'COOL_REQUIRED',
'reason' => 'temperature > limit'
);
}
return array(
'state' => 'CONFIRMED',
'conclusion' => 'COOL_NOT_REQUIRED',
'reason' => 'temperature <= limit'
);
}
}
class Decision
{
public function decide($reasoning)
{
if ($reasoning['state'] !== 'CONFIRMED') {
return array(
'state' => 'UNKNOWN',
'selected' => null
);
}
if ($reasoning['conclusion'] ===
'COOL_REQUIRED') {
return array(
'state' => 'DECIDED',
'selected' => 'COOL',
'reason' => $reasoning['reason']
);
}
return array(
'state' => 'DECIDED',
'selected' => 'CONTINUE',
'reason' => $reasoning['reason']
);
}
}
class Behavior
{
public function execute($decision)
{
if ($decision['state'] !== 'DECIDED') {
return array(
'state' => 'FAILED',
'result' => null
);
}
if ($decision['selected'] === 'COOL') {
return array(
'state' => 'SUCCESS',
'behavior' => 'COOL',
'result' => 'Cooling started'
);
}
return array(
'state' => 'SUCCESS',
'behavior' => 'CONTINUE',
'result' => 'Machine continues'
);
}
}
class Individual
{
protected $id;
protected $name;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
}
/*
* 1. 创建 Individual
*/
$individual = new Individual(
'Individual_A',
'Simple SAI'
);
/*
* 2. 创建组件
*/
$perception = new Perception();
$cognition = new Cognition();
$memory = new Memory();
$reasoning = new Reasoning();
$decision = new Decision();
$behavior = new Behavior();
/*
* 3. 创建 Information
*/
$information = new Information(array(
'object' => 'Machine_A',
'temperature' => 92,
'limit' => 80
));
/*
* 4. Perception
*/
$perceptionResult =
$perception->perceive($information);
/*
* 5. Cognition
*/
$cognitionResult =
$cognition->understand(
$perceptionResult
);
/*
* 6. Memory
*/
$memory->store(
$cognitionResult
);
/*
* 7. Reasoning
*/
$reasoningResult =
$reasoning->reason($memory);
/*
* 8. Decision
*/
$decisionResult =
$decision->decide(
$reasoningResult
);
/*
* 9. Behavior
*/
$behaviorResult =
$behavior->execute(
$decisionResult
);
/*
* 10. Output
*/
$result = array(
'individual' => array(
'id' => $individual->getId(),
'name' => $individual->getName()
),
'information' => $information->getData(),
'perception' => $perceptionResult,
'cognition' => $cognitionResult,
'memory' => $memory->all(),
'reasoning' => $reasoningResult,
'decision' => $decisionResult,
'behavior' => $behaviorResult
);
header('Content-Type: application/json; charset=utf-8');
echo json_encode(
$result,
JSON_PRETTY_PRINT
);
13. 实际运行结果
输入:
Machine_A
temperature = 92
limit = 80
系统运行:
Information
↓
Perception
↓
Cognition
↓
Memory
↓
Reasoning
↓
Decision
↓
Behavior
最终结构化结果应该类似:
{
"individual": {
"id": "Individual_A",
"name": "Simple SAI"
},
"information": {
"object": "Machine_A",
"temperature": 92,
"limit": 80
},
"perception": {
"object": "Machine_A",
"temperature": 92,
"limit": 80
},
"cognition": {
"object": "Machine_A",
"temperature": 92,
"limit": 80,
"state": "HIGH"
},
"reasoning": {
"state": "CONFIRMED",
"conclusion": "COOL_REQUIRED",
"reason": "temperature > limit"
},
"decision": {
"state": "DECIDED",
"selected": "COOL"
},
"behavior": {
"state": "SUCCESS",
"behavior": "COOL",
"result": "Cooling started"
}
}
14. 这个简单 SAI 已经具备什么
到这里,它已经不是单纯的:
输入 → if → 输出
而是形成了明确的 SAI 结构:
Individual
↓
Information
↓
Perception
↓
Cognition
↓
Memory
↓
Reasoning
↓
Decision
↓
Behavior
每一层具有不同职责。
| 层 | 主要作用 |
|---|---|
| Individual | 运行主体 |
| Information | 输入信息 |
| Perception | 获取并结构化信息 |
| Cognition | 理解信息 |
| Memory | 保存认知结果 |
| Reasoning | 根据事实和规则形成结论 |
| Decision | 选择执行方案 |
| Behavior | 组织执行过程 |
本章核心模型
第100章最重要的是把前面99章中的理论第一次压缩成一个最小 SAI 运行单元:
Individual
│
▼
Information
│
▼
Perception
│
▼
Cognition
│
▼
Memory
│
▼
Reasoning
│
▼
Decision
│
▼
Behavior
实际运行:
Machine_A
temperature = 92
│
▼
Perception
│
▼
Cognition
temperature = HIGH
│
▼
Memory
│
▼
Reasoning
COOL_REQUIRED
│
▼
Decision
COOL
│
▼
Behavior
Cooling started
本章核心定义
简单 SAI = 一个能够接收 Information,通过 Perception 获取结构,通过 Cognition 形成理解,通过 Memory 保存信息,通过 Reasoning 产生 Conclusion,通过 Decision 选择方案,并通过 Behavior 组织执行的最小 Individual 运行系统。
这一章真正建立的是 SAI 的最小核心骨架。
下一步如果继续扩展,应该是在这个骨架上逐层加入 Action、Expression、Adapter、Feedback、Experience、Learning、Task、Log、Exception,而不是一次性把所有系统混在一起。