From c9b7908e4ce9c12a530fbf7a9c0a49a3e6d53a04 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 26 Jun 2022 18:10:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=BE=97=E6=96=B9=E5=90=91=E4=B8=8E?= =?UTF-8?q?=E5=A4=84=E7=90=86=E8=AF=B7=E6=B1=82=E4=B8=A4=E4=B8=AA=E6=A7=BD?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/busModel.h | 39 +++++++++++++-- src/busModel.cpp | 119 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 146 insertions(+), 12 deletions(-) diff --git a/include/busModel.h b/include/busModel.h index b4db775..93e9c50 100644 --- a/include/busModel.h +++ b/include/busModel.h @@ -68,7 +68,16 @@ public: */ ~BusControllerModel(); + /** + * 打印当前状态 + * @return 状态的字符串 + */ std::string PrintState(); + + /** + * 判断公交车是否到站 + * @return + */ bool JudgeOnStation(); public slots: @@ -85,6 +94,22 @@ public slots: */ void AddQuerySlot(int query_type, int node_id) const; + /** + * 删除上下车请求的槽函数 + * @param query + */ + void DeleteQuerySlot(bus_query_t *query) const; + + /** + * 获得公交车应该前进方向的槽函数 + */ + void GetBusDirectionSlot(); + + /** + * 处理公交车可以处理请求的槽函数 + */ + void HandleQuerySlot(); + signals: /** * 创建轨道链表完成的信号 @@ -93,9 +118,10 @@ signals: void RailsCreated(int node_num); /** - * 请求发生修改的槽函数 + * 删除指定请求的信号 + * @param query 需要删除的请求 */ - void QueryChangedSignal(); + void DeleteQuerySignal(bus_query_t *query); private: @@ -104,6 +130,10 @@ private: */ int total_distance = 10; + bus_query_t *target_query; + + void SetConnection() const; + /** * 获得公交车当前所在的位置 * @return 公交车当前所在的位置 @@ -145,10 +175,9 @@ private: /** * 根据指定的请求获得前进的方向,也就是前往指定的请求最近的方向 * 在SSTF策略中使用 - * @param query 指定完成的请求 * @return 前进的方向 */ - int SSTFDirection(bus_query_t* query); + int SSTFDirection(); /** * 在当前站上可以顺便服务的请求 @@ -168,7 +197,7 @@ private: * @param query 指定完成的请求 * @return 前进的方向 */ - int SCANDirection(bus_query_t *query); + int SCANDirection(); /** * 在当前站上可以顺便服务的请求 diff --git a/src/busModel.cpp b/src/busModel.cpp index fd14875..afe77c1 100644 --- a/src/busModel.cpp +++ b/src/busModel.cpp @@ -13,9 +13,12 @@ BusControllerModel::BusControllerModel() rail_pos = rail_manager->rails; distance = 0; direction = BUS_STOP; + target_query = nullptr; // 设置初始时间 bus_time = 0; + + SetConnection(); } BusControllerModel::~BusControllerModel() @@ -24,6 +27,12 @@ BusControllerModel::~BusControllerModel() delete(query_manager); } +void BusControllerModel::SetConnection() const +{ + QObject::connect(this, &BusControllerModel::DeleteQuerySignal, + this, &BusControllerModel::DeleteQuerySlot); +} + void BusControllerModel::ReadConfigFileSlot(const QString& file_name) { ReadConfigFile(file_name.toStdString()); @@ -35,6 +44,102 @@ void BusControllerModel::AddQuerySlot(int query_type, int node_id) const query_manager->CreateQuery(query_type, node_pos); } +void BusControllerModel::DeleteQuerySlot(bus_query_t *query) const +{ + query_manager->DeleteQuery(query); +} + +void BusControllerModel::GetBusDirectionSlot() +{ + switch (chosen_strategy) + { + case BUS_FCFS: + direction = FCFSDirection(); + break; + case BUS_SSTF: + if(target_query == nullptr) + { + target_query = SSTFGetQuery(); + } + direction = SSTFDirection(); + case BUS_SCAN: + if(target_query == nullptr) + { + target_query = SCANGetQuery(); + } + direction = SCANDirection(); + default: + break; + } +} + + +void BusControllerModel::HandleQuerySlot() +{ + bus_query_t *finished_query; + + switch (chosen_strategy) + { + case BUS_FCFS: + finished_query = FCFSQuery(); + while (finished_query != nullptr) + { + emit DeleteQuerySignal(finished_query); + finished_query = FCFSQuery(); + } + break; + case BUS_SSTF: + if(target_query == nullptr) + { + // 这里可能需要处理一下新生成的请求可以处理的情况 + } + else if(target_query->node == rail_pos) + { + // 到达目标站点 + while (target_query != nullptr and target_query->node == rail_pos) + { + emit DeleteQuerySignal(target_query); + target_query = SSTFGetQuery(); + } + } + else + { + // 顺便处理请求 + finished_query = SSTFBTWQuery(); + while (finished_query != nullptr) + { + emit DeleteQuerySignal(finished_query); + finished_query = SSTFBTWQuery(); + } + } + break; + case BUS_SCAN: + if(target_query == nullptr) + { + // 这里可能需要处理一下新生成的请求可以处理的情况 + } + else if(target_query->node == rail_pos) + { + // 到达目标站点 + while (target_query != nullptr and target_query->node == rail_pos) + { + emit DeleteQuerySignal(target_query); + target_query = SCANGetQuery(); + } + } + else + { + // 顺便处理 + finished_query = SCANBTWQuery(); + while (finished_query != nullptr) + { + emit DeleteQuerySignal(finished_query); + finished_query = SCANBTWQuery(); + } + } + } +} + /* * 以下函数的实现移植自auto_pilot_bus * 源程序采用C写成 @@ -416,14 +521,14 @@ bus_query_t *BusControllerModel::SSTFGetQuery() return query; } -int BusControllerModel::SSTFDirection(bus_query_t *query) +int BusControllerModel::SSTFDirection() { - if (query == nullptr) + if (target_query == nullptr) { return BUS_STOP; } - int length = GetQueryDistance(query, BUS_CLOCK_WISE); + int length = GetQueryDistance(target_query, BUS_CLOCK_WISE); if(length > total_distance / 2) { return BUS_COUNTER_CLOCK_WISE; @@ -530,16 +635,16 @@ bus_query_t *BusControllerModel::SCANGetQuery() } } -int BusControllerModel::SCANDirection(bus_query_t *query) +int BusControllerModel::SCANDirection() { - if(query == nullptr) + if(target_query == nullptr) { return BUS_STOP; } if(direction == BUS_STOP) { - int length = GetQueryDistance(query, BUS_CLOCK_WISE); + int length = GetQueryDistance(target_query, BUS_CLOCK_WISE); if(length > total_distance / 2) { return BUS_COUNTER_CLOCK_WISE; @@ -551,7 +656,7 @@ int BusControllerModel::SCANDirection(bus_query_t *query) } else { - int length = GetQueryDistance(query, direction); + int length = GetQueryDistance(target_query, direction); if(length > total_distance / 2) { if(direction == BUS_CLOCK_WISE)