第七十五章
WSaiOS Service Framework Implementation
服务框架工程实现
75.1 为什么需要Service Framework
目前WSaiOS已经拥有:
Memory Engine
Knowledge Engine
Reasoning Engine
Decision Engine
Execution Engine
如果:
Reasoning:
直接:
knowledge.query()
decision.run()
那么:
所有模块:
都会:
互相引用。
最终形成:
A
↓
B
↓
C
↓
A
循环依赖。
维护成本极高。
因此:
WSaiOS采用:
Service Framework。
工程定义
Service:
是:
具有统一生命周期、统一接口、统一通信方式的软件模块。
例如:
Knowledge Service
Reasoning Service
Decision Service
Execution Service
全部:
属于:
Service。
75.2 Service架构
整体:
Kernel
↓
Service Manager
↓
Service Registry
↓
Knowledge Service
Reasoning Service
Decision Service
Execution Service
Service之间:
禁止:
直接new对象。
统一:
通过:
Service Manager。
75.3 Base Service
所有服务:
继承:
BaseService。
class BaseService:
def start(self):
pass
def stop(self):
pass
def execute(self,data):
pass
以后:
所有模块:
全部统一。
75.4 Service生命周期
统一:
Created
↓
Initialized
↓
Running
↓
Paused
↓
Stopped
↓
Destroyed
生命周期:
由:
Kernel:
统一管理。
而不是:
模块:
自己决定。
75.5 Service Registry
负责:
保存:
全部Service。
class ServiceRegistry:
def register(
self,
service
):
pass
例如:
Knowledge
↓
Registry
↓
Reasoning
↓
Registry
↓
Execution
任何模块:
都可以:
通过:
Registry:
找到:
Service。
75.6 Service Manager
Manager负责:
启动:
全部服务。
例如:
manager.start_all()
内部:
Knowledge.start()
Reasoning.start()
Decision.start()
Execution.start()
统一:
管理。
75.7 Service通信
WSaiOS:
Service:
禁止:
直接调用。
统一:
发送:
Message。
例如:
Reasoning
↓
Event Bus
↓
Decision
以后:
替换:
远程RPC:
几乎不用修改业务。
75.8 Service Descriptor
每个Service:
拥有:
描述信息。
class ServiceDescriptor:
name="Knowledge"
version="1.0"
author="WSaiOS"
以后:
插件:
也使用:
Descriptor。
75.9 Service配置
每个Service:
拥有:
config。
例如:
knowledge.yaml
reasoning.yaml
decision.yaml
统一:
加载。
以后:
无需:
修改代码。
75.10 Service目录
service/
├── base_service.py
├── manager.py
├── registry.py
├── descriptor.py
├── loader.py
└── config.py
75.11 Service Loader
Kernel:
启动:
Config
↓
Loader
↓
Create Service
↓
Register
↓
Start
Loader:
以后:
可以:
动态加载。
75.12 最小实验
建立:
KnowledgeService。
实现:
start()
execute()
stop()
Kernel:
启动:
Runtime
↓
Loader
↓
Knowledge Service
↓
Running
验证:
Registry:
是否:
存在:
Knowledge。
Manager:
是否:
成功启动。
75.13 WSaiOS Service Framework v1.0
Kernel
│
Service Manager
│
Service Registry
│
┌─────────┬─────────┬─────────┐
▼ ▼ ▼
Knowledge Reasoning Decision
Service Service Service
│
▼
Execution Service
75.14 本章总结
完成:
WSaiOS Service Framework
实现:
- Base Service
- 生命周期管理
- Service Registry
- Service Manager
- Service Loader
- Service Descriptor
- Service Config
- Service Messaging
这里开始,WSaiOS已经具备了一个真正的软件内核雏形。
目前的工程主线已经非常完整:
Perception
│
Memory
│
Knowledge
│
Reasoning
│
Decision
│
Runtime Scheduler
│
Execution
│
Experience
│
Optimization
再往上,还有一层一直没有正式定义,但对于一个可扩展系统非常关键,那就是 Module Framework(模块框架)。Service 解决的是“运行”,Module 解决的是“组织与扩展”。
因此,我建议下一章不是插件,而是:
第七十六章《WSaiOS Module Framework Implementation(模块框架工程实现)》。
这样整个 WSaiOS 将形成:
Kernel
│
Module
│
Service
│
Runtime
│
Scheduler
│
Execution
这个层次更符合可验证、可扩展、可维护的软件工程体系。