tuiliji/GenAlogrithm/DBLoader.cpp

583 lines
14 KiB
C++

#include "DBLoader.h"
#include "ABDBUtil.h"
#include"Func.h"
CDBLoader::CDBLoader(void)
{
//连接数据库
FighterDataList.clear();
ShipDataList.clear();
AirDefenseDataList.clear();
GroundTargetDataList.clear();
//在程序退出时调用Release()函数
//atexit(Release);
}
/*************************
*函数名:Instance()
*功 能:构建实例
*输 入:
*输 出:
***************************/
CDBLoader * CDBLoader::Instance()
{
CDBLoader* m_Instance=NULL;
if(m_Instance == NULL)
{
m_Instance = new CDBLoader();
if(m_Instance == NULL)
{
//AfxMessageBox("申请内存失败");
exit(0);
}
}
return m_Instance;
}
/*************************
*函数名:Release()
*功 能:释放实例
*输 入:
*输 出:
***************************/
//void CDBLoader::Release()
//{
// if(NULL != Instance)
// {
// delete Instance;
// Instance = NULL;
// }
//}
/*************************
*函数名:GetFighterDBRecord()
*功 能:获取战机性能参数数据集
***************************/
ABILITY* CDBLoader::GetFighterDBRecord(std::string & strModel)
{
if (FighterDataList.empty())
{
exit(0);
}
else if(FighterDataList.find(strModel) != FighterDataList.end())
{
return FighterDataList[strModel];
}
else
{
return FighterDataList.begin()->second;//begin()还是end()?
}
}
/*************************
*函数名:GetFighterDBRecordList()
*功 能:获取战机参数数据集
***************************/
std::map<std::string, ABILITY*>& CDBLoader::GetFighterDBRecordList()
{
if (FighterDataList.empty())
{
exit(0);
}
else
{
return FighterDataList;
}
}
/*************************
*函数名:GetShipDBRecord()
*功 能:获取战船参数数据集
***************************/
ABILITY* CDBLoader::GetShipDBRecord(std::string & strModel)
{
if (ShipDataList.empty())
{
exit(0);
}
else if(ShipDataList.find(strModel) != ShipDataList.end())
{
return ShipDataList[strModel];
}
else
{
return ShipDataList.begin()->second;//begin()还是end()?
}
}
/*************************
*函数名:GetABMissileDBRecordList()
*功 能:获取空空导弹参数数据集
*输 入:
*输 出:导弹参数map
***************************/
std::map<std::string, ABILITY*>& CDBLoader::GetShipDBRecordList()
{
if (ShipDataList.empty())
{
exit(0);
}
else
{
return ShipDataList;
}
}
/*************************
*函数名:GetAirDefenseDBRecord()
*功 能:获取防空系统参数数据集
*输 入:导弹型号
*输 出:导弹参数
***************************/
ABILITY* CDBLoader::GetAirDefenseDBRecord(std::string & strModel)
{
if (AirDefenseDataList.empty())
{
exit(0);
}
else if(AirDefenseDataList.find(strModel) != AirDefenseDataList.end())
{
return AirDefenseDataList[strModel];
}
else
{
return AirDefenseDataList.begin()->second;//begin()还是end()?
}
}
/*************************
*函数名:GetAirDefenseDBRecordList()
*功 能:获取防空系统参数数据集
*输 入:
*输 出:导弹参数map
***************************/
std::map<std::string, ABILITY*>& CDBLoader::GetAirDefenseDBRecordList()
{
if (AirDefenseDataList.empty())
{
exit(0);
}
else
{
return AirDefenseDataList;
}
}
/*************************
*函数名:GetGroundTargetDBRecord()
*功 能:获取地面目标参数数据集
*输 入:导弹型号
*输 出:导弹参数
***************************/
ABILITY* CDBLoader::GetGroundTargetDBRecord(std::string & strModel)
{
if (GroundTargetDataList.empty())
{
exit(0);
}
else if(GroundTargetDataList.find(strModel) != GroundTargetDataList.end())
{
return GroundTargetDataList[strModel];
}
else
{
return GroundTargetDataList.begin()->second;//begin()还是end()?
}
}
/*************************
*函数名:GetGroundTargetDBRecordList()
*功 能:获地面目标参数数据集
*输 入:
*输 出:导弹参数map
***************************/
std::map<std::string, ABILITY*>& CDBLoader::GetGroundTargetDBRecordList()
{
if (GroundTargetDataList.empty())
{
exit(0);
}
else
{
return GroundTargetDataList;
}
}
// 读取战机数据库
void CDBLoader::readFighterDB()
{
FighterDataList.clear();
//访问orcl数据库获取数据
CABDBUtil* pDBUtil = CABDBUtil::GetInstance();
_bstr_t SQL="";
SQL=SQL+"SELECT MODEL, St, theta, m1, m2, K, N, RCS, Lq, Lz, Nmax, Vp, Pj, Gj, Cost FROM FIGHTER";
if(!pDBUtil->Query((_bstr_t)SQL))
{
//执行失败,返回错误
std::cout << "错误!!!\n" << std::endl;
return;
}
_bstr_t strRecord;
while (!pDBUtil->m_pRecordset->adoEOF)
{
//开辟内存
ABILITY* fighterModel = new ABILITY;
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("MODEL");
fighterModel->model = (std::string)strRecord;
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("St");
fighterModel->St = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("theta");
fighterModel->theta = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("m1");
fighterModel->m1 = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("m2");
fighterModel->m2 = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("K");
fighterModel->K = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("N");
fighterModel->N = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("RCS");
fighterModel->RCS = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Lq");
fighterModel->Lq= atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Lz");
fighterModel->Lz= atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Nmax");
fighterModel->Nmax= atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Vp");
fighterModel->Vp= atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Pj");
fighterModel->Pj= atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Gj");
fighterModel->Gj= atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Cost");
fighterModel->Cost= atof(strRecord);
// 计算能力值
fighterModel->DetectAbility = compDetectValue(fighterModel->St,fighterModel->theta,fighterModel->m1);
fighterModel->AttackAbility = computeAttackValue(fighterModel->K,fighterModel->N);
fighterModel->SurAbility = computeSurValue(fighterModel->Lz,fighterModel->Lq,fighterModel->RCS,fighterModel->Nmax,fighterModel->Vp);
fighterModel->InterfereAbility = computeInterfereValue(fighterModel->Pj,fighterModel->Gj);
FighterDataList[fighterModel->model] = fighterModel;
pDBUtil->m_pRecordset->MoveNext();
}
}
// 读取战船数据库
void CDBLoader::readShipDB()
{
ShipDataList.clear();
//访问orcl数据库获取数据
CABDBUtil* pDBUtil = CABDBUtil::GetInstance();
_bstr_t SQL="";
SQL=SQL+"SELECT MODEL, St, theta, m1, m2, K, N, Pj, Gj, RCS, Lq, Lz, Nmax, Vp FROM SHIP";
if(!pDBUtil->Query((_bstr_t)SQL))
{
//执行失败,返回错误
std::cout << "错误!!!\n" << std::endl;
return;
}
_bstr_t strRecord;
while (!pDBUtil->m_pRecordset->adoEOF)
{
//开辟内存
ABILITY* shipModel = new ABILITY;
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("MODEL");
shipModel->model = (std::string)strRecord;
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("St");
shipModel->St = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("theta");
shipModel->theta = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("m1");
shipModel->m1 = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("m2");
shipModel->m2 = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("K");
shipModel->K = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("N");
shipModel->N = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Pj");
shipModel->Pj = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Gj");
shipModel->Gj= atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("RCS");
shipModel->RCS= atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Lq");
shipModel->Lq= atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Lz");
shipModel->Lz= atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Nmax");
shipModel->Nmax= atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Vp");
shipModel->Vp= atof(strRecord);
// 计算能力值
shipModel->DetectAbility = compDetectValue(shipModel->St,shipModel->theta,shipModel->m1);
shipModel->AttackAbility = computeAttackValue(shipModel->K,shipModel->N);
shipModel->SurAbility = computeSurValue(shipModel->Lz,shipModel->Lq,shipModel->RCS,shipModel->Nmax,shipModel->Vp);
shipModel->InterfereAbility = computeInterfereValue(shipModel->Pj,shipModel->Gj);
ShipDataList[shipModel->model] = shipModel;
pDBUtil->m_pRecordset->MoveNext();
}
}
// 读取防空系统数据库
void CDBLoader::readAirDefenseDB()
{
AirDefenseDataList.clear();
//访问orcl数据库获取数据
CABDBUtil* pDBUtil = CABDBUtil::GetInstance();
_bstr_t SQL="";
SQL=SQL+"SELECT MODEL, St, theta, m1, m2, K, N, RCS, Lq, Lz, Nmax, Vp, Pj, Gj FROM AIRDEFENSE";
if(!pDBUtil->Query((_bstr_t)SQL))
{
//执行失败,返回错误
std::cout << "错误!!!\n" << std::endl;
return;
}
_bstr_t strRecord;
while (!pDBUtil->m_pRecordset->adoEOF)
{
//开辟内存
ABILITY* airdefenseModel = new ABILITY;
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("MODEL");
airdefenseModel->model = (std::string)strRecord;
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("St");
airdefenseModel->St = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("theta");
airdefenseModel->theta = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("m1");
airdefenseModel->m1 = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("m2");
airdefenseModel->m2 = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("K");
airdefenseModel->K = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("N");
airdefenseModel->N = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("RCS");
airdefenseModel->RCS = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Lq");
airdefenseModel->Lq = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Lz");
airdefenseModel->Lz = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Nmax");
airdefenseModel->Nmax = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Vp");
airdefenseModel->Vp = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Pj");
airdefenseModel->Pj = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Gj");
airdefenseModel->Gj = atof(strRecord);
// 计算能力值
airdefenseModel->DetectAbility = compDetectValue(airdefenseModel->St,airdefenseModel->theta,airdefenseModel->m1);
airdefenseModel->AttackAbility = computeAttackValue(airdefenseModel->K,airdefenseModel->N);
airdefenseModel->SurAbility = computeSurValue(airdefenseModel->Lz,airdefenseModel->Lq,airdefenseModel->RCS,airdefenseModel->Nmax,airdefenseModel->Vp);
airdefenseModel->InterfereAbility = computeInterfereValue(airdefenseModel->Pj,airdefenseModel->Gj);
AirDefenseDataList[airdefenseModel->model] = airdefenseModel;
pDBUtil->m_pRecordset->MoveNext();
}
}
// 读取地面目标数据库
void CDBLoader::readGroundTargetDB()
{
GroundTargetDataList.clear();
//访问orcl数据库获取数据
CABDBUtil* pDBUtil = CABDBUtil::GetInstance();
_bstr_t SQL="";
SQL=SQL+"SELECT MODEL, St, theta, m1, m2, K, N, RCS, Lq, Lz, Nmax, Vp, Pj, Gj FROM GROUNDTARGET";
if(!pDBUtil->Query((_bstr_t)SQL))
{
//执行失败,返回错误
std::cout << "错误!!!\n" << std::endl;
return;
}
_bstr_t strRecord;
while (!pDBUtil->m_pRecordset->adoEOF)
{
//开辟内存
ABILITY* groundtargetModel = new ABILITY;
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("MODEL");
groundtargetModel->model = (std::string)strRecord;
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("St");
groundtargetModel->St = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("theta");
groundtargetModel->theta = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("m1");
groundtargetModel->m1 = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("m2");
groundtargetModel->m2 = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("K");
groundtargetModel->K = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("N");
groundtargetModel->N = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("RCS");
groundtargetModel->RCS = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Lq");
groundtargetModel->Lq = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Lz");
groundtargetModel->Lz = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Nmax");
groundtargetModel->Nmax = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Vp");
groundtargetModel->Vp = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Pj");
groundtargetModel->Pj = atof(strRecord);
strRecord = (LPCTSTR)(_bstr_t)pDBUtil->m_pRecordset->GetCollect("Gj");
groundtargetModel->Gj = atof(strRecord);
// 计算能力值
groundtargetModel->DetectAbility = compDetectValue(groundtargetModel->St,groundtargetModel->theta,groundtargetModel->m1);
groundtargetModel->AttackAbility = computeAttackValue(groundtargetModel->K,groundtargetModel->N);
groundtargetModel->SurAbility = computeSurValue(groundtargetModel->Lz,groundtargetModel->Lq,groundtargetModel->RCS,groundtargetModel->Nmax,groundtargetModel->Vp);
groundtargetModel->InterfereAbility = computeInterfereValue(groundtargetModel->Pj,groundtargetModel->Gj);
GroundTargetDataList[groundtargetModel->model] = groundtargetModel;
pDBUtil->m_pRecordset->MoveNext();
}
}
void CDBLoader::readALLDB()
{
readFighterDB();
readShipDB();
readAirDefenseDB();
readGroundTargetDB();
}
CDBLoader::~CDBLoader(void){}