190 lines
5.6 KiB
Python
190 lines
5.6 KiB
Python
"""
|
|
状态机插件
|
|
提供完整的有限状态机功能用于管理对象状态
|
|
包含状态管理、转换系统、上下文管理等完整功能
|
|
"""
|
|
|
|
from plugins.plugin_manager import BasePlugin
|
|
from typing import Dict, Optional, Any
|
|
|
|
class Plugin(BasePlugin):
|
|
"""
|
|
状态机插件
|
|
提供完整的有限状态机功能用于管理对象状态
|
|
"""
|
|
|
|
def __init__(self, plugin_manager, name):
|
|
super().__init__(plugin_manager, name)
|
|
self.config = {
|
|
"version": "1.0.0",
|
|
"author": "EG Team",
|
|
"description": "状态机插件,用于管理对象状态,包含完整的状态管理、转换系统和上下文管理"
|
|
}
|
|
self.state_machines = {}
|
|
self.machine_manager = None
|
|
self.is_initialized = False
|
|
|
|
def initialize(self) -> bool:
|
|
"""
|
|
初始化插件
|
|
"""
|
|
if self.is_initialized:
|
|
return True
|
|
|
|
try:
|
|
# 导入核心模块
|
|
from plugins.user.state_machine.core.state_machine import StateMachineManager
|
|
|
|
# 创建管理器实例
|
|
self.machine_manager = StateMachineManager()
|
|
|
|
print(f"初始化状态机插件: {self.name}")
|
|
self.is_initialized = True
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"状态机插件初始化失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
def enable(self) -> bool:
|
|
"""
|
|
启用插件
|
|
"""
|
|
if not super().enable():
|
|
return False
|
|
|
|
if not self.is_initialized:
|
|
if not self.initialize():
|
|
return False
|
|
|
|
print(f"启用状态机插件: {self.name}")
|
|
return True
|
|
|
|
def disable(self) -> bool:
|
|
"""
|
|
禁用插件
|
|
"""
|
|
if not super().disable():
|
|
return False
|
|
|
|
# 停止所有激活的状态机
|
|
if self.machine_manager:
|
|
self.machine_manager.active_machines.clear()
|
|
|
|
print(f"禁用状态机插件: {self.name}")
|
|
return True
|
|
|
|
def finalize(self) -> None:
|
|
"""
|
|
清理插件资源
|
|
"""
|
|
print(f"清理状态机插件资源: {self.name}")
|
|
|
|
try:
|
|
# 停止所有激活的状态机
|
|
if self.machine_manager:
|
|
# 停止所有状态机
|
|
for machine in self.machine_manager.state_machines.values():
|
|
if machine.is_running:
|
|
machine.stop()
|
|
# 清空激活列表
|
|
self.machine_manager.active_machines.clear()
|
|
|
|
# 清理资源
|
|
self.state_machines.clear()
|
|
self.machine_manager = None
|
|
self.is_initialized = False
|
|
|
|
print(f"状态机插件资源清理完成: {self.name}")
|
|
|
|
except Exception as e:
|
|
print(f"清理状态机插件资源失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
def create_state_machine(self, name: str) -> Optional['StateMachine']:
|
|
"""
|
|
创建状态机
|
|
"""
|
|
try:
|
|
from plugins.user.state_machine.core.state_machine import StateMachine
|
|
machine = StateMachine(name)
|
|
self.state_machines[name] = machine
|
|
|
|
# 添加到管理器
|
|
if self.machine_manager:
|
|
self.machine_manager.add_machine(name, machine)
|
|
|
|
return machine
|
|
except Exception as e:
|
|
print(f"创建状态机失败: {e}")
|
|
return None
|
|
|
|
def get_state_machine(self, name: str) -> Optional['StateMachine']:
|
|
"""
|
|
获取状态机实例
|
|
"""
|
|
return self.state_machines.get(name)
|
|
|
|
def remove_state_machine(self, name: str) -> bool:
|
|
"""
|
|
移除状态机
|
|
"""
|
|
if name in self.state_machines:
|
|
# 从管理器移除
|
|
if self.machine_manager:
|
|
self.machine_manager.remove_machine(name)
|
|
del self.state_machines[name]
|
|
return True
|
|
return False
|
|
|
|
def get_machine_manager(self) -> Optional['StateMachineManager']:
|
|
"""
|
|
获取状态机管理器
|
|
"""
|
|
return self.machine_manager
|
|
|
|
def update_all_machines(self) -> Dict[str, bool]:
|
|
"""
|
|
更新所有激活的状态机
|
|
"""
|
|
if self.machine_manager:
|
|
return self.machine_manager.update_all()
|
|
return {}
|
|
|
|
def get_machine_statistics(self, name: str) -> Optional[Dict[str, Any]]:
|
|
"""
|
|
获取状态机统计信息
|
|
"""
|
|
if self.machine_manager:
|
|
return self.machine_manager.get_machine_stats(name)
|
|
return None
|
|
|
|
def get_all_statistics(self) -> Dict[str, Dict[str, Any]]:
|
|
"""
|
|
获取所有状态机统计信息
|
|
"""
|
|
if self.machine_manager:
|
|
return self.machine_manager.get_all_stats()
|
|
return {}
|
|
|
|
def start_all_machines(self) -> Dict[str, bool]:
|
|
"""
|
|
启动所有状态机
|
|
"""
|
|
if self.machine_manager:
|
|
return self.machine_manager.start_all()
|
|
return {}
|
|
|
|
def stop_all_machines(self) -> Dict[str, bool]:
|
|
"""
|
|
停止所有状态机
|
|
"""
|
|
if self.machine_manager:
|
|
return self.machine_manager.stop_all()
|
|
return {}
|
|
|
|
# 兼容性接口
|
|
Plugin = Plugin |