From ad37da5974263e85260b1d28cf802720c2823062 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 27 Jun 2022 13:59:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E4=BA=86=E7=AD=96=E7=95=A5?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=9F=BA=E7=B1=BB=20=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E4=BA=86=E4=B8=89=E7=A7=8D=E7=AD=96=E7=95=A5=E7=9A=84=E7=AD=96?= =?UTF-8?q?=E7=95=A5=E6=8E=A7=E5=88=B6=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/BusFCFSStrategy.h | 19 ++++++ include/BusSCANStrategy.h | 21 ++++++ include/BusSSTFStrategy.h | 19 ++++++ include/BusStrategyBase.h | 40 +++++++++++ src/BusFCFSStrategy.cpp | 75 +++++++++++++++++++++ src/BusSCANStrategy.cpp | 138 ++++++++++++++++++++++++++++++++++++++ src/BusSSTFStrategy.cpp | 100 +++++++++++++++++++++++++++ src/BusStrategyBase.cpp | 80 ++++++++++++++++++++++ 8 files changed, 492 insertions(+) create mode 100644 include/BusFCFSStrategy.h create mode 100644 include/BusSCANStrategy.h create mode 100644 include/BusSSTFStrategy.h create mode 100644 include/BusStrategyBase.h create mode 100644 src/BusFCFSStrategy.cpp create mode 100644 src/BusSCANStrategy.cpp create mode 100644 src/BusSSTFStrategy.cpp create mode 100644 src/BusStrategyBase.cpp diff --git a/include/BusFCFSStrategy.h b/include/BusFCFSStrategy.h new file mode 100644 index 0000000..080a284 --- /dev/null +++ b/include/BusFCFSStrategy.h @@ -0,0 +1,19 @@ +// +// Created by ricardo on 2022/6/27. +// + +#ifndef AUTO_BUS_GUI_BUS_FCFS_STRATEGY_H +#define AUTO_BUS_GUI_BUS_FCFS_STRATEGY_H +#include "BusStrategyBase.h" + +class BusFCFSStrategy : public BusStrategyBase +{ + int GetBusDirection(bus_query_t *query); + + bus_query_t *GetTargetQuery(); + + bus_query_t *HandleQuery(); +}; + + +#endif //AUTO_BUS_GUI_BUS_FCFS_STRATEGY_H diff --git a/include/BusSCANStrategy.h b/include/BusSCANStrategy.h new file mode 100644 index 0000000..6264724 --- /dev/null +++ b/include/BusSCANStrategy.h @@ -0,0 +1,21 @@ +// +// Created by ricardo on 2022/6/27. +// + +#ifndef AUTO_BUS_GUI_BUS_SCAN_STRATEGY_H +#define AUTO_BUS_GUI_BUS_SCAN_STRATEGY_H +#include "BusStrategyBase.h" + + +class BusSCANStrategy : public BusStrategyBase +{ + int GetBusDirection(bus_query_t *query); + + bus_query_t *GetTargetQuery(); + + bus_query_t *HandleQuery(); + +}; + + +#endif //AUTO_BUS_GUI_BUS_SCAN_STRATEGY_H diff --git a/include/BusSSTFStrategy.h b/include/BusSSTFStrategy.h new file mode 100644 index 0000000..ed20658 --- /dev/null +++ b/include/BusSSTFStrategy.h @@ -0,0 +1,19 @@ +// +// Created by ricardo on 2022/6/27. +// + +#ifndef AUTO_BUS_GUI_BUS_SSTF_STRATEGY_H +#define AUTO_BUS_GUI_BUS_SSTF_STRATEGY_H +#include "BusStrategyBase.h" + +class BusSSTFStrategy : public BusStrategyBase +{ + int GetBusDirection(bus_query_t *query); + + bus_query_t *GetTargetQuery(); + + bus_query_t *HandleQuery(); +}; + + +#endif //AUTO_BUS_GUI_BUS_SSTF_STRATEGY_H diff --git a/include/BusStrategyBase.h b/include/BusStrategyBase.h new file mode 100644 index 0000000..6c2b628 --- /dev/null +++ b/include/BusStrategyBase.h @@ -0,0 +1,40 @@ +// +// Created by ricardo on 2022/6/27. +// + +#ifndef AUTO_BUS_GUI_BUS_CONTROLLER_BASE_H +#define AUTO_BUS_GUI_BUS_CONTROLLER_BASE_H +#include "QObject" +#include "QString" + +#include "railsModel.h" +#include "queryModel.h" +#include "busModel.h" + +class BusStrategyBase : public QObject +{ + Q_OBJECT +public: + RailsModel *rails_model; + QueryModel *query_model; + BusModel *bus_model; + + int bus_tick; + + BusStrategyBase(); + + virtual ~BusStrategyBase(); + + virtual int GetBusDirection(bus_query_t *query) = 0; + + virtual bus_query_t *GetTargetQuery() = 0; + + virtual bus_query_t *HandleQuery() = 0; + +private: + QString PrintState() const; + +}; + + +#endif //AUTO_BUS_GUI_BUS_CONTROLLER_BASE_H diff --git a/src/BusFCFSStrategy.cpp b/src/BusFCFSStrategy.cpp new file mode 100644 index 0000000..d54d1e4 --- /dev/null +++ b/src/BusFCFSStrategy.cpp @@ -0,0 +1,75 @@ +// +// Created by ricardo on 2022/6/27. +// + +#include "BusFCFSStrategy.h" + +int BusFCFSStrategy::GetBusDirection(bus_query_t *query) +{ + bus_query_t *p = query; + + if(p == NULL) + { + return BUS_STOP; + } //如果没有请求,公交车停止 + + else + { + int clockwise = 0; + int counterclockwise = 0; //用于顺,逆时针方向所经站台计数 + + /** + * 公交车当前所在位置 + */ + rail_node_t *now_position = bus_model->rail_pos; + /** + * 公交车应该前进的方向 + */ + rail_node_t *target_position = p->node; + + rail_node_t *pos = now_position; + while (pos != target_position) //顺时针计数 + { + clockwise++; + pos = pos->next_node; + } + + pos = now_position; + while (pos != target_position) //逆时针计数 + { + counterclockwise++; + pos = pos->last_node; + } + + if(clockwise <= counterclockwise) + { + return BUS_CLOCK_WISE; + }//若顺时针距离短(或顺逆相等),公交车顺时针运行 + else + { + return BUS_COUNTER_CLOCK_WISE; + }//若逆时针距离短,公交车逆时针运行 + } + +} + +bus_query_t *BusFCFSStrategy::GetTargetQuery() +{ + return query_model->queries; +} + +bus_query_t *BusFCFSStrategy::HandleQuery() +{ + bus_query_t *result = NULL; + bus_query_t *query = query_model->queries; + + if(query != nullptr) + { + if(bus_model->rail_pos == query->node) + { + result = query; + } + } + + return result; +} diff --git a/src/BusSCANStrategy.cpp b/src/BusSCANStrategy.cpp new file mode 100644 index 0000000..37ade41 --- /dev/null +++ b/src/BusSCANStrategy.cpp @@ -0,0 +1,138 @@ +// +// Created by ricardo on 2022/6/27. +// + +#include "BusSCANStrategy.h" + +int BusSCANStrategy::GetBusDirection(bus_query_t *query) +{ + int orientation = bus_model->direction; + + if(query == NULL) + { + return BUS_STOP; + } + + if(orientation == BUS_STOP) + { + int distance = bus_model->GetQueryDistance(query, BUS_CLOCK_WISE); + if(distance > rails_model->total_distance / 2) + { + return BUS_COUNTER_CLOCK_WISE; + } + else + { + return BUS_CLOCK_WISE; + } + } + else + { + int distance = bus_model->GetQueryDistance(query, orientation); + if(distance > rails_model->total_distance / 2) + { + if(orientation == BUS_CLOCK_WISE) + { + return BUS_COUNTER_CLOCK_WISE; + } + else + { + return BUS_CLOCK_WISE; + } + } + else + { + return orientation; + } + } +} + +bus_query_t *BusSCANStrategy::GetTargetQuery() +{ + bus_query_t *queries = query_model->queries; + int direction = bus_model->direction; + + // 当前没有请求 + if(queries == NULL) + { + return NULL; + } + + if(direction == BUS_STOP) + { + // 在停止的状态下第一次开始选择方向 + int distance = 9999; + bus_query_t *query = NULL; + bus_query_t *p = queries; + + // 遍历顺时针方向 + // 在两个方向路程相同时选择顺时针方向 + // 所以先遍历顺时针方向 + while (p != NULL) + { + int temp = bus_model->GetQueryDistance(p, BUS_CLOCK_WISE); + if(temp < distance) + { + distance = temp; + query = p; + } + p = p->next_node; + } + + // 遍历逆时针方向 + p = queries; + while (p != NULL) + { + int temp = bus_model->GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE); + if(temp < distance) + { + distance = temp; + query = p; + } + p = p->next_node; + } + + return query; + } + else + { + // 在已经有方向的情况下处理方向 + int distance = 9999; + bus_query_t *query = NULL; + bus_query_t *p = queries; + + while (p != NULL) + { + int temp = bus_model->GetQueryDistance(p, direction); + if(temp < distance) + { + query = p; + distance = temp; + } + p = p->next_node; + } + + return query; + } +} + +bus_query_t *BusSCANStrategy::HandleQuery() +{ + //获取公交车当前所在站点 + rail_node_t *now_position = bus_model->rail_pos; + + bus_query_t *p = query_model->queries; + + while(p != NULL) + { + if(p->node == now_position) + { + if(p->time < bus_tick - 1) + { + return p; + } + } + p = p->next_node; + }//遍历请求链表,判断是否有可以顺便处理的请求 + + return NULL; +} diff --git a/src/BusSSTFStrategy.cpp b/src/BusSSTFStrategy.cpp new file mode 100644 index 0000000..bc12380 --- /dev/null +++ b/src/BusSSTFStrategy.cpp @@ -0,0 +1,100 @@ +// +// Created by ricardo on 2022/6/27. +// + +#include "BusSSTFStrategy.h" + +int BusSSTFStrategy::GetBusDirection(bus_query_t *query) +{ + if (query == NULL) + { + return BUS_STOP; + } + + int distance = bus_model->GetQueryDistance(query, BUS_CLOCK_WISE); + if(distance > rails_model->total_distance / 2) + { + return BUS_COUNTER_CLOCK_WISE; + } + else if(distance == 0) + { + return BUS_STOP; + } + else + { + return BUS_CLOCK_WISE; + } +} + +bus_query_t *BusSSTFStrategy::HandleQuery() +{ + // 这里只是处理顺便服务的代码 + + bus_query_t *query = query_model->queries; + bus_query_t *allow_query = NULL; + rail_node_t *now_node = bus_model->rail_pos; + + while (query != NULL) + { + if(query->node == now_node) + { + // 这里是设计上的缺陷,在bus_time显示时间的前一秒,公交车就实际上到达站台了 + if(query->time < bus_tick - 1) + { + if(query->type == bus_model->direction || query->type == BUS_TARGET) + { + allow_query = query; + break; + } + } + } + query = query->next_node; + } + + return allow_query; +} + +bus_query_t *BusSSTFStrategy::GetTargetQuery() +{ + // 获得请求头节点 + bus_query_t *queries = query_model->queries; + + // 当前没有请求 + if(queries == NULL) + { + return NULL; + } + + int distance = 9999; + bus_query_t *query = NULL; + bus_query_t *p = queries; + + // 遍历顺时针方向 + // 在两个方向路程相同时选择顺时针方向 + // 所以先遍历顺时针方向 + while (p != NULL) + { + int temp = bus_model->GetQueryDistance(p, BUS_CLOCK_WISE); + if(temp < distance) + { + distance = temp; + query = p; + } + p = p->next_node; + } + + // 遍历逆时针方向 + p = queries; + while (p != NULL) + { + int temp = bus_model->GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE); + if(temp < distance) + { + distance = temp; + query = p; + } + p = p->next_node; + } + + return query; +} diff --git a/src/BusStrategyBase.cpp b/src/BusStrategyBase.cpp new file mode 100644 index 0000000..2779698 --- /dev/null +++ b/src/BusStrategyBase.cpp @@ -0,0 +1,80 @@ +// +// Created by ricardo on 2022/6/27. +// + +#include "moc_BusStrategyBase.cpp" + +BusStrategyBase::BusStrategyBase() +{ + rails_model = new RailsModel; + query_model = new QueryModel; + bus_model = new BusModel(); + + bus_tick = 0; +} + +BusStrategyBase::~BusStrategyBase() +{ + delete rails_model; + delete query_model; + delete bus_model; +} + +QString BusStrategyBase::PrintState() const +{ + int count = 0; + rail_node_t *node = rails_model->rails; + char target[25], clockwise[25], counterclockwise[25]; + + if(node == nullptr) + { + return QString("No rails"); + } + + do + { + target[count] = '0'; + clockwise[count] = '0'; + counterclockwise[count] = '0'; + node = node->next_node; + count++; + } while (node != rails_model->rails); + + // 字符串末尾填0 + target[count] = '\0'; + clockwise[count] = '\0'; + counterclockwise[count] = '\0'; + + bus_query_t *query = query_model->queries; + int pos; + + while (query != nullptr) + { + pos = query->node->id - 1; + + if(query->type == BUS_CLOCK_WISE) + { + clockwise[pos] = '1'; + } + else if (query->type == BUS_COUNTER_CLOCK_WISE) + { + counterclockwise[pos] = '1'; + } + else + { + target[pos] = '1'; + } + + query = query->next_node; + } + + QString str = QString::asprintf("Time:%d\n", bus_tick); + str += "BUS:\n"; + str += QString::asprintf("position:%lf\n", bus_model->GetBusPosition()); + str = str + "target:" + target + "\n"; + str += "STATION:\n"; + str = str + "clockwise" + clockwise + "\n"; + str = str + "counterclockwise" + counterclockwise + "\n"; + + return str; +} \ No newline at end of file