第33章 Relation
本章大纲
- Relation 定义
- Object Relation
- Element Relation
- Property Relation
- 时间关系
- 空间关系
- 状态关系
- 从属关系
- 因果关系
- Relation 数据结构
- Relation 示例
33.1 Relation 定义
在 SAI Framework 中,Relation(关系)是用于描述两个或多个元素、对象、属性、状态、事件或其他实体之间联系的数据结构。
前面已经建立:
Element
↓
Object
├── Property
├── Method
└── State
但是一个 Object 通常不会独立存在。
例如:
Person
Fan
Room
TemperatureSensor
SAI 还需要知道:
Person → 在哪里?
Fan → 在哪里?
Sensor → 检测什么?
Fan → 属于哪个房间?
Temperature → 什么时候发生变化?
这些信息就由 Relation 表示。
最基本的关系结构:
Subject
↓
Relation
↓
Object
例如:
Person
│
│ located_at
↓
LivingRoom
这里:
Subject = Person
Relation = located_at
Object = LivingRoom
33.2 Relation 的核心作用
Relation 解决的是:
“谁和谁之间有什么关系?”
例如:
Fan → located_at → LivingRoom
Person → located_at → LivingRoom
Sensor → detects → Temperature
Fan → belongs_to → Room
没有 Relation:
Person
Fan
Room
Sensor
Temperature
只是几个独立对象。
加入 Relation 后:
Person ──located_at──→ Room
Fan ───located_at───→ Room
Sensor ──detects────→ Temperature
对象之间形成结构。
因此:
Object
↓
Relation
↓
Object
是 SAI 构建对象世界的重要基础。
33.3 Object Relation
Object Relation 是最常见的 Relation。
例如:
Person
│
└── located_at
↓
LivingRoom
或者:
Fan
│
└── located_at
↓
LivingRoom
再例如:
Person
│
└── controls
↓
Fan
Object Relation 的基本形式
Object A
↓
Relation
↓
Object B
例如:
person-001
located_at
room-001
表示:
Person 位于 Room。
一个 Object 可以有多个关系
例如:
Person
├── located_at → Room
├── controls → Fan
└── near → Door
因此 Object 可以形成关系网络:
Room
↑ ↑
located_at │
│ │
Person Fan
│
controls
↓
Fan
这为后面的 Cognition、Reasoning 提供结构基础。
33.4 Element Relation
Relation 不仅可以存在于 Object 之间,也可以存在于 Element 之间。
例如两个 Element:
Element A
type = temperature
value = 32
Element B
type = room
value = living-room
可以建立:
temperature-element
located_at
living-room-element
或者:
Temperature Element
│
└── belongs_to
↓
Room Element
为什么需要 Element Relation?
因为在 Perception 阶段,SAI 可能还没有形成完整 Object。
例如刚刚获得:
temperature = 32
person = present
fan = off
这些首先可能都是 Element。
SAI 可以先建立:
temperature
│
└── related_to → room
随着后续元素不断组织:
Element
↓
Element Relation
↓
Object
↓
Object Relation
因此 Relation 可以贯穿整个认知数据形成过程。
33.5 Property Relation
Property 之间同样可以存在关系。
例如:
temperature = 32
humidity = 65
二者可以存在:
temperature
│
└── related_to
↓
humidity
更具体的关系也可以表示:
temperature
│
└── affects
↓
humidity
或者:
speed
│
└── affects
↓
power
Property Relation 与 Object Relation
例如:
Fan
├── speed = 1200
└── power = 50
Object Relation:
Fan → located_at → Room
Property Relation:
speed → affects → power
二者层次不同:
Object Relation
→ 对象之间的关系
Property Relation
→ 属性之间的关系
属性与对象的关系
还可以表达:
Fan
│
└── has_property
↓
speed
因此:
Object
↓
has_property
↓
Property
这使 Object、Property 和 Relation 可以形成统一结构。
33.6 时间关系
时间 Relation 用于描述事件、状态、对象或信息之间的时间联系。
例如:
Event A
↓
before
↓
Event B
表示 A 发生在 B 之前。
常见时间关系:
before
after
during
starts
ends
overlaps
simultaneous
Before
例如:
unlock()
↓
before
↓
open()
表示:
unlock 发生在 open 之前。
After
open()
↓
after
↓
unlock()
During
例如:
Person
↓
present_during
↓
Meeting
时间关系在 SAI 中的作用
时间关系可以帮助 SAI 建立事件顺序:
Information A
↓
Event A
↓
before
↓
Event B
↓
Event C
于是可以形成:
A → B → C
这对于后续:
Memory
Experience
Reasoning
Learning
都具有重要作用。
33.7 空间关系
空间 Relation 用于描述 Object、位置和区域之间的空间联系。
常见空间关系:
located_at
inside
contains
near
far
left_of
right_of
front_of
behind
above
below
Located At
Fan
│
└── located_at
↓
LivingRoom
Inside
Person
│
└── inside
↓
Building
Contains
反方向:
Building
│
└── contains
↓
Person
因此:
A → inside → B
通常可以对应:
B → contains → A
Near
例如:
Person
│
└── near
↓
Door
表示两者存在空间接近关系。
方向关系
例如:
Car
│
└── left_of
↓
Truck
或者:
Robot
│
└── front_of
↓
Obstacle
空间关系对于机器人、车辆和环境感知尤其重要。
33.8 状态关系
State Relation 用于描述不同对象或状态之间的联系。
例如:
Door
state = open
与:
Room
state = occupied
可以形成:
Door.open
↓
related_to
↓
Room.occupied
状态依赖
例如:
Door
state = locked
可能意味着:
Door.open
不能直接执行。
形成:
Door
state = locked
↓
affects
↓
open()
这里 Relation 连接:
State
↓
Method
状态转换关系
还可以描述:
OFF
↓
causes_transition
↓
ON
或者:
locked
↓
after
↓
unlock
这些关系可以为状态机和行为控制提供基础。
33.9 从属关系
从属关系描述对象、属性、设备、模块之间的归属关系。
常见类型:
belongs_to
part_of
contains
owns
has
member_of
Belongs To
例如:
Fan
│
└── belongs_to
↓
Room
表示:
Fan 属于 Room。
Part Of
例如:
Wheel
│
└── part_of
↓
Vehicle
表示:
Wheel 是 Vehicle 的组成部分。
Contains
反方向:
Vehicle
│
└── contains
↓
Wheel
从属关系的重要作用
它可以建立对象层级:
Vehicle
│
├── Engine
├── Wheel
├── Sensor
└── Controller
再进一步:
Vehicle
↓
contains
↓
Engine
↓
contains
↓
EnginePart
形成对象层级结构。
33.10 因果关系
Causal Relation(因果关系)用于描述:
一个事件、状态或条件对另一个事件、状态或结果产生影响。
例如:
Temperature > 30
↓
causes
↓
Fan ON
但是这里要注意:
causes
本身只是关系描述。
它不等于完整推理。
真正的:
Temperature > 30
→ Rule
→ Reasoning
→ Decision
→ Fan ON
属于后续 Rule、Reasoning 和 Decision。
Relation 只是保存:
A → causes → B
因果关系示例
Rain
↓
causes
↓
RoadWet
或者:
Collision
↓
causes
↓
VehicleStopped
再例如:
PowerOff
↓
causes
↓
DeviceOffline
因果关系与时间关系
因果关系通常具有时间方向:
Cause
↓
before
↓
Effect
例如:
TemperatureRise
↓
before
↓
FanTurnOn
但是:
before ≠ causes
A 发生在 B 之前,不一定意味着 A 导致 B。
因此:
before → 时间关系
causes → 因果关系
必须严格区分。
33.11 Relation 数据结构
Relation 可以设计成统一结构:
Relation
{
id
type
subject
object
state
source
time
metadata
}
Relation ID
例如:
relation-000001
用于唯一标识一条 Relation。
Relation Type
例如:
located_at
contains
belongs_to
near
before
after
causes
controls
detects
Subject
关系的起点:
person-001
Object
关系的目标:
room-001
State
关系本身也可以有状态:
active
inactive
valid
invalid
confirmed
unconfirmed
例如:
Fan
located_at
Room
state = confirmed
Source
表示关系来自哪里:
source = position-sensor-001
或者:
source = perception
Time
记录关系建立或发现的时间:
time = 2026-09-12 15:30:00
Metadata
保存其他信息:
confidence
distance
measurement
context
例如空间关系:
Person
near
Door
metadata:
distance = 1.2m
33.12 Relation PHP 类
可以建立基础 Relation:
<?php
namespace SAI\Relation;
class Relation
{
protected $id;
protected $type;
protected $subject;
protected $object;
protected $state;
protected $source;
protected $time;
protected $metadata = array();
public function __construct(
$id,
$type,
$subject,
$object,
$source = null
) {
$this->id = $id;
$this->type = $type;
$this->subject = $subject;
$this->object = $object;
$this->source = $source;
$this->state = 'valid';
$this->time = time();
}
public function getId()
{
return $this->id;
}
public function getType()
{
return $this->type;
}
public function getSubject()
{
return $this->subject;
}
public function getObject()
{
return $this->object;
}
public function getState()
{
return $this->state;
}
public function setState($state)
{
$this->state = $state;
}
public function getSource()
{
return $this->source;
}
public function getTime()
{
return $this->time;
}
public function setMetadata($key, $value)
{
$this->metadata[$key] = $value;
}
public function getMetadata($key)
{
if (!isset($this->metadata[$key])) {
return null;
}
return $this->metadata[$key];
}
public function getMetadata()
{
return $this->metadata;
}
}
对应:
Relation
├── ID
├── Type
├── Subject
├── Object
├── State
├── Source
├── Time
└── Metadata
33.13 Relation 示例
建立三个 Object:
$room = new Object(
'room-001',
'room'
);
$person = new Object(
'person-001',
'person'
);
$fan = new Object(
'fan-001',
'fan'
);
建立 Person 与 Room 的关系:
$relation1 = new Relation(
'relation-000001',
'located_at',
'person-001',
'room-001',
'scene-001'
);
建立 Fan 与 Room 的关系:
$relation2 = new Relation(
'relation-000002',
'located_at',
'fan-001',
'room-001',
'scene-001'
);
建立 Person 与 Fan 的控制关系:
$relation3 = new Relation(
'relation-000003',
'controls',
'person-001',
'fan-001',
'scene-001'
);
最终形成:
Person
│
├── located_at ──→ Room
│
└── controls ────→ Fan
Fan
│
└── located_at ──→ Room
33.14 RelationManager
当 Relation 数量增加后,需要统一管理。
<?php
namespace SAI\Relation;
class RelationManager
{
protected $relations = array();
public function register(Relation $relation)
{
$this->relations[
$relation->getId()
] = $relation;
}
public function get($id)
{
if (!isset($this->relations[$id])) {
return null;
}
return $this->relations[$id];
}
public function has($id)
{
return isset($this->relations[$id]);
}
public function remove($id)
{
unset($this->relations[$id]);
}
public function all()
{
return $this->relations;
}
public function count()
{
return count($this->relations);
}
}
这样:
RelationManager
│
├── relation-000001
├── relation-000002
├── relation-000003
└── ...
可以统一管理整个 Individual 的关系。
33.15 完整 Relation 场景
现在把第29、30、31、32、33章连接起来。
场景:
LivingRoom
Temperature = 32°C
Person = present
Fan = off
首先获取 Element:
Element
├── room
├── temperature
├── 32
├── person
├── present
├── fan
└── off
组织为 Object:
Room
Person
Fan
Object 加入 Property:
Room
└── temperature = 32
Person
└── location = living-room
Fan
└── speed = 0
Object 加入 State:
Room
└── occupied
Person
└── present
Fan
└── off
Object 加入 Method:
Fan
├── turnOn()
├── turnOff()
└── setSpeed()
然后建立 Relation:
Person
│
└── located_at → Room
Fan
│
└── located_at → Room
Temperature
│
└── property_of → Room
最终形成:
Room
/ | \
/ | \
temperature located_at located_at
↑ ↑ ↑
32 Person Fan
│
├── speed = 0
├── state = off
└── methods
├── turnOn
├── turnOff
└── setSpeed
这时 SAI 已经不再只是拥有一组零散信息,而是形成了:
Element
↓
Object
↓
Property
↓
State
↓
Method
↓
Relation
组成的结构化世界。
33.16 Relation 与 Cognition
Relation 本身不是 Cognition。
例如:
Person
located_at
Room
Relation 只是描述:
Person 与 Room 存在 located_at 关系。
SAI 后续可以利用这个关系进行认知:
Person
↓
located_at
↓
Room
↓
temperature = 32
进一步组织:
Person 在一个温度为 32°C 的 Room 中。
再进一步进入:
Cognition
↓
Reasoning
↓
Decision
因此:
Relation → 提供联系
Cognition → 理解联系
Reasoning → 利用联系进行推理
本章小结
第33章建立了 SAI Framework 的 Relation 关系层。
核心定义:
Relation 是用于描述元素、对象、属性、状态、事件以及其他实体之间联系的数据结构。
基本结构:
Subject
↓
Relation
↓
Object
Relation 可以存在于多个层次:
Element Relation
Object Relation
Property Relation
State Relation
Event Relation
主要关系类型:
时间关系
├── before
├── after
├── during
└── overlaps
空间关系
├── located_at
├── inside
├── contains
├── near
├── left_of
├── right_of
├── above
└── below
从属关系
├── belongs_to
├── part_of
└── contains
因果关系
└── causes
核心数据结构:
Relation
├── ID
├── Type
├── Subject
├── Object
├── State
├── Source
├── Time
└── Metadata
到目前为止,SAI 的核心对象结构已经形成:
Information
↓
Scene
↓
Element
↓
Object
├── Property
├── Method
├── State
└── Relation
↓
Cognition
其中最关键的一点是:
Element → 基础信息单元
Object → 实体
Property → 实体特征
Method → 实体操作
State → 实体状态
Relation → 实体之间的联系
这样,SAI 才真正开始从**“信息集合”转向“结构化对象世界”**。
说明:以上 PHP 代码是本章的设计示例,用于说明 SAI Framework 的 OOP 结构,未作为实际项目代码执行验证。