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

第9章 Bootstrap

第9章 Bootstrap

本章大纲

  1. Bootstrap 定义
  2. 框架启动过程
  3. 环境初始化
  4. 自动加载
  5. 配置加载
  6. Container 初始化
  7. Application 创建
  8. Central 初始化
  9. Individual 初始化
  10. Bootstrap 执行顺序
  11. 完整代码
  12. 启动测试

9.1 Bootstrap 定义

Bootstrap 可以理解为 SAI Framework 的启动引导程序

它不是 Individual,也不是 Central,更不是 Cognition、Reasoning 或 Decision。

Bootstrap 的主要任务是:

环境准备
 ↓
自动加载
 ↓
配置加载
 ↓
Container 初始化
 ↓
Individual 初始化
 ↓
Central 初始化
 ↓
Application 创建
 ↓
Application 初始化
 ↓
Application 启动

因此:

Bootstrap 负责把一个尚未运行的 PHP 程序,逐步建立成为可以运行的 SAI Framework Runtime。

可以把它理解成:

Bootstrap
    │
    ├── PHP环境
    ├── Autoload
    ├── Config
    ├── Container
    ├── Individual
    ├── Central
    └── Application
             │
             ▼
          Running

Bootstrap 本身不负责模拟人工智能。

它只是负责启动人工个体运行所需要的基础环境和对象


9.2 框架启动过程

一个 SAI Framework 程序不能直接从:

Individual

开始运行。

首先必须建立运行环境。

完整过程可以表示为:

PHP Runtime
    ↓
Bootstrap
    ↓
Autoload
    ↓
Configuration
    ↓
Container
    ↓
Individual
    ↓
Central
    ↓
Application
    ↓
Initialize
    ↓
Start
    ↓
Run

如果是 Web 环境,则可能是:

Web Server
    ↓
public/index.php
    ↓
bootstrap.php
    ↓
Application
    ↓
Individual
    ↓
Central
    ↓
SAI Runtime

如果不是 Web,也可以:

CLI
 ↓
bootstrap.php
 ↓
Application
 ↓
Individual
 ↓
Central

因此 Bootstrap 不属于 Web 专属机制


9.3 环境初始化

Bootstrap 首先需要准备 PHP 运行环境。

例如:

error_reporting(E_ALL);
ini_set('display_errors', '1');

开发环境可以打开错误显示:

error_reporting(E_ALL);
ini_set('display_errors', '1');

生产环境则通常应该关闭直接输出错误:

ini_set('display_errors', '0');

Bootstrap 还可以准备:

基础路径
配置路径
Storage路径
Extension路径
日志路径
运行环境
时区
错误处理
异常处理

例如:

date_default_timezone_set('Asia/Shanghai');

于是环境初始化可以形成:

Bootstrap
 │
 ├── Error
 ├── Timezone
 ├── Path
 ├── Storage
 └── Runtime

9.4 自动加载

SAI Framework 使用大量 PHP Class。

例如:

SAI\Core\Application
SAI\Core\Container
SAI\Central\Central
SAI\Individual\Individual
SAI\Memory\Memory
SAI\Reasoning\Reasoning

如果每一个 Class 都手动:

require_once ...

项目会非常复杂。

因此 Bootstrap 建立自动加载机制。

PHP 可以使用:

spl_autoload_register()

例如:

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;
    }
});

例如:

use SAI\Individual\Individual;

当 PHP 使用:

new Individual();

自动加载器会寻找:

app/Individual/Individual.php

因此:

Namespace
   ↓
Class
   ↓
Autoload
   ↓
PHP File
   ↓
Class Loaded

自动加载的意义不是 SAI 智能机制,而是PHP 工程基础设施


9.5 配置加载

Bootstrap 第二个重要任务是加载 Framework 配置。

例如:

config/
└── app.php

配置:

<?php

return array(
    'name' => 'SAI Framework',
    'version' => '0.1.0',
    'debug' => true
);

