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

第203章 Object Class|对象类

第203章 Object Class|对象类

203.1 从 Element 到 Object

第202章已经完成了:

Real-Time Data
实时数据
 ↓
Element
元素
 ↓
Element Object
元素对象

但一个现实对象通常不会由一个 Element 构成。

例如一个鸡蛋:

Egg
鸡蛋
 │
 ├── Position Element
 │   位置元素
 │
 ├── Orientation Element
 │   姿态元素
 │
 ├── Mass Element
 │   质量元素
 │
 ├── Fragility Element
 │   脆弱性元素
 │
 ├── Pressure Element
 │   压力元素
 │
 ├── Force Element
 │   力度元素
 │
 └── Stability Element
     稳定性元素

因此,本章开始建立第二层软件对象:

Element
元素
 ↓
Object
对象
 ↓
Object Instance
对象实例

核心变化是:

Element 是认知数据单元,Object 是能够组织这些 Element 的认知实体。


203.2 Object|对象

ICAI 中的 Object 不是简单的 PHP 数据容器。

它代表机器当前认知中的一个现实对象实体

例如:

Egg
鸡蛋

Hand
机械手

Table
桌面

Obstacle
障碍物

每一个对象都可以拥有自己的:

Identity
身份

Elements
元素

Attributes
属性

State
状态

因此可以表示为:

Object=Identity+Elements+StateObject = Identity + Elements + State

而后面的 Relation 会进一步负责:

Object
 ↓
Relation
 ↓
Object

因此当前阶段暂时不把 Relation 混入 Object 本身。


203.3 Object 与 Element 的关系

最重要的结构是:

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

例如:

Egg Object
鸡蛋对象
 │
 ├── position
 ├── orientation
 ├── mass
 ├── fragility
 ├── pressure
 ├── force
 └── stability

所以:

Object={Element1,Element2,…,Elementn}Object=\{Element_1,Element_2,\ldots,Element_n\}

Object 并不是把 Element 简单堆在一起。

它负责:

Create
创建

Store
保存

Find
查找

Update
更新

Remove
删除

Evaluate
评价

这些 Element。


203.4 Object Class|对象类

首先建立基础 Object

<?php

