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

第211章 Method Class|方法类

第211章可以直接进入 ICAI Method 类的工程定义。这里不把 Method 理解成普通程序函数,而是把它作为 ICAI 中连接 Cognition → Action 的独立认知工程对象。

第211章 Method Class|方法类

211.1 Method 的位置

ICAI 的基本链路可以表示为:

Element
   ↓
Object
   ↓
Relation
   ↓
Scene
   ↓
Cognition
   ↓
Method
   ↓
Action
   ↓
Device
   ↓
Feedback

其中:

Cognition 负责形成“知道什么、判断什么”。
Method 负责形成“如何处理”。

因此:

Cognition ≠ Method
Method ≠ Action

例如:

Cognition:
当前目标是寻找一个满足条件的供应商

Method:
1. 提取供应商条件
2. 建立候选集合
3. 比较候选对象
4. 排除不符合条件者
5. 输出最优候选

Action:
执行搜索、读取数据、计算、写入结果

Method 是中间的可计算过程结构


211.2 Method 的基本定义

ICAI 中可以定义:

Method =
{
    method_id,
    name,
    purpose,
    input,
    condition,
    procedure,
    output,
    state,
    priority,
    constraints,
    feedback
}

对应:

属性 含义
method_id 方法唯一标识
name 方法名称
purpose 方法目的
input 输入对象
condition 使用条件
procedure 方法步骤
output 输出结果
state 当前执行状态
priority 方法优先级
constraints 方法约束
feedback 方法反馈

所以 Method 不是一段简单的:

function xxx()

而是一个具有输入、条件、过程、输出和反馈关系的对象。


211.3 Method Class

ICAI 的 PHP 工程可以首先建立基础类:

<?php

class Method
{
    protected string $methodId;
    protected string $name;
    protected string $purpose;

    protected array $input = [];
    protected array $conditions = [];
    protected array $procedure = [];
    protected array $output = [];

    protected string $state = 'created';

    protected int $priority = 0;

    protected array $constraints = [];
    protected array $feedback = [];

    public function __construct(
        string $methodId,
        string $name,
        string $purpose = ''
    ) {
        $this->methodId = $methodId;
        $this->name = $name;
        $this->purpose = $purpose;
    }