Bootstrap:

$config = require __DIR__ . '/config/app.php';

得到:

$config

例如:

echo $config['name'];

结果:

SAI Framework

配置可以包括:

Framework名称
版本
Debug
Storage路径
日志路径
Extension配置
Individual配置
运行模式

配置的作用是:

告诉 Framework 应该以什么运行环境和参数启动。

配置本身不是知识,也不是 Memory。


9.6 Container 初始化

Container 是 Framework 的对象和服务管理容器。

它可以负责保存:

Application
Individual
Central
Memory
Cognition
Reasoning
Decision
Behavior

等对象。

一个最简单的 Container:

<?php

namespace SAI\Core;

class Container
{
    protected $items = array();

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

    public function get($name)
    {
        if (isset($this->items[$name])) {
            return $this->items[$name];
        }

        return null;
    }

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

Bootstrap:

$container = new Container();

然后:

$container->set('config', $config);

之后可以:

$container->get('config');

Container 的作用是:

创建对象
 ↓
保存对象
 ↓
提供对象
 ↓
统一管理对象依赖

但是必须注意:

Container 不负责认知,不负责推理,也不负责决策。

它只是 Framework 的对象管理基础设施。


9.7 Application 创建

Bootstrap 完成基础环境以后,需要创建 Application。

Application 是 Framework Runtime 的运行载体。

关系:

Bootstrap
    ↓
Application
    ↓
Individual
    ↓
Central

Application 可以接收:

$config
$container
$individual
$central

例如:

$app = new Application(
    $config,
    $container,
    $individual,
    $central
);

这里需要注意:

Bootstrap 创建 Application

不等于:

Application 创建 Individual

在较清晰的架构中,可以让 Bootstrap 或专门的 Factory/Manager 完成对象建立,再交给 Application 管理。

这样职责更加清楚。


9.8 Central 初始化

Central 是 Individual 的内部协调中心。

Bootstrap 可以先创建:

$central = new Central();

Central 的职责是协调:

Information
Perception
Cognition
Memory
Reasoning
Decision
Behavior
Learning
Feedback

例如:

                    Central
                       │
        ┌──────────────┼──────────────┐
        ▼              ▼              ▼
    Cognition       Memory        Reasoning
        │              │              │
        └──────────────┼──────────────┘
                       ▼
                    Decision
                       │
                       ▼
                    Behavior

Central 自己并不等于:

Cognition

也不等于:

Reasoning

它主要负责:

协调 Individual 内部各个功能模块的运行。


9.9 Individual 初始化

Individual 是 SAI Framework 最终要构建的对象。

例如:

$individual = new Individual(
    'sai_001',
    'Indoor SAI'
);

创建以后:

Individual
├── Identity
├── State
├── Ability
├── Memory
├── Cognition
├── Learning
└── Behavior

当前简单示例可能只有:

ID
Name
State

但完整 SAI Individual 会逐渐增加:

Memory
Cognition
Reasoning
Decision
Behavior
Learning
Ability

Bootstrap 的任务不是实现这些能力,而是:

准备 Individual 对象
 ↓
交给 Application
 ↓
进入 Runtime

9.10 Bootstrap 执行顺序

Bootstrap 的执行顺序非常重要。

推荐顺序:

① 环境初始化
        ↓
② 自动加载
        ↓
③ 配置加载
        ↓
④ Container 创建
        ↓
⑤ Individual 创建
        ↓
⑥ Central 创建
        ↓
⑦ Application 创建
        ↓
⑧ Application 初始化
        ↓
⑨ Application 启动
        ↓
⑩ Application 运行

完整表示:

PHP
 │
 ▼
Bootstrap
 │
 ├── Environment
 │
 ├── Autoload
 │
 ├── Config
 │
 ├── Container
 │
 ├── Individual
 │
 ├── Central
 │
 └── Application
          │
          ▼
      Initialize
          │
          ▼
        Start
          │
          ▼
         Run

这个顺序形成:

Bootstrap → Application → Individual → Central

需要进一步区分:

Bootstrap

负责启动 Framework。

Application

负责管理 Runtime。

Individual

是模拟人工个体。

Central

负责 Individual 内部协调。


9.11 完整代码

下面给出一个教程级完整示例

注意:下面代码用于说明第9章 Bootstrap 的结构和执行顺序。它是教程示例,不代表当前已经在用户实际环境中创建或验证运行。

目录:

sai-bootstrap-demo/
│
├── app/
│   ├── Core/
│   │   ├── Application.php
│   │   └── Container.php
│   │
│   ├── Central/
│   │   └── Central.php
│   │
│   └── Individual/
│       └── Individual.php
│
├── config/
│   └── app.php
│
├── bootstrap.php
└── index.php

9.11.1 Container.php

<?php

namespace SAI\Core;

class Container
{
    protected $items = array();

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