class CognitiveObject
{
    protected $id;
    protected $type;
    protected $name;
    protected $elements = array();
    protected $state;

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

这里:

$id
对象身份

$type
对象类型

$name
对象名称

$elements
对象包含的元素

$state
对象当前状态

203.5 Add Element|加入元素

Object 必须能够动态加入 Element。

因此:

public function addElement(Element $element)
{
    $this->elements[$element->getType()] = $element;
}

例如:

$egg = new CognitiveObject(
    1,
    'physical_object',
    'egg'
);

然后加入元素:

$egg->addElement(
    new Element(
        1,
        'position',
        array(10, 20, 30),
        'camera'
    )
);

$egg->addElement(
    new Element(
        2,
        'pressure',
        0,
        'sensor'
    )
);

$egg->addElement(
    new Element(
        3,
        'fragility',
        0.9,
        'cognitive'
    )
);

此时:

Egg Object
鸡蛋对象
 │
 ├── Position Element
 ├── Pressure Element
 └── Fragility Element

已经真正存在于 PHP Runtime 中。


203.6 Get Element|读取元素

Object 需要根据类型取得 Element。

public function getElement($type)
{
    if (isset($this->elements[$type])) {
        return $this->elements[$type];
    }

    return null;
}

例如:

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

得到的是:

Pressure Element
压力元素

再读取:

$value =
    $pressure->getValue();

得到:

0

因此:

Object
 ↓
Element
 ↓
Value

成为一个完整的数据读取链。


203.7 Update Element|更新元素

现实世界是动态的,因此 Object 中的 Element 必须能够实时更新。

例如机械手接触鸡蛋以后:

Pressure
0
 ↓
0.15
 ↓
0.25
 ↓
0.35

可以直接:

$egg
    ->getElement('pressure')
    ->updateValue(0.35);

于是:

Egg Object
 ↓
Pressure Element
 ↓
Value = 0.35

对象的当前认知状态就发生了变化。


203.8 Object State|对象状态

Object 不能只有 Element,还需要形成自己的整体 State。

例如:

Egg
 ↓
Position = ...
Pressure = 0.35
Force = 0.20
Contact = true
Stability = 0.92

这些 Element 当前值共同构成:

Egg Current State
鸡蛋当前状态

因此:

State(Objectt)=F(Element1,…,Elementn)State(Object_t) = F(Element_1,\ldots,Element_n)

这里的 State 并不是另一个静态标签。

它是:

对象当前所有相关动态属性共同形成的当前状态。


203.9 Object Instance|对象实例

Class 定义对象结构:

class CognitiveObject
{
}

真正进入认知系统的是 Object Instance:

$egg = new CognitiveObject(
    1,
    'physical_object',
    'egg'
);

于是:

CognitiveObject Class
认知对象类
        ↓
$egg
鸡蛋对象实例

再创建机械手:

$hand = new CognitiveObject(
    2,
    'device',
    'robot_hand'
);

以及桌面:

$table = new CognitiveObject(
    3,
    'environment',
    'table'
);

此时系统已经拥有:

Object Instance
对象实例
 │
 ├── Egg
 ├── Robot Hand
 └── Table

这就是后面建立 Relation 的基础。


203.10 不为每个现实对象写一个程序

这里必须明确一个 ICAI 工程原则。

不能这样设计:

Egg.php
Glass.php
Cup.php
Fruit.php
Ceramic.php

然后每一种对象都写一套行为逻辑。

这会重新陷入:

一个对象一个程序。

ICAI 应该采用:

CognitiveObject
      ↓
Object Instance
      ↓
Dynamic Elements

因此:

Egg
Glass
Cup
Fruit
Ceramic

都可以成为:

CognitiveObject

的不同实例。

它们的区别主要来自:

Type
+
Elements
+
Attributes
+
State

而不是来自完全不同的程序。


203.11 Object 与动态行为

这一步开始与后面的 Behavior 工程连接起来。

例如:

Egg Object
 ↓
Pressure = 0.35
Force = 0.20
Distance = 0.05
Stability = 0.92
Contact = true

这些状态以后进入:

Cognition
 ↓
Method
 ↓
Behavior

因此 Object 本身不负责决定怎么抓鸡蛋

它负责提供:

当前对象是什么,以及当前对象处于什么状态。

这就保持了软件架构层次的清晰:

Object
对象
 ↓
提供当前事实

Cognition
认知
 ↓
理解当前事实

Method
方法
 ↓
计算应该怎么做

Behavior
行为
 ↓
形成持续行为

Action
动作
 ↓
具体执行

203.12 Object Class 完整基础版本

第203章可以形成这样的基础 Class:

<?php

class CognitiveObject
{
    protected $id;
    protected $type;
    protected $name;
    protected $elements = array();
    protected $state;

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

    public function getId()
    {
        return $this->id;
    }

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

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

    public function addElement(Element $element)
    {
        $this->elements[$element->getType()] = $element;
    }

    public function getElement($type)
    {
        if (isset($this->elements[$type])) {
            return $this->elements[$type];
        }

        return null;
    }

    public function getElements()
    {
        return $this->elements;
    }

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

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

203.13 本章完成的转换

到这里,ICAI 已经从:

Element
元素

进入:

Object
对象

形成:

Real-Time Data
实时数据
      ↓
Element
元素
      ↓
Element Object
元素对象
      ↓
CognitiveObject
认知对象
      ↓
Object Instance
对象实例

一个 Object 可以拥有:

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

然后这些动态 Element 共同形成:

Object State
对象状态

最终进入下一层:

Object
 ↓
Relation
 ↓
State
 ↓
Scene

第203章核心结论

ICAI Object Class 的作用,不是为现实中的每一种物体编写一个独立程序,而是建立一个通用的认知对象容器,使不同现实对象能够通过动态 Element、Attribute 和 State 进入同一个认知运行时。

因此到第203章结束,核心链已经变成:

Element
元素
 ↓
Object Class
对象类
 ↓
Object Instance
对象实例
 ↓
Dynamic Elements
动态元素
 ↓
Current State
当前状态

下一章进入 第204章 Attribute Class|属性类

这一章将进一步解决一个非常关键的问题:

Object 里面的 Attribute 到底应该只是普通变量,还是应该成为可以动态变化、被计算、被感知、被 Method 直接使用的软件认知属性对象?

这正好接上你前面提出的核心思想:

Attribute
属性
 ↓
Method Parameter
方法参数
 ↓
Dynamic Calculation
动态计算
 ↓
Behavior
行为

Leave a Reply

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