第202章 Element Class|元素类
202.1 Element 从认知概念进入软件对象
在 ICAI 的认知体系中:
Real-Time Data
实时数据
↓
Element
元素
↓
Object
对象
Element 是机器接收现实信息后形成的基础认知单元。
但从第201章开始,ICAI 已经进入软件工程阶段,因此本章不再停留在:
“Element 是什么?”
而是直接解决:
Element 如何在 PHP 中成为一个真正可以运行的软件对象?
因此:
ICAI Element
ICAI 元素
↓
Element Class
元素类
↓
Element Object
元素对象实例
202.2 Element 的软件定义
一个 Element 至少需要具备:
ID
标识
Type
类型
Value
值
Source
数据来源
Timestamp
时间
State
状态
因此可以抽象成:
Element=(ID,Type,Value,Source,Timestamp,State)Element=(ID,Type,Value,Source,Timestamp,State)
这里的重点是:
Element 不是一个普通字符串或数字。
例如传感器得到:
pressure = 0.35
在普通程序中可能只是:
$pressure = 0.35;
而 ICAI 中,它首先可以成为:
Element
↓
Type = pressure
Value = 0.35
Source = sensor
Timestamp = ...
于是这个数据具备了认知系统能够继续处理的结构。
202.3 Element Class|元素类
首先建立基础 Class:
<?php
class Element
{
protected $id;
protected $type;
protected $value;
protected $source;
protected $timestamp;
protected $state;
}
这就是 ICAI 第一个真正的软件认知对象。
202.4 Constructor|构造方法
Element 创建时,需要能够直接初始化。
<?php
class Element
{
protected $id;
protected $type;
protected $value;
protected $source;
protected $timestamp;
protected $state;
public function __construct(
$id,
$type,
$value,
$source = null
) {
$this->id = $id;
$this->type = $type;
$this->value = $value;
$this->source = $source;
$this->timestamp = time();
$this->state = 'active';
}
}
于是:
$element = new Element(
1,
'pressure',
0.35,
'sensor'
);
得到:
Element Object
元素对象
ID = 1
Type = pressure
Value = 0.35
Source = sensor
Timestamp = current time
State = active
202.5 Property|元素属性
为了让其他 ICAI Class 能够读取 Element,需要提供接口。
public function getId()
{
return $this->id;
}
public function getType()
{
return $this->type;
}
public function getValue()
{
return $this->value;
}
public function getSource()
{
return $this->source;
}
public function getTimestamp()
{
return $this->timestamp;
}
public function getState()
{
return $this->state;
}
这样其他认知对象就可以读取:
$element->getType();
$element->getValue();
$element->getState();
202.6 Dynamic Value|动态值
ICAI 的 Element 最重要的特征之一是:
Element 的 Value 可以随着实时数据不断变化。
例如压力:
t0
Pressure = 0.10
t1
Pressure = 0.25
t2
Pressure = 0.40
t3
Pressure = 0.55
因此需要:
public function updateValue($value)
{
$this->value = $value;
$this->timestamp = time();
}
调用:
$element->updateValue(0.40);
对象立即变成:
Value = 0.40
Timestamp = new time
这就是实时认知数据进入对象状态的第一步。
202.7 Element 不等于 Object
这一点在工程实现中必须保持清楚。
Element
元素
↓
Object
对象
Element 是基础认知单元。
Object 是更高层的认知实体。
例如:
pressure = 0.35
可以是:
Element
而:
Egg
是:
Object
Egg 可以拥有:
Position Element
位置元素
Pressure Element
压力元素
Force Element
力度元素
Stability Element
稳定性元素
于是:
Egg Object
鸡蛋对象
│
├── Position Element
├── Pressure Element
├── Force Element
└── Stability Element
这为第203章 Object Class|对象类做好了接口基础。
202.8 Element Collection|元素集合
一个 Object 不可能只有一个 Element。
因此后续需要形成:
Object
↓
Element Collection
元素集合
例如:
$elements = array();
$elements[] = new Element(
1,
'position',
array(10, 20, 30),
'camera'
);
$elements[] = new Element(
2,
'pressure',
0.35,
'force_sensor'
);
$elements[] = new Element(
3,
'stability',
0.92,
'cognitive_engine'
);
这些 Element 最终可以被 Object 统一管理。
202.9 Element 与实时数据
因此,ICAI 的实时数据处理可以开始形成:
Real-Time Data
实时数据
↓
Element Creation
元素创建
↓
Element Instance
元素实例
↓
Element Update
元素更新
↓
Object
对象
例如传感器:
Pressure Sensor
压力传感器
↓
0.35
↓
Element
↓
pressure
↓
Egg Object
这意味着:
现实世界的原始数据开始具有软件对象身份。
202.10 Element 与数学模型
进入行为逻辑工程以后,Element 的 Value 不一定只是单个数字。
它可以是:
Scalar
标量
Vector
向量
Matrix
矩阵
Boolean
布尔值
String
字符串
Structured Data
结构化数据
例如位置:
Position=[xyz]Position= \begin{bmatrix} x\\ y\\ z \end{bmatrix}
在 PHP 中:
$position = array(
10,
20,
30
);
然后:
$positionElement = new Element(
1,
'position',
$position,
'camera'
);
于是:
Position Element
位置元素
↓
[x, y, z]
↓
Object State
对象状态
这就为后面的 Linear Algebra|线性代数进入 ICAI 行为计算留下接口。
202.11 Element 的最终 Class
本章可以暂时形成一个干净的基础版本:
<?php
class Element
{
protected $id;
protected $type;
protected $value;
protected $source;
protected $timestamp;
protected $state;
public function __construct(
$id,
$type,
$value,
$source = null
) {
$this->id = $id;
$this->type = $type;
$this->value = $value;
$this->source = $source;
$this->timestamp = time();
$this->state = 'active';
}
public function getId()
{
return $this->id;
}
public function getType()
{
return $this->type;
}
public function getValue()
{
return $this->value;
}
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();
}
public function updateState($state)
{
$this->state = $state;
$this->timestamp = time();
}
}
202.12 本章形成的工程关系
到第202章结束,ICAI 已经完成:
Real-Time Data
实时数据
↓
Element
元素
↓
Element Class
元素类
↓
Element Object
元素对象
↓
Dynamic Value
动态值
↓
Updated State
更新状态
下一层马上就是:
Element
↓
Object
因此第203章进入:
第203章 Object Class|对象类
核心解决:
Element
↓
Object
↓
Object Instance
也就是如何让多个动态 Element 组合成一个真正的 ICAI Cognitive Object(认知对象)。