SimsWorld/ComponentBasic/DeduceFramework.cpp

372 lines
11 KiB
C++
Raw Normal View History

2025-06-11 15:09:20 +00:00
#include "DeduceFramework.h"
#include <stdexcept>
2025-06-10 18:05:00 +00:00
#include <QMutex>
#include <QMutexLocker>
2025-06-10 18:05:00 +00:00
QMutex DeduceFramework::_static_mutex;
2025-06-08 12:17:02 +00:00
QList<std::shared_ptr<WsMessage>> deduct_each(std::shared_ptr<WsEntity> item)
{
2025-06-10 18:05:00 +00:00
QMutexLocker loc(&DeduceFramework::_static_mutex);
QList<std::shared_ptr<WsMessage>> rets;
for (auto msg : DeduceFramework::_accept_stack) {
if (msg->targetEntity() == item->entityID()) {
2025-06-28 17:38:37 +00:00
auto enters = item->getRespondWithInType(msg->topicString());
for (auto efunc : enters)
efunc(msg, rets);
}
}
return rets;
}
QList<std::shared_ptr<WsMessage>> DeduceFramework::_accept_stack;
void DeduceFramework::accept(const QList<std::shared_ptr<WsMessage>>& set)
{
2025-06-10 18:05:00 +00:00
QMutexLocker loc(&DeduceFramework::_static_mutex);
_accept_stack.append(set);
}
#include <QTime>
#include <QtConcurrent>
void DeduceFramework::deduceBegin(std::shared_ptr<const DeduceRequest> ins)
{
auto dt = QTime::currentTime();
2025-06-10 18:05:00 +00:00
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<RespondDefault>();
fmsg->reset(ins->targetEntity(), ins->sourceEntity());
fmsg->_time_consume = dt.msecsTo(QTime::currentTime());
emit this->complete(fmsg);
}
#include <simsbasic.h>
2025-06-28 17:38:37 +00:00
#include "ComponentsInfoPull.h"
2025-06-08 12:17:02 +00:00
DeduceFramework::DeduceFramework()
:_factory_ins(std::make_shared<ComponentFactory>()) {
}
#include "RouteManage.h"
2025-06-10 13:01:20 +00:00
void DeduceFramework::init_framework()
2025-06-08 12:17:02 +00:00
{
2025-06-10 04:41:33 +00:00
auto mgr_e = std::make_shared<RtEntityManager>();
mgr_e->append(this->shared_from_this());
2025-06-10 15:44:38 +00:00
auto info_pulls = std::make_shared<ComponentsInfoPull>();
mgr_e->append(info_pulls);
2025-06-08 12:17:02 +00:00
this->_entity_map_over_0xffff[mgr_e->entityID()] = mgr_e;
2025-06-12 00:04:17 +00:00
auto res_e = std::make_shared<RtResourceManager>();
info_pulls = std::make_shared<ComponentsInfoPull>();
res_e->append(info_pulls);
2025-06-22 02:05:31 +00:00
auto route_mgr = std::make_shared<PlainRouteManagePlugin>();
res_e->append(route_mgr);
2025-06-12 00:04:17 +00:00
this->_entity_map_over_0xffff[res_e->entityID()] = res_e;
}
2025-06-28 17:38:37 +00:00
std::shared_ptr<Serializable> DeduceFramework::newDefault() const
{
throw std::logic_error("The method or operation is not implemented.");
}
2025-06-08 12:17:02 +00:00
void DeduceFramework::bindEntity(std::weak_ptr<WsEntity> ins)
{
2025-06-08 12:17:02 +00:00
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.");
}
2025-06-28 17:38:37 +00:00
void DeduceFramework::execute(
2025-06-08 12:17:02 +00:00
std::shared_ptr<const EntityOperate> in, QList<std::shared_ptr<RespondDefault>>& out)
{
2025-06-08 12:17:02 +00:00
auto dt = QTime::currentTime();
auto result = std::make_shared<RespondDefault>();
result->reset(in->targetEntity(), in->sourceEntity());
switch ((EntityOperateType)in->_operate_code)
{
2025-06-28 17:38:37 +00:00
case EntityOperateType::NEW: {
if (!this->_templets_within_0x2ff_0xffff.contains(in->_template_name)) {
result->_reason_text = QString("ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Template<EFBFBD><EFBFBD>%1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>").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<RtWsEntity>(ins_t->newDefault());
new_copy->resetID(0xffff);
auto desc_p = std::make_shared<ComponentsInfoPull>();
new_copy->append(desc_p);
while (_entity_map_over_0xffff.contains(new_copy->entityID()))
{
auto ncode = new_copy->entityID() + 1;
new_copy->resetID(ncode);
2025-06-08 12:17:02 +00:00
}
2025-06-28 17:38:37 +00:00
_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("ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>id<EFBFBD><EFBFBD>%1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>").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("ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>id<EFBFBD><EFBFBD>%1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>").arg(in->_entity_id_over_0xffff);
}
else {
result->_success_mark = true;
auto target_ins = std::static_pointer_cast<RtWsEntity>(
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("ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>%1").arg(in->_operate_code);
break;
2025-06-08 12:17:02 +00:00
}
result->_time_consume = dt.msecsTo(QTime::currentTime());
out << result;
}
2025-06-08 12:17:02 +00:00
2025-06-28 17:38:37 +00:00
void DeduceFramework::execute(
std::shared_ptr<const EntityTotalGet> in, QList<std::shared_ptr<EntityTotalList>>& out)
{
auto rst = std::make_shared<EntityTotalList>();
rst->reset(in->targetEntity(), in->sourceEntity());
2025-06-08 12:17:02 +00:00
rst->_entities_list = _entity_map_over_0xffff.keys();
out << rst;
}
2025-06-08 12:17:02 +00:00
2025-06-28 17:38:37 +00:00
void DeduceFramework::execute(
2025-06-08 12:17:02 +00:00
std::shared_ptr<const TypesQuery> in, QList<std::shared_ptr<TypesQueryResult>>& out)
{
auto ins = std::make_shared<TypesQueryResult>();
ins->reset(in->targetEntity(), in->sourceEntity());
ins->_component_types = this->_factory_ins->allComponentTypes();
2025-06-10 15:44:38 +00:00
ins->_entity_templets[NAME(RtEntityManager)] = RtEntityManager::const_id;
2025-06-12 00:04:17 +00:00
ins->_entity_templets[NAME(RtResourceManager)] = RtResourceManager::const_id;
2025-06-08 12:17:02 +00:00
for (auto ekey : this->_templets_within_0x2ff_0xffff.keys())
ins->_entity_templets[ekey] = this->_templets_within_0x2ff_0xffff[ekey]->entityID();
out << ins;
}
2025-06-28 17:38:37 +00:00
void DeduceFramework::execute(
2025-06-08 12:17:02 +00:00
std::shared_ptr<const TempletOperate> in, QList<std::shared_ptr<RespondDefault>>& out)
{
auto dt = QTime::currentTime();
auto nins = std::make_shared<RespondDefault>();
nins->reset(in->targetEntity(), in->sourceEntity());
switch ((EntityOperateType)in->_operate_code) {
2025-06-28 17:38:37 +00:00
case EntityOperateType::NEW: {
nins->_success_mark = true;
auto ent_t = std::make_shared<RtWsEntity>();
ent_t->resetID(0x2ff);
ent_t->resetTemplet(in->_template_name);
auto desc_p = std::make_shared<ComponentsInfoPull>();
ent_t->append(desc_p);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>е<EFBFBD>ģ<EFBFBD><C4A3>id
auto _tm_ins = _templets_within_0x2ff_0xffff.values();
QList<uint64_t> allids;
std::transform(_templets_within_0x2ff_0xffff.begin(), _templets_within_0x2ff_0xffff.end(),
std::back_inserter(allids), [](std::shared_ptr<RtWsEntity> i) { return i->entityID(); });
// <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>id
while (allids.contains(ent_t->entityID())) {
auto nid = ent_t->entityID() + 1;
ent_t->resetID(nid);
}
2025-06-08 12:17:02 +00:00
2025-06-28 17:38:37 +00:00
// ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ
_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("ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>id<EFBFBD><EFBFBD>%1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>").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;
2025-06-08 12:17:02 +00:00
}
2025-06-28 17:38:37 +00:00
}
}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("ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>id<EFBFBD><EFBFBD>%1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>").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;
2025-06-08 12:17:02 +00:00
}
2025-06-28 17:38:37 +00:00
}
}break;
default:
nins->_reason_text = QString("ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>%1").arg(in->_operate_code);
break;
2025-06-08 12:17:02 +00:00
}
nins->_time_consume = dt.msecsTo(QTime::currentTime());
out << nins;
}
2025-06-28 17:38:37 +00:00
void DeduceFramework::execute(
2025-06-08 12:17:02 +00:00
std::shared_ptr<const ComponentOperate> in, QList<std::shared_ptr<RespondDefault>>& out)
{
auto dt = QTime::currentTime();
auto result = std::make_shared<RespondDefault>();
result->reset(in->targetEntity(), in->sourceEntity());
// <20><>ȡĿ<C8A1><C4BF>ʵ<EFBFBD><CAB5> ====================================================
2025-06-10 04:41:33 +00:00
std::shared_ptr<RtWsEntity> target_ins = nullptr;
2025-06-08 12:17:02 +00:00
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)) {
2025-06-10 04:41:33 +00:00
target_ins = std::static_pointer_cast<RtWsEntity>(
2025-06-08 12:17:02 +00:00
this->_entity_map_over_0xffff[in->_entity_id_within_0x2ff_0xffffffffffffffff]);
}
}
if (!target_ins) {
result->_reason_text = QString("ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>id<EFBFBD><EFBFBD>%1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>").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<EFBFBD><EFBFBD><EFBFBD>󣬰<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:%1").arg(chk);
break;
}
}
if (result->_success_mark) {
auto comps_list = target_ins->components();
2025-06-10 06:12:53 +00:00
for (auto comp : comps_list) {
if (!in->_component_types.contains(comp->name())) {
target_ins->remove(comp->name());
}
}
2025-06-08 12:17:02 +00:00
2025-06-10 06:12:53 +00:00
comps_list = target_ins->components();
2025-06-08 12:17:02 +00:00
QStringList exists_component_types;
std::transform(comps_list.begin(), comps_list.end(),
std::back_inserter(exists_component_types),
2025-07-10 00:04:21 +00:00
[](std::shared_ptr<WsSpecializedSystem> c) { return c->name(); });
2025-06-08 12:17:02 +00:00
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;
}
2025-06-14 05:30:42 +00:00
#include "VisibleCube.h"
2025-06-22 08:56:37 +00:00
#include "SurfaceMotion.h"
2025-06-08 12:17:02 +00:00
ComponentFactory::ComponentFactory()
{
2025-06-22 08:57:41 +00:00
auto ins0 = std::make_shared<VisibleCubePlugin>();
_comp_types[ins0->name()] = ins0;
2025-06-22 08:56:37 +00:00
auto ins1 = std::make_shared<SurfaceMotionPlugin>();
_comp_types[ins1->name()] = ins1;
2025-06-08 12:17:02 +00:00
}
2025-07-10 00:04:21 +00:00
std::shared_ptr<WsSpecializedSystem> ComponentFactory::makeNew(const QString& type)
2025-06-08 12:17:02 +00:00
{
if (!this->_comp_types.contains(type))
return NULL;
auto ntype = _comp_types[type];
2025-07-10 00:04:21 +00:00
return std::static_pointer_cast<WsSpecializedSystem>(ntype->newDefault());
2025-06-08 12:17:02 +00:00
}
QList<QString> ComponentFactory::allComponentTypes() const
{
return _comp_types.keys();
}
2025-06-28 17:38:37 +00:00
std::shared_ptr<Serializable> ComponentFactory::newDefault() const
{
return nullptr;
}
2025-06-08 12:17:02 +00:00
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
void ComponentFactory::saveTo(QJsonObject& obj) const
{
QStringList tlist = _comp_types.keys();
std::sort(tlist.begin(), tlist.end());
STRLIST_SAVE(tlist);
}