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

Element and places ids

parent 2ef6be01
......@@ -47,9 +47,10 @@ Loco::AreaParser::AddArea(InputData &data, const INISection &sect) const
// Inner indicators, level=1
// EntryPoints, level=2
}
auto [ptr, ins] = data.mAreas.Add(
sect.mIdent, sect.mIdent, sect.ValueWithDefault("name", ""),
std::move(inner_indicators), std::move(entry_points));
auto [ptr, ins] =
data.mAreas.Add(sect.mIdent, Area::NextId(), sect.mIdent,
sect.ValueWithDefault("name", ""),
std::move(inner_indicators), std::move(entry_points));
if (!ins)
{
throw InconsistentDataException("AreaParser::AddArea",
......@@ -74,7 +75,8 @@ Loco::AreaParser::ParseIndicator(const InputData &data,
throw InconsistentDataException("AreaParser::ParseIndicator",
"section not found");
}
return {{TopSection().mIdent, sect.mIdent},
return {Indicator::NextId(),
{TopSection().mIdent, sect.mIdent},
sect.ValueWithDefault("name", ""),
std::move(sect_ptr)};
}
......@@ -100,7 +102,8 @@ Loco::AreaParser::ParseEntryPoint(const InputData &data,
{
indicators.push_back(ParseIndicator(data, subsect));
}
return {{TopSection().mIdent, sect.mIdent},
return {EntryPoint::NextId(),
{TopSection().mIdent, sect.mIdent},
sect.ValueWithDefault("name", ""),
std::move(sect_ptr),
std::move(indicators)};
......
......@@ -37,5 +37,6 @@ Loco::DataElement::Print(std::ostream &os, const std::string &prefix,
{
os << prefix << kTabString << "name = " << mName << std::endl;
}
os << prefix << kTabString << "id = " << mId << std::endl;
return PrintData(os, prefix + kTabString, region_hint);
}
......@@ -25,6 +25,7 @@ namespace Loco
{
class DataElement
{
id_type mId{};
GlobalIdent mIdent{};
std::string mName{};
......@@ -35,8 +36,8 @@ namespace Loco
virtual unsigned Level() const;
public:
DataElement(GlobalIdent ident, std::string name)
: mIdent(std::move(ident)), mName(std::move(name))
DataElement(id_type id, GlobalIdent ident, std::string name)
: mId(id), mIdent(std::move(ident)), mName(std::move(name))
{
}
......@@ -45,6 +46,11 @@ namespace Loco
virtual std::ostream &Print(std::ostream &os, const std::string &prefix,
const std::string &region_hint) const;
id_type
Id() const
{
return mId;
}
const GlobalIdent &
Ident() const
{
......
......@@ -30,9 +30,9 @@ Loco::EdgeParser::AddDataElementFromSection(InputData &data,
}
edge_data.len = sect.LengthValue("length").value_or(0);
auto [node_ptr, ins] =
reg.mEdges.Add(sect.mIdent, GlobalIdent(reg.mIdent, sect.mIdent),
sect.ValueWithDefault("name", ""), std::move(edge_data));
auto [node_ptr, ins] = reg.mEdges.Add(
sect.mIdent, Edge::NextId(), GlobalIdent(reg.mIdent, sect.mIdent),
sect.ValueWithDefault("name", ""), std::move(edge_data));
if (!ins)
{
throw InconsistentDataException("AddEdgeFromSection",
......
......@@ -20,6 +20,7 @@ namespace Loco
{
using length_type = unsigned;
using activity_time_type = long unsigned;
using id_type = long unsigned;
struct GlobalIdent
{
......
......@@ -105,6 +105,7 @@ Loco::Area::Print(std::ostream &os, const std::string &prefix) const
<< std::endl;
const std::string pfix = prefix + kTabString;
os << pfix << "name = " << mName << std::endl;
os << pfix << "id = " << mId << std::endl;
std::for_each(mInnerIndicators.begin(), mInnerIndicators.end(),
[&os, &pfix](const auto &ind) { ind.Print(os, pfix, ""); });
std::for_each(mEntryPoints.begin(), mEntryPoints.end(),
......
......@@ -27,6 +27,8 @@ namespace Loco
{
class Point : public DataElement
{
inline static id_type sNextId{0};
public:
struct Position
{
......@@ -46,12 +48,13 @@ namespace Loco
const std::string &region_hint) const override;
public:
Point(GlobalIdent ident, std::string name, const Position &pos)
: DataElement(std::move(ident), std::move(name)), mPosition(pos)
Point(id_type id, GlobalIdent ident, std::string name,
const Position &pos)
: DataElement(id, std::move(ident), std::move(name)), mPosition(pos)
{
}
Point(GlobalIdent ident, std::string name, Position &&pos)
: DataElement(std::move(ident), std::move(name)),
Point(id_type id, GlobalIdent ident, std::string name, Position &&pos)
: DataElement(id, std::move(ident), std::move(name)),
mPosition(std::move(pos))
{
}
......@@ -67,12 +70,20 @@ namespace Loco
{
return mPosition.offset;
}
static id_type
NextId()
{
return sNextId++;
}
};
using PointCPtr = std::shared_ptr<const Point>;
class Section : public DataElement
{
inline static id_type sNextId{0};
public:
struct Data
{
......@@ -94,15 +105,15 @@ namespace Loco
const std::string &region_hint) const override;
public:
Section(GlobalIdent ident, std::string name, const Data &data,
Section(id_type id, GlobalIdent ident, std::string name, const Data &data,
const std::vector<PointCPtr> &points)
: DataElement(std::move(ident), std::move(name)),
: DataElement(id, std::move(ident), std::move(name)),
mData(std::move(data)), mPoints(points)
{
}
Section(GlobalIdent ident, std::string name, Data &&data,
Section(id_type id, GlobalIdent ident, std::string name, Data &&data,
std::vector<PointCPtr> &&points)
: DataElement(std::move(ident), std::move(name)),
: DataElement(id, std::move(ident), std::move(name)),
mData(std::move(data)), mPoints(std::move(points))
{
}
......@@ -118,12 +129,19 @@ namespace Loco
{
return mData.buf_offset;
}
static id_type
NextId()
{
return sNextId++;
}
};
using SectionCPtr = std::shared_ptr<const Section>;
class Indicator : public DataElement
{
inline static id_type sNextId{0};
SectionCPtr mSection;
virtual std::ostream &
......@@ -131,14 +149,24 @@ namespace Loco
const std::string &region_hint) const override;
public:
Indicator(GlobalIdent ident, std::string name, SectionCPtr section)
: DataElement(std::move(ident), std::move(name)),
Indicator(id_type id, GlobalIdent ident, std::string name,
SectionCPtr section)
: DataElement(id, std::move(ident), std::move(name)),
mSection(std::move(section))
{
}
static id_type
NextId()
{
return sNextId++;
}
};
class EntryPoint : public DataElement
{
inline static id_type sNextId{0};
SectionCPtr mSection;
std::vector<Indicator> mIndicators;
......@@ -150,33 +178,47 @@ namespace Loco
virtual unsigned Level() const override;
public:
EntryPoint(GlobalIdent ident, std::string name, SectionCPtr section,
std::vector<Indicator> indicators)
: DataElement(std::move(ident), std::move(name)),
EntryPoint(id_type id, GlobalIdent ident, std::string name,
SectionCPtr section, std::vector<Indicator> indicators)
: DataElement(id, std::move(ident), std::move(name)),
mSection(std::move(section)), mIndicators(std::move(indicators))
{
}
static id_type
NextId()
{
return sNextId++;
}
};
class Area
{
private:
inline static id_type sNextId{0};
id_type mId;
std::string mIdent;
std::string mName;
std::vector<Indicator> mInnerIndicators{};
std::vector<EntryPoint> mEntryPoints{};
public:
Area(std::string ident, std::string name,
Area(id_type id, std::string ident, std::string name,
std::vector<Indicator> inner_indicators,
std::vector<EntryPoint> entry_points)
: mIdent(std::move(ident)), mName(std::move(name)),
: mId(id), mIdent(std::move(ident)), mName(std::move(name)),
mInnerIndicators(std::move(inner_indicators)),
mEntryPoints(std::move(entry_points))
{
}
std::ostream &Print(std::ostream &os, const std::string &prefix) const;
static id_type
NextId()
{
return sNextId++;
}
};
} // namespace Loco
......
......@@ -15,9 +15,9 @@ Loco::NodeParser::AddDataElementFromSection([[maybe_unused]] InputData &data,
const INISection &sect,
Region &reg) const
{
auto [node_ptr, ins] =
reg.mNodes.Add(sect.mIdent, GlobalIdent(reg.mIdent, sect.mIdent),
sect.ValueWithDefault("name", ""));
auto [node_ptr, ins] = reg.mNodes.Add(sect.mIdent, Node::NextId(),
GlobalIdent(reg.mIdent, sect.mIdent),
sect.ValueWithDefault("name", ""));
if (!ins)
{
throw InconsistentDataException("AddNodeFromSection",
......
......@@ -27,6 +27,8 @@ namespace Loco
{
class Node : public DataElement
{
inline static id_type sNextId{0};
virtual std::ostream &
PrintData(std::ostream &os, [[maybe_unused]] const std::string &prefix,
[[maybe_unused]] const std::string &region_hint) const override
......@@ -36,12 +38,20 @@ namespace Loco
public:
using DataElement::DataElement;
static id_type
NextId()
{
return sNextId++;
}
};
using NodeCPtr = std::shared_ptr<const Node>;
class Edge : public DataElement
{
inline static id_type sNextId{0};
public:
struct Data
{
......@@ -60,13 +70,13 @@ namespace Loco
const std::string &region_hint) const override;
public:
Edge(GlobalIdent ident, std::string name, const Data &data)
: DataElement(std::move(ident), std::move(name)),
Edge(id_type id, GlobalIdent ident, std::string name, const Data &data)
: DataElement(id, std::move(ident), std::move(name)),
mFrom(data.from_node), mTo(data.to_node), mLength(data.len)
{
}
Edge(GlobalIdent ident, std::string name, Data &&data)
: DataElement(std::move(ident), std::move(name)),
Edge(id_type id, GlobalIdent ident, std::string name, Data &&data)
: DataElement(id, std::move(ident), std::move(name)),
mFrom(std::move(data.from_node)), mTo(std::move(data.to_node)),
mLength(data.len)
{
......@@ -79,6 +89,12 @@ namespace Loco
}
NodeCPtr From() const;
NodeCPtr To() const;
static id_type
NextId()
{
return sNextId++;
}
};
using EdgeCPtr = std::shared_ptr<const Edge>;
......
......@@ -10,7 +10,7 @@
#include "places.h"
Loco::SectionGroupPlace::SectionGroupPlace(unsigned cg_id,
Loco::SectionGroupPlace::SectionGroupPlace(id_type cg_id,
std::vector<SectionCPtr> sections)
: mSectionGroupId(cg_id), mSections(std::move(sections))
{
......@@ -34,9 +34,10 @@ Loco::SectionGroupPlace::Print(std::ostream &os,
return os;
}
Loco::TrainPlace::TrainPlace(bool enter, unsigned train_id, TrainCPtr train)
: mEnter(enter), mTrainId(train_id), mTrain(std::move(train))
Loco::TrainPlace::TrainPlace(bool enter, TrainCPtr train)
: mEnter(enter), mTrain(std::move(train))
{
mIdent = (mEnter ? kPrefixEnter : kPrefixLeave) + mTrain->Ident();
}
std::string
......
......@@ -26,11 +26,11 @@ namespace Loco
inline static const std::string kPrefix{"SG"};
private:
unsigned mSectionGroupId;
id_type mSectionGroupId;
std::vector<SectionCPtr> mSections;
public:
SectionGroupPlace(unsigned sg_id, std::vector<SectionCPtr> sections);
SectionGroupPlace(id_type sg_id, std::vector<SectionCPtr> sections);
std::string Ident() const;
std::ostream &Print(std::ostream &os, const std::string &prefix) const;
......@@ -44,22 +44,22 @@ namespace Loco
class TrainPlace
{
public:
inline static const std::string kPrefixEnter{"TE"};
inline static const std::string kPrefixLeave{"TL"};
inline static const std::string kPrefixEnter{"TE_"};
inline static const std::string kPrefixLeave{"TL_"};
private:
bool mEnter;
unsigned mTrainId;
TrainCPtr mTrain;
std::string mIdent{};
public:
TrainPlace(bool enter, unsigned train_id, TrainCPtr train);
TrainPlace(bool enter, TrainCPtr train);
std::string Ident() const;
const std::string &Ident() const;
std::string Desc() const;
const TrainCPtr &Train() const;
unsigned TrainId() const;
id_type TrainId() const;
};
using SGPlaceCPtr = std::shared_ptr<const SectionGroupPlace>;
......@@ -105,16 +105,16 @@ Loco::TrainPlace::Train() const -> const TrainCPtr &
return mTrain;
}
inline unsigned
Loco::TrainPlace::TrainId() const
inline auto
Loco::TrainPlace::TrainId() const -> id_type
{
return mTrainId;
return mTrain->Id();
}
inline std::string
inline const std::string &
Loco::TrainPlace::Ident() const
{
return (mEnter ? kPrefixEnter : kPrefixLeave) + std::to_string(mTrainId);
return mIdent;
}
#endif
......
......@@ -29,9 +29,9 @@ Loco::PointParser::AddDataElementFromSection(InputData &data,
"Missing point offset");
}
auto [node_ptr, ins] =
reg.mPoints.Add(sect.mIdent, GlobalIdent(reg.mIdent, sect.mIdent),
sect.ValueWithDefault("name", ""), std::move(pos));
auto [node_ptr, ins] = reg.mPoints.Add(
sect.mIdent, Point::NextId(), GlobalIdent(reg.mIdent, sect.mIdent),
sect.ValueWithDefault("name", ""), std::move(pos));
if (!ins)
{
throw InconsistentDataException("AddPointFromSection",
......
......@@ -11,6 +11,7 @@
*/
#include <algorithm>
#include <limits>
#include <numeric>
#include <queue>
#include <unordered_set>
......@@ -287,12 +288,14 @@ Loco::SectionGraph::BuildPlaces()
for (unsigned cg_id = 0; cg_id < mConflictGroups.size(); ++cg_id)
{
std::vector<SectionCPtr> sections;
id_type id = std::numeric_limits<id_type>::max();
for (const auto &sect_id : mConflictGroups[cg_id])
{
sections.push_back(mSections[sect_id].mSection);
id = std::min(id, mSections[sect_id].mSection->Id());
}
mPlaces.push_back(
std::make_shared<SectionGroupPlace>(cg_id, std::move(sections)));
std::make_shared<SectionGroupPlace>(id, std::move(sections)));
}
}
......
......@@ -24,7 +24,7 @@ Loco::SectionParser::AddDataElementFromSection(InputData &data,
sect_data.buf_length = sect.UnsignedValue("buf-length").value_or(0);
auto [sect_ptr, ins] = reg.mSections.Add(
sect.mIdent, GlobalIdent(reg.mIdent, sect.mIdent),
sect.mIdent, Section::NextId(), GlobalIdent(reg.mIdent, sect.mIdent),
sect.ValueWithDefault("name", ""), std::move(sect_data),
GetObjectArrayByField<Point>(
data, sect, reg, "points",
......
......@@ -33,8 +33,9 @@ Loco::TrainListParser::AddSectionTrainList(InputData &data,
std::cerr << "[AddSectionTrainList] No train with ident '"
<< train_ident << "' found, adding a train for this purpose"
<< std::endl;
auto [tp, ins] = data.mTrains.Add(
train_ident, train_ident, TrainData(), std::vector<TrainStop>());
auto [tp, ins] =
data.mTrains.Add(train_ident, Train::NextId(), train_ident,
TrainData(), std::vector<TrainStop>());
if (!ins)
{
throw InternalErrorException("TrainListParser::AddSectionTrainList",
......
......@@ -24,8 +24,9 @@ Loco::TrainParser::AddToInputData(InputData &data) const
void
Loco::TrainParser::AddTrain(InputData &data, const INISection &sect) const
{
auto [ptr, ins] = data.mTrains.Add(
sect.mIdent, sect.mIdent, ParseTrainData(sect), ParseStops(data, sect));
auto [ptr, ins] =
data.mTrains.Add(sect.mIdent, Train::NextId(), sect.mIdent,
ParseTrainData(sect), ParseStops(data, sect));
if (!ins)
{
throw InconsistentDataException("TrainParser::AddArea",
......@@ -79,7 +80,8 @@ Loco::TrainParser::ParseSingleStop(const InputData &data,
<< "' - stop '" << sect.mIdent
<< "' has no traffic point, adding one just for this train"
<< std::endl;
tp = std::make_shared<TrafficPoint>(GlobalIdent("", sect.mIdent),
tp = std::make_shared<TrafficPoint>(TrafficPoint::NextId(),
GlobalIdent("", sect.mIdent),
sect.ValueWithDefault("name", ""));
}
TrainStopSpec spec;
......
......@@ -33,8 +33,8 @@ Loco::TrainRouteGraph::TrainRouteGraph(TrainCPtr train, unsigned train_id,
: mTrain(std::move(train)), mTrainId(train_id),
mSectionGraph(std::move(section_graph))
{
mEnterPlace = std::make_shared<const TrainPlace>(true, mTrainId, mTrain);
mLeavePlace = std::make_shared<const TrainPlace>(false, mTrainId, mTrain);
mEnterPlace = std::make_shared<const TrainPlace>(true, mTrain);
mLeavePlace = std::make_shared<const TrainPlace>(false, mTrain);
BuildElements();
if (mElements.empty())
{
......@@ -85,15 +85,13 @@ Loco::TrainRouteGraph::InitActList()
void
Loco::TrainRouteGraph::AddEnterActivity()
{
mActList.emplace_back("EnterT" + std::to_string(mTrainId), mEnterPlace,
false);
mActList.emplace_back(mTrain->Ident() + "Enter", mEnterPlace, false);
}
void
Loco::TrainRouteGraph::AddLeaveActivity()
{
mActList.emplace_back("LeaveT" + std::to_string(mTrainId), mLeavePlace,
true);
mActList.emplace_back(mTrain->Ident() + "Leave", mLeavePlace, true);
}
void
......@@ -157,7 +155,7 @@ Loco::TrainRouteGraph::PrintActivities(std::ostream &os,
if (!has_used)
{
has_used = true;
os << prefix << "Train" << mTrainId << ":" << std::endl;
os << prefix << mTrain->Ident() << ":" << std::endl;
os << prefix << "{" << std::endl;
}
os << prefix << kTabString;
......
......@@ -21,6 +21,7 @@ namespace Loco
{
class TrafficPoint : public DataElement
{
inline static id_type sNextId{0};
std::vector<SectionCPtr> mSections{};
virtual std::ostream &
......@@ -28,14 +29,14 @@ namespace Loco
const std::string &region_hint) const override;
public:
TrafficPoint(GlobalIdent ident, std::string name)
: DataElement(std::move(ident), std::move(name))
TrafficPoint(id_type id, GlobalIdent ident, std::string name)
: DataElement(id, std::move(ident), std::move(name))
{
}
TrafficPoint(GlobalIdent ident, std::string name,
TrafficPoint(id_type id, GlobalIdent ident, std::string name,
std::vector<SectionCPtr> sections)
: DataElement(std::move(ident), std::move(name)),
: DataElement(id, std::move(ident), std::move(name)),
mSections(std::move(sections))
{
}
......@@ -51,6 +52,11 @@ namespace Loco
{
return mSections.empty();
}
static id_type
NextId()
{
return sNextId++;
}
};
using TrafficPointCPtr = std::shared_ptr<const TrafficPoint>;
......@@ -138,19 +144,27 @@ namespace Loco
class Train
{
inline static id_type sNextId{0};
id_type mId;
std::string mIdent;
TrainData mData;
std::vector<TrainStop> mStops;
public:
Train(std::string ident, TrainData data, std::vector<TrainStop> stops)
: mIdent(std::move(ident)), mData(std::move(data)),
Train(id_type id, std::string ident, TrainData data,
std::vector<TrainStop> stops)
: mId(id), mIdent(std::move(ident)), mData(std::move(data)),
mStops(std::move(stops))
{