    public function get($name)
    {
        if (isset($this->items[$name])) {
            return $this->items[$name];
        }

        return null;
    }

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

9.11.2 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;
    }
}

9.11.3 Central.php

<?php

namespace SAI\Central;

class Central
{
    protected $state;

    public function __construct()
    {
        $this->state = 'created';
    }

    public function initialize()
    {
        $this->state = 'initialized';
    }

    public function run()
    {
        $this->state = 'running';
    }

    public function stop()
    {
        $this->state = 'stopped';
    }

    public function getState()
    {
        return $this->state;
    }
}

9.11.4 Application.php

<?php

namespace SAI\Core;

class Application
{
    protected $config;
    protected $container;
    protected $individual;
    protected $central;
    protected $state;

    public function __construct(
        array $config,
        Container $container,
        $individual,
        $central
    ) {
        $this->config = $config;
        $this->container = $container;
        $this->individual = $individual;
        $this->central = $central;
        $this->state = 'created';
    }

    public function initialize()
    {
        $this->central->initialize();

        $this->state = 'initialized';
    }

    public function start()
    {
        $this->individual->start();
        $this->central->run();

        $this->state = 'running';
    }

    public function stop()
    {
        $this->central->stop();
        $this->individual->stop();

        $this->state = 'stopped';
    }

    public function getState()
    {
        return $this->state;
    }
}

这里可以看到 Application 负责 Runtime 生命周期:

initialize()
start()
stop()

而不是:

think()
reason()
decide()
learn()

这些属于 Individual 内部能力体系。


9.11.5 config/app.php

<?php

return array(
    'name' => 'SAI Framework',
    'version' => '0.1.0',
    'debug' => true
);

9.11.6 bootstrap.php

这是本章最重要的文件。

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

date_default_timezone_set('Asia/Shanghai');

/*
 * SAI Framework Autoload
 */
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;
    }
});

/*
 * Load Configuration
 */
$config = require __DIR__ . '/config/app.php';

/*
 * Create Container
 */
$container = new SAI\Core\Container();

$container->set(
    'config',
    $config
);

/*
 * Create Individual
 */
$individual = new SAI\Individual\Individual(
    'sai_001',
    'Indoor SAI'
);

/*
 * Create Central
 */
$central = new SAI\Central\Central();

/*
 * Create Application
 */
$app = new SAI\Core\Application(
    $config,
    $container,
    $individual,
    $central
);

/*
 * Register core objects
 */
$container->set('individual', $individual);
$container->set('central', $central);
$container->set('application', $app);

这里的执行顺序就是:

Environment
    ↓
Autoload
    ↓
Config
    ↓
Container
    ↓
Individual
    ↓
Central
    ↓
Application
    ↓
Register Objects

9.11.7 index.php

<?php

require_once __DIR__ . '/bootstrap.php';

$app->initialize();
$app->start();

echo '<pre>';

echo "Framework: "
    . $config['name']
    . PHP_EOL;

