WSaiOS EOM模拟人工认知工程理论
第四十四章:Smarty认知表现层设计——从认知结果到动态应用页面
44.1 为什么认知系统需要表现层
前面章节完成:
WSaiOS内部工程结构:
用户输入
↓
Controller
↓
Service
↓
Object
↓
Knowledge
↓
Matching
↓
Result
但是:
认知结果最终需要被用户理解。
例如:
系统内部:
$result=[
"product"=>"electric toothbrush",
"city"=>"Miami",
"score"=>90
];
用户不能直接看到:
PHP数组。
需要转换为:
可阅读的信息。
因此:
需要:
View表现层。
WSaiOS采用:
Smarty模板系统。
44.2 Smarty在WSaiOS中的定位
传统网站:
Smarty:
负责:
HTML模板。
WSaiOS:
Smarty负责:
认知结果表达。
结构:
Cognitive Result
↓
Smarty Template
↓
HTML Page
↓
User
也就是说:
Smarty不参与:
认知计算。
它负责:
把认知结果转换为:
人可以理解的信息。
44.3 表现层与认知层分离
错误设计:
<?php
//查询数据库
//计算评分
//生成HTML
?>
所有内容混合。
正确:
认知层
Element
Object
Knowledge
Matching
↓
表现层
Smarty
HTML
优势:
认知逻辑变化:
不影响页面。
页面变化:
不影响认知逻辑。
44.4 Smarty目录结构设计
WSaiOS:
views/
smarty/
├── layout/
│
│── header.tpl
│── footer.tpl
├── geo/
│
│── analysis.tpl
├── knowledge/
│
│── object.tpl
├── memory/
│
│── history.tpl
不同认知应用:
使用不同模板。
44.5 Controller向Smarty传递认知对象
Controller:
$result =
$cognitiveService
->analyze($keyword);
$smarty->assign(
"result",
$result
);
$smarty->display(
"analysis.tpl"
);
Smarty获得:
认知结果对象。
44.6 Smarty显示认知对象
例如:
认知对象:
Supplier Object
{
name:
ABC Factory
product:
Electric Toothbrush
location:
China
score:
90
}
模板:
analysis.tpl
<h1>
供应商认知分析
</h1>
名称:
{$result.name}
产品:
{$result.product}
地区:
{$result.location}
匹配评分:
{$result.score}
输出:
供应商认知分析
ABC Factory
产品:
Electric Toothbrush
地区:
China
匹配:
90分
44.7 Smarty循环显示对象关系
EOM:
对象之间存在关系。
例如:
Supplier
↓
Supply
↓
Product
PHP:
传递:
$relations=[
[
"source"=>"Factory A",
"type"=>"Supply",
"target"=>"Electric Toothbrush"
]
];
Smarty:
<table>
{foreach $relations as $r}
<tr>
<td>
{$r.source}
</td>
<td>
{$r.type}
</td>
<td>
{$r.target}
</td>
</tr>
{/foreach}
</table>
显示:
| 对象 | 关系 | 对象 |
|---|---|---|
| Factory A | Supply | Electric Toothbrush |
44.8 Smarty显示知识结构
知识:
不是单一数据。
结构:
对象
+
关系
+
经验
页面:
知识:
电动牙刷供应商
相关对象:
Factory A
关系:
生产
经验:
美国市场成功率85%
模板:
<h2>
知识结构
</h2>
{$knowledge.name}
{foreach $knowledge.relations as $relation}
{$relation.source}
{$relation.type}
{$relation.target}
{/foreach}
44.9 Smarty生成GEO页面
WSaiOS GEO应用:
需要大量页面。
例如:
标题:
Electric Toothbrush Supplier Miami Florida
数据:
$page=[
"title"=>"Electric Toothbrush Supplier Miami Florida",
"product"=>"Electric Toothbrush",
"city"=>"Miami",
"faq"=>$faq
];
模板:
<h1>
{$page.title}
</h1>
<h2>
Product
</h2>
{$page.product}
<h2>
Location
</h2>
{$page.city}
生成:
SEO/GEO页面。
44.10 Smarty与WordPress内容生成结合
WSaiOS:
可以作为:
内容认知引擎。
流程:
认知对象
↓
内容对象
↓
Smarty模板
↓
HTML
↓
WordPress
例如:
生成:
Electric Toothbrush Distributor in Miami
Market Analysis
Supplier Information
FAQ
Schema
44.11 Smarty模板组件化设计
大型系统:
不能只有一个模板。
设计:
组件。
例如:
components/
object_card.tpl
relation_graph.tpl
memory_panel.tpl
score_panel.tpl
主页面:
组合:
{include file="object_card.tpl"}
{include file="score_panel.tpl"}
类似:
认知模块组合。
44.12 表现层对应EOM模型
对应关系:
| EOM | Smarty表现 |
|---|---|
| Element | 信息标签 |
| Object | 对象卡片 |
| Relationship | 关系列表 |
| Knowledge | 知识页面 |
| Memory | 历史记录 |
| Decision | 推荐结果 |
44.13 完整页面生成流程
案例:
输入:
electric toothbrush supplier Miami
内部:
Element
↓
Object
↓
Knowledge
↓
Matching
↓
Result Object
表现:
Smarty
↓
HTML
↓
用户浏览
44.14 Smarty表现层工程原则
原则一:
模板不写业务逻辑。
错误:
{if score>80}
推荐
{/if}
大量判断。
应该:
PHP处理:
$result.status="推荐";
模板显示:
{$result.status}
原则二:
所有输出来源于对象。
不是:
直接拼接字符串。
原则三:
模板组件化。
方便:
多个行业应用复用。
44.15 本章总结
Smarty在WSaiOS中:
不是简单网页模板。
它是:
人工认知结果表达层。
完整架构:
EOM认知模型
↓
PHP OOP对象
↓
MVC Service处理
↓
Smarty模板表达
↓
用户理解
WSaiOS工程体系:
PHP OOP
负责:
认知对象
MVC
负责:
流程组织
Smarty
负责:
认知表达
MySQL
负责:
知识保存
下一章:
第四十五章:WSaiOS MySQL认知知识库设计——对象、关系、记忆的数据结构实现
重点研究:
- 为什么知识库不是普通数据表;
- Object表设计;
- Relationship表设计;
- Memory表设计;
- PHP Model如何操作认知数据库;
- MVC与MySQL知识库连接。