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

第227章 ICAI MySQL数据库总体设计

第227章 ICAI MySQL数据库总体设计

227.1 数据库总体定位

经过前面 Domain Object、Service、Engine、Repository 等章节的建立,ICAI 已经形成完整的认知工程对象体系。

数据库不能再按照普通业务软件简单地设计成:

用户表
订单表
产品表
日志表

而应该围绕 ICAI 的认知对象和运行过程建立结构化数据库。

ICAI 数据库需要保存:

Individual
Object
State
Relation
Knowledge
Goal
Capability
Method
Decision
Behavior
Action
Execution
Result
Feedback
Memory
Experience
Risk
Conflict
Abnormality
Diagnosis
Repair
Verification
Learning
Update
History

因此可以定义:

ICAI MySQL=Core+Cognitive+Runtime+Maintenance+Learning+History+Relation+StateICAI\ MySQL = Core + Cognitive + Runtime + Maintenance + Learning + History + Relation + State

数据库的作用是:

保存 ICAI 运行过程中已经形成并需要长期保留的结构化事实、对象、状态、关系、认知结果和历史记录。

数据库本身不负责认知计算。

因此:

MySQL≠CognitiveEngineMySQL \neq CognitiveEngine MySQL≠DecisionEngineMySQL \neq DecisionEngine MySQL≠LearningEngineMySQL \neq LearningEngine

数据库负责存储,Engine 负责计算。


227.2 总体数据库架构

ICAI 数据库建议采用以下分层:

ICAI MySQL Database
│
├── 01 Core核心表
│   ├── individuals
│   ├── objects
│   ├── object_attributes
│   └── system_rules
│
├── 02 Cognitive认知业务表
│   ├── goals
│   ├── knowledge
│   ├── capabilities
│   ├── methods
│   ├── decisions
│   └── matching_records
│
├── 03 Runtime运行表
│   ├── behaviors
│   ├── actions
│   ├── executions
│   ├── results
│   └── feedbacks
│
├── 04 Memory经验学习表
│   ├── memories
│   ├── memory_relations
│   ├── experiences
│   ├── experience_relations
│   └── learning_records
│
├── 05 Risk维护表
│   ├── risks
│   ├── conflicts
│   ├── protections
│   ├── abnormalities
│   ├── diagnoses
│   ├── repairs
│   └── repair_verifications
│
├── 06 State状态表
│   ├── states
│   ├── state_history
│   └── state_transition_rules
│
├── 07 Relation关系表
│   ├── object_relations
│   ├── goal_relations
│   ├── knowledge_relations
│   ├── memory_relations
│   └── experience_relations
│
└── 08 History历史表
    ├── individual_history
    ├── object_history
    ├── method_history
    ├── decision_history
    ├── execution_history
    ├── diagnosis_repair_history
    └── update_history

这里的“层”是数据库设计逻辑层,不要求必须建立不同的 MySQL Database。

在实际部署中,可以统一使用一个数据库,例如:

wsaios_icai

然后通过表命名和模块目录进行逻辑分组。


227.3 Domain到MySQL的完整链路

ICAI 数据库不能直接被 Domain Object 操作。

正确结构:

Controller
    ↓
Service
    ↓
Engine
    ↓
Domain Object
    ↓
Mapper
    ↓
Repository
    ↓
PDO
    ↓
MySQL

读取:

MySQL
    ↓
PDO
    ↓
Repository
    ↓
Persistence Data
    ↓
Mapper
    ↓
Domain Object
    ↓
Engine

因此:

Domain→Mapper→Repository→MySQLDomain \rightarrow Mapper \rightarrow Repository \rightarrow MySQL

是核心持久化链。

数据库设计必须服从 Domain 模型,而不是反过来让数据库表决定认知结构。


227.4 数据库六大基础分类

本章重点将数据库划分为六类:

DB=Core+Business+History+State+Relation+RuntimeDB= Core + Business + History + State + Relation + Runtime

其中:

1. Core核心表

保存系统最基础的对象。

例如:

individuals
objects
object_attributes

2. Business业务表

保存认知业务结构。

例如:

goals
capabilities
methods
decisions
knowledge

3. History历史表

保存变化过程。

例如:

object_history
method_history
decision_history
execution_history

4. State状态表

保存当前状态和状态变化。

例如:

states
state_history
state_transition_rules

5. Relation关系表

保存对象之间的显式关系。

例如:

object_relations
goal_relations
knowledge_relations

6. Runtime运行表

保存实际运行过程。

例如:

behaviors
actions
executions
results
feedbacks

227.5 核心表Core Tables

核心表是整个 ICAI 数据库的基础。

227.5.1 individuals

Individual 是 ICAI 的主体。

