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

第163章 Attribute类

第163章 Attribute类

163.1 本章提出背景

第162章建立了 ICAI 的 Object Class(对象类)。Object 具有 Object ID、Object Type、Attributes、State 和 Relations。

但是,如果把 Attributes 仅仅保存为:

$attributes = array();

那么属性仍然只是 Object 内部的一组数据,缺少独立的对象结构。

在 ICAI 中,属性本身也是一个需要被识别、保存、判断和更新的认知对象。因此,本章进一步建立 Attribute Class(属性类)

Attribute Class 主要解决五个问题:

属性对象 → 属性类型 → 属性值 → 属性状态 → 属性更新

由此,Object 与 Attribute 的关系从简单的数据数组:

Object
  ↓
attributes[]

提升为:

Object
  ↓
Attribute Object
  ├── Type
  ├── Value
  ├── State
  └── Update

这一步是 ICAI 从“对象具有属性”进入“属性本身也是可管理对象”的重要工程转换。


163.2 属性对象

**Attribute Object(属性对象)**是用于表示某个对象具体属性的程序对象。

例如,一个 Egg Object 可以具有:

Egg
├── Shape = oval
├── Color = white
└── Size = small

过去可以简单写成:

$egg->setAttribute('shape', 'oval');

但在正式的 OOP 模型中,可以进一步建立:

Shape Attribute
├── Type
├── Value
└── State

因此:

Egg Object
      ↓
Attribute Objects
      ├── Shape Attribute
      ├── Color Attribute
      └── Size Attribute

Attribute 不再只是一个数组键值,而成为具有自身结构的独立 Object。


163.3 Attribute Class

可以建立基础 Attribute Class:

class AttributeObject
{
    protected $type;
    protected $value;
    protected $state;

    public function __construct($type, $value)
    {
        $this->type = $type;
        $this->value = $value;
        $this->state = 'active';
    }
}

创建属性:

$shape = new AttributeObject(
    'shape',
    'oval'
);

形成:

Attribute Object
├── Type  = shape
├── Value = oval
└── State = active

因此,Attribute Class 的核心作用是:

把对象属性从普通数据值提升为具有结构和生命周期的程序对象。


163.4 属性类型

**Attribute Type(属性类型)**用于确定这个属性描述的是对象的哪一种特征。

例如:

shape
color
size
weight
material
position
temperature

可以表示:

Attribute
├── Type = shape
└── Value = oval

或者:

Attribute
├── Type = color
└── Value = white

类型与值必须分开。

例如:

shape = oval

其中:

shape → 属性类型
oval  → 属性值

而:

color = white

其中:

color → 属性类型
white → 属性值

因此基本结构为:

Attribute → Type + Value


163.5 属性值

**Attribute Value(属性值)**是属性当前实际保存的数据。

例如:

shape = oval
color = white
size = small
weight = 50

可以表示:

Attribute
├── Type  = weight
└── Value = 50

PHP 中:

class AttributeObject
{
    protected $type;
    protected $value;

    public function __construct($type, $value)
    {
        $this->type = $type;
        $this->value = $value;
    }

    public function getType()
    {
        return $this->type;
    }

    public function getValue()
    {
        return $this->value;
    }
}

使用:

$weight = new AttributeObject(
    'weight',
    50
);

形成:

Weight Attribute
├── Type  = weight
└── Value = 50

属性值是 Attribute 对象真正需要保存的具体信息。


163.6 属性状态

Attribute 不一定永远保持有效。

因此需要建立:

Attribute State(属性状态)

例如:

active
inactive
unknown
confirmed
changed
expired

例如:

Color Attribute
├── Type  = color
├── Value = white
└── State = confirmed

也可以出现:

Color Attribute
├── Type  = color
├── Value = white
└── State = unknown

这里的 State 描述的不是 Object 本身的状态,而是该属性当前的认知或运行状态

这是 Object State 与 Attribute State 的重要区别。

Object State
    ↓
对象当前处于什么状态

Attribute State
    ↓
某个属性当前处于什么状态

例如:

Egg Object
State = held

Color Attribute
State = confirmed

两个状态属于不同层级。


163.7 Attribute状态的工程意义

Attribute State 可以用于表示属性信息的当前有效程度和运行状态。

例如:

unknown
    ↓
detected
    ↓
confirmed
    ↓
changed

形成属性状态变化:

Unknown → Detected → Confirmed → Changed

例如系统最初不知道 Egg 的颜色:

Color
Value = null
State = unknown

系统获得信息以后:

Color
Value = white
State = detected

进一步确认:

Color
Value = white
State = confirmed

如果后来发现颜色发生变化:

Color
Value = brown
State = changed

因此 Attribute 本身也可以具有动态生命周期。


163.8 属性更新

**Attribute Update(属性更新)**是修改 Attribute Value 或 Attribute State 的过程。

例如:

class AttributeObject
{
    protected $type;
    protected $value;
    protected $state;

    public function updateValue($value)
    {
        $this->value = $value;
        $this->state = 'changed';
    }
}

例如:

$color = new AttributeObject(
    'color',
    'white'
);

$color->updateValue('brown');

变化过程:

color = white
      ↓
updateValue()
      ↓
color = brown

同时:

State:
confirmed
   ↓
changed

因此:

Attribute Update = Value Update + State Change


163.9 属性更新不是简单覆盖

在 ICAI 中,属性更新不能简单理解为:

$value = $newValue;

因为属性变化本身就是一种系统事件。

例如:

旧属性:
color = white

        ↓

新属性:
color = brown

系统至少应该能够确定:

属性类型:color
旧值:white
新值:brown
状态:changed

因此可以进一步建立:

Attribute Update
├── Attribute Type
├── Old Value
├── New Value
└── New State

