第160章 Method类
160.1 提出背景
在第159章中,Capability(能力)解决了个体是否具有某项能力的问题。
但是,拥有能力并不意味着已经确定了具体的执行方式。
例如,一个个体具有:
grasp
能力,只能说明个体具有抓取对象的能力。
但是,当目标确定以后,系统还必须进一步回答:
“应该采用什么方法完成这个目标?”
例如,对于一个杯子,可以存在:
双指抓取
掌部抓取
侧面抓取
这些都可能属于抓取能力下的不同Method。
因此:
Capability解决“能不能做”,Method解决“怎么做”。
Method是连接Capability与实际执行过程的重要工程对象。
其基本运行过程可以表示为:
Capability → Method → Condition → Process → Action → Result
其中:
- Capability确定能力范围;
- Method确定执行方法;
- Condition确定方法成立的条件;
- Process确定执行过程;
- Action产生具体动作;
- Result产生实际结果。
因此,本章建立 Method类,将方法从抽象概念转换为可以被系统保存、匹配、选择、执行和验证的PHP OOP运行对象。
160.2 Method概念定义
Method(方法)是个体利用已有Capability,在满足一定Condition的情况下,通过规定的Process组织Action,以获得预期Result的结构化执行方法。
可以定义:
Method=Type+Condition+Process+Action+ResultMethod = Type + Condition + Process + Action + Result
因此,一个完整Method对象可以表示为:
M=(T,C,P,A,R)M=(T,C,P,A,R)
其中:
M:Method,方法;T:Method Type,方法类型;C:Condition,执行条件;P:Process,执行过程;A:Action,动作;R:Result,结果。
Method并不是单个Action。
例如:
Method = grasp_method_01
可能包含:
Process
↓
定位对象
↓
接近对象
↓
建立接触
↓
闭合手部
↓
确认对象被持有
因此:
Method是一个结构化执行方案,Action是其中的实际动作单元。
160.3 Method与Capability的区别
Capability与Method具有直接关系,但二者不能混淆。
Capability:
grasp
表示:
具有抓取能力
Method:
two_finger_grasp
表示:
采用双指抓取方法
因此:
Capability
↓
可使用的方法集合
↓
Method Selection
↓
具体Method
可以形式化表示:
Capability→{M1,M2,…,Mn}Capability \rightarrow \{M_1,M_2,…,M_n\}
一个Capability可以对应多个Method。
例如:
Capability: grasp
↓
┌──────┼────────┐
↓ ↓ ↓
M1 M2 M3
双指 掌部 侧面
抓取 抓取 抓取
因此,Capability定义能力空间,Method定义执行路径。
160.4 Method Type
Method Type(方法类型)用于确定方法属于哪一种方法。
例如:
two_finger_grasp
palm_grasp
side_grasp
direct_move
step_move
object_identify
value_compare
data_store
data_retrieve
Method Type应该具有明确的系统标识。
例如:
$method = new Method('two_finger_grasp');
系统可以通过Method Type进行查询:
Required Capability
↓
Method Type
↓
Method Repository
↓
Candidate Methods
这使Method可以参与后续的方法选择。
160.5 Condition
Condition(方法条件)是Method能够开始执行或继续执行所必须满足的条件。
虽然Capability也具有Condition,但是二者的作用不同。
Capability Condition回答:
“能力在什么条件下成立?”
Method Condition回答:
“这个具体方法在什么条件下可以执行?”
例如:
Capability:
grasp
Capability Condition:
hand_available = true
而具体Method:
two_finger_grasp
可能要求:
object_visible = true
object_accessible = true
object_width >= 1cm
object_width <= 8cm
因此:
Capability Condition
↓
Capability Available
↓
Method Condition
↓
Method Executable
二者不能合并为一个简单的条件。
160.6 Method Condition对象化
Method Condition应该作为独立对象进入系统。
例如:
class MethodCondition
{
protected $type;
protected $operator;
protected $value;
public function __construct($type, $operator, $value)
{
$this->type = $type;
$this->operator = $operator;
$this->value = $value;
}
public function getType()
{
return $this->type;
}
public function getOperator()
{
return $this->operator;
}
public function getValue()
{
return $this->value;
}
public function check($context)
{
if (!isset($context[$this->type])) {
return false;
}
switch ($this->operator) {
case '=':
return $context[$this->type] == $this->value;
case '!=':
return $context[$this->type] != $this->value;
case '>':
return $context[$this->type] > $this->value;
case '<':
return $context[$this->type] < $this->value;
case '>=':
return $context[$this->type] >= $this->value;
case '<=':
return $context[$this->type] <= $this->value;
}
return false;
}
}
这样Condition就从静态文字转换成为可执行的判断对象。
运行过程:
Context
↓
MethodCondition
↓
Operator
↓
Compare
↓
true / false
160.7 Process
Process(过程)是Method内部Action按照一定顺序执行所形成的结构化执行流程。
Method回答:
采用什么方法?
Process回答:
按照什么顺序执行?
例如:
Method:
two_finger_grasp
对应Process:
1. 定位对象
2. 接近对象
3. 调整位置
4. 建立接触
5. 闭合手指
6. 确认持有
因此:
Process={A1,A2,…,An}Process = \{A_1,A_2,…,A_n\}
其中每一个:
A_i
都是一个Action。
所以:
Process是Action的有序集合。
160.8 Process的顺序关系
Action不是随机执行的。
例如:
定位
↓
接近
↓
接触
↓
抓取
↓
确认
如果改变顺序:
抓取
↓
接近
则过程就失去意义。
因此Process必须保存Action的顺序。
可以定义:
class MethodProcess
{
protected $actions;
public function __construct()
{
$this->actions = array();
}
public function addAction($action)
{
$this->actions[] = $action;
}
public function getActions()
{
return $this->actions;
}
}
于是:
MethodProcess
├── Action 1
├── Action 2
├── Action 3
├── Action 4
└── Action 5
形成一个明确的离散执行序列。
160.9 Action
Action(动作)是Process中可以被实际执行的最小执行单元之一。
例如:
move
open
close
touch
grasp
release
store
retrieve
compare
Action不同于Method。
例如:
Method:
two_finger_grasp
包含:
Action:
move
touch
close
hold
因此:
Method
↓
Process
↓
Action
Action是执行层的基本单位。
160.10 Action对象化
Action可以建立独立的PHP对象。
class Action
{
protected $type;
protected $parameters;
protected $state;
protected $result;
public function __construct($type, $parameters)
{
$this->type = $type;
$this->parameters = $parameters;
$this->state = 'pending';
$this->result = null;
}
public function getType()
{
return $this->type;
}
public function getParameters()
{
return $this->parameters;
}
public function setState($state)
{
$this->state = $state;
}
public function getState()
{
return $this->state;
}
public function setResult($result)
{
$this->result = $result;
}
public function getResult()
{
return $this->result;
}
}
例如:
$action = new Action(
'move',
array(
'target' => 'cup'
)
);
此时Action已经成为一个独立运行对象。
160.11 Result
Result(结果)是Method执行后产生的实际结果。
结果不能被预先假定为成功。
例如:
Method:
two_finger_grasp
执行以后可能产生:
object_held
也可能产生:
object_dropped
或者:
object_not_reached
因此:
Result=f(Process,Action,Environment)Result = f(Process, Action, Environment)
结果是执行过程的实际输出。
完整关系为:
Method
↓
Process
↓
Action
↓
Execution
↓
Result
160.12 Result对象化
Result也可以建立独立对象。
class MethodResult
{
protected $status;
protected $value;
protected $message;
public function __construct($status, $value, $message)
{
$this->status = $status;
$this->value = $value;
$this->message = $message;
}
public function getStatus()
{
return $this->status;
}
public function getValue()
{
return $this->value;
}
public function getMessage()
{
return $this->message;
}
}
例如:
$result = new MethodResult(
'success',
'object_held',
'Object grasped successfully'
);
系统因此得到:
Result
Status = success
Value = object_held
Message = Object grasped successfully
160.13 Method类
综合前面的结构,可以建立Method类:
class Method
{
protected $type;
protected $conditions;
protected $process;
protected $actions;
protected $result;
protected $state;
public function __construct($type)
{
$this->type = $type;
$this->conditions = array();
$this->process = null;
$this->actions = array();
$this->result = null;
$this->state = 'inactive';
}
public function getType()
{
return $this->type;
}
public function addCondition($condition)
{
$this->conditions[] = $condition;
}
public function setProcess($process)
{
$this->process = $process;
}
public function addAction($action)
{
$this->actions[] = $action;
}
public function setResult($result)
{
$this->result = $result;
}
public function getResult()
{
return $this->result;
}
public function setState($state)
{
$this->state = $state;
}
public function getState()
{
return $this->state;
}
public function checkConditions($context)
{
foreach ($this->conditions as $condition) {
if (!$condition->check($context)) {
return false;
}
}
return true;
}
}
因此Method对象形成:
Method
├── Type
├── Conditions
├── Process
├── Actions
├── Result
└── State
其中Process与Actions在工程上可以进一步统一设计。
更严格的结构可以采用:
Method
↓
Process
↓
Actions
而不是同时由Method直接管理Process和Actions。
因此正式工程实现时推荐:
Method
├── Type
├── Conditions
├── Process
├── Result
└── State
Process
└── Actions
这样对象关系更加清晰。
160.14 Method执行条件
Method执行前必须检查条件。
例如:
$context = array(
'object_visible' => true,
'object_accessible' => true,
'object_width' => 5
);
系统执行:
if ($method->checkConditions($context)) {
$method->setState('ready');
}
于是:
Method
↓
Condition Check
↓
All Conditions True?
↓
Yes
↓
Method Ready
如果存在一个条件不满足:
Method
↓
Condition Check
↓
Condition Failed
↓
Method Blocked
因此Method不能跳过Condition直接进入Action。
160.15 Process执行
当Method条件满足以后,Process才可以开始。
例如:
Method Ready
↓
Process Start
↓
Action 1
↓
Action 2
↓
Action 3
↓
Action 4
↓
Process Complete
PHP可以建立:
public function execute()
{
if ($this->process == null) {
return false;
}
$this->state = 'running';
return $this->process->execute();
}
Process则可以依次执行Action:
class MethodProcess
{
protected $actions;
public function __construct()
{
$this->actions = array();
}
public function addAction($action)
{
$this->actions[] = $action;
}
public function execute()
{
foreach ($this->actions as $action) {
$action->setState('running');
/*
* Action实际执行逻辑
*/
$action->setState('completed');
}
return true;
}
}
这里的执行逻辑最终需要连接实际的Execution层,而不能停留在对象定义层。
160.16 Action执行失败
Process中的某个Action可能失败。
例如:
Action 1 → success
Action 2 → success
Action 3 → failed
此时Process不能继续假设后续Action全部成功。
应该:
Action 3 Failed
↓
Process Stop
↓
Method Failed
↓
Result Generated
因此:
ProcessSuccess=A1∧A2∧…∧AnProcessSuccess = A_1 \land A_2 \land … \land A_n
如果任何一个必要Action失败:
ProcessSuccess=falseProcessSuccess=false
这是一种离散执行规则。
160.17 Method State
Method State(方法状态)表示Method当前处于什么执行状态。
例如:
inactive
ready
running
completed
failed
blocked
cancelled
运行过程:
Inactive
↓
Condition Check
↓
Ready
↓
Running
↓
Completed
失败时:
Running
↓
Action Failed
↓
Failed
条件不满足时:
Inactive
↓
Condition Failed
↓
Blocked
Method State可以因此反映Method实际执行生命周期。
160.18 Method与Result关系
Method不能直接声明:
Result = success
因为Result必须来自实际执行。
正确过程是:
Method
↓
Process
↓
Action
↓
Actual Execution
↓
Actual Result
例如:
Method:
two_finger_grasp
实际结果:
object_held
则:
Result Status = success
如果:
object_dropped
则:
Result Status = failed
因此:
Result≠ExpectedResultResult \neq ExpectedResult
Expected Result是预期结果,Actual Result是实际结果。
二者必须进行比较。
160.19 Expected Result与Actual Result
Method可以保存Expected Result(预期结果)。
例如:
Expected Result:
object_held
执行之后:
Actual Result:
object_dropped
系统进行:
Expected Result
↓
Actual Result
↓
Compare
↓
Verification
如果:
Expected = Actual
则:
Method Verification = passed
否则:
Method Verification = failed
因此,Result不仅用于告诉系统“发生了什么”,还用于验证Method是否有效。
160.20 Method选择
一个Capability可能具有多个Method,因此需要Method Selection(方法选择)。
例如:
Capability:
grasp
Candidate Methods:
M1 = two_finger_grasp
M2 = palm_grasp
M3 = side_grasp
系统根据当前Condition与Range进行筛选:
Capability
↓
Candidate Methods
↓
Condition Matching
↓
Range Matching
↓
Method Availability
↓
Method Selection
选择不是随机行为。
它应该基于明确规则:
Condition满足
+
Range满足
+
Method状态允许
+
历史验证有效
才能进入候选执行集合。
160.21 Method与Capability的完整关系
可以建立:
Capability
↓
Capability Range
↓
Method Candidates
↓
Method Condition
↓
Method Selection
↓
Method Process
↓
Action
↓
Result
因此:
Capability提供能力空间,Method提供执行路径,Process提供执行顺序,Action提供执行单元,Result提供实际输出。
这五者形成一个连续的工程结构。
160.22 Method Manager
当系统存在大量Method时,需要使用MethodManager进行管理。
class MethodManager
{
protected $methods;
public function __construct()
{
$this->methods = array();
}
public function addMethod($method)
{
$this->methods[] = $method;
}
public function findByType($type)
{
foreach ($this->methods as $method) {
if ($method->getType() == $type) {
return $method;
}
}
return null;
}
}
进一步可以建立:
MethodManager
├── grasp_method_01
├── grasp_method_02
├── move_method_01
├── move_method_02
└── identify_method_01
Manager负责:
Method Registration
Method Search
Method Retrieval
Method Selection
而不是负责实际Action执行。
这样可以保持职责分离。
160.23 Method与Knowledge的关系
Method本身也可以成为Knowledge的一部分。
例如系统通过实际运行得到:
Method:
two_finger_grasp
并发现:
weight <= 2kg
时成功率稳定。
系统可以形成结构化Knowledge:
Method
↓
Condition
↓
Result
↓
Experience
↓
Knowledge
因此Method不是孤立对象。
它可以与Knowledge、Experience形成反馈关系:
Knowledge
↓
Method Selection
↓
Execution
↓
Result
↓
Experience
↓
Knowledge Update
这使方法能够随着实际运行结果进行离散规则更新。
160.24 Method与Experience
Experience(经验)记录Method实际执行后的历史结果。
例如:
Method = two_finger_grasp
Object = cup
Weight = 1kg
Result = success
经过多次实际运行以后,可以形成:
Experience
Method = two_finger_grasp
ObjectType = cup
WeightRange = 0~2kg
Result = success
如果某种条件下反复失败:
Method
↓
Repeated Failure
↓
Experience
↓
Method State / Condition Update
例如原来的范围:
weight <= 2kg
实际运行发现:
weight > 1.5kg
经常失败。
系统可以根据明确规则更新方法适用范围。
这不是生成式学习,而是:
Result → Record → Compare → Rule → Update
160.25 Method数据库映射
Method可以映射到MySQL数据库。
例如:
CREATE TABLE methods (
id INT NOT NULL AUTO_INCREMENT,
method_type VARCHAR(100) NOT NULL,
method_state VARCHAR(50) NOT NULL,
expected_result VARCHAR(255),
created_at DATETIME,
updated_at DATETIME,
PRIMARY KEY (id)
);
Method条件:
CREATE TABLE method_conditions (
id INT NOT NULL AUTO_INCREMENT,
method_id INT NOT NULL,
condition_type VARCHAR(100) NOT NULL,
operator VARCHAR(20) NOT NULL,
condition_value VARCHAR(255),
PRIMARY KEY (id)
);
Process:
CREATE TABLE method_processes (
id INT NOT NULL AUTO_INCREMENT,
method_id INT NOT NULL,
process_name VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
Action:
CREATE TABLE method_actions (
id INT NOT NULL AUTO_INCREMENT,
process_id INT NOT NULL,
action_type VARCHAR(100) NOT NULL,
action_order INT NOT NULL,
action_parameters TEXT,
PRIMARY KEY (id)
);
Result:
CREATE TABLE method_results (
id INT NOT NULL AUTO_INCREMENT,
method_id INT NOT NULL,
result_status VARCHAR(50) NOT NULL,
result_value TEXT,
created_at DATETIME,
PRIMARY KEY (id)
);
于是形成:
methods
↓
method_conditions
↓
method_processes
↓
method_actions
↓
method_results
这与PHP OOP结构形成对应:
Method
MethodCondition
MethodProcess
Action
MethodResult
MethodManager
160.26 Method完整运行模型
综合本章内容,Method的完整运行过程可以表示为:
Capability → Candidate Methods → Condition → Method Selection → Process → Action → Result
结果产生以后:
Result → Expected Result Comparison → Verification → Method State
再进入经验:
Verification → Experience → Knowledge Update → Future Method Selection
完整闭环:
Capability
↓
Method Candidates
↓
Condition
↓
Method Selection
↓
Process
↓
Action
↓
Actual Execution
↓
Result
↓
Expected / Actual Comparison
↓
Verification
↓
Method State
↓
Experience
↓
Knowledge Update
160.27 Method在个体认知架构中的位置
结合前面已经建立的Individual、Object、Attribute、State、Relation、Scene、Knowledge、Need、Goal、Capability,可以形成更加完整的认知执行结构:
Individual
↓
Knowledge
↓
Need
↓
Goal
↓
Capability
↓
Method
↓
Condition
↓
Process
↓
Action
↓
Result
↓
Verification
↓
Experience
↓
Knowledge Update
其中:
Goal决定需要达到什么目标。
Capability判断是否具有完成目标的能力。
Method确定采用什么方法。
Condition判断方法是否可以执行。
Process确定执行顺序。
Action执行具体动作。
Result记录实际结果。
Verification判断执行是否成功。
Experience保存历史执行经验。
这就形成了从目标到执行再回到认知结构的完整工程闭环。
160.28 Method类的核心判定模型
可以将Method是否能够执行定义为:
E=C∧P∧AE = C \land P \land A
其中:
E:Method Executable,方法可执行;C:Condition满足;P:Process有效;A:Action集合有效。
如果三者均成立:
E=trueE=true
则Method可以进入执行阶段。
而Method执行成功可以进一步定义为:
S=Ps∧As∧(Ra=Re)S = P_s \land A_s \land (R_a = R_e)
其中:
S:Method Success;P_s:Process成功;A_s:必要Action成功;R_a:Actual Result,实际结果;R_e:Expected Result,预期结果。
只有:
Process成功
+
Action成功
+
实际结果符合预期
才可以将Method判定为成功。
160.29 Method与Action的层级关系
必须特别明确Method、Process和Action的层级。
它们不是三个平行概念。
正确关系是:
Method
↓
Process
↓
Action
Method是方法整体。
Process是方法内部的执行过程。
Action是过程中的具体动作。
例如:
Method:two_finger_grasp
↓
Process
↓
定位对象
↓
接近对象
↓
建立接触
↓
闭合手指
↓
确认持有
因此:
Method=Process(Action1,Action2,…,Actionn)Method = Process(Action_1,Action_2,…,Action_n)
Method通过Process组织多个Action,最终形成完整执行路径。
160.30 本章总结
Method类建立了个体“如何完成任务”的工程表达。
本章建立五个核心结构:
Method
├── Condition
├── Process
├── Action
└── Result
其中:
Method定义具体执行方法。
Condition定义方法执行的前提条件。
Process定义Action的执行顺序。
Action定义实际执行单元。
Result定义实际执行结果。
Capability与Method的关系可以总结为:
Capability回答“能不能做”,Method回答“怎么做”。
Method内部则形成:
Method → Condition → Process → Action → Result
执行完成以后形成:
Result → Verification → Experience → Knowledge Update
因此,第159章建立的Capability与本章建立的Method形成连续的工程结构:
Goal → Capability → Method → Process → Action → Result
这意味着个体认知系统已经从:
“是否具有能力”
进一步进入:
“如何调用能力完成目标”
的执行阶段。
后续的 Behavior类 则可以进一步解决一个更具体的问题:
Method确定执行方法以后,实际运行过程中形成的行为是什么,以及行为如何产生、变化、结束并反馈结果。