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

第204章 Attribute Class|属性类

第204章 Attribute Class|属性类

属性不再只是变量,而成为可动态管理的认知对象

第203章已经完成:

Element
 ↓
Object Class
 ↓
Object Instance

现在已经能够在 PHP Runtime 中创建一个具体的认知对象。

但是,一个真正的 ICAI Object 不能只有:

ID
Type
Name

它必须知道自己当前具有什么特征,以及这些特征正在发生什么变化。

例如:

Egg Object
鸡蛋对象
 │
 ├── Position
 │   位置
 │
 ├── Distance
 │   距离
 │
 ├── Pressure
 │   压力
 │
 ├── Force
 │   力度
 │
 ├── Fragility
 │   脆弱性
 │
 └── Stability
     稳定性

因此,第204章开始建立:

Attribute
属性
 ↓
Attribute Class
属性类
 ↓
Attribute Instance
属性实例

204.1 为什么属性不能只是 PHP 变量

传统程序中可能直接写:

$position = 100;
$pressure = 0.3;
$force = 0.5;

这种方式当然可以保存数据。

但是对于 ICAI 来说,它存在一个问题:

$pressure = 0.3;

程序只知道:

这里有一个数字。

它并不知道:

这个数字是什么?
从哪里来的?
什么时候产生?
是否发生变化?
属于哪个对象?
是否可信?
是否参与当前认知?

因此 ICAI 不把重要的动态认知属性简单处理成普通变量。

而是把它提升为:

可以被识别、更新、管理和计算的软件认知对象。


204.2 Attribute|属性

ICAI 中的 Attribute 可以抽象为:

Attribute=(Name,Value,Type,Source,Time,State)Attribute=(Name,Value,Type,Source,Time,State)

也就是:

Name
属性名称

Value
属性值

Type
属性类型

Source
数据来源

Time
更新时间

State
属性状态

例如:

Pressure

Name = pressure
Value = 0.35
Type = numeric
Source = force_sensor
Time = current
State = active

这样,Pressure 就不再只是:

$pressure = 0.35;

而成为一个完整的 Attribute Instance。


204.3 Attribute Class|属性类

建立基础类:

<?php

class Attribute
{
    protected $name;
    protected $value;
    protected $type;
    protected $source;
    protected $timestamp;
    protected $state;
}

这个 Class 描述:

一个 ICAI 动态属性应该具有什么基本结构。


204.4 Constructor|构造方法

class Attribute
{
    protected $name;
    protected $value;
    protected $type;
    protected $source;
    protected $timestamp;
    protected $state;

    public function __construct(
        $name,
        $value,
        $type = null,
        $source = null
    ) {
        $this->name = $name;
        $this->value = $value;
        $this->type = $type;
        $this->source = $source;
        $this->timestamp = time();
        $this->state = 'active';
    }
}

于是可以创建:

$pressure = new Attribute(
    'pressure',
    0.35,
    'numeric',
    'force_sensor'
);

此时:

Attribute Instance
属性实例

Name      = pressure
Value     = 0.35
Type      = numeric
Source    = force_sensor
Timestamp = ...
State     = active

204.5 Attribute Value|属性值

属性最核心的是 Value。

例如:

Pressure = 0.35
Distance = 0.12
Force = 0.20
Fragility = 0.90
Stability = 0.92

但 ICAI 不限制 Value 必须是一个数字。

它可以是:

Scalar
标量

Vector
向量

Matrix
矩阵

Boolean
布尔值

String
字符串

Array
数组

Structured Value
结构化值

例如 Position:

P=[xyz]P= \begin{bmatrix} x\\ y\\ z \end{bmatrix}

PHP:

$position = new Attribute(
    'position',
    array(10, 20, 30),
    'vector',
    'camera'
);

因此:

Attribute 是值的认知载体,而不是简单的数据变量。


204.6 Dynamic Attribute|动态属性

ICAI 最重要的一点是:

属性不是静态的。

例如机械手靠近鸡蛋:

Distance

20 cm
 ↓
15 cm
 ↓
10 cm
 ↓
5 cm
 ↓
1 cm

同一个 Attribute Instance 持续更新:

$distance->updateValue(0.15);
$distance->updateValue(0.10);
$distance->updateValue(0.05);
$distance->updateValue(0.01);

所以:

Attribute
 ↓
Value(t0)
 ↓
Value(t1)
 ↓
Value(t2)
 ↓
Value(t3)

形成动态属性变化。


204.7 Update Method|属性更新方法

建立:

public function updateValue($value)
{
    $this->value = $value;
    $this->timestamp = time();
}

完整一点:

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

于是:

Real-Time Data
实时数据
 ↓
Attribute
属性
 ↓
updateValue()
 ↓
New Value
新值

204.8 Attribute 与 Element 的关系

第202章已经建立 Element。

现在需要区分:

Element
元素
 ↓
Attribute
属性

二者不能简单等同。

可以暂时理解为:

Element
=
进入认知系统的基础信息单元

而:

Attribute
=
对象所具有的、可以持续管理和计算的特征

例如实时传感器产生:

0.35

可以首先成为:

Pressure Element

经过对象认知结构组织以后:

Egg
 ↓
Pressure Attribute
 ↓
Value = 0.35

因此形成:

Real-Time Data
 ↓
Element
 ↓
Object
 ↓
Attribute

这也是第204章存在的重要意义。


204.9 Attribute 与 Object

第203章的 Object 已经拥有:

protected $elements = array();

现在开始增加属性管理:

protected $attributes = array();

并提供:

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

这样:

$egg->addAttribute($pressure);
$egg->addAttribute($force);
$egg->addAttribute($fragility);

形成:

