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

Activity connections

parent 9ad1474a
......@@ -11,7 +11,7 @@ default-directory = data
trains = trains.ini
[output]
# directory = data
directory = data/out
indications = indications.ini
diagnostic = deadlock.log
model-data = model.dat
......
......@@ -9,6 +9,7 @@
*/
#include "train_route.h"
#include "util.h"
Loco::TrainRoutes::TrainRoutes(const InputData &input_data,
const SectionGraphCPtr &section_graph)
......@@ -35,7 +36,12 @@ Loco::TrainRouteGraph::TrainRouteGraph(TrainCPtr train, unsigned train_id,
mEnterPlace = std::make_shared<const TrainPlace>(true, mTrainId, mTrain);
mLeavePlace = std::make_shared<const TrainPlace>(false, mTrainId, mTrain);
BuildElements();
if (mElements.empty())
{
return;
}
InitActList();
ConnectActivities();
}
void
......@@ -47,7 +53,7 @@ Loco::TrainRouteGraph::BuildElements()
++i;
}
while (i + 1 < mTrain->StopsCount()
&& !mTrain->StopAt(i).EmptyTrafficPoint())
&& !mTrain->StopAt(i + 1).EmptyTrafficPoint())
{
AddElement(i);
++i;
......@@ -71,7 +77,7 @@ Loco::TrainRouteGraph::InitActList()
AddEnterActivity();
for (unsigned i = 0; i < mElements.size(); ++i)
{
AddElementActivities(i, i + 1 == mElements.size());
AddElementActivities(i);
}
AddLeaveActivity();
}
......@@ -79,27 +85,31 @@ Loco::TrainRouteGraph::InitActList()
void
Loco::TrainRouteGraph::AddEnterActivity()
{
mActList.emplace_back(mEnterPlace->Ident(), mEnterPlace, false);
mActList.emplace_back("EnterT" + std::to_string(mTrainId), mEnterPlace,
false);
}
void
Loco::TrainRouteGraph::AddLeaveActivity()
{
mActList.emplace_back(mLeavePlace->Ident(), mLeavePlace, true);
mActList.emplace_back("LeaveT" + std::to_string(mTrainId), mLeavePlace,
true);
}
void
Loco::TrainRouteGraph::AddElementActivities(unsigned element_id,
bool add_leaves)
Loco::TrainRouteGraph::AddElementActivities(unsigned element_id)
{
auto &element = mElements[element_id];
for (const auto &[sect, node_info] : element.mSubgraph->mSections)
{
if (!add_leaves && node_info.mNodeType == SGSubgraph::NODE_TYPE::LEAF)
const SGPlaceCPtr &place = mSectionGraph->PlaceOfSection(sect);
if (element_id > 0
&& node_info.mNodeType == SGSubgraph::NODE_TYPE::SOURCE)
{
element.mPlaceActivities[place] =
mElements[element_id - 1].mPlaceActivities[place];
continue;
}
const SGPlaceCPtr &place = mSectionGraph->PlaceOfSection(sect);
if (element.mPlaceActivities.find(place)
!= element.mPlaceActivities.end())
{
......@@ -170,3 +180,67 @@ Loco::TrainRouteGraph::PrintActNamesList(
}
return os << "}";
}
void
Loco::TrainRouteGraph::ConnectActivities()
{
ConnectEnterActivity();
for (unsigned i = 0; i < mElements.size(); ++i)
{
ConnectElementActivities(i);
}
ConnectLeaveActivity();
for (auto &act : mActList)
{
sort_unique(act.mPred);
sort_unique(act.mSucc);
}
}
void
Loco::TrainRouteGraph::ConnectEnterActivity()
{
for (auto src : mElements.front().mSubgraph->mSources)
{
auto i = mElements.front().mPlaceActivities.at(
mSectionGraph->PlaceOfSection(src));
if (mActList[i].mPred.empty())
{
mActList[i].mPred.push_back(0);
}
}
}
void
Loco::TrainRouteGraph::ConnectLeaveActivity()
{
auto &pred = mActList.back().mPred;
for (auto leaf : mElements.back().mSubgraph->mTargets)
{
auto i = mElements.back().mPlaceActivities.at(
mSectionGraph->PlaceOfSection(leaf));
pred.push_back(i);
}
}
void
Loco::TrainRouteGraph::ConnectElementActivities(unsigned element_id)
{
auto &element = mElements[element_id];
for (const auto &[sect, node_info] : element.mSubgraph->mSections)
{
auto i = element.mPlaceActivities.at(mSectionGraph->PlaceOfSection(sect));
for (const auto &succ : node_info.mSucc)
{
auto j =
element.mPlaceActivities.at(mSectionGraph->PlaceOfSection(succ));
mActList[i].mSucc.push_back(j);
}
for (const auto &pred : node_info.mPred)
{
auto j =
element.mPlaceActivities.at(mSectionGraph->PlaceOfSection(pred));
mActList[i].mPred.push_back(j);
}
}
}
......@@ -77,7 +77,11 @@ namespace Loco
void InitActList();
void AddEnterActivity();
void AddLeaveActivity();
void AddElementActivities(unsigned element_id, bool add_leaves);
void AddElementActivities(unsigned element_id);
void ConnectActivities();
void ConnectEnterActivity();
void ConnectElementActivities(unsigned element_id);
void ConnectLeaveActivity();
std::ostream &
PrintActNamesList(std::ostream &os,
......
......@@ -71,6 +71,17 @@ namespace Loco
E enum_of_name(const std::string &val,
const std::array<std::string, S> &names);
template <class E> void sort_unique(std::vector<E> &vec);
template <class E>
std::vector<E> vector_subtract(const std::vector<E> &a,
const std::vector<E> &b);
template <class E>
std::vector<E> vector_union(const std::vector<E> &a,
const std::vector<E> &b);
template <class E, class Pred>
void vector_erase_indices(std::vector<E> &a, Pred pred);
template <class T> void vector_erase(std::vector<T> &vec, size_t i);
} // namespace Loco
// Definitions of templates and inline functions
......@@ -146,4 +157,66 @@ Loco::enum_of_name(const std::string &val,
return enum_of_int<E>(i - names.begin());
}
template <class E>
void
Loco::sort_unique(std::vector<E> &vec)
{
std::sort(vec.begin(), vec.end());
auto end = std::unique(vec.begin(), vec.end());
vec.erase(end, vec.end());
}
template <class E>
std::vector<E>
Loco::vector_subtract(const std::vector<E> &a, const std::vector<E> &b)
{
std::vector<E> c;
std::set_difference(a.begin(), a.end(), b.begin(), b.end(),
std::back_inserter(c));
return c;
}
template <class E>
std::vector<E>
Loco::vector_union(const std::vector<E> &a, const std::vector<E> &b)
{
std::vector<E> c;
std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(c));
return c;
}
template <class E, class Pred>
void
Loco::vector_erase_indices(std::vector<E> &a, Pred pred)
{
size_t i = 0;
for (size_t j = 0; j < a.size(); ++j)
{
if (!pred(j))
{
if (j > i)
{
a[i] = std::move(a[j]);
}
++i;
}
}
a.resize(i);
}
template <class T>
void
Loco::vector_erase(std::vector<T> &vec, size_t i)
{
if (i >= vec.size())
{
throw OutOfBoundsException("vector_erase", "i is out of bounds");
}
if (i + 1 < vec.size())
{
vec[i] = std::move(vec.back());
}
vec.pop_back();
}
#endif
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