第30章 Object
本章大纲
- Object 定义
- Object ID
- Object Type
- Object Property
- Object Method
- Object State
- Object Relation
- Object 生命周期
- Object Manager
- 完整 Object 示例
30.1 Object 定义
在 SAI Framework 中,Object(对象)是由一个或多个 Element 组织形成的、具有明确身份、属性、状态、方法以及关系的可识别实体。
简单来说:
Element
↓
组织
↓
Object
Element 是基础信息单元,Object 则是具有完整结构的实体。
例如场景中获取到:
Element
├── person
├── present
├── living-room
└── moving
这些元素经过组织后,可以形成:
Object
Person
├── Property
│ └── location = living-room
│
├── State
│ └── present
│
└── Method
└── move()
因此:
Element → 描述局部信息
Object → 组织成为完整实体
Object 在 SAI 中的位置
Information
↓
Perception
↓
Element
↓
Object
↓
Property / State / Method / Relation
↓
Cognition
Object 是从元素进入对象化认知的重要一步。
Object 不等于 PHP Object
这里必须区分两个概念。
PHP 中:
$person = new Person();
这里的 $person 是 PHP 运行时对象。
而 SAI Framework 中:
Object = Person
表示 SAI 对现实世界中“人”这一实体的对象化描述。
所以:
SAI Object
↓
由 PHP Class 实现
而不是:
PHP Object = SAI Object
PHP OOP 是实现机制,SAI Object 是系统中的认知对象模型。
30.2 Object ID
Object ID 是对象的唯一标识。
例如:
person-001
fan-001
room-001
sensor-001
vehicle-001
Object ID 的主要作用是:
唯一识别
对象查找
对象关联
对象更新
对象删除
对象关系建立
对象状态跟踪
例如:
Object ID = fan-001
表示:
这是哪一个风扇?
→ fan-001
Object ID 与 Element ID
二者不能混淆。
Element ID
element-000001
Object ID
fan-001
例如:
Element
ID = element-000001
type = state
value = off
Object
ID = fan-001
type = fan
state = off
关系:
Element
│
│ describes
↓
Object
Element ID 标识元素。
Object ID 标识对象。
Object ID 的基本要求
一个 Object ID 应当:
唯一
稳定
可引用
可关联
不要把动态属性直接作为 ID。
例如:
fan-001
可以。
但是:
fan-off
fan-32-degree
fan-living-room
不适合作为稳定 Object ID,因为这些信息可能发生变化。
30.3 Object Type
Object Type 用于描述对象属于什么类型。
例如:
person
fan
room
sensor
vehicle
robot
door
machine
对象可以具有层级类型:
Object
├── Device
│ ├── Sensor
│ ├── Fan
│ └── Camera
│
├── Person
│
├── Vehicle
│
└── Environment
例如:
Object
ID = fan-001
Type = fan
表示:
fan-001 是一个 fan 类型对象。
Type 与 Property 的区别
例如:
Object
ID = fan-001
Type = fan
Property
speed = 1200
power = 50
location = living-room
其中:
fan
是对象类型。
而:
speed
power
location
是对象属性。
因此:
Type → 它是什么
Property → 它有什么特征
30.4 Object Property
Property(属性)用于描述 Object 的特征。
例如:
Fan
├── name
├── speed
├── power
├── location
└── manufacturer
可以表示:
fan-001
type = fan
properties:
name = Living Room Fan
speed = 1200
power = 50
location = living-room
Property 的基本结构
可以抽象为:
Property
{
name
value
type
state
source
}
例如:
Property
name = temperature
value = 32
type = number
state = valid
source = sensor-001
Property 与 Element
Property 本身可以由 Element 构成。
例如:
Element
type = property
value = temperature
Element
type = value
value = 32
组织后:
Object: Room
Property:
temperature = 32
所以可以形成:
Element
↓
Property
↓
Object
Property 的变化
对象运行过程中,属性可能发生变化:
temperature = 32
之后:
temperature = 31
对象仍然是:
room-001
只是属性发生了更新。
因此:
Object ID
不变
Property Value
可以变化
30.5 Object Method
Method(方法)描述 Object 能够执行的操作。
例如:
Fan
├── turnOn()
├── turnOff()
└── setSpeed()
门:
Door
├── open()
├── close()
└── lock()
机器人:
Robot
├── move()
├── stop()
├── turn()
└── charge()
Method 的基本结构
可以抽象为:
Method
{
name
parameters
return
state
}
例如:
Method
name = setSpeed
parameters = speed
return = state
调用:
fan-001.setSpeed(1200)
产生:
speed = 1200
Object Method 与 Behavior
二者也必须区分。
Object Method
→ 对象具有什么操作能力
Behavior
→ Individual 决定采取什么行为
例如:
Fan
Method:
turnOn()
并不代表 SAI 一定要打开风扇。
可能是:
Cognition
↓
Reasoning
↓
Decision
↓
Behavior
↓
调用 Fan.turnOn()
所以:
Method = 可以做什么
Behavior = 实际采取什么行为
30.6 Object State
State(状态)描述 Object 当前处于什么状态。
例如风扇:
fan-001
state = off
启动以后:
fan-001
state = on
机器人:
robot-001
state = moving
停止以后:
robot-001
state = stopped
State 与 Property
State 可以被看作 Object 的一种特殊动态描述,但在 SAI Framework 中建议将 State 独立管理。
例如:
Fan
Property:
speed = 1200
power = 50
location = living-room
State:
power_state = on
connection = online
区别:
Property → 对象特征
State → 对象当前运行状态
State 的变化
对象生命周期中:
off
↓
starting
↓
on
↓
stopping
↓
off
因此 State 是动态的。
SAI 可以通过状态变化产生事件:
StateChanged
然后进入:
Information
→ Perception
→ Cognition
形成新的处理循环。
30.7 Object Relation
Object 不会孤立存在。
多个 Object 之间可以建立 Relation(关系)。
例如:
Person
│
│ located_at
↓
LivingRoom
风扇:
Fan
│
│ located_at
↓
LivingRoom
传感器:
TemperatureSensor
│
│ detects
↓
Temperature
因此:
Object
↓
Relation
↓
Object
Relation 的基本结构
SAI 可以采用:
Subject
Relation
Object
例如:
fan-001
located_at
room-001
即:
Subject = fan-001
Relation = located_at
Object = room-001
Object Relation 示例
一个完整房间可以形成:
Person
│
└── located_at ──→ LivingRoom
Fan
│
└── located_at ──→ LivingRoom
TemperatureSensor
│
└── located_at ──→ LivingRoom
TemperatureSensor
│
└── detects ──→ Temperature
这样,SAI 就不再只是看到:
person
fan
32
而是形成:
Person → located_at → LivingRoom
Fan → located_at → LivingRoom
TemperatureSensor → detects → Temperature
这就是从:
Element
→ Object
→ Relation
进一步形成结构化世界描述。
30.8 Object 生命周期
Object 并不是创建后永远不变。
一个 Object 可以具有完整生命周期:
Create
↓
Initialize
↓
Active
↓
Update
↓
Inactive
↓
Destroy
例如:
fan-001
生命周期:
created
↓
initialized
↓
offline
↓
online
↓
on
↓
off
↓
disabled
↓
removed
Object 生命周期阶段
1. Create
创建对象:
Object ID = fan-001
2. Initialize
初始化属性、状态、方法:
type = fan
state = off
speed = 0
3. Active
对象进入正常运行状态:
state = on
4. Update
更新属性或状态:
speed = 1200
5. Inactive
对象暂时停止:
state = inactive
6. Destroy
对象从当前系统中移除:
fan-001 → removed
Object 生命周期与 Individual
Individual 可以管理大量 Object:
Individual
│
├── Person
├── Room
├── Fan
├── Sensor
└── Door
因此 Object 生命周期实际上属于 Individual 对外部世界进行持续建模的一部分。
30.9 Object Manager
当系统中只有一个 Object 时,可以直接操作。
但是实际 SAI 可能同时管理大量对象:
person-001
person-002
fan-001
fan-002
room-001
sensor-001
sensor-002
因此需要:
ObjectManager(对象管理器)
ObjectManager 负责:
创建
注册
获取
更新
删除
查询
统计
ObjectManager 结构
ObjectManager
│
├── register()
├── get()
├── has()
├── update()
├── remove()
├── all()
└── count()
Object Class
下面建立一个基础 Object 类:
<?php
namespace SAI\Object;
class Object
{
protected $id;
protected $type;
protected $properties = array();
protected $methods = array();
protected $state = 'created';
protected $relations = array();
public function __construct($id, $type)
{
$this->id = $id;
$this->type = $type;
}
public function getId()
{
return $this->id;
}
public function getType()
{
return $this->type;
}
public function setProperty($name, $value)
{
$this->properties[$name] = $value;
}
public function getProperty($name)
{
if (!isset($this->properties[$name])) {
return null;
}
return $this->properties[$name];
}
public function getProperties()
{
return $this->properties;
}
public function setState($state)
{
$this->state = $state;
}
public function getState()
{
return $this->state;
}
public function addMethod($name, $method)
{
$this->methods[$name] = $method;
}
public function getMethods()
{
return $this->methods;
}
public function addRelation($relation)
{
$this->relations[] = $relation;
}
public function getRelations()
{
return $this->relations;
}
}
这个类表达的是:
Object
├── ID
├── Type
├── Property
├── Method
├── State
└── Relation
ObjectManager
<?php
namespace SAI\Object;
class ObjectManager
{
protected $objects = array();
public function register(Object $object)
{
$this->objects[$object->getId()] = $object;
}
public function get($id)
{
if (!isset($this->objects[$id])) {
return null;
}
return $this->objects[$id];
}
public function has($id)
{
return isset($this->objects[$id]);
}
public function remove($id)
{
unset($this->objects[$id]);
}
public function all()
{
return $this->objects;
}
public function count()
{
return count($this->objects);
}
}
这样:
ObjectManager
│
├── fan-001
├── room-001
├── person-001
└── sensor-001
就可以统一管理。
30.10 完整 Object 示例
下面建立一个完整的客厅场景。
第一步:创建 Room Object
$room = new Object(
'room-001',
'room'
);
$room->setProperty(
'name',
'living-room'
);
$room->setProperty(
'temperature',
32
);
$room->setProperty(
'humidity',
65
);
$room->setState(
'occupied'
);
形成:
Object
ID = room-001
Type = room
Property
├── name = living-room
├── temperature = 32
└── humidity = 65
State
└── occupied
第二步:创建 Person Object
$person = new Object(
'person-001',
'person'
);
$person->setProperty(
'location',
'living-room'
);
$person->setState(
'present'
);
形成:
Person
ID = person-001
Type = person
Property
└── location = living-room
State
└── present
第三步:创建 Fan Object
$fan = new Object(
'fan-001',
'fan'
);
$fan->setProperty(
'name',
'living-room-fan'
);
$fan->setProperty(
'speed',
0
);
$fan->setProperty(
'power',
50
);
$fan->setState(
'off'
);
形成:
Fan
ID = fan-001
Type = fan
Property
├── name = living-room-fan
├── speed = 0
└── power = 50
State
└── off
第四步:加入 Method
可以描述 Fan 具有:
turnOn
turnOff
setSpeed
例如:
$fan->addMethod(
'turnOn',
'turnOn'
);
$fan->addMethod(
'turnOff',
'turnOff'
);
$fan->addMethod(
'setSpeed',
'setSpeed'
);
此时:
Fan
├── Property
│ ├── name
│ ├── speed
│ └── power
│
├── State
│ └── off
│
└── Method
├── turnOn()
├── turnOff()
└── setSpeed()
第五步:建立 Object Relation
例如:
Person → located_at → LivingRoom
Fan → located_at → LivingRoom
可以加入关系:
$person->addRelation(
array(
'type' => 'located_at',
'target' => 'room-001'
)
);
$fan->addRelation(
array(
'type' => 'located_at',
'target' => 'room-001'
)
);
形成:
person-001
│
└── located_at
↓
room-001
fan-001
│
└── located_at
↓
room-001
第六步:注册到 ObjectManager
$manager = new ObjectManager();
$manager->register($room);
$manager->register($person);
$manager->register($fan);
现在:
ObjectManager
│
├── room-001
├── person-001
└── fan-001
可以通过:
$fan = $manager->get('fan-001');
取得对象。
30.11 Element → Object 完整转换
前一章中,SAI 已经获得:
Element
├── object = person
├── state = present
├── location = living-room
│
├── object = fan
├── state = off
└── temperature = 32
现在通过 Object 组织:
Element
↓
Object
↓
Property
↓
State
↓
Relation
最终形成:
Room
ID = room-001
Type = room
Temperature = 32
Humidity = 65
State = occupied
Person
ID = person-001
Type = person
State = present
Fan
ID = fan-001
Type = fan
State = off
Speed = 0
再建立关系:
Person
│
└── located_at → Room
Fan
│
└── located_at → Room
于是 SAI 获得了一个结构化对象世界:
Room
│
┌──────────┴──────────┐
│ │
Person Fan
│ │
present off
│ │
└──── located_at ─────┘
│
↓
Room
30.12 Object 在 SAI 认知链中的位置
到第30章为止,可以把前面的结构连接起来:
外部世界
↓
InformationSource
↓
Sensor
↓
InformationReceiver
↓
Information
↓
SceneCollector
↓
Scene
↓
Central
↓
Dispatcher
↓
Coordinator
↓
Perception
↓
Element
↓
Object
├── Property
├── Method
├── State
└── Relation
↓
Cognition
这条链非常重要:
Information
→ 描述获取到的信息
Scene
→ 描述当前场景
Element
→ 分解基础信息单元
Object
→ 组织成为实体
Property
→ 描述实体特征
State
→ 描述实体当前状态
Method
→ 描述实体能够执行的操作
Relation
→ 描述实体之间的关系
Cognition
→ 对这些结构进行理解
因此 Object 并不是认知本身。
它是认知所使用的结构化对象基础。
本章完整示例
最终可以得到:
Individual
│
├── Object: room-001
│ ├── Type: room
│ ├── Property
│ │ ├── temperature = 32
│ │ └── humidity = 65
│ └── State
│ └── occupied
│
├── Object: person-001
│ ├── Type: person
│ ├── Property
│ │ └── location = living-room
│ └── State
│ └── present
│
└── Object: fan-001
├── Type: fan
├── Property
│ ├── speed = 0
│ └── power = 50
├── State
│ └── off
└── Method
├── turnOn()
├── turnOff()
└── setSpeed()
关系:
person-001
│
│ located_at
↓
room-001
↑
│ located_at
│
fan-001
如果温度发生变化:
32°C
↓
31°C
则:
room-001
Property.temperature
32 → 31
Object ID 不发生变化。
如果风扇启动:
fan-001
State:
off → on
Object 仍然是:
fan-001
只是 State 发生变化。
这说明:
Object 是稳定身份,Property 描述对象特征,State 描述对象当前状态,Method 描述对象能力,Relation 描述对象之间的联系。
本章小结
第30章建立了 SAI Framework 的 Object 层。
核心结构:
Object
├── ID
├── Type
├── Property
├── Method
├── State
└── Relation
核心关系:
Element
↓
Object
├── Property
├── Method
├── State
└── Relation
核心职责:
Element → 基础信息单元
Object → 实体组织
Property → 对象特征
Method → 对象操作能力
State → 对象当前状态
Relation → 对象之间关系
ObjectManager → 对象统一管理
因此,SAI Framework 从前面的:
Information
→ Scene
→ Element
进一步进入:
Element
→ Object
→ Property
→ Method
→ State
→ Relation
为下一章 第31章 Property 建立基础。
说明:以上 PHP 代码是本章的设计示例,用于说明 SAI Framework 的 OOP 结构,未作为实际项目代码执行验证。