CREATE TABLE individuals (
    id BIGINT NOT NULL AUTO_INCREMENT,
    individual_code VARCHAR(100) NOT NULL,
    individual_type VARCHAR(100) NOT NULL,
    name VARCHAR(255) NULL,
    state VARCHAR(50) NOT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_individual_code (individual_code),
    KEY idx_individual_type (individual_type),
    KEY idx_individual_state (state)
);

Domain:

Individual=(ID,T,O,K,C,M,B,MM,E,R,S)Individual=(ID,T,O,K,C,M,B,MM,E,R,S)

数据库不需要把所有 Composition 都直接塞进 individuals

例如:

Individual
   ├── Objects
   ├── Knowledge
   ├── Capabilities
   ├── Methods
   ├── Behaviors
   ├── Memories
   └── Experiences

通过关联表或业务表进行组织。


227.6 objects对象核心表

Object 是 ICAI 最基础的认知对象之一。

O=(ID,T,A,S,R)O=(ID,T,A,S,R)

数据库:

CREATE TABLE objects (
    id BIGINT NOT NULL AUTO_INCREMENT,
    object_code VARCHAR(100) NOT NULL,
    individual_id BIGINT NULL,
    object_type VARCHAR(100) NOT NULL,
    name VARCHAR(255) NULL,
    state VARCHAR(50) NOT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_object_code (object_code),
    KEY idx_object_individual (individual_id),
    KEY idx_object_type (object_type),
    KEY idx_object_state (state)
);

Object 的属性不全部直接放在 objects 中。

属性可以独立保存:

object_attributes

这样可以支持对象属性的动态变化和历史追踪。


227.7 object_attributes

建议:

CREATE TABLE object_attributes (
    id BIGINT NOT NULL AUTO_INCREMENT,
    object_id BIGINT NOT NULL,
    attribute_name VARCHAR(100) NOT NULL,
    attribute_value TEXT,
    attribute_type VARCHAR(50) NULL,
    state VARCHAR(50) NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    KEY idx_attribute_object (object_id),
    KEY idx_attribute_name (attribute_name)
);

因此:

Object
   ↓
Object Attributes

而不是:

Object
   ↓
所有可能字段

227.8 Individual与Object

Individual 与 Object 的关系:

Individual→ObjectIndividual \rightarrow Object

数据库:

individuals
     ↓
objects.individual_id

但是这并不意味着 Object 只能属于 Individual。

未来如果某些 Object 是系统级对象,可以允许:

individual_id = NULL

因此数据库设计不能过度假定所有对象都必须属于某个 Individual。


227.9 状态表State Tables

状态是 ICAI 的基础运行结构。

第172章 StateService、第188章 StateEngine、第217章 StateRepository 已经形成:

S=(O,V,T,C,R)S=(O,V,T,C,R)

数据库需要区分:

Current State

和:

State History

因此至少需要:

states
state_history

227.10 states当前状态表

CREATE TABLE states (
    id BIGINT NOT NULL AUTO_INCREMENT,
    state_code VARCHAR(100) NOT NULL,
    owner_type VARCHAR(100) NOT NULL,
    owner_id BIGINT NOT NULL,
    state_value VARCHAR(100) NOT NULL,
    context_data TEXT,
    reason_data TEXT,
    state_time DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_state_code (state_code),
    KEY idx_state_owner (owner_type, owner_id),
    KEY idx_state_value (state_value)
);

这里采用:

owner_type
owner_id

是为了让 State 能够服务于:

Individual
Object
Goal
Capability
Method
Behavior
Action
Execution

等不同 Domain。


227.11 state_history状态历史表

CREATE TABLE state_history (
    id BIGINT NOT NULL AUTO_INCREMENT,
    history_code VARCHAR(100) NOT NULL,
    owner_type VARCHAR(100) NOT NULL,
    owner_id BIGINT NOT NULL,
    state_before VARCHAR(100) NULL,
    state_after VARCHAR(100) NOT NULL,
    event_data TEXT,
    condition_data TEXT,
    reason_data TEXT,
    evidence TEXT,
    created_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_state_history_code (history_code),
    KEY idx_state_history_owner (owner_type, owner_id),
    KEY idx_state_history_time (created_at)
);

核心原则:

CurrentState≠StateHistoryCurrentState \neq StateHistory

当前状态回答:

现在是什么状态?

历史回答:

曾经经历过什么状态变化?


227.12 关系表Relation Tables

ICAI 中对象并不是孤立存在。

关系模型:

R=(ID,O1,T,O2,C,S,Tm)R=(ID,O_1,T,O_2,C,S,T_m)

核心关系表:

object_relations

同时不同 Domain 可以拥有自己的关系。

例如:

goal_relations
knowledge_relations
memory_relations
experience_relations

227.13 object_relations

