SimsWorld/ComponentBasic/BehaviorPerform.cpp

839 lines
18 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "BehaviorPerform.h"
void BehaviorPerformPlugin::mapReset(const QString& path)
{
}
void BehaviorPerformPlugin::execute(std::shared_ptr<const EntityInitialRequest> in, QList<std::shared_ptr<RespondDefault>>& out)
{
}
void BehaviorPerformPlugin::execute(std::shared_ptr<const EntityPreparedRequest> in, QList<std::shared_ptr<RespondDefault>>& out)
{
}
void BehaviorPerformPlugin::execute(std::shared_ptr<const SyncRequest> in, QList<std::shared_ptr<RespondDefault>>& out)
{
}
void BehaviorPerformPlugin::execute(std::shared_ptr<const DeduceRequest> in, QList<std::shared_ptr<RespondDefault>>& out)
{
}
void BehaviorPerformPlugin::recoveryFrom(const QJsonObject& obj)
{
}
void BehaviorPerformPlugin::saveTo(QJsonObject& obj) const
{
}
std::shared_ptr<Serializable> BehaviorPerformPlugin::newDefault() const
{
return std::make_shared<BehaviorPerformPlugin>();
}
void BehaviorPerformPlugin::bindEntity(std::weak_ptr<WsEntity> host)
{
this->_bind_entity = host;
}
QString BehaviorPerformPlugin::name() const
{
return NAME(BehaviorPerformPlugin);
}
#include <MessageLoader.h>
MapKernal::MapKernal(std::shared_ptr<MessageLoader> ins)
: _bind_loader(ins) {
}
QList<QString> MapKernal::compareTypes() const
{
return _compare_types.keys();
}
void MapKernal::registCompare(std::shared_ptr<CompareDelegate> ins)
{
_compare_types[ins->name()] = ins;
emit this->compareTypeListChanged();
}
std::shared_ptr<CompareDelegate> MapKernal::getCompare(const QString& name)
{
if (!_compare_types.contains(name))
return nullptr;
return _compare_types[name];
}
void MapKernal::registConvert(std::shared_ptr<ConvertDelegate> ins)
{
_convert_types[ins->name()] = ins;
emit this->convertTypeListChanged();
}
QList<QString> MapKernal::convertTypes() const
{
return _convert_types.keys();
}
std::shared_ptr<ConvertDelegate> MapKernal::getConvert(const QString& name)
{
if (!_convert_types.contains(name))
return nullptr;
return _convert_types[name];
}
void MapKernal::registExecute(std::shared_ptr<ExecuteDelegate> ins)
{
_execute_types[ins->typeName()] = ins;
emit this->executeTypeListChanged();
}
std::shared_ptr<MessageLoader> MapKernal::messageLoader() const
{
return this->_bind_loader;
}
void MapKernal::initial()
{
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
registData(std::make_shared<GeneralData>());
for (auto key : messageLoader()->allType())
registData(messageLoader()->makeDefault(key));
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD>ýڵ<C3BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
registNode(std::make_shared<SequenceNode>());
registNode(std::make_shared<SelectorNode>());
registNode(std::make_shared<ParallelNode>());
registNode(std::make_shared<CompareNode>(this->shared_from_this()));
registNode(std::make_shared<ExecuteNode>(this->shared_from_this()));
registNode(std::make_shared<BehaviorMapNode>(this->shared_from_this()));
}
void MapKernal::registData(std::shared_ptr<TopicData> init)
{
if (_variable_types.contains(init->topicString()))
throw new UniException(QString(u8"<EFBFBD>ظ<EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>{%1}").arg(init->topicString()));
_variable_types[init->topicString()] = init;
}
QList<QString> MapKernal::dataTypes() const
{
return _variable_types.keys();
}
std::shared_ptr<TopicData> MapKernal::getData(const QString& topic)
{
return _variable_types[topic];
}
QList<QString> MapKernal::executeTypes() const
{
return _execute_types.keys();
}
std::shared_ptr<ExecuteDelegate> MapKernal::getExecute(const QString& name)
{
return _execute_types[name];
}
void MapKernal::registNode(std::shared_ptr<LogicalNode> ins)
{
_logicalnode_types[ins->typeName()] = ins;
}
QList<QString> MapKernal::nodeTypes() const
{
return _logicalnode_types.keys();
}
std::shared_ptr<LogicalNode> MapKernal::getNode(const QString& name)
{
return _logicalnode_types[name];
}
std::shared_ptr<Serializable> MapKernal::newDefault() const
{
return nullptr;
}
LogicalNode::LogicalNode(NodeKind t /*= NodeKind::ACTIONNODE*/)
:_node_type(t) {
}
void LogicalNode::_set_parent_node(std::weak_ptr<LogicalNode> pnode)
{
this->_parent_bind = pnode;
}
NodeKind LogicalNode::nodeKind() const
{
return _node_type;
}
int LogicalNode::depth() const
{
auto node_temp = this->parent().lock();
if (!node_temp) return 0;
return this->parent().lock()->depth() + 1;
}
std::shared_ptr<BehaviorMapNode> 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 std::dynamic_pointer_cast<BehaviorMapNode>(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(u8"%1(%2)").arg(this->typeName()).arg(getID());
}
std::weak_ptr<LogicalNode> LogicalNode::parent() const
{
return this->_parent_bind;
}
BehaviorMapNode::BehaviorMapNode(std::shared_ptr<MapKernal> core)
:LogicalNode(NodeKind::MAPNODE), _bind_kernal(core) {
}
std::shared_ptr<MapKernal> BehaviorMapNode::getKernal() const
{
return _bind_kernal;
}
void BehaviorMapNode::setVariable(const QString& key, IO_TYPE t, std::shared_ptr<TopicData> ins)
{
if (_variables.contains(key))
if (_variables[key].second->topicString() != ins->topicString())
throw new UniException(u8"<EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD><EFBFBD>ͬһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>");
_variables[key] = std::make_pair(t, ins);
}
std::shared_ptr<TopicData> 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<Serializable> BehaviorMapNode::newDefault() const
{
return std::make_shared<BehaviorMapNode>(this->_bind_kernal);
}
void BehaviorMapNode::recoveryFrom(const QJsonObject& obj)
{
}
void BehaviorMapNode::saveTo(QJsonObject& obj) const
{
}
QList<std::shared_ptr<LogicalNode>> BehaviorMapNode::getForwards() const
{
return children();
}
bool BehaviorMapNode::fallback(std::shared_ptr<LogicalNode> 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<QString, std::shared_ptr<TopicData>> BehaviorMapNode::inputList() const
{
QHash<QString, std::shared_ptr<TopicData>> list;
for (auto key : this->_variables.keys()) {
if (this->_variables[key].first == IO_TYPE::INPUT && key.contains(u8"::"))
list[key] = _variables[key].second;
}
return list;
}
QHash<QString, std::shared_ptr<TopicData>> BehaviorMapNode::outputList() const
{
QHash<QString, std::shared_ptr<TopicData>> list;
for (auto key : this->_variables.keys()) {
if (this->_variables[key].first == IO_TYPE::OUTPUT && key.contains(u8"::"))
list[key] = _variables[key].second;
}
return list;
}
SequenceNode::SequenceNode()
: LogicalNode(NodeKind::SEQUENCENODE) {
}
QList<std::shared_ptr<LogicalNode>> SequenceNode::getForwards() const
{
for (auto item : children()) {
// <20>ӽڵ<D3BD>ִ<EFBFBD><D6B4>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
if (_state_map.contains(item->getID())) {
if (_state_map[item->getID()] != LogicalResult::SUCCESS)
return QList<std::shared_ptr<LogicalNode>>();
}
// <20><>ȡ<EFBFBD>½ڵ<C2BD>
if (!_state_map.contains(item->getID()))
return QList<std::shared_ptr<LogicalNode>>() << item;
}
// ȫ<><C8AB><EFBFBD>ɹ<EFBFBD>
return QList<std::shared_ptr<LogicalNode>>();
}
QString SequenceNode::typeName() const
{
return u8"˳<EFBFBD><EFBFBD>";
}
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<QString, std::shared_ptr<TopicData>> SequenceNode::inputList() const
{
return QHash<QString, std::shared_ptr<TopicData>>();
}
QHash<QString, std::shared_ptr<TopicData>> SequenceNode::outputList() const
{
return QHash<QString, std::shared_ptr<TopicData>>();
}
void SequenceNode::recoveryFrom(const QJsonObject& obj)
{
QStringList state_items;
STRLIST_PEAK(state_items);
for (auto kv : state_items) {
auto kv_pair = kv.split(u8":");
_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(u8"%1:%2").arg(inv).arg((int)_state_map[inv]);
}
STRLIST_SAVE(state_items);
}
bool SequenceNode::fallback(std::shared_ptr<LogicalNode> node, LogicalResult ste)
{
this->_state_map[node->getID()] = ste;
return true;
}
std::shared_ptr<Serializable> SequenceNode::newDefault() const
{
return std::make_shared<SequenceNode>();
}
void SequenceNode::reset()
{
this->_state_map.clear();
}
QList<std::shared_ptr<LogicalNode>> LogicalNode::children() const
{
return _child_list;
}
void LogicalNode::insert(std::shared_ptr<LogicalNode> 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<LogicalNode> node)
{
_child_list.removeAll(node);
}
SelectorNode::SelectorNode()
: LogicalNode(NodeKind::SEQUENCENODE) {
}
QList<std::shared_ptr<LogicalNode>> SelectorNode::getForwards() const
{
for (auto item : children()) {
// <20>ӽڵ<D3BD>ִ<EFBFBD><D6B4>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
if (_state_map.contains(item->getID())) {
if (_state_map[item->getID()] == LogicalResult::SUCCESS)
return QList<std::shared_ptr<LogicalNode>>();
}
// <20><>ȡ<EFBFBD>½ڵ<C2BD>
if (!_state_map.contains(item->getID()))
return QList<std::shared_ptr<LogicalNode>>() << item;
}
// ȫ<><C8AB><EFBFBD>ɹ<EFBFBD>
return QList<std::shared_ptr<LogicalNode>>();
}
bool SelectorNode::fallback(std::shared_ptr<LogicalNode> node, LogicalResult ste)
{
_state_map[node->getID()] = ste;
return true;
}
std::shared_ptr<Serializable> SelectorNode::newDefault() const
{
return std::make_shared<SelectorNode>();
}
void SelectorNode::recoveryFrom(const QJsonObject& obj)
{
QStringList state_items;
STRLIST_PEAK(state_items);
for (auto kv : state_items) {
auto kv_pair = kv.split(u8":");
_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(u8"%1:%2").arg(inv).arg((int)_state_map[inv]);
}
STRLIST_SAVE(state_items);
}
void SelectorNode::reset()
{
_state_map.clear();
}
QString SelectorNode::typeName() const
{
return u8"ѡ<EFBFBD><EFBFBD>";
}
LogicalResult SelectorNode::execute()
{
for (auto value : _state_map) {
if (value == LogicalResult::SUCCESS)
return LogicalResult::SUCCESS;
}
return LogicalResult::FAILURE;
}
QHash<QString, std::shared_ptr<TopicData>> SelectorNode::inputList() const
{
return {};
}
QHash<QString, std::shared_ptr<TopicData>> SelectorNode::outputList() const
{
return {};
}
QList<std::shared_ptr<LogicalNode>> ParallelNode::getForwards() const
{
if (this->children().size() == _state_map.size())
return QList<std::shared_ptr<LogicalNode>>();
return this->children();
}
bool ParallelNode::fallback(std::shared_ptr<LogicalNode> 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 u8"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
}
LogicalResult ParallelNode::execute()
{
for (auto ste : this->_state_map)
if (ste != LogicalResult::SUCCESS)
return LogicalResult::FAILURE;
return LogicalResult::SUCCESS;
}
std::shared_ptr<Serializable> ParallelNode::newDefault() const
{
return std::make_shared<ParallelNode>();
}
ParallelNode::ParallelNode()
: LogicalNode(NodeKind::PARALLELNODE) {
}
QHash<QString, std::shared_ptr<TopicData>> ParallelNode::inputList() const
{
return QHash<QString, std::shared_ptr<TopicData>>();
}
QHash<QString, std::shared_ptr<TopicData>> ParallelNode::outputList() const
{
return QHash<QString, std::shared_ptr<TopicData>>();
}
void ParallelNode::recoveryFrom(const QJsonObject& obj)
{
QStringList state_items;
STRLIST_PEAK(state_items);
for (auto kv : state_items) {
auto kv_pair = kv.split(u8":");
_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(u8"%1:%2").arg(inv).arg((int)_state_map[inv]);
}
STRLIST_SAVE(state_items);
}
void CompareNode::reset() {
}
QString CompareNode::typeName() const
{
return QString(u8"%1<%2>").arg(u8"<EFBFBD>Ƚ<EFBFBD>", delegateName());
}
LogicalResult CompareNode::execute()
{
if (this->_bind_delegate) {
auto vl = _data_map[u8"<EFBFBD><EFBFBD>ֵ"];
auto vb = _data_map[u8"<EFBFBD><EFBFBD>ֵ"];
if (this->_bind_delegate->compare(vl, vb))
return LogicalResult::SUCCESS;
}
return LogicalResult::FAILURE;
}
void CompareNode::insert(std::shared_ptr<LogicalNode> node, int index /*= -1*/)
{
}
QList<std::shared_ptr<LogicalNode>> CompareNode::getForwards() const
{
return QList<std::shared_ptr<LogicalNode>>();
}
bool CompareNode::fallback(std::shared_ptr<LogicalNode> node, LogicalResult ste) {
return true;
}
std::shared_ptr<Serializable> CompareNode::newDefault() const
{
return std::make_shared<CompareNode>(_bind_kernal);
}
CompareNode::CompareNode(std::shared_ptr<MapKernal> ins)
: LogicalNode(NodeKind::COMPARENODE), _bind_kernal(ins) {
_data_map[u8"<EFBFBD><EFBFBD>ֵ"] = std::make_shared<GeneralData>();
_data_map[u8"<EFBFBD><EFBFBD>ֵ"] = std::make_shared<GeneralData>();
}
QString CompareNode::delegateName() const
{
if (_bind_delegate)
return _bind_delegate->name();
return u8"";
}
void CompareNode::bindDelegate(std::shared_ptr<CompareDelegate> ins)
{
this->_bind_delegate = ins;
}
QHash<QString, std::shared_ptr<TopicData>> CompareNode::inputList() const
{
return _data_map;
}
QHash<QString, std::shared_ptr<TopicData>> CompareNode::outputList() const
{
return QHash<QString, std::shared_ptr<TopicData>>();
}
#include <QJsonObject>
void CompareNode::recoveryFrom(const QJsonObject& obj)
{
auto vala = obj[u8"ValL"];
auto valb = obj[u8"ValR"];
_data_map[u8"<EFBFBD><EFBFBD>ֵ"]->recoveryFrom(vala.toObject());
_data_map[u8"<EFBFBD><EFBFBD>ֵ"]->recoveryFrom(valb.toObject());
QString delegate_name = "";
STRING_PEAK(delegate_name);
auto deins = _bind_kernal->getCompare(delegate_name);
bindDelegate(deins);
}
void CompareNode::saveTo(QJsonObject& obj) const
{
QJsonObject vala, valb;
_data_map[u8"<EFBFBD><EFBFBD>ֵ"]->saveTo(vala);
_data_map[u8"<EFBFBD><EFBFBD>ֵ"]->saveTo(valb);
obj[u8"ValL"] = vala;
obj[u8"ValR"] = valb;
QString delegate_name = "";
if (_bind_delegate) {
delegate_name = _bind_delegate->name();
}
STRING_SAVE(delegate_name);
}
#include <QJsonDocument>
QString GeneralData::topicString() const
{
return u8"GeneralData";
}
std::shared_ptr<Serializable> GeneralData::newDefault() const
{
return std::make_shared<GeneralData>();
}
void GeneralData::recoveryFrom(const QJsonObject& obj)
{
_element_bind = obj;
}
void GeneralData::saveTo(QJsonObject& obj) const
{
obj = _element_bind;
}
CompareDelegate::CompareDelegate(QJSEngine& bind_engine, const QString& func)
:_script_engine(bind_engine), _function_name(func) {
}
QString CompareDelegate::name() const
{
return _function_name;
}
bool CompareDelegate::compare(std::shared_ptr<TopicData> vleft, std::shared_ptr<TopicData> vright)
{
return true;
}
ConvertDelegate::ConvertDelegate(std::shared_ptr<MapKernal> host, QJSEngine& bind_engine, const QString& func)
: _host_bind(host), _script_engine(bind_engine), _function_name(func) {
}
QString ConvertDelegate::name() const
{
return _function_name;
}
QList<std::pair<ConvertDelegate::DATA_TOPIC_STRING, QString>> ConvertDelegate::inputTable() const
{
return _input_table;
}
void ConvertDelegate::inputReset(const QList<std::pair<DATA_TOPIC_STRING, QString>>& table)
{
this->_input_table = table;
}
std::pair<ConvertDelegate::DATA_TOPIC_STRING, QString> ConvertDelegate::outputVariable() const
{
return _output_appoint;
}
void ConvertDelegate::outputReset(std::pair<DATA_TOPIC_STRING, QString> appoint)
{
this->_output_appoint = appoint;
}
std::shared_ptr<TopicData> ConvertDelegate::convert(const QList<std::shared_ptr<TopicData>>& input_variables)
{
return nullptr;
}
ExecuteNode::ExecuteNode(std::shared_ptr<MapKernal> ins)
: LogicalNode(NodeKind::ACTIONNODE), _bind_kernal(ins) {
}
QString ExecuteNode::delegateName() const
{
if (_bind_delegate)
return _bind_delegate->typeName();
return "";
}
void ExecuteNode::bindDelegate(std::shared_ptr<ExecuteDelegate> ins)
{
_bind_delegate = ins;
}
QList<std::shared_ptr<LogicalNode>> ExecuteNode::getForwards() const
{
return QList<std::shared_ptr<LogicalNode>>();
}
QString ExecuteNode::typeName() const
{
return QString(u8"ִ<EFBFBD><EFBFBD><%1>").arg(delegateName());
}
QHash<QString, std::shared_ptr<TopicData>> ExecuteNode::inputList() const
{
if (_bind_delegate)
return _bind_delegate->inputList();
return QHash<QString, std::shared_ptr<TopicData>>();
}
QHash<QString, std::shared_ptr<TopicData>> ExecuteNode::outputList() const
{
if (_bind_delegate)
return _bind_delegate->outputList();
return QHash<QString, std::shared_ptr<TopicData>>();
}
void ExecuteNode::insert(std::shared_ptr<LogicalNode> node, int index /*= -1*/)
{
}
void ExecuteNode::reset()
{
if (_bind_delegate)
_bind_delegate->reset();
}
LogicalResult ExecuteNode::execute()
{
if (_bind_delegate)
return _bind_delegate->execute();
return LogicalResult::FAILURE;
}
bool ExecuteNode::fallback(std::shared_ptr<LogicalNode> node, LogicalResult ste)
{
return true;
}
std::shared_ptr<Serializable> ExecuteNode::newDefault() const
{
return std::make_shared<ExecuteNode>(this->_bind_kernal);
}
void ExecuteNode::recoveryFrom(const QJsonObject& obj)
{
QString delegate_name = "";
STRING_PEAK(delegate_name);
auto de_ins = _bind_kernal->getExecute(delegate_name);
this->bindDelegate(de_ins);
if (obj.contains("data_json")) {
auto delegate_json = obj["data_json"].toObject();
de_ins->recoveryFrom(delegate_json);
}
}
void ExecuteNode::saveTo(QJsonObject& obj) const
{
QString delegate_name = "";
if (_bind_delegate)
delegate_name = _bind_delegate->typeName();
STRING_SAVE(delegate_name);
if (_bind_delegate) {
QJsonObject data_json;
this->_bind_delegate->saveTo(data_json);
obj["data_json"] = data_json;
}
}