第8章 Application
📅 2026年09月12日
👤 东塬一老翁
📂 第三篇 SAI Framework Core
第8章 Application
本章大纲
Application 定义
Framework Application
Application 生命周期
Application 初始化
Application 启动
Application 运行
Application 结束
Application 与 Individual
Application 与 Central
Application 类设计
完整 PHP 示例
实际运行结果
8.1 Application 定义
在 SAI Framework 中,Application 是整个框架运行过程的应用管理对象。
它负责把已经定义好的:
Configuration
↓
Core
↓
Individual
↓
Central
↓
Modules
组织起来,并启动一次完整的 SAI 运行过程。
因此 Application 不是:
Cognition
Reasoning
Decision
也不是 Individual 本身。
它更接近:
Application 是 SAI Framework 的运行载体和生命周期管理对象。
可以表示为:
Application
│
├── 初始化 Framework
│
├── 创建 / 加载 Individual
│
├── 启动 Central
│
├── 运行生命周期
│
└── 结束运行
8.2 Framework Application
普通 PHP 程序可能只是:
index.php
↓
执行代码
↓
输出结果
而 Framework Application 会把整个程序组织起来:
Application
↓
Bootstrap
↓
Configuration
↓
Container
↓
Individual
↓
Central
↓
SAI Lifecycle
因此 Application 的意义不是简单地:
$app = new Application();
而是建立:
一次完整的 Framework Runtime。
8.3 Application 生命周期
Application 本身也存在生命周期。
可以设计为:
Created
↓
Initialized
↓
Started
↓
Running
↓
Stopping
↓
Stopped
完整结构:
Application
│
▼
Created
│
▼
Initialize
│
▼
Initialized
│
▼
Start
│
▼
Started
│
▼
Run
│
▼
Running
│
▼
Stop
│
▼
Stopped
这与 Individual 的生命周期并不是完全相同的。
例如:
Application
↓
管理 Framework Runtime
而:
Individual
↓
执行模拟人工个体生命周期
两者需要区分。
8.4 Application 初始化
Application 初始化阶段主要完成运行前准备。
例如:
Application
↓
读取 Configuration
↓
创建 Container
↓
注册基础服务
↓
加载 Individual
↓
准备 Central
可以理解为:
Initialize
├── Configuration
├── Container
├── Core Services
├── Individual
└── Central
例如配置:
$config = array(
'name' => 'SAI Framework',
'version' => '0.1.0',
'debug' => true
);
Application 可以读取这些配置。
8.5 Application 启动
初始化完成后,Application 进入启动阶段。
例如:
Application
↓
initialize()
↓
start()
启动过程中可以:
启动 Individual
启动 Central
启动必要 Engine
准备运行环境
例如:
$individual->start();
Individual 状态:
created
↓
running
8.6 Application 运行
Application 启动后进入运行阶段。
例如:
Application
↓
Central
↓
Information
↓
Perception
↓
Cognition
↓
Memory
↓
Reasoning
↓
Decision
↓
Behavior
Application 本身不应该实现这些认知机制。
它负责:
让这些模块按照生命周期进入运行状态。
可以理解为:
Application = 运行管理者
Central = 内部协调中心
Individual = 模拟人工个体
8.7 Application 结束
Application 结束时,需要让运行中的对象有序停止。
例如:
Running
↓
Stopping
↓
Stop Individual
↓
Stop Central
↓
Release Runtime
↓
Stopped
例如:
$individual->stop();
Individual:
running
↓
stopped
结束阶段可以处理:
日志
运行状态
缓存
资源释放
设备断开
运行数据保存
具体保存机制由相应模块负责,而不是由 Application 把所有逻辑写进去。
8.8 Application 与 Individual
Application 与 Individual 是两个不同层级。
可以表示为:
Application
│
▼
Individual
Application 负责:
创建
加载
启动
运行
停止
Individual 负责:
Identity
State
Ability
Memory
Cognition
Learning
Behavior
因此:
Application
≠
Individual
例如,一个 Application Runtime 可以管理一个或多个 Individual:
Application
│
├── Individual A
├── Individual B
└── Individual C
是否允许多个 Individual,则由具体 Framework 架构决定。
在最简单的 SAI 示例中,可以先采用:
Application
│
└── Individual
8.9 Application 与 Central
Application 与 Central 也不能混淆。
两者关系:
Application
│
▼
Individual
│
▼
Central
Application 负责 Framework Runtime。
Central 负责 Individual 内部协调。
例如:
Application
│
│ start
▼
Individual
│
│ run
▼
Central
│
├── Perception
├── Cognition
├── Memory
├── Reasoning
├── Decision
└── Behavior
所以:
Application
回答:
Framework 怎么启动和运行?
Central
回答:
Individual 内部各模块如何协调运行?
8.10 Application 类设计
一个简单的 Application 可以设计为:
<?php
namespace SAI\Core;
class Application
{
protected $config;
protected $individual;
protected $central;
protected $state;
public function __construct(
array $config,
$individual,
$central
) {
$this->config = $config;
$this->individual = $individual;
$this->central = $central;
$this->state = 'created';
}
public function initialize()
{
$this->state = 'initialized';
}
public function start()
{
$this->individual->start();
$this->state = 'started';
}
public function run()
{
$this->central->run();
$this->state = 'running';
}
public function stop()
{
$this->individual->stop();
$this->state = 'stopped';
}
public function getState()
{
return $this->state;
}
}
这个 Application 的职责非常明确:
initialize()
↓
start()
↓
run()
↓
stop()
它没有把:
Cognition
Reasoning
Decision
Memory
全部塞进去。
这符合模块化设计。
8.11 Individual 类
为了配合 Application,需要一个简单的 Individual。
<?php
namespace SAI\Individual;
class Individual
{
protected $id;
protected $name;
protected $state;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
$this->state = 'created';
}
public function start()
{
$this->state = 'running';
}
public function stop()
{
$this->state = 'stopped';
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getState()
{
return $this->state;
}
}
8.12 Central 类
再建立一个最简单的 Central。
<?php
namespace SAI\Central;
class Central
{
protected $state;
public function __construct()
{
$this->state = 'created';
}
public function run()
{
$this->state = 'running';
}
public function stop()
{
$this->state = 'stopped';
}
public function getState()
{
return $this->state;
}
}
这里暂时没有加入真正的:
Perception
Cognition
Memory
Reasoning
Decision
Behavior
因为本章重点只是:
Application 如何管理 Framework Runtime。
8.13 Application 三者关系
现在:
Application
│
▼
Individual
│
▼
Central
对应 PHP:
$central = new Central();
$individual = new Individual(
'sai_001',
'Indoor SAI'
);
$app = new Application(
$config,
$individual,
$central
);
Application 持有:
$config
$individual
$central
形成 Composition / Dependency。
8.14 Application 生命周期实现
执行:
$app->initialize();
状态:
created
↓
initialized
然后:
$app->start();
状态:
initialized
↓
started
同时 Individual:
created
↓
running
然后:
$app->run();
Application:
started
↓
running
Central:
created
↓
running
最后:
$app->stop();
Application:
running
↓
stopped
Individual:
running
↓
stopped
8.15 完整 PHP 示例
下面把前面的类组合成一个完整的教程运行示例。
目录可以暂时设计为:
sai-application-demo/
│
├── app/
│ ├── Core/
│ │ └── Application.php
│ │
│ ├── Individual/
│ │ └── Individual.php
│ │
│ └── Central/
│ └── Central.php
│
├── config/
│ └── app.php
│
├── bootstrap.php
└── index.php
config/app.php
<?php
return array(
'name' => 'SAI Framework',
'version' => '0.1.0',
'debug' => true
);
bootstrap.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
spl_autoload_register(function ($class) {
$prefix = 'SAI\\';
if (strpos($class, $prefix) !== 0) {
return;
}
$relative = substr($class, strlen($prefix));
$file = __DIR__
. '/app/'
. str_replace('\\', '/', $relative)
. '.php';
if (is_file($file)) {
require_once $file;
}
});
app/Core/Application.php
<?php
namespace SAI\Core;
class Application
{
protected $config;
protected $individual;
protected $central;
protected $state;
public function __construct(
array $config,
$individual,
$central
) {
$this->config = $config;
$this->individual = $individual;
$this->central = $central;
$this->state = 'created';
}
public function initialize()
{
$this->state = 'initialized';
}
public function start()
{
$this->individual->start();
$this->state = 'started';
}
public function run()
{
$this->central->run();
$this->state = 'running';
}
public function stop()
{
$this->central->stop();
$this->individual->stop();
$this->state = 'stopped';
}
public function getState()
{
return $this->state;
}
}
app/Individual/Individual.php
<?php
namespace SAI\Individual;
class Individual
{
protected $id;
protected $name;
protected $state;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
$this->state = 'created';
}
public function start()
{
$this->state = 'running';
}
public function stop()
{
$this->state = 'stopped';
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getState()
{
return $this->state;
}
}
app/Central/Central.php
<?php
namespace SAI\Central;
class Central
{
protected $state;
public function __construct()
{
$this->state = 'created';
}
public function run()
{
$this->state = 'running';
}
public function stop()
{
$this->state = 'stopped';
}
public function getState()
{
return $this->state;
}
}
index.php
<?php
require_once __DIR__ . '/bootstrap.php';
use SAI\Core\Application;
use SAI\Individual\Individual;
use SAI\Central\Central;
$config = require __DIR__ . '/config/app.php';
$individual = new Individual(
'sai_001',
'Indoor SAI'
);
$central = new Central();
$app = new Application(
$config,
$individual,
$central
);
$app->initialize();
$app->start();
$app->run();
echo '<pre>';
echo "Framework: "
. $config['name']
. PHP_EOL;
echo "Version: "
. $config['version']
. PHP_EOL;
echo "Application State: "
. $app->getState()
. PHP_EOL;
echo "Individual ID: "
. $individual->getId()
. PHP_EOL;
echo "Individual Name: "
. $individual->getName()
. PHP_EOL;
echo "Individual State: "
. $individual->getState()
. PHP_EOL;
echo "Central State: "
. $central->getState()
. PHP_EOL;
$app->stop();
echo PHP_EOL;
echo "After Stop:" . PHP_EOL;
echo "Application State: "
. $app->getState()
. PHP_EOL;
echo "Individual State: "
. $individual->getState()
. PHP_EOL;
echo "Central State: "
. $central->getState()
. PHP_EOL;
echo '</pre>';
8.16 实际运行流程
执行:
index.php
首先:
bootstrap.php
注册 Autoload。
然后:
config/app.php
加载配置。
然后创建:
Individual
Central
Application
之后:
$app->initialize()
得到:
Application
created
↓
initialized
继续:
$app->start()
得到:
Application = started
Individual = running
继续:
$app->run()
得到:
Application = running
Central = running
最后:
$app->stop()
得到:
Application = stopped
Individual = stopped
Central = stopped
8.17 预期运行结果
如果按照本章示例建立对应文件,并且 PHP 环境正常,预期可以看到类似:
Framework: SAI Framework
Version: 0.1.0
Application State: running
Individual ID: sai_001
Individual Name: Indoor SAI
Individual State: running
Central State: running
After Stop:
Application State: stopped
Individual State: stopped
Central State: stopped
这里的“预期运行结果”是根据示例代码推导出的结果,不是本次实际执行验证结果。
如果以后真正建立项目,需要通过实际 PHP CLI 或 Web 环境执行验证后,才能称为“实际运行结果”。
8.18 Application 与完整 SAI 生命周期
目前的 Application 示例还比较简单:
Application
↓
Individual
↓
Central
随着后续章节加入 SAI 模块,它可以逐步扩展为:
Application
│
▼
Individual
│
▼
Central
│
├── Information
│
├── Scene
│
├── Perception
│
├── Cognition
│
├── Memory
│
├── Reasoning
│
├── Decision
│
├── Behavior
│
├── Feedback
│
├── Experience
│
└── Learning
因此 Application 不需要不断增加认知代码。
它只需要保持:
Application
↓
Runtime Management
↓
Individual
↓
Central
↓
SAI Modules
8.19 Application 的核心职责边界
可以把 Application 的职责总结为:
Application
├── Configuration
├── Initialization
├── Startup
├── Runtime
├── Shutdown
└── Resource Lifecycle
而不应该承担:
Application
├── Cognition
├── Reasoning
├── Decision
├── Learning
└── Behavior Logic
这些属于 Individual 内部的模块。
这条边界非常重要。
否则最终很容易形成一个巨大的:
Application.php
里面包含整个 SAI Framework 的所有代码。
那就失去了模块化设计的意义。
8.20 本章结构总结
SAI Framework 的运行层次可以表示为:
Application
│
Framework Runtime
│
▼
Individual
│
▼
Central
│
┌─────────────────┼─────────────────┐
▼ ▼ ▼
Information Cognition Memory
│ │ │
└─────────────────┼─────────────────┘
▼
Reasoning
│
▼
Decision
│
▼
Behavior
│
▼
Action
│
▼
Feedback
│
▼
Experience
│
▼
Learning
因此:
Application 管理 Framework 的运行生命周期;Individual 是模拟人工个体;Central 是 Individual 的内部协调中心;各 SAI Engine 和模块负责具体能力。
本章小结
第8章建立了 SAI Framework 的第一个真正的运行管理层。
核心关系是:
Application
↓
初始化 Framework
↓
启动 Individual
↓
运行 Central
↓
执行 SAI 生命周期
↓
停止 Individual
↓
结束 Framework
Application 生命周期:
Created
↓
Initialized
↓
Started
↓
Running
↓
Stopping
↓
Stopped
三者关系:
Application
│
│ 管理运行
▼
Individual
│
│ 内部协调
▼
Central
│
├── Cognition
├── Memory
├── Reasoning
├── Decision
├── Behavior
└── Learning
因此可以形成一句非常重要的架构定义:
Application 不等于 Individual,Central 也不等于 Application。Application 管理 Framework Runtime,Individual 表示模拟人工个体,Central 负责 Individual 内部模块协调。
下一章进入 第9章 Bootstrap 时,就可以进一步解决一个问题:
Application 从哪里来?
↓
谁负责启动 Application?
↓
Configuration 如何加载?
↓
Autoload 如何建立?
↓
Container 如何初始化?
↓
Framework 如何正式进入运行状态?
这就会把 Bootstrap → Application → Individual → Central 这一条 SAI Framework 基础启动链完整建立起来。