CREATE TABLE object_relations (
    id BIGINT NOT NULL AUTO_INCREMENT,
    relation_code VARCHAR(100) NOT NULL,
    object1_id BIGINT NOT NULL,
    relation_type VARCHAR(100) NOT NULL,
    object2_id BIGINT NOT NULL,
    condition_data TEXT,
    state VARCHAR(50) NOT NULL,
    evidence TEXT,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_relation_code (relation_code),
    KEY idx_relation_object1 (object1_id),
    KEY idx_relation_object2 (object2_id),
    KEY idx_relation_type (relation_type),
    KEY idx_relation_state (state)
);

关系方向必须明确:

Object A
   |
   | supports
   ↓
Object B

不能把:

A supports B

自动理解为:

B supports A

除非存在明确逆关系规则。


227.14 认知业务表

核心认知业务对象:

Goal
Knowledge
Capability
Method
Decision

其总体链:

Goal→Capability→Matching→Method→DecisionGoal \rightarrow Capability \rightarrow Matching \rightarrow Method \rightarrow Decision


227.15 goals目标表

Goal:

G=(ID,N,T,C,P,S)G=(ID,N,T,C,P,S)

数据库:

CREATE TABLE goals (
    id BIGINT NOT NULL AUTO_INCREMENT,
    goal_code VARCHAR(100) NOT NULL,
    individual_id BIGINT NULL,
    name VARCHAR(255) NOT NULL,
    goal_type VARCHAR(100) NOT NULL,
    target_data TEXT,
    condition_data TEXT,
    priority INT NULL,
    state VARCHAR(50) NOT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_goal_code (goal_code),
    KEY idx_goal_individual (individual_id),
    KEY idx_goal_state (state),
    KEY idx_goal_priority (priority)
);

Goal 的完成不能由数据库简单判断。

真正完成需要:

Result
+
Verification

因此:

GoalCompleted≠goal.state=′completed′GoalCompleted \neq goal.state=’completed’

数据库状态是保存结果,不是计算来源。


227.16 capabilities能力表

能力:

C=(T,Co,S,R,V)C=(T,Co,S,R,V)

数据库:

CREATE TABLE capabilities (
    id BIGINT NOT NULL AUTO_INCREMENT,
    capability_code VARCHAR(100) NOT NULL,
    individual_id BIGINT NULL,
    capability_type VARCHAR(100) NOT NULL,
    condition_data TEXT,
    state VARCHAR(50) NOT NULL,
    range_data TEXT,
    verification_data TEXT,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_capability_code (capability_code),
    KEY idx_capability_individual (individual_id),
    KEY idx_capability_type (capability_type),
    KEY idx_capability_state (state)
);

理论能力与验证能力必须区分。

Declared Capability
        ↓
Execution
        ↓
Result
        ↓
Verification
        ↓
Verified Capability

227.17 methods方法表

Method:

M=(T,C,P,A,R)M=(T,C,P,A,R)

数据库:

CREATE TABLE methods (
    id BIGINT NOT NULL AUTO_INCREMENT,
    method_code VARCHAR(100) NOT NULL,
    method_type VARCHAR(100) NOT NULL,
    condition_data TEXT,
    process_data TEXT,
    action_data TEXT,
    required_capability_data TEXT,
    expected_result TEXT,
    state VARCHAR(50) NOT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_method_code (method_code),
    KEY idx_method_type (method_type),
    KEY idx_method_state (state)
);

复杂 Method 可以进一步拆成:

method_conditions
method_processes
method_actions
method_dependencies
method_compositions

因此 methods 是方法主表,而不是所有方法结构必须塞进一个字段。


227.18 decisions决策表

Decision:

D=(C,Ca,R,H)D=(C,Ca,R,H)

数据库:

CREATE TABLE decisions (
    id BIGINT NOT NULL AUTO_INCREMENT,
    decision_code VARCHAR(100) NOT NULL,
    individual_id BIGINT NULL,
    goal_code VARCHAR(100) NULL,
    condition_data TEXT,
    selected_candidate TEXT,
    result_data TEXT,
    state VARCHAR(50) NOT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_decision_code (decision_code),
    KEY idx_decision_individual (individual_id),
    KEY idx_decision_goal (goal_code),
    KEY idx_decision_state (state)
);

候选方案应独立保存:

decision_candidates
decision_conditions
decision_results
decision_history

这样能够保留:

Candidate A
Candidate B
Candidate C
     ↓
Decision
     ↓
Candidate B Selected

227.19 Matching匹配表

ICAI 的匹配不是简单 Boolean。

匹配状态:

matched
partial
unmatched
unknown
blocked
conflicted
expired
invalid

因此可以建立:

CREATE TABLE matching_records (
    id BIGINT NOT NULL AUTO_INCREMENT,
    matching_code VARCHAR(100) NOT NULL,
    source_type VARCHAR(100) NOT NULL,
    source_id BIGINT NOT NULL,
    target_type VARCHAR(100) NOT NULL,
    target_id BIGINT NOT NULL,
    match_state VARCHAR(50) NOT NULL,
    score DECIMAL(12,6) NULL,
    reason TEXT,
    evidence TEXT,
    created_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_matching_code (matching_code),
    KEY idx_matching_source (source_type, source_id),
    KEY idx_matching_target (target_type, target_id),
    KEY idx_matching_state (match_state)
);

