新建了策略控制基类
实现了三种策略的策略控制类
This commit is contained in:
parent
ce7986b5a6
commit
ad37da5974
19
include/BusFCFSStrategy.h
Normal file
19
include/BusFCFSStrategy.h
Normal file
|
@ -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
|
21
include/BusSCANStrategy.h
Normal file
21
include/BusSCANStrategy.h
Normal file
|
@ -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
|
19
include/BusSSTFStrategy.h
Normal file
19
include/BusSSTFStrategy.h
Normal file
|
@ -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
|
40
include/BusStrategyBase.h
Normal file
40
include/BusStrategyBase.h
Normal file
|
@ -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
|
75
src/BusFCFSStrategy.cpp
Normal file
75
src/BusFCFSStrategy.cpp
Normal file
|
@ -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;
|
||||
}
|
138
src/BusSCANStrategy.cpp
Normal file
138
src/BusSCANStrategy.cpp
Normal file
|
@ -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;
|
||||
}
|
100
src/BusSSTFStrategy.cpp
Normal file
100
src/BusSSTFStrategy.cpp
Normal file
|
@ -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;
|
||||
}
|
80
src/BusStrategyBase.cpp
Normal file
80
src/BusStrategyBase.cpp
Normal file
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user