#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) { } void BehaviorPerformPlugin::saveTo(QJsonObject& obj) const { } std::shared_ptr BehaviorPerformPlugin::newDefault() const { return std::make_shared(); } void BehaviorPerformPlugin::bindEntity(std::weak_ptr host) { this->_bind_entity = host; } QString BehaviorPerformPlugin::name() const { return NAME(BehaviorPerformPlugin); } #include MapKernal::MapKernal(std::shared_ptr ins) : _bind_loader(ins) { for (auto key : ins->allType()) registData(ins->makeDefault(key)); } QList MapKernal::compareTypes() const { 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::_set_parent_node(std::weak_ptr pnode) { this->_parent_bind = pnode; } NodeKind LogicalNode::nodeKind() const { return _node_type; } std::shared_ptr LogicalNode::bindMap() const { auto node_temp = this->parent().lock(); if (!node_temp) return nullptr; while (node_temp->nodeKind() != NodeKind::MAPNODE) { node_temp = node_temp->parent().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::rtName() const { return QString("%1(%2)").arg(this->typeName()).arg(getID()); } std::weak_ptr LogicalNode::parent() const { return this->_parent_bind; } BehaviorMapNode::BehaviorMapNode(std::shared_ptr core) :LogicalNode(NodeKind::MAPNODE), _bind_kernal(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 _variables[key].second; } void BehaviorMapNode::resetName(const QString& val) { this->_map_name = val; } std::shared_ptr BehaviorMapNode::newDefault() const { return std::make_shared(this->_bind_kernal); } 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> LogicalNode::children() const { return _child_list; } void LogicalNode::insert(std::shared_ptr node, int index /*= -1*/) { for (auto it : _child_list) if (it->getID() == node->getID()) return; node->_set_parent_node(this->shared_from_this()); _child_list.insert(index, node); } void LogicalNode::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; } void CompareNode::insert(std::shared_ptr node, int index /*= -1*/) { if (this->children().size()) return; LogicalNode::insert(node, index); } QList> CompareNode::getForwards() const { return QList>(); } bool CompareNode::fallback(std::shared_ptr node, LogicalResult ste) { return true; } 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; }