echo "Version: "
    . $config['version']
    . PHP_EOL;

echo 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;

echo '</pre>';

9.12 启动测试

如果按照上面的目录建立示例,可以进行 PHP 语法检查。

例如:

php -l bootstrap.php

然后:

php -l index.php

再检查:

php -l app/Core/Application.php
php -l app/Core/Container.php
php -l app/Individual/Individual.php
php -l app/Central/Central.php
php -l config/app.php

最后运行:

php index.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

这里的“预期”是根据代码逻辑推导出的结果,不是本次实际运行验证结果


9.13 Bootstrap 与 Application 的关系

现在可以把第8章和第9章连接起来。

第8章:

Application

解决:

Framework Runtime 如何运行?

第9章:

Bootstrap

解决:

Application 从哪里来?Framework 如何启动?

因此:

Bootstrap
    │
    │ 创建
    ▼
Application
    │
    ├── Individual
    │
    └── Central

更完整:

Bootstrap
 │
 ├── Environment
 │
 ├── Autoload
 │
 ├── Configuration
 │
 ├── Container
 │
 ├── Individual
 │
 ├── Central
 │
 └── Application
          │
          ▼
      initialize()
          │
          ▼
        start()
          │
          ▼
         run()

9.14 Bootstrap 与 Individual 的关系

Bootstrap 不应该成为 Individual 的智能核心。

错误设计:

Bootstrap
 ├── Cognition
 ├── Reasoning
 ├── Decision
 ├── Learning
 └── Behavior

这样会让 Bootstrap 变成一个巨大的程序入口。

更合理:

Bootstrap
    ↓
Application
    ↓
Individual
    ↓
Central
    ↓
各功能模块

因此 Bootstrap 只负责:

准备
创建
注册
启动

而 Individual 负责:

感知
认知
记忆
推理
决策
行为
反馈
学习

9.15 Bootstrap 的最终职责边界

可以将 Bootstrap 的职责限定为:

Bootstrap
│
├── Environment
├── Autoload
├── Configuration
├── Container
├── Object Initialization
├── Application Creation
└── Runtime Startup

不负责:

Cognition
Reasoning
Decision
Behavior
Learning
Memory

这些应该由 SAI Framework 的其他模块完成。

最终形成:

                 Bootstrap
                     │
              Framework Startup
                     │
                     ▼
               Application
                     │
                     ▼
                 Individual
                     │
                   Central
                     │
       ┌─────────────┼─────────────┐
       ▼             ▼             ▼
   Cognition      Memory       Reasoning
       │             │             │
       └─────────────┼─────────────┘
                     ▼
                  Decision
                     │
                     ▼
                  Behavior

本章小结

Bootstrap 是 SAI Framework 的启动引导层

它完成:

环境初始化
 ↓
自动加载
 ↓
配置加载
 ↓
Container
 ↓
Individual
 ↓
Central
 ↓
Application
 ↓
Initialize
 ↓
Start
 ↓
Run

其中最重要的关系是:

Bootstrap
    ↓
Application
    ↓
Individual
    ↓
Central
    ↓
SAI Modules

同时必须保持清晰的职责边界:

组件 核心职责
Bootstrap 启动 Framework
Application 管理 Runtime
Container 管理对象与依赖
Individual 模拟人工个体
Central 协调 Individual 内部模块
Cognition 认知
Memory 记忆
Reasoning 推理
Decision 决策
Behavior 行为
Learning 学习

因此,第8章 Application 解决了“运行什么”,第9章 Bootstrap 解决了“如何启动”。

下一步自然进入:

第10章 Container

进一步解决:

对象如何注册?
对象如何获取?
对象之间如何依赖?
Individual 如何获得 Memory、Cognition、Reasoning?
Container 如何管理 SAI Framework 内部对象?

这会把 Bootstrap 与 Framework 内部对象体系真正连接起来。

Leave a Reply

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