MatchingEngine负责:

Match(X,R,C)→MMatch(X,R,C)\rightarrow M

Repository只保存 MM


227.20 Runtime运行表

ICAI 实际执行过程:

Behavior→Action→Execution→Result→FeedbackBehavior \rightarrow Action \rightarrow Execution \rightarrow Result \rightarrow Feedback

数据库必须保存这条链。

核心表:

behaviors
actions
executions
results
feedbacks

227.21 behaviors

CREATE TABLE behaviors (
    id BIGINT NOT NULL AUTO_INCREMENT,
    behavior_code VARCHAR(100) NOT NULL,
    goal_code VARCHAR(100) NULL,
    method_code VARCHAR(100) NULL,
    state VARCHAR(50) NOT NULL,
    context_data TEXT,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_behavior_code (behavior_code),
    KEY idx_behavior_goal (goal_code),
    KEY idx_behavior_method (method_code),
    KEY idx_behavior_state (state)
);

227.22 actions

CREATE TABLE actions (
    id BIGINT NOT NULL AUTO_INCREMENT,
    action_code VARCHAR(100) NOT NULL,
    behavior_code VARCHAR(100) NOT NULL,
    action_type VARCHAR(100) NOT NULL,
    object_id BIGINT NULL,
    condition_data TEXT,
    parameter_data TEXT,
    sequence_no INT NOT NULL,
    state VARCHAR(50) NOT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_action_code (action_code),
    KEY idx_action_behavior (behavior_code),
    KEY idx_action_sequence (behavior_code, sequence_no),
    KEY idx_action_state (state)
);

Action 的顺序必须保存。

因为:

A={A1,A2,…,An}A=\{A_1,A_2,\ldots,A_n\}

不是无序集合。


227.23 executions

CREATE TABLE executions (
    id BIGINT NOT NULL AUTO_INCREMENT,
    execution_code VARCHAR(100) NOT NULL,
    behavior_code VARCHAR(100) NULL,
    action_code VARCHAR(100) NULL,
    started_at DATETIME NULL,
    ended_at DATETIME NULL,
    state VARCHAR(50) NOT NULL,
    input_data TEXT,
    output_data TEXT,
    error_data TEXT,
    created_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_execution_code (execution_code),
    KEY idx_execution_behavior (behavior_code),
    KEY idx_execution_action (action_code),
    KEY idx_execution_state (state)
);

Execution 是实际运行过程。

因此:

Action≠ExecutionAction \neq Execution


227.24 results

Result 保存实际结果。

CREATE TABLE results (
    id BIGINT NOT NULL AUTO_INCREMENT,
    result_code VARCHAR(100) NOT NULL,
    execution_code VARCHAR(100) NOT NULL,
    expected_result TEXT,
    actual_result TEXT,
    comparison_result VARCHAR(50) NULL,
    state VARCHAR(50) NOT NULL,
    evidence TEXT,
    created_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_result_code (result_code),
    KEY idx_result_execution (execution_code),
    KEY idx_result_state (state)
);

因此:

Execution
    ↓
Result

227.25 feedbacks

Feedback:

F=(R,S,Envf,C,T)F=(R,S,Env_f,C,T)

实际数据库可以保存:

CREATE TABLE feedbacks (
    id BIGINT NOT NULL AUTO_INCREMENT,
    feedback_code VARCHAR(100) NOT NULL,
    execution_code VARCHAR(100) NULL,
    result_code VARCHAR(100) NULL,
    feedback_type VARCHAR(100) NOT NULL,
    comparison_data TEXT,
    state_data TEXT,
    environment_data TEXT,
    evidence TEXT,
    created_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_feedback_code (feedback_code),
    KEY idx_feedback_execution (execution_code),
    KEY idx_feedback_result (result_code),
    KEY idx_feedback_type (feedback_type)
);

FeedbackEngine计算反馈,Repository保存反馈。


227.26 Memory与Experience表

记忆:

M=(I,C,W,T,R)M=(I,C,W,T,R)

经验:

E=(H,M,C,R,P)E=(H,M,C,R,P)

数据库至少包括:

memories
memory_relations
experiences
experience_relations

227.27 memories

CREATE TABLE memories (
    id BIGINT NOT NULL AUTO_INCREMENT,
    memory_code VARCHAR(100) NOT NULL,
    information_data TEXT,
    context_data TEXT,
    weight DECIMAL(12,6) NULL,
    memory_time DATETIME NOT NULL,
    relation_data TEXT,
    state VARCHAR(50) NOT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_memory_code (memory_code),
    KEY idx_memory_state (state),
    KEY idx_memory_time (memory_time)
);

