WSaiOS EOM模拟人工认知工程理论
第四十八章:WSaiOS工程开发规范——PHP OOP项目结构、代码组织与模块扩展
48.1 为什么需要工程开发规范
前面的章节完成:
WSaiOS认知工程模型:
EOM理论
↓
Element
↓
Object
↓
Relationship
↓
Knowledge
↓
Memory
↓
Matching
并实现:
PHP OOP
+
MVC
+
Smarty
+
MySQL
但是:
一个真正的软件系统不能依靠单个文件堆积。
如果没有工程规范:
随着功能增加:
GEO系统
健康分析系统
企业知识系统
用户个人助手
都会出现:
- Class重复;
- 数据混乱;
- 逻辑耦合;
- 模块无法复用。
因此:
WSaiOS需要:
面向对象工程规范。
48.2 WSaiOS核心工程思想
传统软件:
以功能为中心:
用户管理
订单管理
商品管理
WSaiOS:
以认知对象为中心:
Object
↓
Relationship
↓
Knowledge
↓
Application
工程结构:
Core
负责认知能力
Application
负责行业应用
例如:
WSaiOS
├── Core
│
├── CognitiveObject
├── Memory
├── Matching
├── Knowledge
└── Applications
├── GEO
├── Health
└── Enterprise
48.3 WSaiOS项目整体目录规范
推荐结构:
wsaios/
app/
├── Core/
│
├── Element/
│
├── Object/
│
├── Relationship/
│
├── Knowledge/
│
├── Memory/
│
└── Matching/
├── Applications/
│
├── GEO/
│
├── Health/
│
└── Enterprise/
├── Controllers/
├── Services/
├── Models/
├── Views/
├── Config/
├── Database/
├── Public/
└── Vendor/
核心原则:
Core不依赖业务。
业务依赖Core。
48.4 Class设计规范
PHP OOP中:
Class是认知能力的基本单位。
1. 一个Class只负责一个对象
错误:
class Everything
{
分析
数据库
页面
发布
}
正确:
class CognitiveObject
{
对象定义
}
class Memory
{
记忆管理
}
class Matcher
{
匹配计算
}
符合:
单一职责原则。
48.5 CognitiveObject基类规范
所有认知对象:
继承:
CognitiveObject
基础:
class CognitiveObject
{
protected $id;
protected $type;
protected $attributes=[];
protected $state;
protected $relations=[];
public function addAttribute(
$key,
$value
)
{
$this->attributes[$key]=$value;
}
public function addRelation(
$relation
)
{
$this->relations[]=$relation;
}
}
作用:
提供统一认知结构。
48.6 Interface接口设计规范
为什么需要Interface?
因为:
不同对象:
可能具有相同能力。
例如:
供应商:
可以匹配。
产品:
也可以匹配。
定义:
interface Matchable
{
public function match($object);
}
供应商:
class SupplierObject
implements Matchable
{
public function match($object)
{
}
}
产品:
class ProductObject
implements Matchable
{
public function match($object)
{
}
}
实现:
能力统一。
48.7 Service服务层规范
Service:
是认知流程执行中心。
原则:
Controller不能写业务逻辑。
错误:
Controller
查询数据库
计算评分
生成页面
正确:
Controller
↓
Service
↓
Object
↓
Model
例如:
class CognitiveAnalysisService
{
public function analyze($input)
{
$object=
$this->buildObject($input);
$result=
$this->matching($object);
return $result;
}
}
48.8 Model数据访问规范
Model:
负责:
知识库访问。
不负责:
认知判断。
例如:
正确:
class KnowledgeModel
{
public function findObject($id)
{
return database result;
}
}
错误:
class KnowledgeModel
{
判断市场机会
推荐供应商
生成报告
}
Model:
只是:
数据桥梁。
48.9 MVC目录规范
标准:
Controllers
↓
接收请求
Services
↓
执行认知流程
Models
↓
访问知识库
Views
↓
结果表达
对应:
Controller
= 感知入口
Service
= 认知处理
Model
= 知识记忆
Smarty
= 表达输出
48.10 WSaiOS模块扩展方式
WSaiOS设计:
核心 + 应用。
新增行业:
不修改核心。
例如:
增加:
健康分析。
只需要:
新增:
Applications/
Health/
├── Objects
├── Services
├── Controllers
├── Models
调用:
Core:
Element
Object
Memory
Matching
48.11 GEO应用结构
例如:
GEO:
Applications/GEO/
Objects/
GeoDemandObject.php
Services/
GeoAnalysisService.php
Models/
GeoMarketModel.php
Controllers/
GeoController.php
继承:
class GeoDemandObject
extends CognitiveObject
{
}
48.12 健康应用结构
健康分析:
不是重新开发。
使用:
同样核心。
结构:
Applications/Health/
Objects/
HealthObject.php
Services/
HealthAnalysisService.php
Models/
HealthMemoryModel.php
对象:
class HealthObject
extends CognitiveObject
{
public $bloodPressure;
public $weight;
}
调用:
Core:
Memory
Matching
Knowledge
48.13 企业知识系统结构
企业:
例如:
供应链分析。
对象:
Supplier
Customer
Product
Market
结构:
Applications/Enterprise/
Objects/
SupplierObject.php
CustomerObject.php
Services/
BusinessAnalysisService.php
48.14 三类应用共享架构
最终:
WSaiOS Core
Element
Object
Relation
Knowledge
Memory
Matching
|
--------------------------------
GEO Health Enterprise
核心不变。
变化的是:
业务对象。
48.15 配置管理规范
Config:
统一管理。
例如:
config/
database.php
memory.php
matching.php
application.php
数据库:
return [
'host'=>'localhost',
'name'=>'wsaios'
];
避免:
代码中写死。
48.16 自动加载规范
使用:
Autoload。
例如:
spl_autoload_register(
function($class)
{
require_once
$class.'.php';
});
以后:
直接:
new CognitiveObject();
无需:
大量require。
48.17 代码命名规范
Class:
大写:
CognitiveObject
MemoryEngine
MatchingService
Method:
小驼峰:
addRelation()
findKnowledge()
calculateScore()
数据表:
小写:
cognitive_objects
memories
relationships
48.18 WSaiOS开发流程规范
新增功能:
流程:
第一步:
定义认知对象。
例如:
SupplierObject
第二步:
定义关系。
例如:
SupplyRelation
第三步:
设计知识模型。
第四步:
建立Service。
第五步:
建立Controller。
第六步:
Smarty输出。
完整:
对象设计
↓
数据库设计
↓
Service实现
↓
MVC连接
↓
页面展示
48.19 本章总结
WSaiOS工程规范核心:
不是传统MVC网站开发。
而是:
面向认知对象的软件工程。
整体结构:
EOM理论
↓
PHP OOP
↓
Class对象
↓
MVC架构
↓
Smarty表达
↓
MySQL知识库
↓
行业应用
最终形成:
WSaiOS Cognitive Framework
Core
+
Application
=
可扩展人工认知软件系统
下一章:
第四十九章:WSaiOS认知内核(Cognitive Kernel)工程设计——从应用框架到人工认知OS
重点研究:
- Cognitive Kernel为什么存在;
- Core与Kernel区别;
- 感知、对象化、匹配、记忆调度;
- Kernel如何管理认知流程;
- WSaiOS如何从软件框架升级为认知操作系统。