diff --git a/ComponentBasic/BehaviorPerform.cpp b/ComponentBasic/BehaviorPerform.cpp index 0fcb8db..43c19e4 100644 --- a/ComponentBasic/BehaviorPerform.cpp +++ b/ComponentBasic/BehaviorPerform.cpp @@ -1,6 +1,31 @@ #include "BehaviorPerform.h" +void BehaviorPerformPlugin::mapReset(const QString& path) +{ + +} + +void BehaviorPerformPlugin::execute(std::shared_ptr in, QList>& out) +{ + +} + +void BehaviorPerformPlugin::execute(std::shared_ptr in, QList>& out) +{ + +} + +void BehaviorPerformPlugin::execute(std::shared_ptr in, QList>& out) +{ + +} + +void BehaviorPerformPlugin::execute(std::shared_ptr in, QList>& out) +{ + +} + void BehaviorPerformPlugin::recoveryFrom(const QJsonObject& obj) { } @@ -9,32 +34,620 @@ void BehaviorPerformPlugin::saveTo(QJsonObject& obj) const { } -std::shared_ptr BehaviorPerformPlugin::defaultNew() const +std::shared_ptr BehaviorPerformPlugin::newDefault() const { - return std::make_shared(); + return std::make_shared(); } void BehaviorPerformPlugin::bindEntity(std::weak_ptr host) { - this->_bind_entity = host; + this->_bind_entity = host; } QString BehaviorPerformPlugin::name() const { - return NAME(BehaviorPerformPlugin); + return NAME(BehaviorPerformPlugin); } -void MapHost::setVariable(const QString& key, std::shared_ptr ins) -{ - if (_variable_map.contains(key) && _variable_map[key]->topicString() != ins->typeName()) - throw new UniException("不同数据类型数据绑定同一个名称!"); - _variable_map[key] = ins; +#include +MapKernal::MapKernal(std::shared_ptr ins) + : _bind_loader(ins) { + for (auto key : ins->allType()) + registData(ins->makeDefault(key)); } -std::shared_ptr MapHost::getVariable(const QString& key) const +QList MapKernal::compareTypes() const { - if(!_variable_map.contains(key)) + return _compare_types.keys(); +} + +void MapKernal::registCompare(std::shared_ptr ins) +{ + _compare_types[ins->name()] = ins; +} + +std::shared_ptr MapKernal::getCompare(const QString& name) +{ + if (!_compare_types.contains(name)) + return nullptr; + return _compare_types[name]; +} + +void MapKernal::registConvert(std::shared_ptr ins) +{ + _convert_types[ins->name()] = ins; +} + +QList MapKernal::convertTypes() const +{ + return _convert_types.keys(); +} + +std::shared_ptr MapKernal::getConvert(const QString& name) +{ + if (!_convert_types.contains(name)) + return nullptr; + return _convert_types[name]; +} + +std::shared_ptr MapKernal::messageLoader() const +{ + return this->_bind_loader; +} + +void MapKernal::registData(std::shared_ptr init) +{ + if (_variable_types.contains(init->topicString())) + throw new UniException(QString("重复注册类型数据{%1}").arg(init->topicString())); + + _variable_types[init->topicString()] = init; +} + +QList MapKernal::dataTypes() const +{ + return _variable_types.keys(); +} + +std::shared_ptr MapKernal::getData(const QString& topic) +{ + return _variable_types[topic]; +} + +std::shared_ptr MapKernal::newDefault() const +{ + return nullptr; +} + +LogicalNode::LogicalNode(NodeKind t /*= NodeKind::ACTIONNODE*/) + :_node_type(t) { +} + +void LogicalNode::setParent(std::weak_ptr pnode) +{ + this->_parent_bind = pnode; +} + +NodeKind LogicalNode::kind() const +{ + return _node_type; +} + +std::shared_ptr LogicalNode::bindParentMap() const +{ + auto node_temp = this->getBackwards().lock(); + if (!node_temp) return nullptr; + + while (node_temp->kind() != NodeKind::MAPNODE) { + node_temp = node_temp->getBackwards().lock(); + } + return node_temp; +} + +void LogicalNode::setID(int unique_id) +{ + this->_node_id = unique_id; +} + +int LogicalNode::getID() const +{ + return this->_node_id; +} + +QString LogicalNode::runtimeName() const +{ + return QString("%1(%2)").arg(this->typeName()).arg(getID()); +} + +std::weak_ptr LogicalNode::getBackwards() const +{ + return this->_parent_bind; +} + +BehaviorMapNode::BehaviorMapNode(std::shared_ptr core) + :LogicalNode(NodeKind::MAPNODE), MapKernal(core) { +} + +void BehaviorMapNode::setVariable(const QString& key, IO_TYPE t, std::shared_ptr ins) +{ + if (_variables.contains(key)) + if (_variables[key].second->topicString() != ins->topicString()) + throw new UniException("不同数据类型数据绑定同一个名称!"); + _variables[key] = std::make_pair(t, ins); +} + +std::shared_ptr BehaviorMapNode::getVariable(const QString& key) const +{ + if (!_variables.contains(key)) return nullptr; - return _variable_map[key]; + return _variables[key].second; +} + +void BehaviorMapNode::resetName(const QString& val) +{ + this->_map_name = val; +} + +std::shared_ptr BehaviorMapNode::newDefault() const +{ + return std::make_shared(this->messageLoader()); +} + +void BehaviorMapNode::recoveryFrom(const QJsonObject& obj) +{ + +} + +void BehaviorMapNode::saveTo(QJsonObject& obj) const +{ + +} + +QList> BehaviorMapNode::getForwards() const +{ + return children(); +} + +bool BehaviorMapNode::fallback(std::shared_ptr node, LogicalResult ste) +{ + this->_state_value = ste; + return true; +} + +void BehaviorMapNode::reset() +{ + this->_state_value = LogicalResult::UNDEFINED; +} + +QString BehaviorMapNode::typeName() const +{ + return _map_name; +} + +LogicalResult BehaviorMapNode::execute() +{ + return _state_value; +} + +QHash> BehaviorMapNode::inputList() const +{ + QHash> list; + for (auto key : this->_variables.keys()) { + if (this->_variables[key].first == IO_TYPE::INPUT && key.contains("::")) + list[key] = _variables[key].second; + } + + return list; +} + +QHash> BehaviorMapNode::outputList() const +{ + QHash> list; + for (auto key : this->_variables.keys()) { + if (this->_variables[key].first == IO_TYPE::OUTPUT && key.contains("::")) + list[key] = _variables[key].second; + } + + return list; +} + +SequenceNode::SequenceNode() + : LogicalNode(NodeKind::SEQUENCENODE) { +} + +QList> SequenceNode::getForwards() const +{ + for (auto item : children()) { + // 子节点执行失败,提前结束 + if (_state_map.contains(item->getID())) { + if (_state_map[item->getID()] != LogicalResult::SUCCESS) + return QList>(); + } + + // 获取新节点 + if (!_state_map.contains(item->getID())) + return QList>() << item; + } + + // 全部成功 + return QList>(); +} + +QString SequenceNode::typeName() const +{ + return "顺序节点"; +} + +LogicalResult SequenceNode::execute() { + if (children().size() >= _state_map.size()) + return LogicalResult::FAILURE; + + for (auto ste : this->_state_map) { + switch (ste) { + case LogicalResult::SUCCESS: + break; + default: + return LogicalResult::FAILURE; + } + } + + return LogicalResult::SUCCESS; +} + +QHash> SequenceNode::inputList() const +{ + return QHash>(); +} + +QHash> SequenceNode::outputList() const +{ + return QHash>(); +} + +void SequenceNode::recoveryFrom(const QJsonObject& obj) +{ + QStringList state_items; + STRLIST_PEAK(state_items); + for (auto kv : state_items) { + auto kv_pair = kv.split(":"); + _state_map[kv_pair[0].toInt()] = (LogicalResult)kv_pair[1].toInt(); + } +} + +void SequenceNode::saveTo(QJsonObject& obj) const +{ + QStringList state_items; + for (auto inv : _state_map.keys()) { + state_items << QString("%1:%2").arg(inv).arg((int)_state_map[inv]); + } + + STRLIST_SAVE(state_items); +} + +bool SequenceNode::fallback(std::shared_ptr node, LogicalResult ste) +{ + this->_state_map[node->getID()] = ste; + return true; +} + +std::shared_ptr SequenceNode::newDefault() const +{ + return std::make_shared(); +} + +void SequenceNode::reset() +{ + this->_state_map.clear(); +} + +QList> ComposeNode::children() const +{ + return _child_list; +} + +void ComposeNode::insert(std::shared_ptr node, int index /*= -1*/) +{ + for (auto it : _child_list) + if (it->getID() == node->getID()) + return; + + _child_list.insert(index, node); +} + +void ComposeNode::remove(std::shared_ptr node) +{ + _child_list.removeAll(node); +} + +SelectorNode::SelectorNode() + : LogicalNode(NodeKind::SEQUENCENODE) { +} + +QList> SelectorNode::getForwards() const +{ + for (auto item : children()) { + // 子节点执行失败,提前结束 + if (_state_map.contains(item->getID())) { + if (_state_map[item->getID()] == LogicalResult::SUCCESS) + return QList>(); + } + + // 获取新节点 + if (!_state_map.contains(item->getID())) + return QList>() << item; + } + + // 全部成功 + return QList>(); +} + +bool SelectorNode::fallback(std::shared_ptr node, LogicalResult ste) +{ + _state_map[node->getID()] = ste; + return true; +} + +std::shared_ptr SelectorNode::newDefault() const +{ + return std::make_shared(); +} + +void SelectorNode::recoveryFrom(const QJsonObject& obj) +{ + QStringList state_items; + STRLIST_PEAK(state_items); + for (auto kv : state_items) { + auto kv_pair = kv.split(":"); + _state_map[kv_pair[0].toInt()] = (LogicalResult)kv_pair[1].toInt(); + } +} + +void SelectorNode::saveTo(QJsonObject& obj) const +{ + QStringList state_items; + for (auto inv : _state_map.keys()) { + state_items << QString("%1:%2").arg(inv).arg((int)_state_map[inv]); + } + + STRLIST_SAVE(state_items); +} + +void SelectorNode::reset() +{ + _state_map.clear(); +} + +QString SelectorNode::typeName() const +{ + return "选择节点"; +} + +LogicalResult SelectorNode::execute() +{ + for (auto value : _state_map) { + if (value == LogicalResult::SUCCESS) + return LogicalResult::SUCCESS; + } + return LogicalResult::FAILURE; +} + +QHash> SelectorNode::inputList() const +{ + return {}; +} + +QHash> SelectorNode::outputList() const +{ + return {}; +} + +QList> ParallelNode::getForwards() const +{ + if (this->children().size() == _state_map.size()) + return QList>(); + return this->children(); +} + +bool ParallelNode::fallback(std::shared_ptr node, LogicalResult ste) +{ + _state_map[node->getID()] = ste; + return children().size() == _state_map.size(); +} + +void ParallelNode::reset() +{ + _state_map.clear(); +} + +QString ParallelNode::typeName() const +{ + return "并行节点"; +} + +LogicalResult ParallelNode::execute() +{ + for (auto ste : this->_state_map) + if (ste != LogicalResult::SUCCESS) + return LogicalResult::FAILURE; + + return LogicalResult::SUCCESS; +} + +std::shared_ptr ParallelNode::newDefault() const +{ + return std::make_shared(); +} + +ParallelNode::ParallelNode() + : LogicalNode(NodeKind::PARALLELNODE) { +} + +QHash> ParallelNode::inputList() const +{ + return QHash>(); +} + +QHash> ParallelNode::outputList() const +{ + return QHash>(); +} + +void ParallelNode::recoveryFrom(const QJsonObject& obj) +{ + QStringList state_items; + STRLIST_PEAK(state_items); + for (auto kv : state_items) { + auto kv_pair = kv.split(":"); + _state_map[kv_pair[0].toInt()] = (LogicalResult)kv_pair[1].toInt(); + } +} + +void ParallelNode::saveTo(QJsonObject& obj) const +{ + QStringList state_items; + for (auto inv : _state_map.keys()) { + state_items << QString("%1:%2").arg(inv).arg((int)_state_map[inv]); + } + + STRLIST_SAVE(state_items); +} + +void CompareNode::reset() { +} + +QString CompareNode::typeName() const +{ + return QString("%1<%2>").arg("比较节点", delegateName()); +} + +LogicalResult CompareNode::execute() +{ + if (this->_bind_delegate) { + auto vl = _data_map["左值"]; + auto vb = _data_map["右值"]; + if (this->_bind_delegate->compare(vl, vb)) + return LogicalResult::SUCCESS; + } + return LogicalResult::FAILURE; +} + +std::shared_ptr CompareNode::newDefault() const +{ + return std::make_shared(); +} + +CompareNode::CompareNode() + : LogicalNode(NodeKind::COMPARENODE) { + _data_map["左值"] = std::make_shared(); + _data_map["右值"] = std::make_shared(); +} + +QString CompareNode::delegateName() const +{ + if (_bind_delegate) + return _bind_delegate->name(); + return ""; +} + +void CompareNode::bindDelegate(std::shared_ptr ins) +{ + this->_bind_delegate = ins; +} + +QHash> CompareNode::inputList() const +{ + return _data_map; +} + +QHash> CompareNode::outputList() const +{ + return QHash>(); +} + +void CompareNode::recoveryFrom(const QJsonObject& obj) +{ + auto vala = obj["ValL"]; + auto valb = obj["ValR"]; + + _data_map["左值"]->recoveryFrom(vala.toObject()); + _data_map["右值"]->recoveryFrom(valb.toObject()); +} + +void CompareNode::saveTo(QJsonObject& obj) const +{ + QJsonObject vala, valb; + _data_map["左值"]->saveTo(vala); + _data_map["右值"]->saveTo(valb); + + obj["ValL"] = vala; + obj["ValR"] = valb; +} + +#include +QString GeneralData::topicString() const +{ + return "GeneralData"; +} + +std::shared_ptr GeneralData::newDefault() const +{ + return std::make_shared(); +} + +void GeneralData::recoveryFrom(const QJsonObject& obj) +{ + _element_bind = obj; +} + +void GeneralData::saveTo(QJsonObject& obj) const +{ + obj = _element_bind; +} + +CompareKernel::CompareKernel(QJSEngine& bind_engine, const QString& func) + :_script_engine(bind_engine), _function_name(func) { +} + +QString CompareKernel::name() const +{ + return _function_name; +} + +bool CompareKernel::compare(std::shared_ptr vleft, std::shared_ptr vright) +{ + return true; +} + +ConvertKernel::ConvertKernel(std::shared_ptr host, QJSEngine& bind_engine, const QString& func) + : _host_bind(host), _script_engine(bind_engine), _function_name(func) { +} + +QString ConvertKernel::name() const +{ + return _function_name; +} + +QList> ConvertKernel::inputTable() const +{ + return _input_table; +} + +void ConvertKernel::inputReset(const QList>& table) +{ + this->_input_table = table; +} + +std::pair ConvertKernel::outputVariable() const +{ + return _output_appoint; +} + +void ConvertKernel::outputReset(std::pair appoint) +{ + this->_output_appoint = appoint; +} + +std::shared_ptr ConvertKernel::convert(const QList>& input_variables) +{ + return nullptr; } diff --git a/ComponentBasic/BehaviorPerform.h b/ComponentBasic/BehaviorPerform.h index aeb2fa9..ca72fc6 100644 --- a/ComponentBasic/BehaviorPerform.h +++ b/ComponentBasic/BehaviorPerform.h @@ -2,116 +2,609 @@ #include "componentbasic.h" #include +#include +#include + +class MessageLoader; +class CompareKernel; +class ConvertKernel; +class LogicalNode; /// /// 行为树实例化节点 /// -class COMPONENTBASIC_EXPORT MapHost : public Serializable { +class COMPONENTBASIC_EXPORT MapKernal : virtual public Serializable { private: /// - /// {Topic,init-value} + /// 绑定载入器 + /// + std::shared_ptr _bind_loader; + + /// + /// {Topic,default-value} /// QHash> _variable_types; + /// - /// 变量表 + /// 比较器 /// - QHash> _variable_map; + QHash> _compare_types; + /// + /// 转换器 + /// + QHash> _convert_types; + + /// + /// 所有可用的节点类型 + /// + QHash> _logicalnode_types; public: - virtual ~MapHost() = default; + MapKernal(std::shared_ptr ins); + virtual ~MapKernal() = default; + + /// + /// 获取MessageLoader单例 + /// + /// + std::shared_ptr messageLoader() const; + + // 数据类型管理 ====================================================== /// /// 注册数据类型 /// /// - virtual void registData(std::shared_ptr init) = 0; + virtual void registData(std::shared_ptr init); + /// + /// 获取所有可用数据类型 + /// + /// + virtual QList dataTypes() const; + /// + /// 获取指定topic的数据类型 + /// + /// + /// + virtual std::shared_ptr getData(const QString &topic); + + + + // 比较器类型管理 ====================================================== + /// + /// 注册比较器 + /// + /// + virtual void registCompare(std::shared_ptr ins); + /// + /// 获取比较器类型 + /// + /// + virtual QList compareTypes() const; + /// + /// 获取比较器单例实例 + /// + /// + /// + virtual std::shared_ptr getCompare(const QString& name); + + + + // 转换器类型管理 ====================================================== + /// + /// 注册转换器 + /// + /// + virtual void registConvert(std::shared_ptr ins); + /// + /// 转换器类型名称 + /// + /// + virtual QList convertTypes() const; + /// + /// 获取指定类型的转换器 + /// + /// + /// + virtual std::shared_ptr getConvert(const QString& name); + + + // 通过 Serializable 继承 + std::shared_ptr newDefault() const override; + void recoveryFrom(const QJsonObject& obj) override{} + void saveTo(QJsonObject& obj) const override{} +}; + + +/// +/// 比较关系实例 +/// +class COMPONENTBASIC_EXPORT CompareKernel { +private: + QJSEngine& _script_engine; + QString _function_name; + +public: + CompareKernel(QJSEngine& bind_engine, const QString& func); + + QString name() const; + bool compare(std::shared_ptr vleft, std::shared_ptr vright); +}; +/// +/// 数据转换实例 +/// +class COMPONENTBASIC_EXPORT ConvertKernel { +public: + typedef QString DATA_TOPIC_STRING; + + /// + /// 构建转换器 + /// + /// 数据类型管理核心 + /// + /// + ConvertKernel(std::shared_ptr host, QJSEngine& bind_engine, const QString& func); + /// + /// 转换器名称:JS函数名 + /// + /// + QString name() const; + + /// + /// 输入变量类型绑定表 + /// + /// list{pair{类型名称,绑定输入变量名}} + QList> inputTable() const; + /// + /// 重置输入变量类型表 + /// + /// list{pair{类型名称,绑定输入变量名}} + void inputReset(const QList> &table); + /// + /// 输入变量 + /// + /// pair{类型名称,绑定输出变量名} + std::pair outputVariable() const; + /// + /// 重置输出变量类型名 + /// + /// + void outputReset(std::pair appoint); + + /// + /// 执行转换 + /// + /// 输入变量列表 + /// + std::shared_ptr convert(const QList>& input_variables); + +private: + std::shared_ptr _host_bind; + + QJSEngine& _script_engine; + QString _function_name; + + QList> _input_table; + std::pair _output_appoint; +}; + +/// +/// 执行结果 +/// +enum class LogicalResult { + UNDEFINED, // 未定义 + SUCCESS, // 成功 + FAILURE // 失败 +}; + +/// +/// 逻辑节点类型 +/// +enum class NodeKind { + MAPNODE, // 逻辑树节点 + SEQUENCENODE, // 顺序节点 + SELECTORNODE, // 选择节点 + PARALLELNODE, // 并行节点 + COMPARENODE, // 比较节点 + ACTIONNODE, // 动作节点 +}; + +/// +/// 通用数据类型 +/// +struct COMPONENTBASIC_EXPORT GeneralData : public TopicData { + QJsonObject _element_bind; + + QString topicString() const override; + std::shared_ptr newDefault() const override; + + void recoveryFrom(const QJsonObject& obj) override; + void saveTo(QJsonObject& obj) const override; +}; + +/// +/// 所有逻辑节点的基类 +/// +class COMPONENTBASIC_EXPORT LogicalNode : virtual public Serializable { +private: + int _node_id = 0; + NodeKind _node_type = NodeKind::ACTIONNODE; + std::weak_ptr _parent_bind; + QList> _child_list; + +public: + LogicalNode(NodeKind t = NodeKind::ACTIONNODE); + virtual ~LogicalNode() = default; + + /// + /// 重置父节点 + /// + /// + void setParent(std::weak_ptr pnode); + + /// + /// 获取节点类型 + /// + /// + NodeKind kind() const; + /// + /// 获取包含树图节点 + /// + /// + std::shared_ptr bindParentMap() const; + + /// + /// 设置节点id + /// + /// + void setID(int unique_id); + /// + /// 提取节点id + /// + /// + int getID() const; + /// + /// 实例化节点名称 + /// + /// + QString runtimeName() const; + /// + /// 回退节点 + /// + /// + std::weak_ptr getBackwards() const; + + /// + /// 重置内部状态 + /// + virtual void reset() = 0; + /// + /// 获取节点名称 + /// + /// + virtual QString typeName() const = 0; + /// + /// 执行逻辑节点 + /// + /// + /// + virtual LogicalResult execute() = 0; + /// + /// 声明输入变量的内部标识和数据接口 + /// + /// map{name, type-ins} + virtual QHash> inputList() const = 0; + /// + /// 声明输出变量的内部标识和数据接口 + /// + /// map{name, type-ins} + virtual QHash> outputList() const = 0; + + /// + /// 新建默认实例 + /// + /// + virtual std::shared_ptr newDefault() const = 0; +}; + +/// +/// 组合节点 +/// +class COMPONENTBASIC_EXPORT ComposeNode { +private: + QList> _child_list; + +public: + /// + /// 获取子节点列表 + /// + /// + virtual QList> children() const; + /// + /// 添加子节点 + /// + /// + /// + virtual void insert(std::shared_ptr node, int index = -1); + /// + /// 移除子节点 + /// + /// + virtual void remove(std::shared_ptr node); + + /// + /// 获取执行入口节点 + /// + /// + virtual QList> getForwards() const = 0; + /// + /// 下行节点执行状态回返填充 + /// + /// + /// + /// 接续执行标志 + virtual bool fallback(std::shared_ptr node, LogicalResult ste) = 0; +}; + +/// +/// 逻辑树节点输入输出类型 +/// +enum class IO_TYPE { + INPUT, OUTPUT +}; +/// +/// 自定义行为树节点根节点实例 +/// +class COMPONENTBASIC_EXPORT BehaviorMapNode + : public MapKernal, public LogicalNode, public ComposeNode { +private: + /// + /// 地图变量表 + /// + QHash>> _variables; + QString _map_name; + LogicalResult _state_value; + +public: + BehaviorMapNode(std::shared_ptr core); + + /// /// 设置变量,如果类型错误会抛出UniException异常 /// 节点初始化时会按照配置注册默认值变量 /// - virtual void setVariable(const QString& key, std::shared_ptr ins); + virtual void setVariable(const QString& key, IO_TYPE t, std::shared_ptr ins); /// /// 获取指定名称的数据变量 /// /// /// 如果不存在指定名称数据,返回nullptr,类型不确定 virtual std::shared_ptr getVariable(const QString& key) const; -}; -/// -/// 所有逻辑节点的基类 -/// -class LogicalNode : public Serializable { -public: - virtual ~LogicalNode() = default; - /// - /// 设置节点id - /// - /// - virtual void setID(int unique_id) = 0; - /// - /// 提取节点id - /// - /// - virtual int getID() const = 0; - /// - /// 获取节点名称 - /// - /// - virtual QString name() const = 0; - - /// - /// 获取子节点列表 - /// - /// - virtual QList> children() const = 0; - - /// - /// 声明输入变量的内部标识和数据接口 - /// - /// map{name, type} - virtual QHash> inputDeclares() const = 0; - /// - /// 声明输出变量的内部标识和数据接口 - /// - /// map{name, type} - virtual QHash> outputDeclares() const = 0; -}; - -/// -/// 自定义行为树节点实例 -/// -class COMPONENTBASIC_EXPORT BehaviorMapNode : public MapHost, public LogicalNode { -public: - /// - /// 重置行为树数据文件 - /// - /// - virtual void setBehaviorMap(const QString &path){} /// /// 设置行为树节点名称 /// /// - virtual void resetName(const QString &val){} + virtual void resetName(const QString& val); + /// + /// 重置执行状态 + /// + void reset() override; + /// + /// 获取节点类型名称 + /// + /// + QString typeName() const override; + + /// + /// 提取前置节点 + /// + /// + QList> getForwards() const override; + /// + /// 填充状态 + /// + /// + /// + /// + bool fallback(std::shared_ptr node, LogicalResult ste) override; + + /// + /// 执行状态 + /// + /// + LogicalResult execute() override; + + /// + /// 输入变量列表 + /// + /// + QHash> inputList() const override; + /// + /// 输出变量列表 + /// + /// + QHash> outputList() const override; + + /// + /// 创建新实例 + /// + /// + /// + virtual std::shared_ptr newDefault() const override; + void recoveryFrom(const QJsonObject& obj) override; + void saveTo(QJsonObject& obj) const override; }; +// 行为树节点 ====================================================== + +/// +/// 顺序节点 +/// +class COMPONENTBASIC_EXPORT SequenceNode + : public ComposeNode, public LogicalNode { +private: + QHash _state_map; + +public: + SequenceNode(); + + QList> getForwards() const override; + QString typeName() const override; + + LogicalResult execute() override; + QHash> inputList() const override; + QHash> outputList() const override; + + void reset() override; + bool fallback(std::shared_ptr node, LogicalResult ste) override; + + /// + /// 新实例 + /// + /// + /// + virtual std::shared_ptr newDefault() const override; + + void recoveryFrom(const QJsonObject& obj) override; + void saveTo(QJsonObject& obj) const override; +}; + +/// +/// 选择节点 +/// +class COMPONENTBASIC_EXPORT SelectorNode + : public ComposeNode, public LogicalNode { +private: + QHash _state_map; + +public: + SelectorNode(); + + QList> getForwards() const override; + QString typeName() const override; + + LogicalResult execute() override; + QHash> inputList() const override; + QHash> outputList() const override; + + void reset() override; + bool fallback(std::shared_ptr node, LogicalResult ste) override; + + /// + /// 新实例 + /// + /// + /// + virtual std::shared_ptr newDefault() const override; + + void recoveryFrom(const QJsonObject& obj) override; + void saveTo(QJsonObject& obj) const override; +}; + +/// +/// 并行节点 +/// +class COMPONENTBASIC_EXPORT ParallelNode + : public ComposeNode, public LogicalNode { +private: + QHash _state_map; + +public: + ParallelNode(); + + QHash> inputList() const override; + QHash> outputList() const override; + + QString typeName() const override; + + QList> getForwards() const override; + bool fallback(std::shared_ptr node, LogicalResult ste) override; + + LogicalResult execute() override; + void reset() override; + + /// + /// 新实例 + /// + /// + /// + virtual std::shared_ptr newDefault() const override; + + void recoveryFrom(const QJsonObject& obj) override; + void saveTo(QJsonObject& obj) const override; +}; + +/// +/// 比较节点 +/// +class COMPONENTBASIC_EXPORT CompareNode : public LogicalNode { +private: + std::shared_ptr _bind_delegate; + QHash> _data_map; + +public: + CompareNode(); + + /// + /// 委托名称 + /// + /// + QString delegateName() const; + /// + /// 绑定比较委托 + /// + /// + void bindDelegate(std::shared_ptr ins); + + QHash> inputList() const override; + QHash> outputList() const override; + + QString typeName() const override; + LogicalResult execute() override; + + void reset() override; + + /// + /// 新实例 + /// + /// + /// + virtual std::shared_ptr newDefault() const override; + + void recoveryFrom(const QJsonObject& obj) override; + void saveTo(QJsonObject& obj) const override; +}; + +// 行为节点 ==================================================================== + /// /// 行为树组件 /// -class COMPONENTBASIC_EXPORT BehaviorPerformPlugin : public ProcList< - WsRespond ->{ +class COMPONENTBASIC_EXPORT BehaviorPerformPlugin : +public RespondRoute< + WsRespond, + WsRespond, + WsRespond, + WsRespond>, +public RequestRoute<> +{ private: std::weak_ptr _bind_entity; public: + void mapReset(const QString &path); + // 通过 ProcList 继承 - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; void recoveryFrom(const QJsonObject& obj) override; void saveTo(QJsonObject& obj) const override; - std::shared_ptr defaultNew() const override; + std::shared_ptr newDefault() const override; void bindEntity(std::weak_ptr host) override; QString name() const override; }; diff --git a/ComponentBasic/ComponentBasic.vcxproj b/ComponentBasic/ComponentBasic.vcxproj index ecd4d11..857fb75 100644 --- a/ComponentBasic/ComponentBasic.vcxproj +++ b/ComponentBasic/ComponentBasic.vcxproj @@ -37,7 +37,7 @@ 5.12.11_msvc2017_64 - core;gui;concurrent + core;xml;gui;qml;concurrent debug diff --git a/ComponentBasic/ComponentsInfoPull.cpp b/ComponentBasic/ComponentsInfoPull.cpp index 045957e..868bc7e 100644 --- a/ComponentBasic/ComponentsInfoPull.cpp +++ b/ComponentBasic/ComponentsInfoPull.cpp @@ -14,7 +14,7 @@ QString ComponentsInfoPull::name() const #include #include -void ComponentsInfoPull::execute(std::shared_ptr map, +void ComponentsInfoPull::execute( std::shared_ptr in, QList>& out) { auto rst = std::make_shared(); @@ -44,7 +44,7 @@ void ComponentsInfoPull::execute(std::shared_ptr map, } #include -void ComponentsInfoPull::execute(std::shared_ptr map, +void ComponentsInfoPull::execute( std::shared_ptr in, QList>& out) { auto ent_ins = std::dynamic_pointer_cast(this->_bind_entity.lock()); @@ -53,12 +53,12 @@ void ComponentsInfoPull::execute(std::shared_ptr map, auto result = std::make_shared(); result->reset(in->targetEntity(), in->sourceEntity()); for (auto cp : comps) { - result->_signature_list.append(cp->signatureTypes()); + result->_signature_list.append(cp->respondSignatures()); } out << result; } -std::shared_ptr ComponentsInfoPull::defaultNew() const +std::shared_ptr ComponentsInfoPull::newDefault() const { return std::make_shared(); } diff --git a/ComponentBasic/ComponentsInfoPull.h b/ComponentBasic/ComponentsInfoPull.h index 879f2f0..8f0fa38 100644 --- a/ComponentBasic/ComponentsInfoPull.h +++ b/ComponentBasic/ComponentsInfoPull.h @@ -1,14 +1,16 @@ #pragma once -#include +#include #include "componentbasic.h" /// /// 组件信息提取 /// -class COMPONENTBASIC_EXPORT ComponentsInfoPull : public ProcList< +class COMPONENTBASIC_EXPORT ComponentsInfoPull : +public RespondRoute< WsRespond, - WsRespond -> { + WsRespond>, +public RequestRoute<> +{ private: std::weak_ptr _bind_entity; @@ -19,10 +21,10 @@ public: void bindEntity(std::weak_ptr ins) override; QString name() const override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; - std::shared_ptr defaultNew() const override; + std::shared_ptr newDefault() const override; void recoveryFrom(const QJsonObject& obj) override; void saveTo(QJsonObject& obj) const override; }; \ No newline at end of file diff --git a/ComponentBasic/DeduceFramework.cpp b/ComponentBasic/DeduceFramework.cpp index 1c7a8ba..40b65f4 100644 --- a/ComponentBasic/DeduceFramework.cpp +++ b/ComponentBasic/DeduceFramework.cpp @@ -9,13 +9,11 @@ QList> deduct_each(std::shared_ptr item) QMutexLocker loc(&DeduceFramework::_static_mutex); QList> rets; - auto immediate = std::make_shared(item); - for (auto msg : DeduceFramework::_accept_stack) { if (msg->targetEntity() == item->entityID()) { - auto enters = item->getEntryWithInType(msg->topicString()); - for (auto e : enters) - e(immediate, msg, rets); + auto enters = item->getRespondWithInType(msg->topicString()); + for (auto efunc : enters) + efunc(msg, rets); } } return rets; @@ -54,7 +52,7 @@ void DeduceFramework::deduceBegin(std::shared_ptr ins) } #include -#include "InvisibleComponent.h" +#include "ComponentsInfoPull.h" DeduceFramework::DeduceFramework() :_factory_ins(std::make_shared()) { } @@ -76,7 +74,7 @@ void DeduceFramework::init_framework() this->_entity_map_over_0xffff[res_e->entityID()] = res_e; } -std::shared_ptr DeduceFramework::defaultNew() const +std::shared_ptr DeduceFramework::newDefault() const { throw std::logic_error("The method or operation is not implemented."); } @@ -101,7 +99,7 @@ void DeduceFramework::saveTo(QJsonObject& obj) const throw std::logic_error("The method or operation is not implemented."); } -void DeduceFramework::execute(std::shared_ptr map, +void DeduceFramework::execute( std::shared_ptr in, QList>& out) { auto dt = QTime::currentTime(); @@ -111,60 +109,60 @@ void DeduceFramework::execute(std::shared_ptr map, switch ((EntityOperateType)in->_operate_code) { - case EntityOperateType::NEW: { - if (!this->_templets_within_0x2ff_0xffff.contains(in->_template_name)) { - result->_reason_text = QString("指定的Template:%1不存在").arg(in->_template_name); - } - else { - result->_success_mark = true; + case EntityOperateType::NEW: { + if (!this->_templets_within_0x2ff_0xffff.contains(in->_template_name)) { + result->_reason_text = QString("指定的Template:%1不存在").arg(in->_template_name); + } + else { + result->_success_mark = true; - auto ins_t = this->_templets_within_0x2ff_0xffff[in->_template_name]; - auto new_copy = std::static_pointer_cast(ins_t->defaultNew()); - new_copy->resetID(0xffff); - auto desc_p = std::make_shared(); - new_copy->append(desc_p); + auto ins_t = this->_templets_within_0x2ff_0xffff[in->_template_name]; + auto new_copy = std::static_pointer_cast(ins_t->newDefault()); + new_copy->resetID(0xffff); + auto desc_p = std::make_shared(); + new_copy->append(desc_p); - while (_entity_map_over_0xffff.contains(new_copy->entityID())) - { - auto ncode = new_copy->entityID() + 1; - new_copy->resetID(ncode); - } - _entity_map_over_0xffff[new_copy->entityID()] = new_copy; + while (_entity_map_over_0xffff.contains(new_copy->entityID())) + { + auto ncode = new_copy->entityID() + 1; + new_copy->resetID(ncode); } - }break; - case EntityOperateType::DELETE: { - if (!this->_entity_map_over_0xffff.contains(in->_entity_id_over_0xffff)) { - result->_reason_text = QString("指定的实体id:%1不存在").arg(in->_entity_id_over_0xffff); - } - else { - result->_success_mark = true; - this->_entity_map_over_0xffff.remove(in->_entity_id_over_0xffff); - } - }break; - case EntityOperateType::MODIFY: { - if (!this->_entity_map_over_0xffff.contains(in->_entity_id_over_0xffff)) { - result->_reason_text = QString("指定的实体id:%1不存在").arg(in->_entity_id_over_0xffff); - } - else { - result->_success_mark = true; + _entity_map_over_0xffff[new_copy->entityID()] = new_copy; + } + }break; + case EntityOperateType::DELETE: { + if (!this->_entity_map_over_0xffff.contains(in->_entity_id_over_0xffff)) { + result->_reason_text = QString("指定的实体id:%1不存在").arg(in->_entity_id_over_0xffff); + } + else { + result->_success_mark = true; + this->_entity_map_over_0xffff.remove(in->_entity_id_over_0xffff); + } + }break; + case EntityOperateType::MODIFY: { + if (!this->_entity_map_over_0xffff.contains(in->_entity_id_over_0xffff)) { + result->_reason_text = QString("指定的实体id:%1不存在").arg(in->_entity_id_over_0xffff); + } + else { + result->_success_mark = true; - auto target_ins = std::static_pointer_cast( - this->_entity_map_over_0xffff[in->_entity_id_over_0xffff] - ); - target_ins->resetName(in->_entity_name); - target_ins->resetTemplet(in->_template_name); - } - }break; - default: - result->_reason_text = QString("指定操作码非法:%1").arg(in->_operate_code); - break; + auto target_ins = std::static_pointer_cast( + this->_entity_map_over_0xffff[in->_entity_id_over_0xffff] + ); + target_ins->resetName(in->_entity_name); + target_ins->resetTemplet(in->_template_name); + } + }break; + default: + result->_reason_text = QString("指定操作码非法:%1").arg(in->_operate_code); + break; } result->_time_consume = dt.msecsTo(QTime::currentTime()); out << result; } -void DeduceFramework::execute(std::shared_ptr map, +void DeduceFramework::execute( std::shared_ptr in, QList>& out) { auto rst = std::make_shared(); @@ -173,7 +171,7 @@ void DeduceFramework::execute(std::shared_ptr map, out << rst; } -void DeduceFramework::execute(std::shared_ptr map, +void DeduceFramework::execute( std::shared_ptr in, QList>& out) { auto ins = std::make_shared(); @@ -188,7 +186,7 @@ void DeduceFramework::execute(std::shared_ptr map, out << ins; } -void DeduceFramework::execute(std::shared_ptr map, +void DeduceFramework::execute( std::shared_ptr in, QList>& out) { auto dt = QTime::currentTime(); @@ -197,68 +195,68 @@ void DeduceFramework::execute(std::shared_ptr map, nins->reset(in->targetEntity(), in->sourceEntity()); switch ((EntityOperateType)in->_operate_code) { - case EntityOperateType::NEW: { - nins->_success_mark = true; - auto ent_t = std::make_shared(); - ent_t->resetID(0x2ff); - ent_t->resetTemplet(in->_template_name); + case EntityOperateType::NEW: { + nins->_success_mark = true; + auto ent_t = std::make_shared(); + ent_t->resetID(0x2ff); + ent_t->resetTemplet(in->_template_name); - auto desc_p = std::make_shared(); - ent_t->append(desc_p); + auto desc_p = std::make_shared(); + ent_t->append(desc_p); - // 提取所有的模板id - auto _tm_ins = _templets_within_0x2ff_0xffff.values(); - QList allids; - std::transform(_templets_within_0x2ff_0xffff.begin(), _templets_within_0x2ff_0xffff.end(), - std::back_inserter(allids), [](std::shared_ptr i) { return i->entityID(); }); + // 提取所有的模板id + auto _tm_ins = _templets_within_0x2ff_0xffff.values(); + QList allids; + std::transform(_templets_within_0x2ff_0xffff.begin(), _templets_within_0x2ff_0xffff.end(), + std::back_inserter(allids), [](std::shared_ptr i) { return i->entityID(); }); - // 重置模板id - while (allids.contains(ent_t->entityID())) { - auto nid = ent_t->entityID() + 1; - ent_t->resetID(nid); + // 重置模板id + while (allids.contains(ent_t->entityID())) { + auto nid = ent_t->entityID() + 1; + ent_t->resetID(nid); + } + + // 模板载入系统 + _templets_within_0x2ff_0xffff[in->_template_name] = ent_t; + }break; + case EntityOperateType::DELETE: { + for (auto ekey : this->_templets_within_0x2ff_0xffff.keys()) { + auto eins = this->_templets_within_0x2ff_0xffff[ekey]; + nins->_success_mark = false; + nins->_reason_text = QString("指定的实体id:%1不存在").arg(in->_template_id_within_0x2ff_0xffff); + + if (eins->entityID() == in->_template_id_within_0x2ff_0xffff) { + this->_templets_within_0x2ff_0xffff.remove(ekey); + nins->_success_mark = true; + nins->_reason_text = ""; + break; } + } + }break; + case EntityOperateType::MODIFY: { + for (auto ekey : this->_templets_within_0x2ff_0xffff.keys()) { + auto eins = this->_templets_within_0x2ff_0xffff[ekey]; + nins->_reason_text = QString("指定的实体id:%1不存在").arg(in->_template_id_within_0x2ff_0xffff); + nins->_success_mark = false; - // 模板载入系统 - _templets_within_0x2ff_0xffff[in->_template_name] = ent_t; - }break; - case EntityOperateType::DELETE: { - for (auto ekey : this->_templets_within_0x2ff_0xffff.keys()) { - auto eins = this->_templets_within_0x2ff_0xffff[ekey]; - nins->_success_mark = false; - nins->_reason_text = QString("指定的实体id:%1不存在").arg(in->_template_id_within_0x2ff_0xffff); - - if (eins->entityID() == in->_template_id_within_0x2ff_0xffff) { - this->_templets_within_0x2ff_0xffff.remove(ekey); - nins->_success_mark = true; - nins->_reason_text = ""; - break; - } + if (eins->entityID() == in->_template_id_within_0x2ff_0xffff) { + eins->resetTemplet(in->_template_name); + nins->_success_mark = true; + nins->_reason_text = ""; + break; } - }break; - case EntityOperateType::MODIFY: { - for (auto ekey : this->_templets_within_0x2ff_0xffff.keys()) { - auto eins = this->_templets_within_0x2ff_0xffff[ekey]; - nins->_reason_text = QString("指定的实体id:%1不存在").arg(in->_template_id_within_0x2ff_0xffff); - nins->_success_mark = false; - - if (eins->entityID() == in->_template_id_within_0x2ff_0xffff) { - eins->resetTemplet(in->_template_name); - nins->_success_mark = true; - nins->_reason_text = ""; - break; - } - } - }break; - default: - nins->_reason_text = QString("指定操作码非法:%1").arg(in->_operate_code); - break; + } + }break; + default: + nins->_reason_text = QString("指定操作码非法:%1").arg(in->_operate_code); + break; } nins->_time_consume = dt.msecsTo(QTime::currentTime()); out << nins; } -void DeduceFramework::execute(std::shared_ptr map, +void DeduceFramework::execute( std::shared_ptr in, QList>& out) { auto dt = QTime::currentTime(); @@ -341,7 +339,7 @@ std::shared_ptr ComponentFactory::makeNew(const QString& type) return NULL; auto ntype = _comp_types[type]; - return ntype->defaultNew(); + return std::static_pointer_cast(ntype->newDefault()); } QList ComponentFactory::allComponentTypes() const @@ -349,6 +347,11 @@ QList ComponentFactory::allComponentTypes() const return _comp_types.keys(); } +std::shared_ptr ComponentFactory::newDefault() const +{ + return nullptr; +} + void ComponentFactory::recoveryFrom(const QJsonObject& obj) { QStringList tlist; diff --git a/ComponentBasic/DeduceFramework.h b/ComponentBasic/DeduceFramework.h index b748f18..1923d6a 100644 --- a/ComponentBasic/DeduceFramework.h +++ b/ComponentBasic/DeduceFramework.h @@ -5,6 +5,7 @@ #include #include #include +#include class COMPONENTBASIC_EXPORT ComponentFactory : public Serializable { private: @@ -17,18 +18,20 @@ public: QList allComponentTypes() const; // 通过 Serializable 继承 + std::shared_ptr newDefault() const override; void recoveryFrom(const QJsonObject& obj) override; void saveTo(QJsonObject& obj) const override; }; class COMPONENTBASIC_EXPORT DeduceFramework : public QObject, -public ProcList< +public RespondRoute< WsRespond, WsRespond, WsRespond, WsRespond, - WsRespond> + WsRespond>, +public RequestRoute<> { Q_OBJECT private: @@ -71,15 +74,15 @@ public: void init_framework(); // ProcList interface ======================================================= - std::shared_ptr defaultNew() const override; + std::shared_ptr newDefault() const override; void bindEntity(std::weak_ptr ins) override; QString name() const override; void recoveryFrom(const QJsonObject& obj) override; void saveTo(QJsonObject& obj) const override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; }; \ No newline at end of file diff --git a/ComponentBasic/RouteManage.cpp b/ComponentBasic/RouteManage.cpp index eedc1ad..d25b23d 100644 --- a/ComponentBasic/RouteManage.cpp +++ b/ComponentBasic/RouteManage.cpp @@ -8,7 +8,7 @@ PlainRouteManagePlugin::PlainRouteManagePlugin() #include -void PlainRouteManagePlugin::execute(std::shared_ptr map, std::shared_ptr in, QList>& out) +void PlainRouteManagePlugin::execute(std::shared_ptr in, QList>& out) { auto resp = std::make_shared(); resp->reset(in->targetEntity(), in->sourceEntity()); @@ -24,7 +24,7 @@ void PlainRouteManagePlugin::execute(std::shared_ptr map, std::shared out << resp; } -void PlainRouteManagePlugin::execute(std::shared_ptr map, std::shared_ptr in, QList>& out) +void PlainRouteManagePlugin::execute(std::shared_ptr in, QList>& out) { auto resp = std::make_shared(); resp->reset(in->targetEntity(), in->sourceEntity()); @@ -40,7 +40,7 @@ void PlainRouteManagePlugin::execute(std::shared_ptr map, std::shared out << resp; } -void PlainRouteManagePlugin::execute(std::shared_ptr map, std::shared_ptr in, QList>& out) +void PlainRouteManagePlugin::execute(std::shared_ptr in, QList>& out) { auto resp = std::make_shared(); resp->reset(in->targetEntity(), in->sourceEntity()); @@ -59,7 +59,7 @@ void PlainRouteManagePlugin::execute(std::shared_ptr map, std::shared out << resp; } -void PlainRouteManagePlugin::execute(std::shared_ptr map, std::shared_ptr in, QList>& out) +void PlainRouteManagePlugin::execute(std::shared_ptr in, QList>& out) { auto resp = std::make_shared(); resp->reset(in->targetEntity(), in->sourceEntity()); @@ -116,7 +116,7 @@ void PlainRouteManagePlugin::saveTo(QJsonObject& obj) const obj["total_route"] = route_array; } -std::shared_ptr PlainRouteManagePlugin::defaultNew() const +std::shared_ptr PlainRouteManagePlugin::newDefault() const { return nullptr; } diff --git a/ComponentBasic/RouteManage.h b/ComponentBasic/RouteManage.h index 6f5c14f..0b6d296 100644 --- a/ComponentBasic/RouteManage.h +++ b/ComponentBasic/RouteManage.h @@ -15,12 +15,14 @@ struct RouteResource { /// /// 平面路径管理插件 /// -class COMPONENTBASIC_EXPORT PlainRouteManagePlugin : public ProcList< +class COMPONENTBASIC_EXPORT PlainRouteManagePlugin : +public RespondRoute< WsRespond, WsRespond, WsRespond, - WsRespond -> { + WsRespond>, +public RequestRoute<> +{ private: std::weak_ptr _bind_entity; QHash _route_resource_map; @@ -29,15 +31,15 @@ public: PlainRouteManagePlugin(); // 通过 ProcList 继承 - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; void recoveryFrom(const QJsonObject& obj) override; void saveTo(QJsonObject& obj) const override; - std::shared_ptr defaultNew() const override; + std::shared_ptr newDefault() const override; void bindEntity(std::weak_ptr host) override; QString name() const override; }; diff --git a/ComponentBasic/SurfaceMotion.cpp b/ComponentBasic/SurfaceMotion.cpp index 16c7802..4527743 100644 --- a/ComponentBasic/SurfaceMotion.cpp +++ b/ComponentBasic/SurfaceMotion.cpp @@ -13,7 +13,7 @@ void SurfaceMotionPlugin::saveTo(QJsonObject& obj) const { } -void SurfaceMotionPlugin::execute(std::shared_ptr map, +void SurfaceMotionPlugin::execute( std::shared_ptr in, QList>& out) { auto exec_result = std::make_shared(); @@ -22,12 +22,12 @@ void SurfaceMotionPlugin::execute(std::shared_ptr map, auto exec = std::make_shared(); exec->reset(in->targetEntity(), in->sourceEntity()); exec->_target_time = _current_cmd->startTimepoint(); - map->execute(exec); + this->query(this->_bind_entity, exec); } auto req = std::make_shared(); req->reset(this->_bind_entity.lock()->entityID(), this->_bind_entity.lock()->entityID()); - auto rst = map->execute(req); + auto rst = WsRequest::impl(this->_bind_entity, req); if (rst.size()) { // 保存当前命令 this->_current_cmd = in; @@ -45,7 +45,7 @@ void SurfaceMotionPlugin::execute(std::shared_ptr map, out << exec_result; } -void SurfaceMotionPlugin::execute(std::shared_ptr map, +void SurfaceMotionPlugin::execute( std::shared_ptr in, QList>& out) { auto exec_result = std::make_shared(); @@ -54,13 +54,13 @@ void SurfaceMotionPlugin::execute(std::shared_ptr map, auto exec = std::make_shared(); exec->reset(in->targetEntity(), in->sourceEntity()); exec->_target_time = _current_cmd->startTimepoint(); - map->execute(exec); + this->query(this->_bind_entity, exec); } // 提取起始机动状态 auto req = std::make_shared(); req->reset(this->_bind_entity.lock()->entityID(), this->_bind_entity.lock()->entityID()); - auto rst = map->execute(req); + auto rst = WsRequest::impl(this->_bind_entity, req); if (rst.size()) { // 保存当前命令 this->_current_cmd = in; @@ -79,21 +79,21 @@ void SurfaceMotionPlugin::execute(std::shared_ptr map, } #include -void SurfaceMotionPlugin::execute(std::shared_ptr map, +void SurfaceMotionPlugin::execute( std::shared_ptr in, QList>& out) { auto reqs = std::make_shared(); reqs->reset(in->targetEntity(), in->sourceEntity()); reqs->_target_time = in->_time_current; - for (auto rst : map->execute(reqs)) { + for (auto rst : this->query(this->_bind_entity, reqs)) { auto conv = std::dynamic_pointer_cast(rst); if (conv) out << conv; } } #include -void SurfaceMotionPlugin::execute(std::shared_ptr map, +void SurfaceMotionPlugin::execute( std::shared_ptr in, QList>& out) { auto rst = std::make_shared(); @@ -126,11 +126,11 @@ void SurfaceMotionPlugin::execute(std::shared_ptr map, auto setpos = std::make_shared(); setpos->_lla_pos = deduce_lla; - map->execute(setpos); + this->query(this->_bind_entity, setpos); auto setpsr = std::make_shared(); setpsr->_posture_d3._azimuth_deg = azimuth_self; - map->execute(setpsr); + this->query(this->_bind_entity, setpsr); rst->_success_mark = true; } @@ -142,7 +142,7 @@ void SurfaceMotionPlugin::execute(std::shared_ptr map, out << rst; } -std::shared_ptr SurfaceMotionPlugin::defaultNew() const +std::shared_ptr SurfaceMotionPlugin::newDefault() const { return std::make_shared(); } diff --git a/ComponentBasic/SurfaceMotion.h b/ComponentBasic/SurfaceMotion.h index feb4760..aadac63 100644 --- a/ComponentBasic/SurfaceMotion.h +++ b/ComponentBasic/SurfaceMotion.h @@ -1,16 +1,21 @@ #pragma once #include #include "componentbasic.h" +#include + /// /// 水面平台机动组件 /// -class COMPONENTBASIC_EXPORT SurfaceMotionPlugin : public ProcList< +class COMPONENTBASIC_EXPORT SurfaceMotionPlugin : +public RespondRoute< WsRespond, WsRespond, WsRespond, - WsRespond ->{ + WsRespond>, +public RequestRoute< + WsRequest> +{ private: std::weak_ptr _bind_entity; std::shared_ptr _current_cmd = nullptr; @@ -21,12 +26,12 @@ public: SurfaceMotionPlugin(); // 通过 ProcList 继承 - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; - std::shared_ptr defaultNew() const override; + std::shared_ptr newDefault() const override; void bindEntity(std::weak_ptr host) override; QString name() const override; diff --git a/ComponentBasic/VisibleCube.cpp b/ComponentBasic/VisibleCube.cpp index 5ca111a..ae770f6 100644 --- a/ComponentBasic/VisibleCube.cpp +++ b/ComponentBasic/VisibleCube.cpp @@ -31,7 +31,7 @@ void VisibleCubePlugin::saveTo(QJsonObject& obj) const { DOUBLE_SAVE(_self_posture._roll_deg); } -std::shared_ptr VisibleCubePlugin::defaultNew() const { +std::shared_ptr VisibleCubePlugin::newDefault() const { auto copy = std::make_shared(); copy->_bind_entity = _bind_entity; copy->_self_d3 = _self_d3; @@ -49,7 +49,7 @@ QString VisibleCubePlugin::name() const { return NAME(VisibleCubePlugin); } -void VisibleCubePlugin::execute(std::shared_ptr map, std::shared_ptr in, QList>& out) +void VisibleCubePlugin::execute(std::shared_ptr in, QList>& out) { auto ins = std::make_shared(); ins->_lla_pos = _self_lla; @@ -59,7 +59,7 @@ void VisibleCubePlugin::execute(std::shared_ptr map, std::shared_ptr< out << ins; } -void VisibleCubePlugin::execute(std::shared_ptr map, std::shared_ptr in, QList>& out) +void VisibleCubePlugin::execute(std::shared_ptr in, QList>& out) { auto resp = std::make_shared(); resp->reset(in->targetEntity(), in->sourceEntity()); @@ -69,7 +69,7 @@ void VisibleCubePlugin::execute(std::shared_ptr map, std::shared_ptr< out << resp; } -void VisibleCubePlugin::execute(std::shared_ptr map, std::shared_ptr in, QList>& out) +void VisibleCubePlugin::execute(std::shared_ptr in, QList>& out) { auto resp = std::make_shared(); resp->reset(in->targetEntity(), in->sourceEntity()); @@ -79,7 +79,7 @@ void VisibleCubePlugin::execute(std::shared_ptr map, std::shared_ptr< out << resp; } -void VisibleCubePlugin::execute(std::shared_ptr map, std::shared_ptr in, QList>& out) +void VisibleCubePlugin::execute(std::shared_ptr in, QList>& out) { auto resp = std::make_shared(); resp->reset(in->targetEntity(), in->sourceEntity()); diff --git a/ComponentBasic/VisibleCube.h b/ComponentBasic/VisibleCube.h index b267c35..f2b2fb5 100644 --- a/ComponentBasic/VisibleCube.h +++ b/ComponentBasic/VisibleCube.h @@ -2,16 +2,18 @@ #include "componentbasic.h" #include #include +#include /// /// 可视化3d体积盒 /// -class COMPONENTBASIC_EXPORT VisibleCubePlugin : public ProcList< +class COMPONENTBASIC_EXPORT VisibleCubePlugin : +public RespondRoute< WsRespond, WsRespond, WsRespond, - WsRespond -> + WsRespond>, +public RequestRoute<> { public: std::weak_ptr _bind_entity; @@ -26,13 +28,13 @@ public: void bindEntity(std::weak_ptr ins) override; QString name() const override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; - void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; + void execute(std::shared_ptr in, QList>& out) override; - std::shared_ptr defaultNew() const override; + std::shared_ptr newDefault() const override; void recoveryFrom(const QJsonObject& obj) override; void saveTo(QJsonObject& obj) const override; }; \ No newline at end of file diff --git a/ComponentBasic/componentbasic.h b/ComponentBasic/componentbasic.h index af649ec..f5e79ec 100644 --- a/ComponentBasic/componentbasic.h +++ b/ComponentBasic/componentbasic.h @@ -5,7 +5,7 @@ #include /// -/// 消息处理单元基类 +/// 消息响应处理单元基类 /// /// 输入类型 /// 输出类型 @@ -28,64 +28,176 @@ public: /// /// /// - virtual void execute(std::shared_ptr map, std::shared_ptr in, QList>& out) = 0; + virtual void execute(std::shared_ptr in, QList>& out) = 0; /// /// WsRespond构造函数 /// /// 注册函数 explicit WsRespond(QHash& map) { - WsRespondEntry ins = [=](std::shared_ptr map, std::shared_ptr in, QList>& out) { + WsRespondEntry ins = [=](std::shared_ptr in, QList>& out) { QList> o_ptrs; - this->execute(map, std::static_pointer_cast(in), o_ptrs); + this->execute(std::static_pointer_cast(in), o_ptrs); std::transform(o_ptrs.begin(), o_ptrs.end(), std::back_inserter(out), - [](std::shared_ptr v) { return std::static_pointer_cast(v); }); + [](std::shared_ptr v) { + return std::static_pointer_cast(v); + }); }; map[SelfType::signature()] = ins; } }; -template struct _ProcRoute_; -template <> struct COMPONENTBASIC_EXPORT _ProcRoute_<> -{ + +template struct _RespondRoute; +/// +/// 响应路由数据基类 +/// +template <> struct COMPONENTBASIC_EXPORT _RespondRoute<> { QHash _execute_map; - - _ProcRoute_() = default; + _RespondRoute() = default; }; -template -struct _ProcRoute_ : public _ProcRoute_, public ProcSig -{ - _ProcRoute_() : _ProcRoute_(), ProcSig(_ProcRoute_<>::_execute_map) {} +/// +/// 消息响应路由综合基类 +/// +/// 类型 +/// 其余类型列表 +template +struct _RespondRoute : public _RespondRoute, public RespSig { + _RespondRoute() : _RespondRoute(), RespSig(_RespondRoute<>::_execute_map) {} }; -template -struct ProcList : public _ProcRoute_, public WsComponent -{ - using SuperType = ProcList; - ProcList() : _ProcRoute_() {} + +/// +/// 消息响应列表 +/// +/// +template +struct RespondRoute : virtual public WsComponent, public _RespondRoute { + RespondRoute() : _RespondRoute() {} // 通过 WsComponent 继承 - QList signatureTypes() const override + virtual QList respondSignatures() const { - return _ProcRoute_<>::_execute_map.keys(); + return _RespondRoute<>::_execute_map.keys(); } - QList getEntryWithSignature(const WsRespondSignatureType& t) const override + virtual QList getRespondWithSignature(const WsRespondSignatureType& t) const { QList list; - for (auto key : _ProcRoute_<>::_execute_map.keys()) { - if (key == t) - list.append(_ProcRoute_<>::_execute_map[key]); - } + if (_RespondRoute<>::_execute_map.contains(t)) + list << _RespondRoute<>::_execute_map[t]; return list; } - QList getEntrysWithInType(const QString& msg_type) const override + virtual QList getRespondWithInType(const QString& msg_type) const { QList list; - for (auto key : _ProcRoute_<>::_execute_map.keys()) { + for (auto key : _RespondRoute<>::_execute_map.keys()) { if (key.first == msg_type) - list.append(_ProcRoute_<>::_execute_map[key]); + list.append(_RespondRoute<>::_execute_map[key]); } return list; } }; + + + + + +// =========================================================== +/// +/// Request发送接口 +/// +/// +/// +template class WsRequest { +public: + using SelfType = WsRequest; + + virtual ~WsRequest() = default; + /// + /// Request的类型签名 + /// + /// + static WsRequestSignatureType signature() + { + return std::make_pair(MsgA().topicString(), MsgB().topicString()); + } + + /// + /// 子类中实际处理函数 + /// + /// + /// + static void request(std::weak_ptr e, std::shared_ptr in, QList>& out) { + auto entries = e.lock()->getRespondWithSignature(SelfType::signature()); + + QList> o_list; + for (auto func_it : entries) { + func_it(in, o_list); + } + for (auto msg : o_list) { + auto conv = std::dynamic_pointer_cast(msg); + if (conv) out << conv; + } + } + + static QList> impl(std::weak_ptr e, std::shared_ptr in) { + QList> out; + WsRequest::request(e, in, out); + return out; + } + + /// + /// WsRequest构造函数 + /// + /// Request类型列表 + explicit WsRequest(QList& request_list) { + request_list << SelfType::signature(); + } +}; + + + +template struct _RequestRoute; +/// +/// 请求发送数据基类 +/// +template<> struct COMPONENTBASIC_EXPORT _RequestRoute<> { + QList _request_list; + _RequestRoute() = default; + + QList> query(std::weak_ptr e, std::shared_ptr in) { + auto binde = e.lock(); + auto entries = binde->getRespondWithInType(in->topicString()); + + QList> out; + for (auto func_it : entries) { + func_it(in, out); + } + return out; + } +}; +/// +/// 消息发送综合基类 +/// +/// +/// +template +struct _RequestRoute : public _RequestRoute, public ReqsSig { + _RequestRoute() : _RequestRoute(), ReqsSig(_RequestRoute<>::_request_list) {} +}; + + + +/// +/// 消息发送注册基类 +/// +/// +template +struct RequestRoute : virtual public WsComponent, public _RequestRoute { + RequestRoute() : _RequestRoute() {} + virtual QList requestSignatures() const { + return _RequestRoute<>::_request_list; + } +}; +