MemoryEngine计算相关性和权重。

Repository只负责保存与查询。


227.28 experiences

CREATE TABLE experiences (
    id BIGINT NOT NULL AUTO_INCREMENT,
    experience_code VARCHAR(100) NOT NULL,
    history_data TEXT,
    memory_data TEXT,
    condition_data TEXT,
    result_data TEXT,
    pattern_data TEXT,
    state VARCHAR(50) NOT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_experience_code (experience_code),
    KEY idx_experience_state (state)
);

Experience 不能简单等于:

success_count
failure_count

而是结构化历史模式。


227.29 风险与冲突表

风险:

R=(C,E,P,I,S)R=(C,E,P,I,S)

冲突:

Cf=(O1,T,O2,K,S)C_f=(O_1,T,O_2,K,S)

核心表:

risks
conflicts
protections

这些已经在第224章建立。

数据库关系:

Risk
 ↓
Protection

Conflict
 ↓
Handling

风险计算属于 RiskEngine,冲突计算属于 ConflictEngine。


227.30 异常、诊断、修复、验证表

第225章建立:

abnormalities
diagnoses
diagnosis_causes
repairs
repair_verifications
diagnosis_repair_history

完整关系:

Abnormality
    ↓
Diagnosis
    ↓
Cause
    ↓
Repair
    ↓
Verification

形式化:

A→D→C→Rp→VA\rightarrow D\rightarrow C\rightarrow Rp\rightarrow V

数据库保存的是整个处理链,而不是只保存最后一个状态。


227.31 Learning学习表

Learning 不等于 Training。

ICAI 中:

Learning=Verified Data+Pattern+Change CandidateLearning = Verified\ Data + Pattern + Change\ Candidate

可以建立:

CREATE TABLE learning_records (
    id BIGINT NOT NULL AUTO_INCREMENT,
    learning_code VARCHAR(100) NOT NULL,
    learning_type VARCHAR(100) NOT NULL,
    target_type VARCHAR(100) NOT NULL,
    target_id BIGINT NULL,
    before_data TEXT,
    new_data TEXT,
    reason_data TEXT,
    evidence TEXT,
    verification_data TEXT,
    state VARCHAR(50) NOT NULL,
    created_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_learning_code (learning_code),
    KEY idx_learning_target (target_type, target_id),
    KEY idx_learning_type (learning_type),
    KEY idx_learning_state (state)
);

LearningEngine产生学习变化候选。

UpdateEngine负责正式应用变化。

Repository负责保存学习记录。


227.32 Update更新表

UpdateEngine需要保存更新过程:

CREATE TABLE update_records (
    id BIGINT NOT NULL AUTO_INCREMENT,
    update_code VARCHAR(100) NOT NULL,
    target_type VARCHAR(100) NOT NULL,
    target_id BIGINT NOT NULL,
    update_type VARCHAR(100) NOT NULL,
    before_data TEXT,
    change_data TEXT,
    after_data TEXT,
    reason_data TEXT,
    verification_data TEXT,
    state VARCHAR(50) NOT NULL,
    created_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_update_code (update_code),
    KEY idx_update_target (target_type, target_id),
    KEY idx_update_state (state)
);

更新过程:

Current+VerifiedChange→UpdatedCurrent + VerifiedChange \rightarrow Updated


227.33 历史表总体设计

ICAI 必须把“当前数据”和“历史数据”分开。

当前:

objects
methods
decisions
states
capabilities

历史:

object_history
method_history
decision_history
state_history
execution_history
update_history

原则:

CurrentData≠HistoryDataCurrentData \neq HistoryData

当前数据可以被更新。

历史数据原则上只追加。

因此:

Historyt+1=Historyt+ΔHHistory_{t+1}=History_t+\Delta H

而不是:

Historyt+1=Overwrite(Historyt)History_{t+1}=Overwrite(History_t)


227.34 历史表分类

ICAI 历史表可以按照对象分类:

IndividualHistory
ObjectHistory
StateHistory
GoalHistory
CapabilityHistory
MethodHistory
DecisionHistory
BehaviorHistory
ExecutionHistory
MemoryHistory
ExperienceHistory
RiskHistory
ConflictHistory
DiagnosisRepairHistory
LearningHistory
UpdateHistory

不一定每一张表在系统第一阶段都必须创建。

但数据库总体设计必须允许这些历史结构独立扩展。


227.35 History统一字段

多数历史表可以保持统一基础字段:

id
history_code
target_type
target_id
state_before
state_after
change_data
reason_data
evidence
created_at

例如:

CREATE TABLE update_history (
    id BIGINT NOT NULL AUTO_INCREMENT,
    history_code VARCHAR(100) NOT NULL,
    target_type VARCHAR(100) NOT NULL,
    target_id BIGINT NOT NULL,
    state_before VARCHAR(100) NULL,
    state_after VARCHAR(100) NULL,
    change_data TEXT,
    reason_data TEXT,
    evidence TEXT,
    created_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uk_update_history_code (history_code),
    KEY idx_update_history_target (target_type, target_id),
    KEY idx_update_history_time (created_at)
);

