COMPASSi/trunk/code/projects/DataManager/Logic/Logic.DataManagement/Interpreters/ModifyInterpreter.cpp

180 lines
5.3 KiB
C++

#include "ModifyInterpreter.h"
#include "Constants.h"
#include "DataManager.h"
#include <algorithm>
#include <stdexcept>
#include <cmath> // For mathematical operations like std::abs and std::sqrt
#include <limits> // For std::numeric_limits
#include <regex>
#include "DataManagerGlobal.h"
#include "Common/tangible_string_helper.h"
DelInterpreter::DelInterpreter()
{
}
void DelInterpreter::ExecuteCommand(std::vector<std::string> &commandLines, std::vector<Empty> &lst , std::vector<std::string>& names)
{
// 现有文档对这些命令如何处理位置,暂时跳过
// if (commandLines.size() > 1)
// {
// std::string redir = StringHelper::toUpper(StringHelper::FirstWord(commandLines[1]));
// if (redir == "SEC" || redir == "MAT" || redir == "BEAMTYPE" || redir == "PLATETYPE")
// {
// RedirectExecuteCommand<DelSecInterpreter>(commandLines);
// return;
// }
// }
std::vector<std::string> lineName = StringHelper::GetWords(commandLines[0]);
if (lineName.size() < 2)
{
throw std::runtime_error("参数错误!");
}
for (size_t i = 1; i < lineName.size(); i++)
{
std::string name = lineName[i];
int delid = FindIDByName(name, false);
if (delid < 0)
{
throw std::runtime_error("未找到对象:" + name);
}
Empty m = Empty();
m.m_ID = delid;
m.m_Name = QString::fromStdString(name);
lst.push_back(m);
names.push_back(name);
}
// 删除排序
sortList(lst);
commandLines.erase(commandLines.begin());
}
void DelInterpreter::sortList(std::vector<Empty>& lst) {
if (lst.empty()) {
return;
}
std::vector<int> lstID;
for (const auto& item : lst) {
lstID.push_back(item.m_ID);
}
lstID = sortListByDependent(lstID); // 根据依赖关系排序 20170601 by czb
lst.clear();
for (const auto& item : lstID) {
Empty m;
m.m_ID = item;
lst.push_back(m);
}
}
vector<int> DelInterpreter::sortListByDependent(vector<int> lst) {
// 检查列表是否为空
if (lst.empty()) {
return lst;
}
// 存储依赖关系的字典
unordered_map<int, vector<int>> dependentDic;
vector<int> newList;
// 初始化依赖字典
for (size_t i = 0; i < lst.size(); ++i) {
dependentDic[lst[i]] = vector<int>();
}
// 建立依赖关系
for (size_t i = 0; i < lst.size(); ++i) {
for (size_t j = 0; j < lst.size(); ++j) {
if (i == j) {
continue;
}
int deriveID = lst[i];
int srcID = lst[j];
// 判断是否存在依赖关系
if (IsDependentByID(deriveID, srcID)) {
// 如果尚未包含此依赖,则添加
if (find(dependentDic[deriveID].begin(), dependentDic[deriveID].end(), srcID) == dependentDic[deriveID].end()) {
dependentDic[deriveID].push_back(srcID);
}
}
}
}
// 依赖排序
for (size_t k = 0; k < lst.size(); ++k) {
for (size_t i = 0; i < lst.size(); ++i) {
int curID = lst[i];
// 检查是否已在新列表中
if (find(newList.begin(), newList.end(), curID) != newList.end()) {
continue;
}
// 如果没有依赖项,则添加到新列表
if (dependentDic[curID].empty()) {
newList.push_back(curID);
// 清理依赖
dependentDic.erase(curID);
for (auto& item : dependentDic) {
auto& l = item.second;
auto it = find(l.begin(), l.end(), curID);
if (it != l.end()) {
l.erase(it);
}
}
}
}
// 如果新列表长度等于原列表,排序完成
if (newList.size() == lst.size()) break;
}
// 返回排序后的列表
return newList;
}
bool DelInterpreter::IsDependentByID(int deriveID, int srcID) {
if (deriveID == srcID) {
return false;
}
IModel* o1 = DataManager::GetObjectByID(deriveID);
IModel* o2 = DataManager::GetObjectByID(srcID);
// 对象不存在或名称、命令为空
if (o1 == nullptr || o2 == nullptr) {
return false;
}
if (o1->m_Command.isEmpty() || o2->m_Name.isEmpty()) {
return false;
}
// 生成比较字符串列表
vector<string> strList;
string nameUpper = " " + o2->m_Name.toStdString();
transform(nameUpper.begin(), nameUpper.end(), nameUpper.begin(), ::toupper); // 转为大写
strList.push_back(" " + nameUpper + " ");
strList.push_back(" " + nameUpper + "/");
strList.push_back("/" + nameUpper + "/");
strList.push_back("/" + nameUpper + " ");
strList.push_back("/" + nameUpper + "\r\n");
strList.push_back(" " + nameUpper + "\r\n");
// 将 o1->Command 转为大写并加上换行符
string cmdUpper = o1->m_Command.toStdString() + "\r\n";
transform(cmdUpper.begin(), cmdUpper.end(), cmdUpper.begin(), ::toupper);
// 检查依赖关系
for (const string& name : strList) {
if (cmdUpper.find(name) != string::npos) {
return true;
}
}
return false;
}