这为后续的 Experience、Memory 和 Learning 系统提供基础。


163.10 Attribute Class完整结构

综合本章内容,可以建立:

class AttributeObject
{
    protected $type;
    protected $value;
    protected $state;

    public function __construct($type, $value = null)
    {
        $this->type = $type;
        $this->value = $value;
        $this->state = 'unknown';
    }

    public function getType()
    {
        return $this->type;
    }

    public function getValue()
    {
        return $this->value;
    }

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

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

    public function updateValue($value)
    {
        $this->value = $value;
        $this->state = 'changed';
    }
}

创建属性:

$shape = new AttributeObject(
    'shape',
    'oval'
);

$color = new AttributeObject(
    'color',
    'white'
);

形成:

Shape Attribute
├── Type  = shape
├── Value = oval
└── State = unknown

Color Attribute
├── Type  = color
├── Value = white
└── State = unknown

163.11 Attribute与Object的组合

第162章的 Object 不再直接保存普通属性值,而可以保存 Attribute Object。

例如:

class CognitiveObject
{
    protected $objectId;
    protected $objectType;
    protected $attributes = array();

    public function addAttribute(AttributeObject $attribute)
    {
        $this->attributes[] = $attribute;
    }

    public function getAttributes()
    {
        return $this->attributes;
    }
}

使用:

$egg = new CognitiveObject(
    'OBJ-000001',
    'egg'
);

$shape = new AttributeObject(
    'shape',
    'oval'
);

$color = new AttributeObject(
    'color',
    'white'
);

$egg->addAttribute($shape);
$egg->addAttribute($color);

形成:

Egg Object
│
├── Object ID = OBJ-000001
├── Object Type = egg
│
└── Attributes
    │
    ├── Shape Attribute
    │   ├── Type = shape
    │   ├── Value = oval
    │   └── State
    │
    └── Color Attribute
        ├── Type = color
        ├── Value = white
        └── State

这样 Object 与 Attribute 已经形成真正的 OOP 对象组合关系。


163.12 Object、Attribute、State的三级结构

到这里可以明确三个不同层级:

Object层

描述对象本身:

Egg
ID = OBJ-000001
Type = egg
State = intact

Attribute层

描述对象特征:

Shape
Type = shape
Value = oval
State = confirmed

Attribute Value层

保存具体属性值:

oval

因此:

Object
 ↓
Attribute
 ↓
Value

同时:

Object
 ↓
Object State

以及:

Attribute
 ↓
Attribute State

这两个 State 不应混为一谈。


163.13 属性更新模型

完整的属性更新过程可以定义为:

Current Attribute
        ↓
Read Current Value
        ↓
Receive New Value
        ↓
Compare
        ↓
Value Changed?
   ↓          ↓
 No          Yes
 ↓            ↓
保持         Update
              ↓
        State = changed
              ↓
        保存更新结果

用简化形式表示:

旧值 → 比较 → 新值 → 属性更新 → 新状态

例如:

color = white
      ↓
检测
      ↓
color = brown
      ↓
比较
      ↓
发生变化
      ↓
更新 Value
      ↓
State = changed

这一机制为后续“状态变化”和“经验形成”建立了基础。


163.14 Attribute与Knowledge的关系

Attribute 是 Object 的属性结构,而 Knowledge 可以保存关于这些 Attribute 的认知信息。

例如:

Object:
Egg

Attribute:
color = white

当系统长期保存后,可以形成:

Knowledge
    ↓
Egg
    ↓
Color
    ↓
White

因此:

Object
 ↓
Attribute
 ↓
Value
 ↓
Knowledge

当 Attribute Value 发生变化:

white
 ↓
brown

系统可以记录:

旧值
→ 新值
→ 变化时间
→ 变化状态

这就为 Learning System 的经验记录提供了对象级数据基础。


163.15 Attribute与认知匹配

属性还是 ICAI 进行对象匹配的重要基础。

例如两个对象:

Object A
color = white
shape = oval

Object B
color = white
shape = oval

系统可以对 Attribute 进行比较:

A.color ↔ B.color
A.shape ↔ B.shape

形成:

Attribute Matching
       ↓
Value Comparison
       ↓
Match / Not Match

因此:

Object Matching 的基础之一就是 Attribute Matching。

而 Attribute Class 的独立化,使这种匹配可以在正式对象层面进行,而不是依赖散乱的数组数据。


163.16 Attribute的工程定位

经过本章以后,ICAI 的对象结构进一步变成:

Individual
   ↓
Object
   ↓
Attribute
   ↓
Value

同时:

Object
├── ID
├── Type
├── Attributes
├── State
└── Relations

其中:

Attributes
   ↓
Attribute Objects
   ├── Type
   ├── Value
   └── State

这样形成了明确的对象层级。


163.17 本章小结

第163章建立了 Attribute Class(属性类)

Attribute 不再只是 Object 中的普通键值,而被提升为独立的程序对象。

其核心结构为:

Attribute = Type + Value + State + Update

其中:

属性对象 → 表示一个独立属性实体

属性类型 → 确定属性描述的类别

属性值 → 保存属性当前具体值

属性状态 → 描述属性当前状态

属性更新 → 修改属性值并形成新的属性状态

由此形成:

Individual → Object → Attribute → Value

同时建立两个重要的动态关系:

Object State → 对象状态变化

Attribute State → 属性状态变化

以及:

Old Value → Compare → New Value → Update → New State

至此,ICAI 的核心对象模型已经从:

Individual → Object

进一步展开为:

Individual → Object → Attribute

下一章可以继续建立 Relation Class(关系类),把第162章中 Object 的 Relations 从简单关系数组正式提升为独立 OOP 对象,形成:

Object → Relation → Object

从而开始建立 ICAI 的对象关系网络。

Leave a Reply

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