但是不能为了统一而牺牲业务语义。

例如 Decision History 和 Execution History 的核心字段不同,就应该保留自己的业务字段。


227.36 数据库中的关系类型

ICAI 数据库至少存在以下关系:

Individual → Object
Object → Attribute
Object → Object
Individual → Goal
Goal → Capability
Goal → Method
Method → Action
Decision → Candidate
Behavior → Action
Action → Execution
Execution → Result
Result → Feedback
Abnormality → Diagnosis
Diagnosis → Repair
Repair → Verification
Memory → Memory
Experience → Experience

因此数据库中的关系不应该全部混成一个模糊字段。

关系可以分为:

Relation=Structural+Cognitive+Runtime+HistoricalRelation= Structural + Cognitive + Runtime + Historical


227.37 外键设计原则

ICAI 不应该为了“数据库看起来完整”而对所有表建立大量强制外键。

因为部分 Domain 关系是动态的、跨模块的,部分对象生命周期可能不同。

例如:

execution_code

可能需要关联:

actions

但历史记录必须能够在原对象归档之后继续存在。

因此数据库设计需要考虑:

Reference Integrity
+
Historical Integrity
+
Lifecycle Independence

数据库外键约束与 Domain 关系规则不是同一回事:

DatabaseConstraint≠DomainRuleDatabaseConstraint \neq DomainRule


227.38 ID设计

ICAI 推荐采用:

Database ID
+
Domain Code

双标识设计。

例如:

id = 25
object_code = O-001

其中:

id

用于数据库内部定位。

object_code

用于 Domain 和业务层。

同样:

G-001
C-001
M-001
D-001
B-001
E-001
A-001
R-001
V-001

分别作为不同 Domain 的业务标识。

因此:

PersistenceID≠DomainIDPersistenceID \neq DomainID


227.39 JSON字段的使用原则

ICAI 数据库中部分结构适合保存为 JSON,例如:

condition_data
result_data
evidence
context_data
parameter_data

但是:

JSONField≠DomainObjectJSONField \neq DomainObject

JSON只是 Persistence Data 的一种表达形式。

例如:

Repair Domain
      ↓
RepairMapper
      ↓
method_data JSON

读取:

method_data JSON
      ↓
RepairMapper
      ↓
Repair Domain

因此不能因为数据库使用 JSON,就直接把数据库 JSON 当成 Domain Object。


227.40 MySQL索引总体设计

数据库查询主要围绕:

ID
Object
Individual
State
Type
Time
Relation
Status

建立索引。

例如:

individual_id
object_id
state
status
type
created_at
updated_at

对于关系表:

object1_id
object2_id
relation_type

对于历史表:

target_type
target_id
created_at

对于运行表:

behavior_code
action_code
execution_code

索引的目标是提高 Repository Query 的效率。

索引不是认知规则。

因此:

Index≠RuleIndex \neq Rule


227.41 数据生命周期

ICAI 数据生命周期:

Create→Validate→Persist→Read→Update→History→ArchiveCreate \rightarrow Validate \rightarrow Persist \rightarrow Read \rightarrow Update \rightarrow History \rightarrow Archive

对于当前对象:

Created
 ↓
Active
 ↓
Updated
 ↓
Inactive
 ↓
Archived

对于历史:

Created
 ↓
Immutable History
 ↓
Archive

历史原则上不能被当前更新覆盖。


227.42 数据保存事务

一个完整认知过程可能产生多个数据对象:

Goal
Method
Decision
Behavior
Execution
Result
Feedback

数据库事务可以协调这些持久化操作:

BEGIN
 ↓
Save Goal
 ↓
Save Decision
 ↓
Save Behavior
 ↓
Save Execution
 ↓
Save Result
 ↓
Save Feedback
 ↓
Save History
 ↓
ReadBack Verification
 ↓
COMMIT

失败:

ROLLBACK

但是:

DBRollback≠ExternalExecutionRollbackDBRollback \neq ExternalExecutionRollback

数据库只能回滚数据库操作。

已经发生的外部执行必须通过 ICAI 自身的 Repair / Recovery 机制处理。


227.43 数据一致性

ICAI 数据一致性分为四层:

第一层:数据库一致性

NOT NULL
UNIQUE
INDEX
FOREIGN KEY

第二层:Persistence一致性

Mapper
Repository

保证数据映射正确。

第三层:Domain一致性

Domain Object

保证对象结构合法。

第四层:Cognitive一致性

Engine
Rule
Condition
Verification

保证认知计算正确。

因此:

DBConsistency≠CognitiveConsistencyDBConsistency \neq CognitiveConsistency

