Commit eb3c22e0 authored by Kučera Petr RNDr. Ph.D.'s avatar Kučera Petr RNDr. Ph.D.
Browse files

Train route graph construction

parent 25e59bb3
......@@ -24,6 +24,7 @@
#include "section_parser.h"
#include "train_list_parser.h"
#include "train_parser.h"
#include "train_route.h"
#include "trapts_parser.h"
template <class Parser, class Printer>
......@@ -111,13 +112,15 @@ main([[maybe_unused]] int argc, char *argv[])
std::ofstream f(cfg.debug.logical_graph);
lg.PrintGraph(f);
}
Loco::SectionGraph sg(input_data, lg);
Loco::SectionGraphCPtr sg =
std::make_shared<const Loco::SectionGraph>(input_data, lg);
{
std::ofstream f(cfg.debug.section_graph);
sg.PrintGraph(f);
sg->PrintGraph(f);
}
{
std::ofstream f(cfg.debug.section_graph_info);
sg.PrintSectionInfo(f);
sg->PrintSectionInfo(f);
}
Loco::TrainRoutes routes(input_data, sg);
}
/*
* Train deadlock detection
* Project Masinky
*
* Petr Kucera, 2021
*/
/**@file train_route.cpp
* Train route graph representation.
*/
#include "train_route.h"
Loco::TrainRoutes::TrainRoutes(const InputData &input_data,
const SectionGraphCPtr &section_graph)
{
BuildRoutes(input_data, section_graph);
}
void
Loco::TrainRoutes::BuildRoutes(const InputData &input_data,
const SectionGraphCPtr &section_graph)
{
for (const auto &[name, ptr] : input_data.mTrains)
{
mRouteGraphs.insert(
{ptr, TrainRouteGraph(ptr, mRouteGraphs.size(), section_graph)});
}
}
Loco::TrainRouteGraph::TrainRouteGraph(TrainCPtr train, unsigned train_id,
SectionGraphCPtr section_graph)
: mTrain(train), mEnterPlace(true, train_id, train),
mLeavePlace(false, train_id, std::move(train)),
mSectionGraph(std::move(section_graph))
{
BuildElements();
}
void
Loco::TrainRouteGraph::BuildElements()
{
unsigned i = 0;
while (i < mTrain->StopsCount() && mTrain->StopAt(i).EmptyTrafficPoint())
{
++i;
}
while (i + 1 < mTrain->StopsCount()
&& !mTrain->StopAt(i).EmptyTrafficPoint())
{
AddElement(i);
++i;
}
}
void
Loco::TrainRouteGraph::AddElement(unsigned start)
{
mElements.emplace_back(start, mSectionGraph->Subgraph(
mTrain->StopAt(start).TrafficPoint(),
mTrain->StopAt(start + 1).TrafficPoint()));
}
/*
* Train deadlock detection
* Project Masinky
*
* Petr Kucera, 2021
*/
/**@file train_route.h
* Train route graph representation.
*/
#ifndef __TRAIN_ROUTE_H
#define __TRAIN_ROUTE_H
#include "section_graph.h"
#include "trains_desc.h"
namespace Loco
{
// Composed of relevant subgraphs
class TrainRouteGraph
{
public:
struct Element
{
// Index within the list of train stops of the train
// We assume that the next stop is at index mStartStop+1.
unsigned mStartStop;
SGSubgraphCPtr mSubgraph;
Element(unsigned start_stop, SGSubgraphCPtr subgraph)
: mStartStop(start_stop), mSubgraph(std::move(subgraph))
{
}
};
private:
TrainCPtr mTrain;
TrainPlace mEnterPlace;
TrainPlace mLeavePlace;
SectionGraphCPtr mSectionGraph;
std::vector<Element> mElements;
void AddElement(unsigned start);
void BuildElements();
public:
TrainRouteGraph(TrainCPtr train, unsigned train_id,
SectionGraphCPtr section_graph);
};
// Manages train route graphs for all trains
// Also assigns an id to each train which can be used in train related
// places.
class TrainRoutes
{
std::unordered_map<TrainCPtr, TrainRouteGraph> mRouteGraphs;
void BuildRoutes(const InputData &input_data,
const SectionGraphCPtr &section_graph);
public:
TrainRoutes(const InputData &input_data,
const SectionGraphCPtr &section_graph);
};
} // namespace Loco
#endif
......@@ -45,6 +45,12 @@ namespace Loco
{
return mSections;
}
bool
Empty() const
{
return mSections.empty();
}
};
using TrafficPointCPtr = std::shared_ptr<const TrafficPoint>;
......@@ -92,6 +98,18 @@ namespace Loco
}
std::ostream &Print(std::ostream &os, const std::string &prefix) const;
const TrafficPointCPtr &
TrafficPoint() const
{
return mTrafficPoint;
}
bool
EmptyTrafficPoint() const
{
return mTrafficPoint->Empty();
}
};
enum class TrainType
......@@ -138,6 +156,18 @@ namespace Loco
{
return mIdent;
}
size_t
StopsCount() const
{
return mStops.size();
}
const TrainStop &
StopAt(size_t i) const
{
return mStops[i];
}
};
using TrainCPtr = std::shared_ptr<const Train>;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment