#include "DeduceFramework.h" #include #include #include QMutex DeduceFramework::_static_mutex; QList> deduct_each(std::shared_ptr item) { QMutexLocker loc(&DeduceFramework::_static_mutex); QList> rets; for (auto msg : DeduceFramework::_accept_stack) { if (msg->targetEntity() == item->entityID()) { auto enters = item->getRespondWithInType(msg->topicString()); for (auto efunc : enters) efunc(msg, rets); } } return rets; } QList> DeduceFramework::_accept_stack; void DeduceFramework::accept(const QList>& set) { QMutexLocker loc(&DeduceFramework::_static_mutex); _accept_stack.append(set); } #include #include void DeduceFramework::deduceBegin(std::shared_ptr ins) { auto dt = QTime::currentTime(); DeduceFramework::_static_mutex.lock(); auto items = _entity_map_over_0xffff.values(); for(auto item : _templets_within_0x2ff_0xffff.values()) items.append(item); DeduceFramework::_static_mutex.unlock(); auto ret_list_set = QtConcurrent::blockingMapped(items, deduct_each); _accept_stack.clear(); for (auto vlist : ret_list_set) emit this->reply(vlist); auto fmsg = std::make_shared(); fmsg->reset(ins->targetEntity(), ins->sourceEntity()); fmsg->_time_consume = dt.msecsTo(QTime::currentTime()); emit this->complete(fmsg); } #include #include "ComponentsInfoPull.h" DeduceFramework::DeduceFramework() :_factory_ins(std::make_shared()) { } #include "RouteManage.h" void DeduceFramework::init_framework() { auto mgr_e = std::make_shared(); mgr_e->append(this->shared_from_this()); auto info_pulls = std::make_shared(); mgr_e->append(info_pulls); this->_entity_map_over_0xffff[mgr_e->entityID()] = mgr_e; auto res_e = std::make_shared(); info_pulls = std::make_shared(); res_e->append(info_pulls); auto route_mgr = std::make_shared(); res_e->append(route_mgr); this->_entity_map_over_0xffff[res_e->entityID()] = res_e; } std::shared_ptr DeduceFramework::newDefault() const { throw std::logic_error("The method or operation is not implemented."); } void DeduceFramework::bindEntity(std::weak_ptr ins) { this->_bind_entity = ins; } QString DeduceFramework::name() const { return NAME(DeduceFramework); } void DeduceFramework::recoveryFrom(const QJsonObject& obj) { throw std::logic_error("The method or operation is not implemented."); } void DeduceFramework::saveTo(QJsonObject& obj) const { throw std::logic_error("The method or operation is not implemented."); } void DeduceFramework::execute( std::shared_ptr in, QList>& out) { auto dt = QTime::currentTime(); auto result = std::make_shared(); result->reset(in->targetEntity(), in->sourceEntity()); 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; 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; } }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; } result->_time_consume = dt.msecsTo(QTime::currentTime()); out << result; } void DeduceFramework::execute( std::shared_ptr in, QList>& out) { auto rst = std::make_shared(); rst->reset(in->targetEntity(), in->sourceEntity()); rst->_entities_list = _entity_map_over_0xffff.keys(); out << rst; } void DeduceFramework::execute( std::shared_ptr in, QList>& out) { auto ins = std::make_shared(); ins->reset(in->targetEntity(), in->sourceEntity()); ins->_component_types = this->_factory_ins->allComponentTypes(); ins->_entity_templets[NAME(RtEntityManager)] = RtEntityManager::const_id; ins->_entity_templets[NAME(RtResourceManager)] = RtResourceManager::const_id; for (auto ekey : this->_templets_within_0x2ff_0xffff.keys()) ins->_entity_templets[ekey] = this->_templets_within_0x2ff_0xffff[ekey]->entityID(); out << ins; } void DeduceFramework::execute( std::shared_ptr in, QList>& out) { auto dt = QTime::currentTime(); auto nins = std::make_shared(); 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); 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 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; 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; } nins->_time_consume = dt.msecsTo(QTime::currentTime()); out << nins; } void DeduceFramework::execute( std::shared_ptr in, QList>& out) { auto dt = QTime::currentTime(); auto result = std::make_shared(); result->reset(in->targetEntity(), in->sourceEntity()); // 提取目标实例 ==================================================== std::shared_ptr target_ins = nullptr; if (in->_entity_id_within_0x2ff_0xffffffffffffffff < 0xffff) { for (auto info_t : this->_templets_within_0x2ff_0xffff) { if (info_t->entityID() == in->_entity_id_within_0x2ff_0xffffffffffffffff) { target_ins = info_t; break; } } } else { if (this->_entity_map_over_0xffff.contains(in->_entity_id_within_0x2ff_0xffffffffffffffff)) { target_ins = std::static_pointer_cast( this->_entity_map_over_0xffff[in->_entity_id_within_0x2ff_0xffffffffffffffff]); } } if (!target_ins) { result->_reason_text = QString("指定的实体id:%1不存在").arg(in->_entity_id_within_0x2ff_0xffffffffffffffff); } else { result->_success_mark = true; QStringList total_component_types = this->_factory_ins->allComponentTypes(); for (auto chk : in->_component_types) { if (!total_component_types.contains(chk)) { result->_success_mark = false; result->_reason_text = QString("ComponentType错误,包含非法类型:%1").arg(chk); break; } } if (result->_success_mark) { auto comps_list = target_ins->components(); for (auto comp : comps_list) { if (!in->_component_types.contains(comp->name())) { target_ins->remove(comp->name()); } } comps_list = target_ins->components(); QStringList exists_component_types; std::transform(comps_list.begin(), comps_list.end(), std::back_inserter(exists_component_types), [](std::shared_ptr c) { return c->name(); }); for (auto comp : in->_component_types) { if (!exists_component_types.contains(comp)) { auto ins_comp = this->_factory_ins->makeNew(comp); target_ins->append(ins_comp); } } } } result->_time_consume = dt.msecsTo(QTime::currentTime()); out << result; } #include "VisibleCube.h" #include "SurfaceMotion.h" ComponentFactory::ComponentFactory() { auto ins0 = std::make_shared(); _comp_types[ins0->name()] = ins0; auto ins1 = std::make_shared(); _comp_types[ins1->name()] = ins1; } std::shared_ptr ComponentFactory::makeNew(const QString& type) { if (!this->_comp_types.contains(type)) return NULL; auto ntype = _comp_types[type]; return std::static_pointer_cast(ntype->newDefault()); } QList ComponentFactory::allComponentTypes() const { return _comp_types.keys(); } std::shared_ptr ComponentFactory::newDefault() const { return nullptr; } void ComponentFactory::recoveryFrom(const QJsonObject& obj) { QStringList tlist; STRLIST_PEAK(tlist); auto curr_list = _comp_types.keys(); for (auto chk : tlist) if (!curr_list.contains(chk)) throw std::logic_error("基础数据不兼容"); } void ComponentFactory::saveTo(QJsonObject& obj) const { QStringList tlist = _comp_types.keys(); std::sort(tlist.begin(), tlist.end()); STRLIST_SAVE(tlist); }