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

Writing places.ini

parent 76ea8f6d
......@@ -146,10 +146,14 @@ main([[maybe_unused]] int argc, char *argv[])
}
Loco::TrainRoutes routes(input_data, sg);
routes.UpdateActivities(input_data.mTrainList,
Loco::global_cfg.model.current_time.Seconds());
Loco::global_cfg.model.current_time.Seconds(), true);
{
std::ofstream f(Loco::global_cfg.output.model_data);
routes.PrintModelData(f, "");
// routes.PrintActivities(f, "", true);
}
{
std::ofstream f(Loco::global_cfg.output.places);
routes.PrintPlacesAsINI(f, "");
}
}
......@@ -15,6 +15,7 @@ default-directory = data
indications = indications.ini
diagnostic = deadlock.log
model-data = model.dat
places = places.ini
[message]
# Soubory s dynamickými daty
......
......@@ -102,6 +102,7 @@ Loco::CfgOutput::Parse(const INISection &sect,
indications = sect.PathValue("indications", directory, indications);
diagnostic = sect.PathValue("diagnostic", directory, diagnostic);
model_data = sect.PathValue("model-data", directory, model_data);
places = sect.PathValue("places", directory, places);
return true;
}
......
......@@ -38,6 +38,7 @@ namespace Loco
std::filesystem::path indications{"indications.ini"};
std::filesystem::path diagnostic{"deadlock.log"};
std::filesystem::path model_data{"model.dat"};
std::filesystem::path places{"places.ini"};
bool Parse(const INISection &sect, const std::string &default_directory);
};
......
......@@ -16,6 +16,7 @@ Loco::SectionGroupPlace::SectionGroupPlace(id_type cg_id,
length_type len)
: mSectionGroupId(cg_id), mSections(std::move(sections)), mLength(len)
{
mIdent = kPrefix + std::to_string(mSectionGroupId);
UpdateDurations();
}
......@@ -36,7 +37,7 @@ std::ostream &
Loco::SectionGroupPlace::Print(std::ostream &os,
const std::string &prefix) const
{
os << prefix << "[" << Ident() << "]" << std::endl;
os << prefix << kLabelDelimLeft << Ident() << kLabelDelimRight << std::endl;
if (!mSections.empty())
{
os << prefix << kTabString << "sections = ";
......@@ -62,7 +63,17 @@ Loco::TrainPlace::Desc() const
return (mEnter ? kPrefixEnter : kPrefixLeave) + ":" + mTrain->Ident();
}
std::string
std::ostream &
Loco::TrainPlace::Print(std::ostream &os, const std::string &prefix) const
{
os << prefix << kLabelDelimLeft << Ident() << kLabelDelimRight << std::endl;
os << prefix << kTabString
<< "train-place = " << (mEnter ? "enter" : "leave") << std::endl;
os << prefix << kTabString << "train = " << mTrain->Ident() << std::endl;
return os;
}
const std::string &
Loco::place_ident(const Place &place)
{
return std::holds_alternative<SGPlaceCPtr>(place)
......
......@@ -26,6 +26,7 @@ namespace Loco
inline static const std::string kPrefix{"SG"};
private:
std::string mIdent{};
id_type mSectionGroupId;
std::vector<SectionCPtr> mSections;
length_type mLength;
......@@ -38,7 +39,7 @@ namespace Loco
SectionGroupPlace(id_type sg_id, std::vector<SectionCPtr> sections,
length_type len);
std::string Ident() const;
const std::string &Ident() const;
std::ostream &Print(std::ostream &os, const std::string &prefix) const;
const std::vector<SectionCPtr> &Sections() const;
......@@ -74,16 +75,42 @@ namespace Loco
const std::string &Ident() const;
std::string Desc() const;
std::ostream &Print(std::ostream &os, const std::string &prefix) const;
const TrainCPtr &Train() const;
id_type TrainId() const;
bool
Enter() const
{
return mEnter;
}
};
using SGPlaceCPtr = std::shared_ptr<const SectionGroupPlace>;
using TPlaceCPtr = std::shared_ptr<const TrainPlace>;
using Place = std::variant<SGPlaceCPtr, TPlaceCPtr>;
std::string place_ident(const Place &place);
const std::string &place_ident(const Place &place);
struct SGPlaceComparator
{
bool
operator()(const SGPlaceCPtr &a, const SGPlaceCPtr &b) const
{
return a->SectionGroupId() < b->SectionGroupId();
}
};
struct TPlaceComparator
{
bool
operator()(const TPlaceCPtr &a, const TPlaceCPtr &b) const
{
return a->TrainId() < b->TrainId()
|| (a->TrainId() == b->TrainId() && a->Enter() < b->Enter());
}
};
} // namespace Loco
inline auto
......@@ -110,10 +137,10 @@ Loco::SectionGroupPlace::SectionGroupId() const
return mSectionGroupId;
}
inline std::string
inline const std::string &
Loco::SectionGroupPlace::Ident() const
{
return kPrefix + std::to_string(mSectionGroupId);
return mIdent;
}
inline auto
......
......@@ -207,23 +207,23 @@ Loco::TrainRoutes::PrintModelData(std::ostream &os,
{
PrintTrains(os, prefix, true);
os << std::endl;
PrintPlaces(os, prefix, true);
PrintPlacesAsModel(os, prefix);
os << std::endl;
return PrintActivities(os, prefix, true);
}
std::ostream &
Loco::TrainRoutes::PrintPlaces(std::ostream &os, const std::string &prefix,
bool only_used) const
Loco::TrainRoutes::PrintPlacesAsModel(std::ostream &os,
const std::string &prefix) 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)
return mUsedPlaces.PrintAsModel(os, prefix);
}
template <class T>
bool
print_place_model_list(std::ostream &os, const T &places, bool print_comma)
{
for (const auto &place : places)
{
if (print_comma)
{
......@@ -233,24 +233,45 @@ Loco::TrainRoutes::PrintPlaces(std::ostream &os, const std::string &prefix,
{
print_comma = true;
}
os << sgplace->Ident();
os << place->Ident();
}
return print_comma;
}
std::ostream &
Loco::UsedPlaces::PrintAsModel(std::ostream &os,
const std::string &prefix) const
{
os << prefix << "Places = {";
bool print_comma = print_place_model_list(os, mSGPlaces, false);
print_place_model_list(os, mTPlaces, print_comma);
os << "};" << std::endl;
return os;
}
std::ostream &
Loco::UsedPlaces::PrintAsINI(std::ostream &os, const std::string &prefix) const
{
os << prefix << "# Section Group Places" << std::endl;
for (const auto &sgplace : mSGPlaces)
{
sgplace->Print(os, prefix);
}
for (const auto &sgplace : used_places.mTPlaces)
os << prefix << "\n# Train Places" << std::endl;
for (const auto &tplace : mTPlaces)
{
if (print_comma)
{
os << ", ";
}
else
{
print_comma = true;
}
os << sgplace->Ident();
tplace->Print(os, prefix);
}
os << "};" << std::endl;
return os;
}
std::ostream &
Loco::TrainRoutes::PrintPlacesAsINI(std::ostream &os,
const std::string &prefix) const
{
return mUsedPlaces.PrintAsINI(os, prefix);
}
std::ostream &
Loco::TrainRouteGraph::PrintActivities(std::ostream &os,
const std::string &prefix,
......@@ -472,12 +493,18 @@ Loco::TrainRouteGraph::ElementWithSection(unsigned sect_id) const
void
Loco::TrainRoutes::UpdateActivities(const TrainList &train_list,
activity_time_type current_time)
activity_time_type current_time,
bool only_used_places)
{
for (const auto &[train, sect] : train_list.TrainSections())
{
mRouteGraphs.at(train).UpdateActivities(sect, current_time);
}
mUsedPlaces.Clear();
for (const auto &[train, g] : mRouteGraphs)
{
g.CollectPlaces(mUsedPlaces, only_used_places);
}
}
void
......@@ -582,3 +609,4 @@ Loco::UsedPlaces::AddPlace(const Place &place)
}
mTPlaces.insert(std::get<TPlaceCPtr>(place));
}
......@@ -55,10 +55,16 @@ namespace Loco
struct UsedPlaces
{
std::set<SGPlaceCPtr> mSGPlaces;
std::set<TPlaceCPtr> mTPlaces;
std::set<SGPlaceCPtr, SGPlaceComparator> mSGPlaces;
std::set<TPlaceCPtr, TPlaceComparator> mTPlaces;
void AddPlace(const Place &place);
void Clear();
std::ostream &PrintAsModel(std::ostream &os,
const std::string &prefix) const;
std::ostream &PrintAsINI(std::ostream &os,
const std::string &prefix) const;
};
// Composed of relevant subgraphs
......@@ -146,6 +152,7 @@ namespace Loco
{
private:
std::unordered_map<TrainCPtr, TrainRouteGraph> mRouteGraphs;
UsedPlaces mUsedPlaces;
void BuildRoutes(const InputData &input_data,
const SectionGraphCPtr &section_graph);
......@@ -159,14 +166,24 @@ namespace Loco
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 &PrintPlacesAsModel(std::ostream &os,
const std::string &prefix) const;
std::ostream &PrintPlacesAsINI(std::ostream &os,
const std::string &prefix) const;
std::ostream &PrintModelData(std::ostream &os,
const std::string &prefix) const;
void UpdateActivities(const TrainList &train_list,
activity_time_type current_time);
activity_time_type current_time,
bool only_used_places);
};
} // namespace Loco
inline void
Loco::UsedPlaces::Clear()
{
mSGPlaces.clear();
mTPlaces.clear();
}
#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