数据库字段存在,并不代表认知事实正确。


227.44 数据库总体关系图

可以把 ICAI 数据库抽象为:

                    Individual
                        │
             ┌──────────┼──────────┐
             ↓          ↓          ↓
          Objects      Goals     Knowledge
             │           │
             ↓           ↓
          States      Capability
             │           ↓
             └──────→ Matching
                         ↓
                       Method
                         ↓
                      Decision
                         ↓
                      Behavior
                         ↓
                       Action
                         ↓
                     Execution
                         ↓
                       Result
                         ↓
                      Feedback
                         ↓
             ┌───────────┼────────────┐
             ↓           ↓            ↓
           Memory     Risk/Conflict  Abnormality
             ↓                        ↓
         Experience                Diagnosis
             ↓                        ↓
          Learning                  Repair
             ↓                        ↓
          Update                 Verification
             │                        │
             └──────────┬─────────────┘
                        ↓
                     History

这个结构体现了 ICAI 的核心数据流。


227.45 数据库与ICAI Engine体系

数据库不是 Engine。

完整关系:

ObjectEngine
      ↓
StateEngine
      ↓
RelationEngine
      ↓
SceneEngine
      ↓
KnowledgeEngine
      ↓
CapabilityEngine
      ↓
MatchingEngine
      ↓
MethodEngine
      ↓
DecisionEngine
      ↓
BehaviorEngine
      ↓
ActionEngine
      ↓
ExecutionEngine
      ↓
FeedbackEngine
      ↓
RiskEngine
      ↓
ConflictEngine
      ↓
DiagnosisEngine
      ↓
RepairEngine
      ↓
VerificationEngine
      ↓
LearningEngine
      ↓
UpdateEngine

每一个 Engine 都可以通过 Repository 获取所需事实,并将计算后的结果交给 Repository 持久化。

因此:

Engine→Domain→Repository→MySQLEngine \rightarrow Domain \rightarrow Repository \rightarrow MySQL

以及:

MySQL→Repository→Domain→EngineMySQL \rightarrow Repository \rightarrow Domain \rightarrow Engine

形成双向数据基础设施。


227.46 Repository体系与数据库

前面各章 Repository 可以统一映射到数据库:

Repository 核心表
IndividualRepository individuals
ObjectRepository objects
StateRepository statesstate_history
RelationRepository object_relations
KnowledgeRepository knowledge
GoalCapabilityRepository goalscapabilitiesmatching_records
MethodDecisionRepository methodsdecisions、历史表
BehaviorActionRepository behaviorsactionsexecutionsresults
MemoryExperienceRepository memoriesexperiences、关系表
RiskConflictRepository risksconflictsprotections
DiagnosisRepairRepository abnormalitiesdiagnosesrepairsrepair_verifications

由此:

Repository→TableGroupRepository \rightarrow TableGroup

而不是:

Repository=OneTableRepository = OneTable

一个 Repository 可以管理一个 Domain 协作边界下的多张表。


227.47 数据库模块目录建议

PHP工程中可以保持:

app/
├── Domain/
│   ├── Individual/
│   ├── Object/
│   ├── State/
│   ├── Relation/
│   ├── Knowledge/
│   ├── Goal/
│   ├── Capability/
│   ├── Method/
│   ├── Decision/
│   ├── Behavior/
│   ├── Action/
│   ├── Execution/
│   ├── Feedback/
│   ├── Memory/
│   ├── Experience/
│   ├── Risk/
│   ├── Conflict/
│   ├── Diagnosis/
│   ├── Repair/
│   ├── Verification/
│   ├── Learning/
│   └── Update/
│
├── Engines/
│
├── Services/
│
├── Repositories/
│
├── Infrastructure/
│   ├── Database/
│   │   ├── Connection.php
│   │   ├── TransactionManager.php
│   │   └── PDOFactory.php
│   │
│   └── Mapping/
│       ├── IndividualMapper.php
│       ├── ObjectMapper.php
│       ├── StateMapper.php
│       ├── RelationMapper.php
│       └── ...
│
└── Database/
    ├── migrations/
    ├── schema/
    └── seeds/

这样数据库结构与 PHP OOP 结构保持对应。


227.48 MySQL连接边界

Repository使用数据库连接:

class DatabaseConnection
{
    private $pdo;

    public function __construct($dsn, $username, $password)
    {
        $this->pdo = new PDO(
            $dsn,
            $username,
            $password,
            array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
            )
        );
    }

    public function getPdo()
    {
        return $this->pdo;
    }
}

Repository:

Repository
    ↓
DatabaseConnection
    ↓
PDO
    ↓
MySQL

Engine 不应该直接:

new PDO(...)

因为:

Engine≠PersistenceLayerEngine \neq PersistenceLayer


227.49 数据库设计的核心边界

最终可以建立以下边界:

Domain
    ↓
