第162章 Object类
162.1 本章提出背景
第161章建立了 Individual Class(个体类)。Individual 是 ICAI 中更高层级的运行主体,它可以拥有 Identity、Type、State、Objects、Relations、Knowledge、Goals、Capabilities、Methods 和 Behaviors。
其中,Objects 是 Individual 内部最重要的组成部分之一。
如果 Individual 是一个能够持续运行的个体,那么 Object 就是这个个体所处理、识别、关联、操作和保存的具体对象。
因此,第162章需要进一步解决一个工程问题:
一个 Object 在 PHP OOP 中究竟应该具有什么基本结构?
本章建立 Object Class 的基础模型:
Object Class → Object ID + Object Type + Attributes + State + Relations
它是 ICAI 对象工程从 Individual 层进一步进入具体对象层的重要一步。
162.2 Object Class:对象类
**Object Class(对象类)**是用于定义 ICAI 具体对象结构的基础 Class。
它规定一个 Object 至少应该具有:
Object
├── Object ID
├── Object Type
├── Attributes
├── State
└── Relations
PHP 中可以建立基础结构:
class CognitiveObject
{
protected $objectId;
protected $objectType;
protected $attributes = array();
protected $state;
protected $relations = array();
}
这里的 CognitiveObject 是 Object 的程序结构定义。
通过:
$object = new CognitiveObject();
才能形成具体的 Object Runtime Instance。
因此:
Object Class
↓
new
↓
Object Instance
Class 描述对象应该具有什么结构,Object 则代表已经进入系统运行环境的具体对象。
162.3 Object ID:对象标识
**Object ID(对象标识)**用于唯一确定一个 Object。
Object ID 与 Object Name 不应混淆。
例如:
Object ID = OBJ-000001
Object Type = egg
其中:
OBJ-000001
用于确定“是哪一个对象”。
而:
egg
用于描述“是什么类型的对象”。
因此:
Object ID → 唯一身份
Object Type → 对象分类
可以建立:
class CognitiveObject
{
protected $objectId;
public function __construct($objectId)
{
$this->objectId = $objectId;
}
public function getObjectId()
{
return $this->objectId;
}
}
例如:
$egg = new CognitiveObject('OBJ-000001');
形成:
Object
└── ID = OBJ-000001
Object ID 是对象进入数据库、Memory、Knowledge、Relation 和 Runtime 管理体系的重要基础。
162.4 Object Type:对象类型
**Object Type(对象类型)**用于描述对象属于什么类别。
例如:
egg
hand
table
person
machine
vehicle
device
PHP 中可以定义:
class CognitiveObject
{
protected $objectId;
protected $objectType;
public function setObjectType($type)
{
$this->objectType = $type;
}
public function getObjectType()
{
return $this->objectType;
}
}
例如:
$egg->setObjectType('egg');
形成:
Object
├── ID = OBJ-000001
└── Type = egg
Object ID 与 Object Type 构成对象识别的两个基础维度:
Object ID
↓
确定具体对象
Object Type
↓
确定对象类别
例如:
OBJ-000001 → egg
OBJ-000002 → egg
OBJ-000003 → hand
其中前两个对象 Type 相同,但 ID 不同。
162.5 Attributes:对象属性
**Attribute(属性)**用于描述 Object 的特征。
例如 Egg Object 可以具有:
shape = oval
color = white
material = shell
size = small
weight = 50g
程序结构可以表示为:
Object
↓
Attributes
├── shape
├── color
├── material
├── size
└── weight
PHP 中可以建立:
class CognitiveObject
{
protected $attributes = array();
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
}
public function getAttribute($name)
{
if (isset($this->attributes[$name])) {
return $this->attributes[$name];
}
return null;
}
public function getAttributes()
{
return $this->attributes;
}
}
使用:
$egg->setAttribute('shape', 'oval');
$egg->setAttribute('color', 'white');
$egg->setAttribute('size', 'small');
形成:
Egg Object
├── shape = oval
├── color = white
└── size = small
因此:
Object 定义“对象是谁”
Attribute 描述“对象具有什么特征”
162.6 Attribute与Object的结构关系
Attribute 不能脱离 Object 单独构成完整的对象认知。
例如:
shape = oval
单独存在时,系统并不知道这个 oval 属于什么。
而:
Egg
└── shape = oval
才形成完整的对象属性结构。
因此:
Object
↓
Attribute
是 ICAI 对象模型中的基本关系。
进一步:
Object
├── ID
├── Type
└── Attributes
形成对象的基本描述结构。
162.7 State:对象状态
**State(状态)**用于描述 Object 当前处于什么状态。
属性与状态必须进行区分。
例如 Egg:
Attribute:
shape = oval
color = white
这些通常描述对象的稳定特征。
而:
State:
intact
描述对象当前状态。
经过一次行为:
grasp
状态可能变成:
held
再经过:
drop
可能变成:
fallen
因此:
intact
↓
grasp
↓
held
↓
drop
↓
fallen
PHP 中:
class CognitiveObject
{
protected $state;
public function setState($state)
{
$this->state = $state;
}
public function getState()
{
return $this->state;
}
}
例如:
$egg->setState('intact');
形成:
Egg
├── Attributes
└── State = intact
状态是 Object 在运行过程中可以发生变化的重要数据。
162.8 Attribute与State的区别
这是 ICAI Object Model 中必须明确区分的一组概念。
Attribute
描述对象具有的特征。
例如:
color = white
shape = oval
material = shell
State
描述对象当前所处状态。
例如:
intact
held
broken
fallen
可以表示为:
Object
│
├── Attributes
│ ├── color
│ ├── shape
│ └── material
│
└── State
└── intact
状态可以随着行为发生变化:
State₁
↓
Behavior
↓
State₂
而对象身份通常保持不变:
Object ID = OBJ-000001
State:
intact → held → broken
因此:
Object Identity 可以保持连续,而 Object State 可以不断变化。
162.9 Relations:对象关系
**Relation(关系)**描述 Object 与其他 Object 之间的结构联系。
例如:
Hand → holds → Egg
Egg → on → Table
Person → owns → Car
Machine → controls → Device
Object 本身不能完整表达这种结构,因此需要 Relation。
可以建立:
class CognitiveObject
{
protected $relations = array();
public function addRelation($relation)
{
$this->relations[] = $relation;
}
public function getRelations()
{
return $this->relations;
}
}
关系对象本身可以进一步设计:
class Relation
{
protected $subject;
protected $type;
protected $object;
public function __construct($subject, $type, $object)
{
$this->subject = $subject;
$this->type = $type;
$this->object = $object;
}
}
例如:
Subject = Hand
Type = holds
Object = Egg
形成:
Hand
↓
holds
↓
Egg
162.10 Object的完整结构
现在可以将 Object 的五个核心组成部分统一起来:
Object
│
├── Object ID
│
├── Object Type
│
├── Attributes
│ ├── Attribute A
│ ├── Attribute B
│ └── Attribute C
│
├── State
│
└── Relations
├── Relation A
├── Relation B
└── Relation C
这可以称为 ICAI 的 Basic Object Structure(基础对象结构)。
其核心表达式为:
Object = Identity + Type + Attributes + State + Relations
其中 Identity 在程序中主要由 Object ID 承担。
162.11 Object Class完整PHP结构
将前面的结构统一起来:
class CognitiveObject
{
protected $objectId;
protected $objectType;
protected $attributes = array();
protected $state;
protected $relations = array();
public function __construct($objectId, $objectType)
{
$this->objectId = $objectId;
$this->objectType = $objectType;
}
public function getObjectId()
{
return $this->objectId;
}
public function getObjectType()
{
return $this->objectType;
}
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
}
public function getAttribute($name)
{
if (isset($this->attributes[$name])) {
return $this->attributes[$name];
}
return null;
}
public function getAttributes()
{
return $this->attributes;
}
public function setState($state)
{
$this->state = $state;
}
public function getState()
{
return $this->state;
}
public function addRelation($relation)
{
$this->relations[] = $relation;
}
public function getRelations()
{
return $this->relations;
}
}
建立一个具体对象:
$egg = new CognitiveObject(
'OBJ-000001',
'egg'
);
$egg->setAttribute('shape', 'oval');
$egg->setAttribute('color', 'white');
$egg->setAttribute('size', 'small');
$egg->setState('intact');
形成:
OBJ-000001
│
├── Type = egg
│
├── Attributes
│ ├── shape = oval
│ ├── color = white
│ └── size = small
│
├── State = intact
│
└── Relations
这已经是一个可以被 PHP Runtime 管理的具体 Object。
162.12 Object与Individual的关系
第161章已经建立:
Individual
↓
Objects
第162章进一步定义 Objects 中每一个 Object 的内部结构。
因此两章之间形成:
Individual
│
├── Identity
├── Type
├── State
│
└── Objects
│
├── Object A
│ ├── ID
│ ├── Type
│ ├── Attributes
│ ├── State
│ └── Relations
│
├── Object B
│
└── Object C
这意味着 Individual 不再只是保存一个简单的 Object 数组,而是开始拥有一个结构化的 Object System。
162.13 Object运行模型
Object 在 ICAI 中不是静态记录。
它可以随着系统运行而发生状态变化。
基本过程可以表示为:
Object → Attributes → State → Relation → Behavior → State Update
例如:
Egg Object
↓
Attributes
↓
State = intact
↓
Relation with Hand
↓
Grasp Behavior
↓
State = held
进一步:
Object
↓
当前状态
↓
关系环境
↓
行为
↓
新状态
因此 Object 成为 ICAI 动态认知系统中的一个基本运行实体。
162.14 Object、Relation与Scene
当 Individual 中存在多个 Object,并且 Object 之间形成 Relations,就可以进一步形成更加复杂的结构。
例如:
Hand → holds → Egg
Egg → on → Table
组合后:
Scene
│
├── Hand
├── Egg
├── Table
│
├── Hand → holds → Egg
└── Egg → on → Table
因此可以形成:
Objects + Relations → Scene Structure
这为后续的 Scene Class 建立基础。
162.15 Object Class在ICAI中的工程位置
Object Class 位于 Individual 与更具体认知元素之间。
可以表示为:
Individual
↓
Object System
↓
Object Class
↓
Object
├── Object ID
├── Object Type
├── Attributes
├── State
└── Relations
再向下展开:
Object
↓
Attribute
↓
State
↓
Relation
最终形成完整对象结构。
162.16 本章小结
第162章建立了 ICAI 的 Object Class(对象类)。
Object 是 Individual 所处理和关联的基本认知实体。一个基础 Object 至少包含五个核心结构:
Object ID → 确定具体对象
Object Type → 确定对象类别
Attributes → 描述对象特征
State → 描述对象当前状态
Relations → 描述对象与其他对象之间的关系
因此:
Object = ID + Type + Attributes + State + Relations
进一步形成完整的工程链:
Individual → Objects → Object → Attributes + State + Relations
而 Object 的动态运行可以表示为:
Object → State → Relation → Behavior → State Update
至此,第161章的 Individual Class 与第162章的 Object Class 已经形成上下层关系:
Individual 是个体运行主体,Object 是个体内部的基本认知对象。
下一步继续建立 Attribute Class(属性类),将 Object 内部目前的属性数组进一步提升为具有独立身份、名称、类型、值和变化机制的正式 OOP 对象,从而完成 Object → Attribute 的工程化展开。