Egg Object
鸡蛋对象
 │
 ├── Pressure Attribute
 ├── Force Attribute
 └── Fragility Attribute

204.10 Attribute Lookup|属性读取

Object 可以根据名称获取属性:

public function getAttribute($name)
{
    if (isset($this->attributes[$name])) {
        return $this->attributes[$name];
    }

    return null;
}

例如:

$pressure =
    $egg->getAttribute('pressure');

再读取:

$value =
    $pressure->getValue();

形成:

Object
 ↓
Attribute
 ↓
Value

204.11 Attribute 不只是保存数据

这是本章最重要的工程思想。

传统程序:

Variable
 ↓
Value

ICAI:

Attribute
 ↓
Identity
 ↓
Value
 ↓
Source
 ↓
Time
 ↓
State
 ↓
Update
 ↓
Calculation

所以 Attribute 自身已经成为一个可管理的软件对象

它可以:

被创建
被读取
被更新
被判断
被计算
被传递

而不是简单存放一个数字。


204.12 Attribute 与后面的 Method

这一点直接连接到你前面提出的思想:

属性就是方法参数。

例如:

Pressure
Force
Distance
Velocity
Stability
Fragility

这些 Attribute 后面都可以进入 Method。

形成:

Attribute
属性
 ↓
Current Value
当前值
 ↓
Method Parameter
方法参数
 ↓
Dynamic Calculation
动态计算
 ↓
Behavior
行为

例如:

A=M(F,P,D,S)A=M(F,P,D,S)

其中:

F = Force
P = Pressure
D = Distance
S = Stability

因此,第204章实际上为后面的:

第211章 Method Class
第212章 Method Parameters
第213章 Dynamic Method

建立了软件数据基础。


204.13 属性变化不等于重新创建对象

这是动态认知系统必须保持的特点。

例如:

Egg Object

始终是同一个对象实例。

但是:

Pressure Attribute

0.10
 ↓
0.20
 ↓
0.35
 ↓
0.40

不断变化。

因此:

Object Identity
不变

Attribute Value
持续变化

这使 ICAI 可以保持对象连续性。


204.14 多属性共同形成对象当前状态

例如:

Egg Object

Position   = [10,20,30]
Distance   = 0.02
Pressure   = 0.30
Force      = 0.25
Velocity   = [0,0,0]
Stability  = 0.91
Fragility  = 0.90

这些 Attribute 共同构成:

Current Object Information
当前对象信息

再由后面的 State Class 形成:

Object
 ↓
Attributes
 ↓
State

本章暂时不实现 State。

这里只完成 Attribute 层。


204.15 完整 Attribute Class

本章可以形成第一版基础代码:

<?php

class Attribute
{
    protected $name;
    protected $value;
    protected $type;
    protected $source;
    protected $timestamp;
    protected $state;

    public function __construct(
        $name,
        $value,
        $type = null,
        $source = null
    ) {
        $this->name = $name;
        $this->value = $value;
        $this->type = $type;
        $this->source = $source;
        $this->timestamp = time();
        $this->state = 'active';
    }

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

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

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

    public function getSource()
    {
        return $this->source;
    }

    public function getTimestamp()
    {
        return $this->timestamp;
    }

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

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

    public function updateState($state)
    {
        $this->state = $state;
        $this->timestamp = time();
    }
}

204.16 Object 中加入 Attribute

第203章的 Object 可以进一步扩展:

class Object
{
    protected $id;
    protected $type;
    protected $name;

    protected $elements = array();
    protected $attributes = array();

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

    public function getAttribute($name)
    {
        if (isset($this->attributes[$name])) {
            return $this->attributes[$name];
        }

        return null;
    }

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

于是:

Object
 │
 ├── Element
 ├── Element
 │
 ├── Attribute
 ├── Attribute
 └── Attribute

开始形成完整的对象内部结构。


204.17 本章形成的工程结构

第204章结束以后:

Real-Time Data
实时数据
      ↓
Element
元素
      ↓
Object
对象
      ↓
Attribute
属性
      ↓
Dynamic Value
动态值

具体到软件:

Element Class
      ↓
Object Class
      ↓
Attribute Class
      ↓
Object Instance
      ↓
Attribute Instance

例如:

Egg Object
      │
      ├── Position Attribute
      ├── Distance Attribute
      ├── Pressure Attribute
      ├── Force Attribute
      ├── Fragility Attribute
      └── Stability Attribute

204.18 本章核心原则

第204章正式确定:

在 ICAI 中,重要属性不是普通程序变量,而是具有名称、值、类型、来源、时间和状态,并能够持续更新和参与后续认知计算的软件认知对象。

因此:

传统程序:

Variable
 ↓
Value

而 ICAI:

Attribute Object
 ↓
Value
 ↓
Time
 ↓
Source
 ↓
State
 ↓
Update
 ↓
Method Parameter

这一步非常关键,因为后面行为工程中的:

Position
Distance
Force
Pressure
Velocity
Stability
Risk
Fragility

都可以进入同一套动态属性机制。


第204章完成

Element
元素
 ↓
Object
对象
 ↓
Attribute
属性
 ↓
Dynamic Attribute
动态属性
 ↓
Method Parameter
方法参数

至此,ICAI 已经完成了从数据单元 → 软件对象 → 动态属性对象的连续转换。

下一章进入:

第205章 Relation Class|关系类

核心不是给每个场景写程序,而是让系统能够在运行时表达:

Hand
 ↓
approaches
 ↓
Egg

Hand
 ↓
contacts
 ↓
Egg

Egg
 ↓
on
 ↓
Table

Obstacle
 ↓
near
 ↓
Egg

也就是把对象之间不断变化的现实关系正式变成可以创建、更新和计算的 Relation 对象。

Leave a Reply

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