372 lines
11 KiB
C++
372 lines
11 KiB
C++
#include "DeduceFramework.h"
|
||
#include <stdexcept>
|
||
#include <QMutex>
|
||
#include <QMutexLocker>
|
||
|
||
QMutex DeduceFramework::_static_mutex;
|
||
QList<std::shared_ptr<WsMessage>> deduct_each(std::shared_ptr<WsEntity> item)
|
||
{
|
||
QMutexLocker loc(&DeduceFramework::_static_mutex);
|
||
|
||
QList<std::shared_ptr<WsMessage>> 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<std::shared_ptr<WsMessage>> DeduceFramework::_accept_stack;
|
||
void DeduceFramework::accept(const QList<std::shared_ptr<WsMessage>>& set)
|
||
{
|
||
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();
|
||
|
||
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>
|
||
#include "ComponentsInfoPull.h"
|
||
DeduceFramework::DeduceFramework()
|
||
:_factory_ins(std::make_shared<ComponentFactory>()) {
|
||
}
|
||
|
||
#include "RouteManage.h"
|
||
void DeduceFramework::init_framework()
|
||
{
|
||
auto mgr_e = std::make_shared<RtEntityManager>();
|
||
mgr_e->append(this->shared_from_this());
|
||
auto info_pulls = std::make_shared<ComponentsInfoPull>();
|
||
mgr_e->append(info_pulls);
|
||
this->_entity_map_over_0xffff[mgr_e->entityID()] = mgr_e;
|
||
|
||
auto res_e = std::make_shared<RtResourceManager>();
|
||
info_pulls = std::make_shared<ComponentsInfoPull>();
|
||
res_e->append(info_pulls);
|
||
auto route_mgr = std::make_shared<PlainRouteManagePlugin>();
|
||
res_e->append(route_mgr);
|
||
this->_entity_map_over_0xffff[res_e->entityID()] = res_e;
|
||
}
|
||
|
||
std::shared_ptr<Serializable> DeduceFramework::newDefault() const
|
||
{
|
||
throw std::logic_error("The method or operation is not implemented.");
|
||
}
|
||
|
||
void DeduceFramework::bindEntity(std::weak_ptr<WsEntity> 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<const EntityOperate> in, QList<std::shared_ptr<RespondDefault>>& out)
|
||
{
|
||
auto dt = QTime::currentTime();
|
||
|
||
auto result = std::make_shared<RespondDefault>();
|
||
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("ָ<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);
|
||
}
|
||
_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;
|
||
}
|
||
|
||
result->_time_consume = dt.msecsTo(QTime::currentTime());
|
||
out << result;
|
||
}
|
||
|
||
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());
|
||
rst->_entities_list = _entity_map_over_0xffff.keys();
|
||
out << rst;
|
||
}
|
||
|
||
void DeduceFramework::execute(
|
||
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();
|
||
|
||
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<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) {
|
||
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);
|
||
}
|
||
|
||
// ģ<><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;
|
||
}
|
||
}
|
||
}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;
|
||
}
|
||
}
|
||
}break;
|
||
default:
|
||
nins->_reason_text = QString("ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>%1").arg(in->_operate_code);
|
||
break;
|
||
}
|
||
|
||
nins->_time_consume = dt.msecsTo(QTime::currentTime());
|
||
out << nins;
|
||
}
|
||
|
||
void DeduceFramework::execute(
|
||
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> ====================================================
|
||
std::shared_ptr<RtWsEntity> 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<RtWsEntity>(
|
||
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();
|
||
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<WsSpecializedSystem> 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<VisibleCubePlugin>();
|
||
_comp_types[ins0->name()] = ins0;
|
||
auto ins1 = std::make_shared<SurfaceMotionPlugin>();
|
||
_comp_types[ins1->name()] = ins1;
|
||
}
|
||
|
||
std::shared_ptr<WsSpecializedSystem> ComponentFactory::makeNew(const QString& type)
|
||
{
|
||
if (!this->_comp_types.contains(type))
|
||
return NULL;
|
||
|
||
auto ntype = _comp_types[type];
|
||
return std::static_pointer_cast<WsSpecializedSystem>(ntype->newDefault());
|
||
}
|
||
|
||
QList<QString> ComponentFactory::allComponentTypes() const
|
||
{
|
||
return _comp_types.keys();
|
||
}
|
||
|
||
std::shared_ptr<Serializable> 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("<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);
|
||
}
|