218 lines
6.7 KiB
Python
218 lines
6.7 KiB
Python
"""
|
|
测试行为树和状态机插件功能
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
|
|
# 添加项目根目录到Python路径
|
|
sys.path.insert(0, '/home/hello/EG')
|
|
|
|
def test_behavior_tree_plugin():
|
|
"""
|
|
测试行为树插件功能
|
|
"""
|
|
print("开始测试行为树插件...")
|
|
|
|
# 导入插件管理器
|
|
from plugins.plugin_manager import PluginManager
|
|
|
|
# 创建一个虚拟的世界对象(用于测试)
|
|
class MockWorld:
|
|
def __init__(self):
|
|
self.accept = lambda event, handler: None
|
|
self.ignore = lambda event: None
|
|
|
|
# 创建世界实例
|
|
world = MockWorld()
|
|
|
|
# 创建插件管理器
|
|
plugin_manager = PluginManager(world)
|
|
plugin_manager.enable_plugin_system()
|
|
|
|
# 加载行为树插件
|
|
bt_plugin = plugin_manager.load_plugin("behavior_tree")
|
|
if not bt_plugin:
|
|
print("❌ 无法加载行为树插件")
|
|
return False
|
|
|
|
# 启用行为树插件
|
|
if not plugin_manager.enable_plugin("behavior_tree"):
|
|
print("❌ 无法启用行为树插件")
|
|
return False
|
|
|
|
print("✅ 行为树插件加载和启用成功")
|
|
|
|
# 导入示例并运行
|
|
try:
|
|
from plugins.user.behavior_tree.examples.patrol_example import run_patrol_example
|
|
run_patrol_example(bt_plugin)
|
|
print("✅ 行为树示例运行成功")
|
|
except Exception as e:
|
|
print(f"❌ 行为树示例运行失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
# 测试基本节点功能
|
|
try:
|
|
from plugins.user.behavior_tree import SelectorNode, SequenceNode, ActionNode, ConditionNode
|
|
|
|
# 创建一个简单的序列节点
|
|
sequence = SequenceNode("TestSequence")
|
|
|
|
# 添加一个条件节点
|
|
condition = ConditionNode("TestCondition", lambda bb: True)
|
|
sequence.add_child(condition)
|
|
|
|
# 添加一个动作节点
|
|
action = ActionNode("TestAction", lambda bb: print("测试动作执行") or True)
|
|
sequence.add_child(action)
|
|
|
|
# 创建黑板
|
|
blackboard = bt_plugin.create_blackboard("test_blackboard")
|
|
|
|
# 创建行为树
|
|
tree = bt_plugin.create_behavior_tree("test_tree", sequence)
|
|
tree.set_blackboard(blackboard)
|
|
|
|
# 执行行为树
|
|
result = tree.run()
|
|
print(f"✅ 基本节点功能测试成功,执行结果: {result.value}")
|
|
|
|
except Exception as e:
|
|
print(f"❌ 基本节点功能测试失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
# 禁用和卸载插件
|
|
plugin_manager.disable_plugin("behavior_tree")
|
|
plugin_manager.unload_plugin("behavior_tree")
|
|
|
|
print("✅ 行为树插件测试完成")
|
|
return True
|
|
|
|
def test_state_machine_plugin():
|
|
"""
|
|
测试状态机插件功能
|
|
"""
|
|
print("开始测试状态机插件...")
|
|
|
|
# 导入插件管理器
|
|
from plugins.plugin_manager import PluginManager
|
|
|
|
# 创建一个虚拟的世界对象(用于测试)
|
|
class MockWorld:
|
|
def __init__(self):
|
|
self.accept = lambda event, handler: None
|
|
self.ignore = lambda event: None
|
|
|
|
# 创建世界实例
|
|
world = MockWorld()
|
|
|
|
# 创建插件管理器
|
|
plugin_manager = PluginManager(world)
|
|
plugin_manager.enable_plugin_system()
|
|
|
|
# 加载状态机插件
|
|
sm_plugin = plugin_manager.load_plugin("state_machine")
|
|
if not sm_plugin:
|
|
print("❌ 无法加载状态机插件")
|
|
return False
|
|
|
|
# 启用状态机插件
|
|
if not plugin_manager.enable_plugin("state_machine"):
|
|
print("❌ 无法启用状态机插件")
|
|
return False
|
|
|
|
print("✅ 状态机插件加载和启用成功")
|
|
|
|
# 导入示例并运行
|
|
try:
|
|
from plugins.user.state_machine.examples.ai_example import run_ai_example
|
|
run_ai_example(sm_plugin)
|
|
print("✅ 状态机示例运行成功")
|
|
except Exception as e:
|
|
print(f"❌ 状态机示例运行失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
# 测试基本状态功能
|
|
try:
|
|
from plugins.user.state_machine import State, StateMachine
|
|
|
|
# 创建一个简单的状态
|
|
class TestState(State):
|
|
def enter(self, context):
|
|
print("进入测试状态")
|
|
|
|
def execute(self, context):
|
|
print("执行测试状态")
|
|
return None # 保持当前状态
|
|
|
|
def exit(self, context):
|
|
print("退出测试状态")
|
|
|
|
# 创建状态机
|
|
state_machine = sm_plugin.create_state_machine("test_state_machine")
|
|
|
|
# 创建上下文(简单字典作为上下文)
|
|
context = {"data": {}}
|
|
|
|
def get_data(key, default=None):
|
|
return context["data"].get(key, default)
|
|
|
|
def set_data(key, value):
|
|
context["data"][key] = value
|
|
|
|
context["get_data"] = get_data
|
|
context["set_data"] = set_data
|
|
|
|
# 设置状态机上下文
|
|
state_machine.set_context(context)
|
|
|
|
# 创建测试状态并切换
|
|
test_state = TestState("TestState")
|
|
state_machine.change_state(test_state)
|
|
|
|
# 更新状态机
|
|
state_machine.update()
|
|
|
|
print("✅ 基本状态功能测试成功")
|
|
|
|
except Exception as e:
|
|
print(f"❌ 基本状态功能测试失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
# 禁用和卸载插件
|
|
plugin_manager.disable_plugin("state_machine")
|
|
plugin_manager.unload_plugin("state_machine")
|
|
|
|
print("✅ 状态机插件测试完成")
|
|
return True
|
|
|
|
if __name__ == "__main__":
|
|
print("开始测试行为树和状态机插件...")
|
|
|
|
# 测试行为树插件
|
|
bt_success = test_behavior_tree_plugin()
|
|
|
|
print("\n" + "="*50 + "\n")
|
|
|
|
# 测试状态机插件
|
|
sm_success = test_state_machine_plugin()
|
|
|
|
print("\n" + "="*50)
|
|
print("测试总结:")
|
|
print(f"行为树插件测试: {'✅ 通过' if bt_success else '❌ 失败'}")
|
|
print(f"状态机插件测试: {'✅ 通过' if sm_success else '❌ 失败'}")
|
|
|
|
if bt_success and sm_success:
|
|
print("🎉 所有插件测试通过!")
|
|
else:
|
|
print("❌ 部分测试失败,请检查插件实现。")
|