定义“是什么”

Engine
    ↓
计算“是什么、是否成立、如何变化”

Service
    ↓
组织“什么时候调用谁”

Repository
    ↓
保存和读取“已经形成的事实”

MySQL
    ↓
长期保存事实

因此:

Domain=StructureDomain=Structure Engine=CalculationEngine=Calculation Service=OrchestrationService=Orchestration Repository=PersistenceRepository=Persistence MySQL=StorageMySQL=Storage


227.50 ICAI数据库总体原则

第一:

Database≠BrainDatabase \neq Brain

MySQL 不是 ICAI 的“智能本体”,而是认知结构的数据持久化基础。

第二:

Storage≠CalculationStorage \neq Calculation

数据库保存计算结果,不负责执行认知计算。

第三:

Current≠HistoryCurrent \neq History

当前数据和历史数据必须区分。

第四:

State≠HistoryState \neq History

状态表和状态历史表必须区分。

第五:

Relation≠ObjectRelation \neq Object

关系是独立的结构。

第六:

Result≠FeedbackResult \neq Feedback

实际结果与反馈必须保持独立。

第七:

Diagnosis≠RepairDiagnosis \neq Repair

诊断和修复不能合并。

第八:

Repair≠VerificationRepair \neq Verification

执行修复不代表已经验证成功。

第九:

DomainID≠DatabaseIDDomainID \neq DatabaseID

业务标识和数据库主键可以分离。

第十:

Repository≠MySQLRepository \neq MySQL

Repository 是持久化抽象边界,MySQL 是具体存储实现。


227.51 ICAI数据库最终总体模型

综合前面的全部章节,可以得到:

ICAI Database=Core+Object+State+Relation+Cognitive+Runtime+Memory+Risk+Diagnosis+Learning+History\boxed{ ICAI\ Database = Core + Object + State + Relation + Cognitive + Runtime + Memory + Risk + Diagnosis + Learning + History }

完整结构:

Core
 ↓
Individual
 ↓
Object
 ↓
State
 ↓
Relation
 ↓
Goal
 ↓
Capability
 ↓
Matching
 ↓
Method
 ↓
Decision
 ↓
Behavior
 ↓
Action
 ↓
Execution
 ↓
Result
 ↓
Feedback
 ↓
Risk / Conflict / Abnormality
 ↓
Diagnosis
 ↓
Repair
 ↓
Verification
 ↓
Memory
 ↓
Experience
 ↓
Learning
 ↓
Update
 ↓
History

数据库最终承担的是:

事实保存+结构保存+状态保存+关系保存+过程保存+结果保存+历史保存\boxed{ 事实保存 + 结构保存 + 状态保存 + 关系保存 + 过程保存 + 结果保存 + 历史保存 }

而 ICAI 的计算能力仍然来自:

Domain Object
+
Rule
+
Condition
+
Engine
+
Runtime
+
History
+
Memory
+
Experience

而不是来自数据库本身。


227.52 本章总结

第227章建立了 ICAI MySQL 数据库总体架构。

数据库总体可以分为:

核心表
业务表
运行表
状态表
关系表
历史表
记忆经验表
风险维护表
学习更新表

核心数据关系为:

Individual→Object→State→RelationIndividual \rightarrow Object \rightarrow State \rightarrow Relation

认知关系为:

Goal→Capability→Matching→Method→DecisionGoal \rightarrow Capability \rightarrow Matching \rightarrow Method \rightarrow Decision

运行关系为:

Decision→Behavior→Action→Execution→Result→FeedbackDecision \rightarrow Behavior \rightarrow Action \rightarrow Execution \rightarrow Result \rightarrow Feedback

维护关系为:

Risk/Conflict/Abnormality→Diagnosis→Repair→VerificationRisk/Conflict/Abnormality \rightarrow Diagnosis \rightarrow Repair \rightarrow Verification

学习关系为:

Feedback→Memory→Experience→Learning→UpdateFeedback \rightarrow Memory \rightarrow Experience \rightarrow Learning \rightarrow Update

最终所有结构进入:

HistoryHistory

形成可追踪的数据生命周期。

因此 ICAI 的数据库不是简单的业务数据库,而是一个围绕 对象、状态、关系、认知、运行、异常、记忆、经验、学习和历史 建立的结构化持久化体系。

最终工程架构统一为:

Controller→Service→Engine→Domain→Mapper→Repository→PDO→MySQL\boxed{ Controller \rightarrow Service \rightarrow Engine \rightarrow Domain \rightarrow Mapper \rightarrow Repository \rightarrow PDO \rightarrow MySQL }

其中 MySQL 只承担数据存储职责,不承担认知计算职责。

这为后续真正进入 ICAI 的 数据库初始化、Migration、Repository实际实现、事务管理、数据生命周期管理以及整个 MySQL Schema 工程化落地 建立了总体基础。

Leave a Reply

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