    public function getId(): string
    {
        return $this->methodId;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getPurpose(): string
    {
        return $this->purpose;
    }

    public function setInput(array $input): void
    {
        $this->input = $input;
    }

    public function addCondition(array $condition): void
    {
        $this->conditions[] = $condition;
    }

    public function addStep(array $step): void
    {
        $this->procedure[] = $step;
    }

    public function setOutput(array $output): void
    {
        $this->output = $output;
    }

    public function setPriority(int $priority): void
    {
        $this->priority = $priority;
    }

    public function addConstraint(array $constraint): void
    {
        $this->constraints[] = $constraint;
    }

    public function addFeedback(array $feedback): void
    {
        $this->feedback[] = $feedback;
    }

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

    public function setState(string $state): void
    {
        $this->state = $state;
    }

    public function toArray(): array
    {
        return [
            'method_id'   => $this->methodId,
            'name'        => $this->name,
            'purpose'     => $this->purpose,
            'input'       => $this->input,
            'conditions'  => $this->conditions,
            'procedure'   => $this->procedure,
            'output'      => $this->output,
            'state'       => $this->state,
            'priority'    => $this->priority,
            'constraints' => $this->constraints,
            'feedback'    => $this->feedback
        ];
    }
}

这个阶段只建立 Method Object,不把 Cognition、Action、LLM 等逻辑塞进 Method。


211.4 Method 的核心不是“步骤”

一个重要区别:

Procedure ≠ Method

例如:

Procedure:

Step 1
Step 2
Step 3
Step 4

只是方法内部的执行结构。

真正的 Method 是:

Method
 ├── Purpose
 ├── Input
 ├── Condition
 ├── Procedure
 ├── Output
 ├── Constraint
 ├── Priority
 └── Feedback

因此:

Method

是一个完整的方法对象

而:

procedure

只是 Method 的一个组成部分。


211.5 Method 的输入

Method 必须明确知道自己处理什么。

例如:

$method->setInput([
    'object' => 'supplier',
    'requirements' => [
        'product' => 'electric toothbrush',
        'region'  => 'Rhode Island'
    ]
]);

这里 Method 并不是自己创造输入。

输入来自上一层:

Cognition
    ↓
Method

所以:

Cognition

形成需要解决的问题。

然后:

Method

定义处理这个问题的方法。


211.6 Condition

同一个问题可能存在多个 Method。

例如:

Method A
快速筛选

Method B
精确筛选

Method C
历史数据筛选

Method D
多条件比较

所以 Method 必须具有适用条件:

$method->addCondition([
    'field'    => 'region',
    'operator' => 'equals',
    'value'    => 'Rhode Island'
]);

形成:

Method
   ↓
Condition
   ↓
是否适用?

如果:

Condition = true

则:

Method → 可执行

如果:

Condition = false

则:

Method → 不适用

211.7 Method Procedure

Procedure 是 Method 内部的执行序列。

例如:

$method->addStep([
    'index'  => 1,
    'action' => 'collect'
]);

$method->addStep([
    'index'  => 2,
    'action' => 'filter'
]);

$method->addStep([
    'index'  => 3,
    'action' => 'compare'
]);

$method->addStep([
    'index'  => 4,
    'action' => 'select'
]);

形成:

Method
  │
  └── Procedure
       ├── collect
       ├── filter
       ├── compare
       └── select

这里需要保持一个边界:

Method 描述“采用什么处理方式”,Action 才负责实际执行动作。

因此 Procedure 中的:

collect
filter
compare
select

最终应该映射到 Action,而不是让 Method 自己直接执行所有动作。


211.8 Method 与 Action

这是 ICAI 中非常重要的一条边界:

Method
   ↓
Action

Method:

决定处理路径

Action:

执行具体动作

例如:

Method:
SearchSupplierMethod

可能包含:

1. IdentifyRequirement
2. RetrieveSupplier
3. FilterSupplier
4. CompareSupplier
5. SelectSupplier

而 Action 才执行:

Retrieve
Filter
Compare
Select

所以:

Method = How
Action  = Execute

但这里的 How 不是自然语言意义上的“怎么做”,而是一个可计算的方法结构


211.9 Method 与 Cognition

两者关系:

Cognition
   │
   │ produces
   ↓
Method
   │
   │ organizes
   ↓
Action

例如 Cognition 得到:

Goal:
寻找符合要求的供应商

Cognition 不应该直接:

search()

而应该形成:

Cognition
    ↓
Goal
    ↓
Method Selection
    ↓
Method
    ↓
Action

这样 ICAI 才形成:

认知
→ 方法
→ 行动

而不是:

输入
→ 直接调用程序

211.10 Method State

Method 本身也需要状态。

基础状态可以定义为:

created
↓
validated
↓
selected
↓
ready
↓
executing
↓
completed

失败则:

executing
    ↓
failed

反馈后:

failed
    ↓
revised
    ↓
ready

因此:

Method

不是静态数据。

它可以参与:

Execution State

211.11 Method Feedback

ICAI 的 Method 最终必须进入反馈闭环:

Method
 ↓
Action
 ↓
Result
 ↓
Feedback
 ↓
Method Evaluation

例如:

Method A

执行后:

result_quality = 0.82

下一次可以记录:

$method->addFeedback([
    'result' => 'success',
    'score'  => 0.82
]);

于是 Method 开始具备历史。

最终可以形成:

Method
 ├── Structure
 ├── Execution History
 ├── Success Rate
 ├── Failure Pattern
 └── Feedback

这一步以后,Method 才可能成为 ICAI 学习系统的重要对象。


211.12 Method Selection

当系统拥有多个 Method 时,需要一个方法选择机制:

Cognition
      ↓
Requirement
      ↓
Method Repository
      ↓
Candidate Methods
      ↓
Condition Matching
      ↓
Priority
      ↓
Method Selection

例如:

M001
快速筛选

M002
精确筛选

M003
历史匹配

M004
多条件决策

系统不是:

随机选择

而是计算:

Applicability
+
Priority
+
Historical Performance
+
Constraints

最终:

Selected Method

211.13 Method Repository

因此后续需要:

MethodRepository

专门管理 Method:

class MethodRepository
{
    protected array $methods = [];

    public function add(Method $method): void
    {
        $this->methods[$method->getId()] = $method;
    }

    public function get(string $methodId): ?Method
    {
        return $this->methods[$methodId] ?? null;
    }

    public function all(): array
    {
        return $this->methods;
    }
}

形成:

Method
   ↑
   │
MethodRepository
   ↑
   │
Cognition / MethodSelector

211.14 ICAI Method 的完整结构

到这里,可以把 Method 定义为:

Method
│
├── Identity
│   ├── method_id
│   └── name
│
├── Purpose
│
├── Input
│
├── Condition
│
├── Procedure
│
├── Output
│
├── Constraint
│
├── Priority
│
├── State
│
└── Feedback

核心关系:

              Cognition
                  │
                  ▼
             MethodSelector
                  │
                  ▼
              ┌────────┐
              │ Method │
              └────────┘
                  │
          ┌───────┴───────┐
          ▼               ▼
      Procedure       Constraint
          │
          ▼
        Action
          │
          ▼
        Result
          │
          ▼
       Feedback
          │
          └──────────► Method

211.15 本章工程边界

第211章只建立:

Method Class

以及它的:

Identity
Input
Condition
Procedure
Output
State
Priority
Constraint
Feedback

暂时不实现

MethodSelector
MethodExecutor
MethodLearning
MethodOptimizer

这些属于后续层。

因此当前 ICAI 工程结构保持:

Element
   ↓
Object
   ↓
Relation
   ↓
Scene
   ↓
Cognition
   ↓
Method        ← 第211章
   ↓
Action
   ↓
Device
   ↓
Feedback

这一章真正完成的是一个关键转换:

ICAI 不再只有“认知对象”,开始拥有独立的“方法对象”。

这样后面的 Action Class 就不需要承担“决定怎么做”的职责,而只需要承担执行 Method 所规定动作的职责。

Leave a Reply

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