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

第77章 自定义 Engine

第77章 自定义 Engine

Extension 解决了 ICAI 如何增加和移除功能的问题。

但 Extension 只是扩展载体。真正执行某一类专业处理任务时,还需要 Engine。

因此可以形成:

Extension
    ↓
Custom Engine
    ↓
Engine Function
    ↓
Engine Result

自定义 Engine 的核心思想是:

按照统一的 Engine Interface 创建一个独立功能引擎,并通过 Extension/Engine Registry 注册,由 Engine Manager 加载和调用。

它不改变 ICAI 的核心认知机制,也不要求把所有功能写入 Core。


1. Engine Interface

Engine Interface 是 Engine 的统一接口。

它规定自定义 Engine 至少应该具备:

  • Engine ID
  • Engine Name
  • Version
  • 初始化
  • 调用
  • 状态
  • 关闭

例如 PHP OOP:

interface EngineInterface
{
    public function getId();

    public function getName();

    public function getVersion();

    public function initialize();

    public function execute($input);

    public function getState();

    public function shutdown();
}

这样,不同 Engine 都可以按照相同方式被系统管理。

例如:

DetectionEngine
LearningEngine
ReasoningEngine
DeviceEngine
CustomEngine

虽然内部功能不同,但外部管理方式统一:

EngineInterface
       ↓
Engine
       ↓
initialize()
       ↓
execute()
       ↓
Result
       ↓
shutdown()

Engine Interface 的意义

Interface 不负责具体算法。

例如:

public function execute($input);

只规定:

Engine 必须能够接收输入并返回执行结果。

至于内部如何处理,由具体 Engine 自己决定。

因此:

Interface = 规定结构
Engine = 实现功能
Manager = 管理 Engine
Registry = 记录 Engine

2. 创建 Engine

创建自定义 Engine 时,首先确定 Engine 的职责。

例如创建一个:

DeviceStatusEngine

它的职责是:

输入 Device Object
        ↓
检查 Device State
        ↓
检查 Device Property
        ↓
形成 Status Result

首先创建类:

class DeviceStatusEngine implements EngineInterface
{
    protected $state = 'NEW';

    public function getId()
    {
        return 'device_status';
    }

    public function getName()
    {
        return 'Device Status Engine';
    }

    public function getVersion()
    {
        return '1.0.0';
    }

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

        return true;
    }

    public function execute($input)
    {
        if (!is_array($input)) {
            return array(
                'state' => 'FAILED',
                'error' => 'Invalid input'
            );
        }

        $this->state = 'RUNNING';

        $result = array(
            'engine' => $this->getId(),
            'device_id' => isset($input['device_id'])
                ? $input['device_id']
                : null,
            'device_state' => isset($input['state'])
                ? $input['state']
                : 'UNKNOWN',
            'result' => 'CHECKED'
        );

        $this->state = 'READY';

        return $result;
    }

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

    public function shutdown()
    {
        $this->state = 'STOPPED';

        return true;
    }
}

这个 Engine 没有改变 ICAI Core。

它只是提供一个独立的:

Device Status Check

功能。


3. 注册 Engine

创建 Engine 后,系统必须知道它的存在。

因此需要 Engine Registry。

基本结构:

EngineRegistry
{
    id
    name
    class
    version
    path
    type
    dependencies
    state
    enabled
}

例如:

{
    "id": "device_status",
    "name": "Device Status Engine",
    "class": "DeviceStatusEngine",
    "version": "1.0.0",
    "path": "engines/DeviceStatusEngine.php",
    "type": "device",
    "dependencies": [],
    "state": "REGISTERED",
    "enabled": true
}

注册过程:

Custom Engine
      ↓
Engine Registry
      ↓
Registered

EngineManager

可以建立统一的 Engine Manager:

class EngineManager
{
    protected $engines = array();

