1#ifndef CDA_RAIL_DATASTRUCTURE_TIMETABLE_HPP
2#define CDA_RAIL_DATASTRUCTURE_TIMETABLE_HPP
9#include "nlohmann/json_fwd.hpp"
37 double m_service_time;
38 double m_service_duration;
39 std::shared_ptr<Station> m_station;
47 static void check_ptr_validity(std::shared_ptr<Station const>
const& ptr) {
50 "Station pointer cannot be null");
73 std::shared_ptr<Station> station)
74 : m_service_time(serviceTime), m_service_duration(serviceDuration),
75 m_station(std::move(station)) {
79 check_ptr_validity(m_station);
101 return m_service_time + m_service_duration;
110 return m_service_duration;
133 m_service_time = new_service_time;
146 m_service_duration = new_service_duration;
158 check_ptr_validity(new_station);
159 m_station = std::move(new_station);
177 double m_entry_time{};
178 double m_exit_time{};
180 double m_initial_velocity{};
181 double m_exit_velocity{};
183 size_t m_entry_vertex{};
184 size_t m_exit_vertex{};
186 std::vector<ScheduledStop> m_stops{};
198 static void check_stops_validity(std::vector<ScheduledStop>
const& stops);
207 static std::pair<bool, std::optional<cda_rail::exceptions::CustomException>>
208 check_stops_validity_helper(std::vector<ScheduledStop>
const& stops);
217 Schedule() =
default;
240 Schedule(
double entry_time,
double initial_velocity,
size_t entry_vertex,
241 double exit_time,
double exit_velocity,
size_t exit_vertex,
242 std::vector<ScheduledStop> stops = {});
270 return m_initial_velocity;
300 [[nodiscard]] std::vector<ScheduledStop>
const&
get_stops()
const {
327 m_entry_time = newEntryTime;
341 m_exit_time = newExitTime;
354 m_initial_velocity = newInitialVelocity;
366 m_exit_velocity = newExitVelocity;
375 m_entry_vertex = newEntryVertex;
399 m_exit_vertex = newExitVertex;
425 check_stops_validity(new_stops);
426 m_stops = std::move(new_stops);
453 bool throw_exception_if_not_existent =
true);
479 std::vector<Schedule> m_schedules{};
491 void set_train_list(
const TrainList& tl) {
492 this->m_train_list = tl;
493 this->m_schedules = std::vector<Schedule>(
503 void parse_schedule_data(
const nlohmann::json& schedule_data,
size_t i);
512 void add_json_data(nlohmann::json& j,
size_t i,
const Network& network)
const;
517 void sort_stops_by_service_time() {
518 for (
auto& schedule : m_schedules) {
519 schedule.sort_stops_by_service_time();
523 [[nodiscard]] std::pair<bool,
524 std::optional<cda_rail::exceptions::CustomException>>
525 check_consistency_helper()
const;
541 const std::vector<Schedule>& schedules);
555 :
Timetable(std::filesystem::path(path), network) {};
563 :
Timetable(std::filesystem::path(path), network) {};
577 : m_station_list(other.m_station_list), m_train_list(other.m_train_list),
578 m_schedules(other.m_schedules) {
582 for (
auto& schedule : m_schedules) {
583 for (
auto& stop : schedule.m_stops) {
585 auto const& station_name = stop.m_station->name;
586 if (m_station_list.has_station(station_name)) {
587 stop.m_station = m_station_list.get_station_ptr(station_name);
600 if (
this != &other) {
601 m_station_list = other.m_station_list;
602 m_train_list = other.m_train_list;
603 m_schedules = other.m_schedules;
606 for (
auto& schedule : m_schedules) {
607 for (
auto& stop : schedule.m_stops) {
608 auto const& station_name = stop.m_station->name;
609 if (m_station_list.has_station(station_name)) {
610 stop.m_station = m_station_list.get_station_ptr(station_name);
628 [[nodiscard]] constexpr auto
begin()
const {
return m_schedules.cbegin(); };
633 [[nodiscard]]
constexpr auto end()
const {
return m_schedules.cend(); };
635 [[nodiscard]]
constexpr auto cbegin()
const {
return m_schedules.cbegin(); };
641 [[nodiscard]]
constexpr auto cend()
const {
return m_schedules.cend(); };
647 [[nodiscard]]
constexpr auto crbegin()
const {
648 return m_schedules.crbegin();
651 [[nodiscard]]
constexpr auto crend()
const {
return m_schedules.crend(); };
687 return {path, network};
709 return {path, network};
719 return m_station_list;
738 if (!m_train_list.has_train(trainIndex)) {
741 return m_schedules.at(trainIndex);
752 return get_schedule(m_train_list.get_train_index(train_name));
767 return m_train_list.editable_train(index);
776 return m_train_list.editable_train(name);
787 if (!m_train_list.has_train(trainIndex)) {
790 return m_schedules.at(trainIndex);
814 [[nodiscard]] std::vector<
815 std::pair<size_t, std::vector<cda_rail::index_vector>>>
819 return m_station_list.get_stop_tracks(
820 station_name, m_train_list.get_train(tr).get_length(), network,
831 m_station_list.add_empty_station(station_name);
842 void insert_stop(
size_t train_index, std::string
const& station_name,
843 double service_time,
double service_duration);
856 std::string
const& station_name,
double const serviceTime,
857 double const serviceDuration) {
858 insert_stop(m_train_list.get_train_index(train_name), station_name,
859 serviceTime, serviceDuration);
870 void remove_stop(
size_t train_index, std::string
const& station_name,
871 bool throw_exception_if_not_existent =
true);
885 std::string
const& station_name,
886 bool throw_exception_if_not_existent =
true) {
887 remove_stop(m_train_list.get_train_index(train_name), station_name,
888 throw_exception_if_not_existent);
901 m_station_list.add_track_to_station(station_name, new_edge, network);
925 [[nodiscard]]
size_t add_train_private_helper(
926 std::string
const& train_name,
double length,
double max_speed,
927 double acceleration,
double deceleration,
bool tim,
double entry_time,
928 double initial_velocity,
size_t entry_vertex,
double exit_time,
929 double exit_velocity,
size_t exit_vertex);
949 [[nodiscard]]
size_t add_train(std::string
const& train_name,
double length,
950 double max_speed,
double acceleration,
951 double deceleration,
bool tim,
952 double entry_time,
double initial_velocity,
954 double exit_time,
double exit_velocity,
957 return add_train_private_helper(
958 train_name, length, max_speed, acceleration, deceleration, tim,
959 entry_time, initial_velocity, entry_vertex.resolve(&network), exit_time,
960 exit_velocity, exit_vertex.resolve(&network));
980 add_train(std::string
const& train_name,
double length,
double max_speed,
981 double acceleration,
double deceleration,
double entry_time,
983 double exit_time,
double exit_velocity,
985 return add_train(train_name, length, max_speed, acceleration, deceleration,
986 true, entry_time, initial_velocity, entry_vertex,
987 exit_time, exit_velocity, exit_vertex, network);
1016 std::vector<std::pair<size_t, cda_rail::index_set>>
const& new_edges) {
1017 m_station_list.update_after_discretization(new_edges);
1026 std::vector<std::pair<size_t, cda_rail::index_vector>>
const& new_edges) {
1027 m_station_list.update_after_discretization(new_edges);
1038 [[nodiscard]] std::pair<size_t, size_t>
1040 bool tn_inclusive =
true)
const;
1052 [[nodiscard]] std::pair<size_t, size_t>
1054 bool const tnInclusive =
true)
const {
Directed multigraph representing a railway network.
Definition RailwayNetwork.hpp:163
bool has_vertex(size_t index) const
Checks whether a numeric vertex index is valid.
Definition RailwayNetwork.hpp:417
Specific schedule object with fixed entry/exit constraints.
Definition Timetable.hpp:175
void insert_stop(ScheduledStop new_stop)
Inserts one stop while preserving stop-list invariants.
Definition Timetable.cpp:86
void remove_stop(std::string const &station_name, bool throw_exception_if_not_existent=true)
Removes a stop by station name.
Definition Timetable.cpp:103
double get_exit_velocity() const
Obtains the exit velocity.
Definition Timetable.hpp:278
double get_entry_time() const
Provides the entry time.
Definition Timetable.hpp:255
double get_exit_time() const
Gets the scheduled exit time.
Definition Timetable.hpp:262
void set_stops(std::vector< ScheduledStop > new_stops)
Replaces the stop list for this schedule.
Definition Timetable.hpp:424
void set_exit_time(double const newExitTime)
Sets the exit time.
Definition Timetable.hpp:338
void set_initial_velocity(double const newInitialVelocity)
Updates the initial velocity.
Definition Timetable.hpp:351
size_t get_exit_vertex() const
Gets the exit vertex of the schedule.
Definition Timetable.hpp:292
void set_entry_vertex(size_t const newEntryVertex)
Sets the entry vertex.
Definition Timetable.hpp:374
std::vector< ScheduledStop > const & get_stops() const
Retrieves the scheduled stops for this train.
Definition Timetable.hpp:300
friend class Timetable
Definition Timetable.hpp:218
void set_exit_vertex(size_t const newExitVertex)
Sets the exit vertex id.
Definition Timetable.hpp:398
void set_exit_vertex(size_t const newExitVertex, Network const &network)
Sets the exit vertex id after validating it against a network.
Definition Timetable.hpp:410
size_t get_station_index(std::string const &station_name) const
Returns the index of a scheduled stop by station name.
Definition Timetable.cpp:43
void set_entry_vertex(size_t const newEntryVertex, Network const &network)
Sets the entry vertex id after validating it against a network.
Definition Timetable.hpp:386
size_t get_entry_vertex() const
Determines the network vertex where the train enters.
Definition Timetable.hpp:285
void set_exit_velocity(double const newExitVelocity)
Sets the exit velocity.
Definition Timetable.hpp:364
double get_initial_velocity() const
Obtains the initial velocity.
Definition Timetable.hpp:269
void set_entry_time(double const newEntryTime)
Sets the entry time.
Definition Timetable.hpp:325
void sort_stops_by_service_time()
Sorts the scheduled stops by service time.
Definition Timetable.hpp:462
Scheduling information for a stop within a timetable.
Definition Timetable.hpp:32
void set_service_time(double new_service_time)
Sets the earliest service start time.
Definition Timetable.hpp:131
ScheduledStop(double const serviceTime, double const serviceDuration, std::shared_ptr< Station > station)
Constructs a scheduled stop for a concrete station.
Definition Timetable.hpp:72
Station const & get_station() const
Accesses the station associated with this stop.
Definition Timetable.hpp:118
double get_earliest_departure() const
Computes the earliest departure time from this stop.
Definition Timetable.hpp:100
double get_service_duration() const
Accesses the minimum service duration.
Definition Timetable.hpp:109
double get_service_time() const
Retrieves the earliest service start time.
Definition Timetable.hpp:93
friend class Timetable
Definition Timetable.hpp:33
void set_service_duration(double new_service_duration)
Sets the minimum service duration.
Definition Timetable.hpp:143
void set_station(std::shared_ptr< Station > new_station)
Sets the station for this scheduled stop.
Definition Timetable.hpp:157
Container and persistence API for named stations.
Definition Station.hpp:81
Container for trains with name-based indexing.
Definition Train.hpp:200
size_t size() const
Retrieves the number of trains in the list.
Definition Train.hpp:283
Properties and characteristics of a train.
Definition Train.hpp:26
Thrown when a referenced train does not exist.
Definition CustomExceptions.hpp:207
Thrown when a referenced vertex does not exist.
Definition CustomExceptions.hpp:133
void throw_if_less_than(double const value, double const threshold, std::string_view const name)
Throws if a value is less than a threshold.
Definition CustomExceptions.hpp:323
void throw_if_negative(double const value, std::string_view const name)
Validates that a value is non-negative.
Definition CustomExceptions.hpp:349
Definition CustomExceptions.hpp:15
std::unordered_set< size_t > index_set
Definition Definitions.hpp:22
Represents a station as a named set of network tracks (edge indices).
Definition Station.hpp:24