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

第四十三章:WSaiOS MVC认知应用架构实现——Controller、Service、Model分层设计

WSaiOS EOM模拟人工认知工程理论

第四十三章:WSaiOS MVC认知应用架构实现——Controller、Service、Model分层设计


43.1 为什么认知系统需要MVC架构

前面章节解决:

认知模型如何转换为PHP对象。

但是:

一个完整应用还需要解决:

用户如何调用认知对象?
认知逻辑如何组织?
数据如何保存?
结果如何展示?


如果所有代码写在一个PHP文件:

例如:

analysis.php

里面同时包含:

接收用户输入

↓

解析元素

↓

生成对象

↓

查询数据库

↓

计算匹配

↓

输出HTML

问题:

  • 代码无法维护;
  • 业务逻辑混乱;
  • 认知模块无法复用。

因此:

WSaiOS采用:

MVC + Service工程结构。


43.2 MVC在WSaiOS中的角色

传统MVC:

Model

数据


View

显示


Controller

控制

WSaiOS扩展:

Controller

请求管理


↓

Service

认知流程


↓

Model

知识数据


↓

Database

知识库


↓

Smarty

结果表达

增加:

Service层。


原因:

认知逻辑不能放Controller。


43.3 WSaiOS MVC整体目录结构

工程:

wsaios/


app/


├── controllers/

│
│── GeoController.php


├── services/

│
│── CognitiveService.php

│── MatchingService.php


├── models/

│
│── ObjectModel.php

│── KnowledgeModel.php

│── MemoryModel.php


├── classes/

│
│── CognitiveObject.php

│── Relationship.php


├── views/

│
│── smarty/

│     └── result.tpl



config/

database/

public/

43.4 Controller控制层设计

Controller职责:

接收请求。


例如:

用户输入:

electric toothbrush supplier Miami

Controller:

负责:

接收

↓

调用Service

↓

获取结果

↓

传递View

代码:

class GeoController
{


private $cognitiveService;



public function __construct()
{

$this->cognitiveService=
new CognitiveService();

}



public function analyze()
{


$keyword=
$_POST['keyword'];



$result=
$this->cognitiveService
->analyze($keyword);



$smarty=
new Smarty();


$smarty->assign(
"result",
$result
);


$smarty->display(
"result.tpl"
);


}


}

Controller:

不负责:

  • 匹配;
  • 评分;
  • 知识查询。

43.5 Service认知服务层设计

Service:

是WSaiOS核心层。


负责:

认知流程。


例如:

CognitiveService:

class CognitiveService
{


public function analyze($input)
{


//1 元素解析


$elements=
$this->elementService
->parse($input);



//2 对象生成


$object=
$this->objectService
->build($elements);



//3 知识匹配


$result=
$this->matchingService
->match($object);



return $result;


}


}

这里对应:

EOM:

元素

↓

对象

↓

关系

↓

知识

↓

匹配

43.6 ElementService设计

负责:

信息拆解。


输入:

electric toothbrush supplier Miami

输出:

Element:

product

role

location

代码:

class ElementService
{


public function parse($text)
{


$elements=[];


//规则解析


return $elements;


}


}

43.7 ObjectService设计

负责:

对象形成。


代码:

class ObjectService
{


public function build($elements)
{


$object=
new CognitiveObject();



foreach($elements as $element)
{


$object
->addElement($element);


}



return $object;


}


}

对应:

EOM:

Element

↓

Object

43.8 MatchingService设计

匹配:

是认知系统的重要功能。


流程:

对象

↓

知识库

↓

关系

↓

记忆

↓

评分

代码:

class MatchingService
{


private $knowledgeModel;


private $memoryModel;



public function match($object)
{


$knowledge=
$this->knowledgeModel
->find($object);



$memory=
$this->memoryModel
->recall($object);



$score=

$this->calculate(
$knowledge,
$memory
);



return [

"score"=>$score,

"knowledge"=>$knowledge

];


}


}

43.9 Model数据层设计

Model负责:

数据库访问。


例如:

KnowledgeModel:

class KnowledgeModel
{


private $db;



public function find($object)
{


$sql="

SELECT *

FROM knowledge

WHERE type='supplier'

";


return $this->db
->query($sql);


}


}

Model不负责:

认知判断。


只负责:

数据获取。


43.10 MVC数据流

完整过程:

用户输入


↓

Controller


↓

CognitiveService


↓

ElementService


↓

ObjectService


↓

MatchingService


↓

KnowledgeModel


↓

MemoryModel


↓

返回结果


↓

Smarty


↓

HTML

43.11 Smarty View层设计

View:

负责:

结果展示。


例如:

result.tpl:

<h2>
WSaiOS认知分析结果
</h2>


产品:

{$result.product}


地区:

{$result.city}


角色:

{$result.role}


匹配评分:

{$result.score}

Smarty不参与:

认知计算。


43.12 MVC与EOM对应关系

EOM结构 MVC实现
Element ElementService
Object ObjectService
Relationship RelationshipService
Knowledge KnowledgeModel
Memory MemoryModel
Matching MatchingService
Decision DecisionService

43.13 一个完整执行案例

用户:

输入:

electric toothbrush distributor Miami Florida

Controller接收

GeoController

Service处理

进入:

CognitiveService

Element解析

得到:

electric toothbrush

distributor

Miami

Object形成

生成:

DemandObject

Knowledge查询

找到:

Supplier Objects

Memory调用

读取:

历史匹配经验

输出:

市场机会:

High


匹配:

90%

43.14 MVC工程优势

采用MVC以后:

第一:

模块独立。

修改页面:

不用修改认知逻辑。


第二:

知识库可替换。

MySQL:

可以替换其他存储。


第三:

认知模块复用。

GEO:

可以调用。

健康:

也可以调用。


43.15 本章总结

WSaiOS MVC架构:

不是普通网页MVC。

而是:

面向认知工程的MVC。


结构:

Controller

负责请求


Service

负责认知流程


Model

负责知识数据


Smarty

负责认知表达

最终形成:

PHP OOP

+

MVC

+

Smarty

+

EOM对象模型

=

WSaiOS认知应用框架

下一章:

第四十四章:Smarty认知表现层设计——从认知结果到动态应用页面

重点研究:

  • 为什么认知系统需要独立表现层;
  • Smarty模板如何接收认知对象;
  • GEO页面、分析报告、知识展示如何生成;
  • 模板系统如何对应认知结果表达。

Leave a Reply

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