    public function register(EngineInterface $engine)
    {
        $id = $engine->getId();

        $this->engines[$id] = $engine;

        return true;
    }

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

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

注册:

$manager = new EngineManager();

$engine = new DeviceStatusEngine();

$manager->register($engine);

此时:

EngineManager
      ↓
device_status
      ↓
DeviceStatusEngine

4. 加载 Engine

注册并不代表 Engine 已经可以执行。

需要经过:

REGISTERED
    ↓
LOAD
    ↓
INITIALIZE
    ↓
READY

例如:

$engine = $manager->get('device_status');

if ($engine) {
    $engine->initialize();
}

初始化成功以后:

DeviceStatusEngine
state = READY

如果初始化失败:

DeviceStatusEngine
state = ERROR

不能继续假定 Engine 可以运行。


Engine 加载检查

实际系统中可以检查:

1. Registry 是否存在
2. 文件是否存在
3. Class 是否存在
4. 是否实现 EngineInterface
5. 依赖是否满足
6. 配置是否有效
7. initialize 是否成功

形成:

Registry
   ↓
File Check
   ↓
Class Check
   ↓
Interface Check
   ↓
Dependency Check
   ↓
Initialize
   ↓
READY

5. 调用 Engine

Engine 加载成功以后才能调用。

调用过程:

Input
  ↓
EngineManager
  ↓
Engine
  ↓
execute()
  ↓
EngineResult

例如:

$input = array(
    'device_id' => 'Motor_A',
    'state' => 'RUNNING',
    'temperature' => 65
);

$result = $manager
    ->get('device_status')
    ->execute($input);

Engine 接收到:

Device_A
state = RUNNING
temperature = 65

然后执行自身的处理逻辑。

这里需要特别区分:

Engine 调用

和:

Decision

Engine 被调用,并不意味着 Engine 自己决定 ICAI 应该做什么。

例如:

Decision
    ↓
调用 DeviceStatusEngine
    ↓
获得设备状态

Engine 是被调用的功能处理组件。


6. 返回结果

Engine 执行完成后必须返回明确的结构化结果。

不能只返回:

OK

最好形成:

EngineResult
{
    id
    engine_id
    state
    input
    output
    changes
    error
    started_at
    completed_at
}

例如:

{
    "id": 10001,
    "engine_id": "device_status",
    "state": "SUCCESS",
    "input": {
        "device_id": "Motor_A",
        "state": "RUNNING",
        "temperature": 65
    },
    "output": {
        "device_id": "Motor_A",
        "status": "NORMAL"
    },
    "changes": [],
    "error": null
}

EngineResult 状态

可以定义:

NEW
READY
RUNNING
SUCCESS
FAILED
PARTIAL
UNKNOWN
CONFLICT
CANCELLED
TIMEOUT
ERROR

其中:

SUCCESS

表示 Engine 的执行过程成功。

但不一定表示:

整个 ICAI 任务成功

例如:

DeviceStatusEngine = SUCCESS

只说明:

设备状态检查已经成功完成。

检查结果仍然可能是:

device_state = ERROR

所以:

Engine Execution State
        ≠
Engine Business Result

这是 Engine 设计中的重要边界。


7. 自定义 Engine 示例

下面建立一个完整的:

TemperatureCheckEngine

用于检查设备温度是否超过规定阈值。


7.1 Engine 定义

class TemperatureCheckEngine implements EngineInterface
{
    protected $state = 'NEW';

    public function getId()
    {
        return 'temperature_check';
    }

    public function getName()
    {
        return 'Temperature Check Engine';
    }

    public function getVersion()
    {
        return '1.0.0';
    }

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

        return true;
    }

