1.10 感知元素面向对象模型
Object-Oriented Model of Perception Element
传统程序:
数据 + 函数
例如:
$data = [
"type"=>"Person",
"name"=>"Tom"
];
detect($data);
存在问题:
- 数据和处理分离;
- 无法扩展;
- 难以形成认知对象。
WSaiOS采用:
Object = Data + Behavior
即:
Perception Element Object
|
|
-----------------
| |
属性 方法
Attribute Function
例如:
人的感知元素:
PersonElement
{
id;
type;
attribute;
state;
detect();
update();
compare();
}
对应 PHP:
class PersonElement
{
private $id;
private $attribute;
private $state;
public function detect()
{
}
public function update()
{
}
}
这就是:
感知元素对象化。
1.11 WSaiOS 感知元素 Class设计
Perception Element Class Structure
WSaiOS核心对象:
Element
|
|
PerceptionElement
|
|
----------------------
PersonElement
VehicleElement
DeviceElement
EnvironmentElement
采用继承结构:
BaseElement
|
|
PerceptionElement
|
|
Specific Element
BaseElement
基础元素类。
文件:
/core
/element
Element.php
PHP:
<?php
abstract class Element
{
protected $id;
protected $type;
protected $created;
public function getID()
{
return $this->id;
}
public function getType()
{
return $this->type;
}
}
作用:
所有 WSaiOS 元素共有属性。
1.12 PerceptionElement实现
目录:
app
└── Models
└── PerceptionElement.php
代码:
class PerceptionElement extends Element
{
protected $source;
protected $attribute;
protected $state;
protected $confidence;
public function perceive($input)
{
// 数据解析
}
public function updateState($state)
{
$this->state=$state;
}
}
对应理论:
Data
↓
Information
↓
PerceptionElement Object
1.13 MVC架构中的感知元素
WSaiOS工程采用 MVC。
结构:
WSaiOS
├── Controller
│
├── Model
│
├── View
│
└── Engine
Model层
负责:
元素对象。
例如:
Model
|
PerceptionElement.php
作用:
保存:
- 属性;
- 状态;
- 关系。
Controller层
负责:
感知流程控制。
例如:
PerceptionController.php
代码:
class PerceptionController
{
public function analyze()
{
$data=$_POST['input'];
$element=new PerceptionElement();
$element->perceive($data);
return $element;
}
}
流程:
用户输入
↓
Controller
↓
PerceptionElement Object
↓
返回结果
1.14 Smarty模板显示感知结果
WSaiOS使用 Smarty 分离:
业务逻辑
和
显示。
结构:
view
|
templates
|
perception.tpl
Controller:
$smarty->assign(
"element",
$element
);
$smarty->display(
"perception.tpl"
);
Smarty:
<h2>
感知对象
</h2>
类型:
{$element->type}
状态:
{$element->state}
显示:
感知对象:
Person
状态:
Moving
形成:
PHP OOP
↓
MVC
↓
Smarty
↓
Human Interface
1.15 感知元素数据库设计
对象需要长期保存。
数据库:
MySQL
表:
perception_elements
结构:
| 字段 | 说明 |
|---|---|
| id | 编号 |
| type | 类型 |
| source | 来源 |
| attribute | 属性 |
| state | 状态 |
| confidence | 可信度 |
| created_at | 时间 |
SQL:
CREATE TABLE perception_elements
(
id INT AUTO_INCREMENT,
type VARCHAR(50),
source VARCHAR(100),
attribute TEXT,
state VARCHAR(50),
confidence INT,
created_at TIMESTAMP
);
对象:
对应:
数据库。
即:
Object
↓
ORM
↓
Database
1.16 Element Repository设计
为了避免:
Controller直接操作数据库。
增加:
Repository层。
结构:
Application
├── Controller
├── Model
├── Repository
└── View
代码:
class ElementRepository
{
public function save($element)
{
// insert database
}
public function find($id)
{
// select
}
}
形成:
Controller
↓
Repository
↓
Database
这是企业级 PHP OOP结构。
1.17 Smarty + MVC完整流程
完整链路:
用户输入
↓
Controller
↓
Service
↓
Element Object
↓
Repository
↓
Database
↓
Smarty
↓
HTML
例如:
分析:
electric toothbrush distributor Miami
进入系统:
Controller:
$request
↓
Service:
KeywordAnalysisService
↓
生成:
GeoElement
↓
保存:
geo_elements
↓
Smarty:
展示:
Miami
Distributor
Opportunity
这就是 WSaiOS GEO 系统工程化。
1.18 感知元素 Service层设计
大型系统:
Model不能承担业务。
增加 Service。
结构:
Service
├── PerceptionService.php
├── ElementService.php
└── AnalysisService.php
例如:
class PerceptionService
{
public function createElement($data)
{
$element=new PerceptionElement();
$element->perceive($data);
return $element;
}
}
MVC升级:
MVC
↓
MVC + Service Layer
1.19 WSaiOS 感知系统工程结构
最终:
WSaiOS
app
├── Controller
│
├── Model
│
├── Service
│
├── Repository
│
├── Engine
│
├── View
│
└── Core
对应人工认知:
| 人脑 | WSaiOS |
|---|---|
| 感觉 | Controller Input |
| 感觉神经 | Perception Engine |
| 感觉记忆 | Repository |
| 对象识别 | Element Class |
| 理解 | Cognitive Service |
| 推理 | Reasoning Engine |
1.20 本章工程总结
WSaiOS感知元素理论最终转换为:
理论模型
↓
Class对象模型
↓
MVC架构
↓
Service业务层
↓
Repository数据层
↓
Smarty表现层
↓
完整软件模块
因此:
感知元素不是一个数据表,而是一个面向对象的软件实体。
它具有:
- 身份 ID;
- 类型 Type;
- 属性 Attribute;
- 状态 State;
- 行为 Function;
- 生命周期 Lifecycle;
- 存储能力 Persistence;
- 扩展能力 Extension。
下一章建议:
第二章
感知元素分类理论与 PHP OOP 实现
重点:
- TextElement(文字感知元素)
- ImageElement(图片感知元素)
- VideoElement(视频感知元素)
- AudioElement(音频感知元素)
- GeoElement(空间感知元素)
- HealthElement(健康感知元素)
并设计:
Element Interface
↓
Abstract Class
↓
具体行业Element
↓
MVC模块
这部分会直接连接你现在的:
- WSaiOS GEO Analysis System
- WSaiOS HealthAnalysis
- WordPress GEO OS PRO
形成统一 OOP 核心架构。