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

Printing trains and places into the model data

parent 5cd39533
......@@ -149,6 +149,7 @@ main([[maybe_unused]] int argc, char *argv[])
routes.UpdateActivities(input_data.mTrainList);
{
std::ofstream f(cfg.output.model_data);
routes.PrintActivities(f, "", false);
routes.PrintModelData(f, "");
// routes.PrintActivities(f, "", true);
}
}
......@@ -135,11 +135,91 @@ Loco::TrainRoutes::PrintActivities(std::ostream &os, const std::string &prefix,
os << "activities = #[" << std::endl;
for (const auto &[train, g] : mRouteGraphs)
{
if (only_used && !g.HasUsableActivity())
{
continue;
}
g.PrintActivities(os, prefix + kTabString, only_used);
}
return os << "]#" << std::endl;
}
std::ostream &
Loco::TrainRoutes::PrintTrains(std::ostream &os, const std::string &prefix,
bool only_used) const
{
os << prefix << "Trains = {";
bool print_comma = false;
for (const auto &[train, g] : mRouteGraphs)
{
if (only_used && !g.HasUsableActivity())
{
continue;
}
if (print_comma)
{
os << ", ";
}
else
{
print_comma = true;
}
os << train->Ident();
}
os << "};" << std::endl;
return os;
}
std::ostream &
Loco::TrainRoutes::PrintModelData(std::ostream &os,
const std::string &prefix) const
{
PrintTrains(os, prefix, true);
os << std::endl;
PrintPlaces(os, prefix, true);
os << std::endl;
return PrintActivities(os, prefix, true);
}
std::ostream &
Loco::TrainRoutes::PrintPlaces(std::ostream &os, const std::string &prefix,
bool only_used) const
{
UsedPlaces used_places;
for (const auto &[train, g] : mRouteGraphs)
{
g.CollectPlaces(used_places, only_used);
}
os << prefix << "Places = {";
bool print_comma = false;
for (const auto &sgplace : used_places.mSGPlaces)
{
if (print_comma)
{
os << ", ";
}
else
{
print_comma = true;
}
os << sgplace->Ident();
}
for (const auto &sgplace : used_places.mTPlaces)
{
if (print_comma)
{
os << ", ";
}
else
{
print_comma = true;
}
os << sgplace->Ident();
}
os << "};" << std::endl;
return os;
}
std::ostream &
Loco::TrainRouteGraph::PrintActivities(std::ostream &os,
const std::string &prefix,
......@@ -407,3 +487,38 @@ Loco::TrainRouteGraph::UpdateLastActivity()
}
}
bool
Loco::TrainRouteGraph::HasUsableActivity() const
{
return mActList.back().mUse;
}
void
Loco::TrainRouteGraph::CollectPlaces(UsedPlaces &used_places,
bool only_used) const
{
// TODO: Using DFS starting from the last activity, we could save some time.
if (only_used && !HasUsableActivity())
{
return;
}
for (const auto &act : mActList)
{
if (only_used && !act.mUse)
{
continue;
}
used_places.AddPlace(act.mPlace);
}
}
void
Loco::UsedPlaces::AddPlace(const Place &place)
{
if (std::holds_alternative<SGPlaceCPtr>(place))
{
mSGPlaces.insert(std::get<SGPlaceCPtr>(place));
return;
}
mTPlaces.insert(std::get<TPlaceCPtr>(place));
}
......@@ -11,6 +11,8 @@
#ifndef __TRAIN_ROUTE_H
#define __TRAIN_ROUTE_H
#include <set>
#include "section_graph.h"
#include "trains_desc.h"
......@@ -38,6 +40,14 @@ namespace Loco
}
};
struct UsedPlaces
{
std::set<SGPlaceCPtr> mSGPlaces;
std::set<TPlaceCPtr> mTPlaces;
void AddPlace(const Place &place);
};
// Composed of relevant subgraphs
class TrainRouteGraph
{
......@@ -105,7 +115,8 @@ namespace Loco
bool only_used) const;
void UpdateActivities(const SectionCPtr &initial_section);
void ResetActivities();
bool HasUsableActivity() const;
void CollectPlaces(UsedPlaces &used_places, bool only_used) const;
};
// Manages train route graphs for all trains
......@@ -113,6 +124,7 @@ namespace Loco
// places.
class TrainRoutes
{
private:
std::unordered_map<TrainCPtr, TrainRouteGraph> mRouteGraphs;
void BuildRoutes(const InputData &input_data,
......@@ -124,6 +136,14 @@ namespace Loco
std::ostream &PrintActivities(std::ostream &os, const std::string &prefix,
bool only_used) const;
std::ostream &PrintTrains(std::ostream &os, const std::string &prefix,
bool only_used) const;
std::ostream &PrintPlaces(std::ostream &os, const std::string &prefix,
bool only_used) const;
std::ostream &PrintModelData(std::ostream &os,
const std::string &prefix) const;
void UpdateActivities(const TrainList &train_list);
};
} // namespace Loco
......
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