    public function execute($input)
    {
        if (!is_array($input)) {
            return array(
                'engine_id' => $this->getId(),
                'state' => 'FAILED',
                'error' => 'Invalid input'
            );
        }

        if (!isset($input['temperature'])) {
            return array(
                'engine_id' => $this->getId(),
                'state' => 'FAILED',
                'error' => 'Temperature is required'
            );
        }

        $this->state = 'RUNNING';

        $temperature = (float)$input['temperature'];
        $limit = isset($input['limit'])
            ? (float)$input['limit']
            : 80;

        if ($temperature > $limit) {
            $status = 'HIGH';
        } else {
            $status = 'NORMAL';
        }

        $this->state = 'READY';

        return array(
            'engine_id' => $this->getId(),
            'state' => 'SUCCESS',
            'input' => $input,
            'output' => array(
                'temperature' => $temperature,
                'limit' => $limit,
                'status' => $status
            ),
            'error' => null
        );
    }

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

    public function shutdown()
    {
        $this->state = 'STOPPED';

        return true;
    }
}

7.2 注册

$manager = new EngineManager();

$temperatureEngine = new TemperatureCheckEngine();

$manager->register($temperatureEngine);

形成:

EngineManager
      ↓
temperature_check
      ↓
TemperatureCheckEngine

7.3 加载

$engine = $manager->get('temperature_check');

if ($engine) {
    $engine->initialize();
}

状态:

NEW
 ↓
REGISTERED
 ↓
READY

7.4 调用

输入:

$input = array(
    'device_id' => 'Motor_A',
    'temperature' => 95,
    'limit' => 80
);

$result = $engine->execute($input);

Engine 计算:

temperature = 95
limit = 80

95 > 80

因此:

status = HIGH

返回:

EngineResult
{
    engine_id: "temperature_check",
    state: "SUCCESS",
    output: {
        temperature: 95,
        limit: 80,
        status: "HIGH"
    }
}

7.5 进入 ICAI 自维护

这里就可以与前面的 Detection、Risk、Diagnosis、Repair、Verification 连接起来:

TemperatureCheckEngine
        ↓
temperature = 95
        ↓
Detection
        ↓
DetectionResult
        ↓
Risk
        ↓
Diagnosis
        ↓
Decision
        ↓
Repair
        ↓
Verification

例如:

Motor_A.temperature = 95

Detection:

ABNORMAL

Risk:

HIGH

Diagnosis:

Motor_A Overheat

Decision:

STOP Motor_A

Action:

STOP

Verification:

Motor_A.state = STOPPED
temperature < 80

如果验证通过:

SELF-MAINTENANCE SUCCESS

自定义 Engine 完整模型

             Engine Interface
                    ↓
             Custom Engine
                    ↓
             Engine Registry
                    ↓
             Engine Manager
                    ↓
                  Load
                    ↓
               Initialize
                    ↓
                  Ready
                    ↓
                 Execute
                    ↓
               EngineResult
                    ↓
          ┌─────────┴─────────┐
          ↓                   ↓
      正常业务流程          异常处理
                              ↓
                          Detection
                              ↓
                             Risk
                              ↓
                          Diagnosis
                              ↓
                           Decision
                              ↓
                            Repair
                              ↓
                         Verification

本章核心定义

Engine Interface

Engine Interface 是规定 Engine 基本结构、初始化、执行、状态和关闭行为的统一接口。

Custom Engine

自定义 Engine 是按照 Engine Interface 创建、用于完成特定结构化处理任务的独立执行组件。

Engine Registry

Engine Registry 是记录 Engine 身份、版本、类、路径、依赖、状态和启用状态的注册结构。

Engine Manager

Engine Manager 是负责 Engine 注册、查找、加载、初始化、调用和关闭的管理组件。

最终形成:

Interface
   ↓
Create
   ↓
Register
   ↓
Load
   ↓
Initialize
   ↓
Execute
   ↓
Result
   ↓
Shutdown

并与第76章 Extension 建立关系:

Extension
   ↓
提供扩展能力
   ↓
Custom Engine
   ↓
执行具体功能
   ↓
Engine Result
   ↓
ICAI Core

这里的关键边界是:

Extension = 扩展载体
Engine = 功能执行器
Manager = 管理执行器
Registry = 记录执行器
Result = 执行结果

这样,ICAI 可以在不破坏核心结构的情况下持续增加新的 Engine。

Leave a Reply

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