第173章 RelationService
173.1 提出背景
在第167章的ICAI OOP组合体系中,Object不仅具有自身的Identity、Type、Attribute和State,还可以与其他Object建立结构关系。
基本Object模型为:
O=(ID,T,A,S,R)O=(ID,T,A,S,R)
其中:
- IDID:Object Identity;
- TT:Object Type;
- AA:Attribute;
- SS:State;
- RR:Relation。
如果只有Object而没有Relation,系统只能知道:
Object A
Object B
Object C
却无法表达:
A contains B
A uses C
B belongs_to C
A located_in C
因此,Relation是将离散Object组织成结构网络的重要基础。
前面已经定义:
R=(O1,T,O2,C,S)R=(O_1,T,O_2,C,S)
其中:
- O1O_1:关系起点对象;
- TT:Relation Type;
- O2O_2:关系目标对象;
- CC:Relation Condition;
- SS:Relation State。
本章进一步建立:
RelationService(关系服务)
负责Relation的完整生命周期:
关系建立
↓
关系读取
↓
关系更新
↓
关系删除
因此:
RelationService=Create+Read+Update+DeleteRelationService = Create + Read + Update + Delete
其核心任务不是简单操作一张关系表,而是:
保证Object之间的关系能够按照统一的领域规则被建立、识别、维护和解除。
173.2 Relation定义
Relation(关系) 是两个或多个Domain Object之间具有明确类型、方向、条件和状态的结构连接。
最基础的二元关系可以定义为:
R=(O1,T,O2)R=(O_1,T,O_2)
扩展后:
R=(ID,O1,T,O2,C,S,Tm)R=(ID,O_1,T,O_2,C,S,Tm)
其中:
- IDID:Relation Identity;
- O1O_1:Source Object;
- TT:Relation Type;
- O2O_2:Target Object;
- CC:Condition;
- SS:State;
- TmTm:Time。
例如:
Robot-001
↓
contains
↓
Arm-001
这里:
Source = Robot-001
Type = contains
Target = Arm-001
Relation不是Object本身。
因此:
Object≠RelationObject \neq Relation
但:
Object→RelationObjectObject \xrightarrow{Relation} Object
173.3 RelationService定义
RelationService 是负责Domain Object之间关系生命周期管理的应用服务。
可以定义:
RS=(C,R,U,D,V,P)RS=(C,R,U,D,V,P)
其中:
- CC:Create Relation;
- RR:Read Relation;
- UU:Update Relation;
- DD:Delete Relation;
- VV:Validation;
- PP:Persistence。
核心流程:
Source Object
+
Target Object
+
Relation Type
↓
Relation Validation
↓
Create Relation
↓
Save
读取:
Object
↓
RelationService
↓
Relations
更新:
Existing Relation
↓
Validate
↓
Update
↓
Save
删除:
Relation
↓
Validate
↓
Delete
173.4 RelationService与Object
RelationService并不负责创建Object。
Object由:
ObjectService
负责。
RelationService负责:
Object A
↓
Relation
↓
Object B
因此:
ObjectService→ObjectObjectService \rightarrow Object
而:
RelationService→RelationRelationService \rightarrow Relation
二者形成协作关系:
ObjectService
↓
Object A
ObjectService
↓
Object B
↓
RelationService
↓
Relation
这保证对象本身和对象之间的关系具有清晰边界。
173.5 Relation类型
关系必须具有明确Type。
常见Relation Type可以包括:
contains
part_of
belongs_to
has
uses
located_in
connected_to
depends_on
supports
controls
owns
associated_with
derived_from
before
after
causes
例如:
Car
↓
contains
↓
Engine
可以表达为:
R1=(Car,contains,Engine)R_1=(Car,contains,Engine)
反向关系:
R2=(Engine,part_of,Car)R_2=(Engine,part\_of,Car)
因此关系类型不仅是字符串,而是领域规则的一部分。
173.6 关系方向
Relation通常具有方向。
例如:
A → contains → B
与:
B → contains → A
不是同一个关系。
因此:
R(A,T,B)≠R(B,T,A)R(A,T,B)\neq R(B,T,A)
但是某些Relation具有反向语义:
A contains B
↕
B part_of A
因此可以建立:
Inverse(contains)=part_ofInverse(contains)=part\_of
RelationService可以通过Relation规则维护这种对应关系。
173.7 关系建立
173.7.1 建立定义
关系建立(Relation Creation) 是在两个合法Object之间创建一个具有明确类型和状态的Relation对象。
基本模型:
CreateRelation(O1,T,O2)→RCreateRelation(O_1,T,O_2) \rightarrow R
完整流程:
Source Object
↓
Check Exists
↓
Target Object
↓
Check Exists
↓
Relation Type
↓
Relation Rule
↓
Create Relation
↓
Save
173.8 关系建立前检查
RelationService不能在没有检查的情况下建立Relation。
至少需要检查:
Source Object是否存在
Target Object是否存在
Source是否允许建立该关系
Target是否允许建立该关系
Relation Type是否合法
是否允许重复
当前State是否允许
是否产生Conflict
因此:
CanCreateRelation=Existence∧Type∧State∧RuleCanCreateRelation = Existence \land Type \land State \land Rule
只有条件成立:
CanCreateRelation=TrueCanCreateRelation=True
才能建立Relation。
173.9 关系建立PHP实现
可以建立:
class Relation
{
protected $id;
protected $sourceId;
protected $type;
protected $targetId;
protected $state;
public function __construct(
$id,
$sourceId,
$type,
$targetId
) {
$this->id = $id;
$this->sourceId = $sourceId;
$this->type = $type;
$this->targetId = $targetId;
$this->state = 'active';
}
public function getId()
{
return $this->id;
}
public function getSourceId()
{
return $this->sourceId;
}
public function getType()
{
return $this->type;
}
public function getTargetId()
{
return $this->targetId;
}
public function getState()
{
return $this->state;
}
}
RelationService:
class RelationService
{
protected $repository;
protected $engine;
public function __construct(
$repository,
$engine
) {
$this->repository = $repository;
$this->engine = $engine;
}
public function create(
$sourceId,
$type,
$targetId
) {
if (!$this->engine->canCreate(
$sourceId,
$type,
$targetId
)) {
return false;
}
$relation = new Relation(
null,
$sourceId,
$type,
$targetId
);
return $this->repository->save(
$relation
);
}
}
这里的职责非常明确:
RelationService
↓
组织建立流程
RelationEngine
↓
判断建立规则
Relation
↓
领域对象
RelationRepository
↓
Persistence
173.10 关系重复检查
某些关系不能重复。
例如:
Robot-001
contains
Arm-001
如果已经存在,就不应该再次建立完全相同的Relation。
可以定义唯一组合:
UniqueKey=(O1,T,O2)UniqueKey=(O_1,T,O_2)
例如:
Robot-001
+
contains
+
Arm-001
作为唯一关系键。
因此:
Exists(O1,T,O2)→RejectExists(O_1,T,O_2) \rightarrow Reject
这样可以防止Relation数据重复。
173.11 关系读取
173.11.1 基本读取
Relation读取回答:
某个Object与哪些Object存在关系?
例如:
Robot-001
↓
contains
↓
Arm-001
Wheel-001
Sensor-001
可以:
$relations =
$relationService->getByObject(
1001
);
173.12 按来源读取
可以读取某Object作为Source的全部Relation:
getOutgoingRelations($objectId)
例如:
Robot
├── contains → Arm
├── contains → Wheel
└── controls → Sensor
173.13 按目标读取
也可以读取某Object作为Target的Relation:
getIncomingRelations($objectId)
例如:
Arm
↑
part_of
↑
Robot
因此:
Outgoing Relations
Incoming Relations
应当分开理解。
173.14 按Relation Type读取
还可以:
getByType(
$objectId,
'contains'
);
例如:
Robot
↓
contains
↓
Arm
Wheel
Sensor
而:
Robot
↓
controls
↓
Sensor
属于另一种Relation。
因此Relation Type是读取和规则判断的重要索引。
173.15 关系网络读取
单个Relation读取:
A → R → B
多个Relation读取:
A
├── R1 → B
├── R2 → C
└── R3 → D
形成:
Network=(O,R)Network=(O,R)
如果进一步加入State:
Network=(O,R,S)Network=(O,R,S)
这就是ICAI对象关系网络的基础。
173.16 关系更新
173.16.1 更新定义
Relation更新是对已经存在Relation的合法属性、状态、条件或目标结构进行修改。
可以定义:
Update(R)=Type+Target+Condition+StateUpdate(R) = Type + Target + Condition + State
但必须根据Relation Type决定哪些属性允许修改。
例如:
Relation State
Condition
Target
可以更新。
但某些系统中:
Source Object
一旦创建就不能修改。
此时如果Source发生变化,应当:
Delete Old Relation
+
Create New Relation
而不是直接修改Source。
173.17 关系更新流程
Relation ID
↓
Read Relation
↓
Validate
↓
Check Update Rule
↓
Update Domain Object
↓
Save
↓
History
例如:
public function update(
$relationId,
$data
) {
$relation =
$this->repository->find(
$relationId
);
if (!$relation) {
return false;
}
if (!$this->engine->canUpdate(
$relation,
$data
)) {
return false;
}
$relation->update(
$data
);
return $this->repository->save(
$relation
);
}
173.18 Relation状态更新
Relation本身也可以具有State:
created
active
inactive
blocked
expired
deleted
例如:
active
↓
blocked
或者:
active
↓
inactive
因此:
Relation→StateServiceRelation \rightarrow StateService
也可以形成:
RelationService
↓
StateService
↓
StateEngine
RelationService负责关系生命周期,StateService负责统一状态处理。
173.19 关系删除
173.19.1 删除定义
关系删除是解除两个Object之间现有Relation的过程。
定义:
DeleteRelation(R)→NoActiveRelationDeleteRelation(R) \rightarrow NoActiveRelation
例如:
Robot
contains
Arm
删除后:
Robot
X
Arm
二者不再具有该Active Relation。
173.20 关系删除不等于删除Object
这是RelationService最重要的边界之一。
例如:
Robot
contains
Arm
删除Relation:
Robot
X
Arm
只表示:
Robot与Arm之间的contains关系不存在。
并不表示:
Arm
这个Object本身被删除。
因此:
DeleteRelation(R)≠DeleteObject(O)DeleteRelation(R) \neq DeleteObject(O)
这也是ObjectService和RelationService必须分开的重要原因。
173.21 关系删除检查
关系删除同样需要规则。
例如某些Relation是系统必要结构:
System
contains
Core
如果删除该Relation可能导致系统结构失效。
因此:
Delete Request
↓
Relation Exists?
↓
Protected Relation?
↓
Active Dependency?
↓
Delete Rule
↓
Delete
可以定义:
CanDelete(R)=Exists∧¬Protected∧¬RequiredCanDelete(R) = Exists \land \neg Protected \land \neg Required
只有满足条件才能解除Relation。
173.22 关系删除与逻辑删除
与Object一样,Relation也可以采用:
Physical Delete
或者:
Logical Delete
逻辑删除:
Relation State
=
inactive
或者:
deleted_at
如果Relation具有重要历史价值,推荐保留历史。
例如:
Robot
contains
Arm
曾经成立。
后来解除:
Robot
X
Arm
系统仍然可以知道:
该关系曾经存在。
这对于Memory、History和Experience具有价值。
173.23 Relation History
Relation的建立和删除本身也是系统事实。
因此可以记录:
Relation Created
Relation Updated
Relation Deactivated
Relation Deleted
例如:
10:00
Robot contains Arm
Created
11:30
Robot contains Arm
Inactive
可以建立:
relation_history
id
relation_id
action
old_value
new_value
reason
created_at
这样:
Relation→History→MemoryRelation \rightarrow History \rightarrow Memory
形成与前面章节一致的事实记录链。
173.24 Relation与StateService
Relation本身具有状态,因此可以由统一StateService管理。
例如:
RelationService
↓
Relation
↓
StateService
↓
StateEngine
建立:
created → active
解除:
active → inactive
因此RelationService不需要自己重新实现一套状态转换逻辑。
173.25 RelationService与Engine
RelationService负责流程:
Create
Read
Update
Delete
RelationEngine负责:
关系合法性
关系类型规则
重复关系检查
方向规则
依赖规则
冲突检查
因此:
RelationService→RelationEngineRelationService \rightarrow RelationEngine
例如:
if (!$this->engine->canCreate(
$sourceId,
$type,
$targetId
)) {
return false;
}
Engine只回答:
这个关系是否允许建立?
Service负责:
按什么流程建立、保存并返回结果?
173.26 RelationService与Repository
Persistence由Repository负责。
基本结构:
RelationService
↓
RelationRepository
↓
MySQL
Repository可以提供:
find($id)
findByObject($objectId)
findOutgoing($objectId)
findIncoming($objectId)
findByType($type)
exists($sourceId, $type, $targetId)
save($relation)
delete($id)
Service不直接执行:
INSERT
SELECT
UPDATE
DELETE
而通过Repository完成Persistence。
173.27 RelationService PHP基础结构
可以建立:
class RelationService
{
protected $repository;
protected $engine;
public function __construct(
$repository,
$engine
) {
$this->repository = $repository;
$this->engine = $engine;
}
public function create(
$sourceId,
$type,
$targetId
) {
if (!$this->engine->canCreate(
$sourceId,
$type,
$targetId
)) {
return false;
}
if ($this->repository->exists(
$sourceId,
$type,
$targetId
)) {
return false;
}
$relation = new Relation(
null,
$sourceId,
$type,
$targetId
);
return $this->repository->save(
$relation
);
}
public function get($id)
{
return $this->repository->find(
$id
);
}
public function getByObject(
$objectId
) {
return $this->repository
->findByObject(
$objectId
);
}
public function update(
$relationId,
$data
) {
$relation =
$this->get($relationId);
if (!$relation) {
return false;
}
if (!$this->engine->canUpdate(
$relation,
$data
)) {
return false;
}
$relation->update($data);
return $this->repository->save(
$relation
);
}
public function delete(
$relationId
) {
$relation =
$this->get($relationId);
if (!$relation) {
return false;
}
if (!$this->engine->canDelete(
$relation
)) {
return false;
}
return $this->repository->delete(
$relationId
);
}
}
这形成完整的:
RelationService
├── create()
├── get()
├── update()
└── delete()
173.28 Relation数据库结构
可以建立:
relations
id
source_object_id
relation_type
target_object_id
state
condition
created_at
updated_at
其中:
source_object_id
relation_type
target_object_id
可以建立唯一约束,防止相同Relation重复建立。
例如:
UNIQUE(
source_object_id,
relation_type,
target_object_id
)
历史表:
relation_history
id
relation_id
action
old_state
new_state
reason
created_at
这样Relation和Relation History形成独立Persistence结构。
173.29 Relation的完整生命周期
Relation可以建立完整生命周期:
Created→Validated→Active→Updated→Inactive→DeletedCreated \rightarrow Validated \rightarrow Active \rightarrow Updated \rightarrow Inactive \rightarrow Deleted
其中:
Created
↓
Validated
↓
Active
↓
Updated
↓
Inactive
↓
Deleted
如果建立失败:
Create Request
↓
Validation Failed
↓
Rejected
因此关系建立失败也是合法Result。
173.30 Relation与Composite Object
第167章建立了Composite Object。
例如:
Car
├── Engine
├── Wheel
├── Door
└── Seat
实际上可以表达为:
Car
├── contains → Engine
├── contains → Wheel
├── contains → Door
└── contains → Seat
因此:
CompositeObject=Objects+RelationsCompositeObject = Objects + Relations
RelationService成为Composite Object形成的重要基础。
173.31 Relation与Individual
Individual可以拥有多个Object:
Individual
↓
Objects
这些Object之间又可以建立Relation:
Individual
↓
Object A
↓
Relation
↓
Object B
因此:
Individual→Composition→Object→Relation→ObjectIndividual \rightarrow Composition \rightarrow Object \rightarrow Relation \rightarrow Object
这使Individual内部形成一个对象关系网络。
173.32 Relation与Knowledge
Knowledge也可以使用Relation表达结构。
例如:
Object A
↓
is_a
↓
Object B
或者:
Object A
↓
located_in
↓
Object B
这些关系可以成为知识结构的组成部分。
因此:
Knowledge=Objects+Relations+FactsKnowledge = Objects + Relations + Facts
RelationService因此不仅服务于普通Object组合,也可以成为Knowledge结构的底层关系管理服务。
173.33 Relation与Memory
Relation变化属于历史事实。
例如:
A contains B
建立:
Relation Created
解除:
Relation Deleted
这些事实可以进入:
History
↓
Memory
从而形成:
RelationChange→History→MemoryRelationChange \rightarrow History \rightarrow Memory
进一步:
Memory→ExperienceMemory \rightarrow Experience
因此Relation并不是静态结构,也可以参与ICAI的经验形成过程。
173.34 Relation与Conflict
Relation建立可能产生Conflict。
例如:
A
must_be_in
Location-A
但又建立:
A
must_be_in
Location-B
如果规则规定A不能同时处于两个互斥Location,则产生:
Relation Conflict
因此:
Relation→ConflictRelation \rightarrow Conflict
RelationEngine可以负责初步检查,Conflict对象则负责正式表达冲突。
173.35 Relation与Diagnosis
如果Relation错误导致系统行为异常:
Behavior
↓
Failure
↓
Diagnosis
↓
Relation Check
↓
Incorrect Relation
例如某Object错误地连接到错误Component。
Diagnosis可以通过:
Current Relations
+
Relation History
+
Memory
+
Experience
定位原因。
因此:
Diagnosis→RelationDiagnosis \rightarrow Relation
Relation成为问题诊断的重要证据来源。
173.36 RelationService统一CRUD模型
最基础模型:
CRUD=Create+Read+Update+DeleteCRUD = Create + Read + Update + Delete
但完整关系管理应该扩展为:
RelationManagement=CRUD+Validation+Direction+Type+State+HistoryRelationManagement = CRUD + Validation + Direction + Type + State + History
因此:
Create
↓
Validate
↓
Read
↓
Update
↓
State
↓
History
↓
Delete
173.37 RelationService完整架构
最终形成:
Controller
↓
RelationService
├── RelationEngine
├── StateService
└── RelationRepository
↓
MySQL
领域层:
Relation
├── Source Object
├── Relation Type
├── Target Object
├── Condition
└── State
关系网络:
Object
↓
Relation
↓
Object
↓
Relation
↓
Object
最终形成:
Network=(O,R,S)Network=(O,R,S)
其中:
- OO:Object集合;
- RR:Relation集合;
- SS:对象与关系状态集合。
173.38 本章核心原则
RelationService必须遵循以下原则。
第一,Relation不是Object
Object ≠ Relation
Relation描述Object之间的结构连接。
第二,Relation必须具有明确类型
Source
+
Type
+
Target
不能只有:
A → B
而不知道二者是什么关系。
第三,Relation具有方向
A contains B
与:
B contains A
不是同一个关系。
第四,关系建立必须经过规则
Existence
+
Type
+
State
+
Rule
第五,Relation删除不等于Object删除
Delete Relation
≠
Delete Object
第六,Relation可以具有State
因此可以复用:
StateService
第七,关系变化可以形成History
Relation Change
↓
History
↓
Memory
第八,Service、Engine、Repository必须分离
Service
↓
Engine
↓
Domain Object
↓
Repository
↓
Persistence
173.39 本章小结
第173章完成了Relation从理论结构到Service层的工程化。
Relation定义:
R=(ID,O1,T,O2,C,S,Tm)R=(ID,O_1,T,O_2,C,S,Tm)
RelationService定义:
RS=(C,R,U,D,V,P)RS=(C,R,U,D,V,P)
其基本职责:
关系建立
↓
关系读取
↓
关系更新
↓
关系删除
完整关系管理进一步扩展为:
RelationManagement=CRUD+Validation+Type+Direction+State+History\boxed{ RelationManagement = CRUD + Validation + Type + Direction + State + History }
工程架构:
RelationService→RelationEngine→Relation→RelationRepository→MySQL\boxed{ RelationService \rightarrow RelationEngine \rightarrow Relation \rightarrow RelationRepository \rightarrow MySQL }
与ObjectService的关系:
ObjectService
↓
Object
RelationService
↓
Relation
↓
Object
与StateService的关系:
RelationService
↓
StateService
↓
StateEngine
与ICAI整体认知体系的关系:
Individual
↓
Object
↓
Relation
↓
Object Network
↓
Knowledge
↓
Behavior
↓
Result
↓
Feedback
↓
Memory
↓
Experience
由此,ICAI的对象体系不再只是一个个孤立Object,而开始形成能够表达 组成、归属、连接、依赖、位置、控制、因果和其他结构关系 的对象网络。
最终形成:
ICAI Object Network=Object+Relation+State+History\boxed{ ICAI\ Object\ Network = Object + Relation + State + History }
这为后续KnowledgeService、CapabilityService、MethodService以及更高层的认知关系计算提供了统一的关系基础。