Skip to content

Commit 3898522

Browse files
authored
Merge pull request #389 from physycom/counterPosition
Add `CounterPosition` to move the counter on a Street
2 parents bb14e39 + e4ec4b2 commit 3898522

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

src/dsf/dsf.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
static constexpr uint8_t DSF_VERSION_MAJOR = 4;
88
static constexpr uint8_t DSF_VERSION_MINOR = 6;
9-
static constexpr uint8_t DSF_VERSION_PATCH = 0;
9+
static constexpr uint8_t DSF_VERSION_PATCH = 1;
1010

1111
static auto const DSF_VERSION =
1212
std::format("{}.{}.{}", DSF_VERSION_MAJOR, DSF_VERSION_MINOR, DSF_VERSION_PATCH);

src/dsf/mobility/Street.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ namespace dsf::mobility {
7979
assert(index < m_exitQueues.size());
8080
m_exitQueues[index] = std::move(queue);
8181
}
82-
void Street::enableCounter(std::string name) {
82+
void Street::enableCounter(std::string name, CounterPosition position) {
8383
if (m_counter.has_value()) {
8484
throw std::runtime_error(
8585
std::format("{} already has a counter named {}", *this, m_counter->name()));
@@ -89,6 +89,7 @@ namespace dsf::mobility {
8989
}
9090
m_counter.emplace();
9191
m_counter->setName(name);
92+
m_counterPosition = position;
9293
}
9394
void Street::resetCounter() {
9495
if (!hasCoil()) {
@@ -102,21 +103,27 @@ namespace dsf::mobility {
102103
assert(!isFull());
103104
spdlog::debug("Adding {} on {}", *pAgent, *this);
104105
m_movingAgents.push(std::move(pAgent));
106+
if (m_counter.has_value() && m_counterPosition == CounterPosition::ENTRY) {
107+
++(*m_counter);
108+
}
105109
}
106110
void Street::enqueue(std::size_t const& queueId) {
107111
assert(!m_movingAgents.empty());
108112
m_movingAgents.top()->incrementDistance(m_length);
109113
m_exitQueues[queueId].push(
110114
std::move(const_cast<std::unique_ptr<Agent>&>(m_movingAgents.top())));
111115
m_movingAgents.pop();
112-
if (m_counter.has_value()) {
116+
if (m_counter.has_value() && m_counterPosition == CounterPosition::MIDDLE) {
113117
++(*m_counter);
114118
}
115119
}
116120
std::unique_ptr<Agent> Street::dequeue(std::size_t const& index) {
117121
assert(!m_exitQueues[index].empty());
118122
auto pAgent{std::move(m_exitQueues[index].front())};
119123
m_exitQueues[index].pop();
124+
if (m_counter.has_value() && m_counterPosition == CounterPosition::EXIT) {
125+
++(*m_counter);
126+
}
120127
return pAgent;
121128
}
122129

src/dsf/mobility/Street.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ namespace dsf::mobility {
3939

4040
class Agent;
4141

42+
/// @brief The position of the counter on the street
43+
enum class CounterPosition : uint8_t { ENTRY = 0, MIDDLE = 1, EXIT = 2 };
44+
4245
/// @brief The Street class represents a street in the network.
4346
class Street : public Road {
4447
private:
@@ -49,6 +52,7 @@ namespace dsf::mobility {
4952
m_movingAgents;
5053
std::vector<Direction> m_laneMapping;
5154
std::optional<Counter> m_counter;
55+
CounterPosition m_counterPosition{CounterPosition::EXIT};
5256
double m_stationaryWeight{1.0};
5357

5458
public:
@@ -94,7 +98,9 @@ namespace dsf::mobility {
9498
}
9599
/// @brief Enable a coil (dsf::Counter sensor) on the street
96100
/// @param name The name of the counter (default is "Coil_<street_id>")
97-
void enableCounter(std::string name = std::string());
101+
/// @param position The position of the counter on the street (default is EXIT)
102+
void enableCounter(std::string name = std::string(),
103+
CounterPosition position = CounterPosition::EXIT);
98104
/// @brief Reset the counter of the street
99105
/// @throw std::runtime_error If the street does not have a coil
100106
void resetCounter();

test/mobility/Test_street.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,24 @@ TEST_CASE("Street") {
182182
}
183183

184184
TEST_CASE("Street with a coil") {
185-
SUBCASE("Counts") {
185+
SUBCASE("Entry Counter") {
186+
GIVEN("A street with an entry counter") {
187+
Street street{1, std::make_pair(0, 1), 3.5};
188+
street.enableCounter("EntryCoil", dsf::mobility::CounterPosition::ENTRY);
189+
CHECK_EQ(street.counterName(), "EntryCoil");
190+
WHEN("An agent is added") {
191+
street.addAgent(std::make_unique<Agent>(0, 1));
192+
THEN("The input flow is one immediately") { CHECK_EQ(street.counts(), 1); }
193+
street.enqueue(0);
194+
THEN("The input flow is still one") { CHECK_EQ(street.counts(), 1); }
195+
}
196+
}
197+
}
198+
199+
SUBCASE("Middle Counter") {
186200
GIVEN("A street with a counter") {
187201
Street street{1, std::make_pair(0, 1), 3.5};
188-
street.enableCounter();
202+
street.enableCounter("", dsf::mobility::CounterPosition::MIDDLE);
189203
CHECK_EQ(street.counterName(), "Coil_1");
190204
WHEN("An agent is added") {
191205
street.addAgent(std::make_unique<Agent>(0, 1));
@@ -197,6 +211,21 @@ TEST_CASE("Street with a coil") {
197211
}
198212
}
199213
}
214+
215+
SUBCASE("Exit Counter") {
216+
GIVEN("A street with an exit counter") {
217+
Street street{1, std::make_pair(0, 1), 3.5};
218+
street.enableCounter("ExitCoil", dsf::mobility::CounterPosition::EXIT);
219+
CHECK_EQ(street.counterName(), "ExitCoil");
220+
WHEN("An agent is added and enqueued") {
221+
street.addAgent(std::make_unique<Agent>(0, 1));
222+
street.enqueue(0);
223+
THEN("The input flow is zero") { CHECK_EQ(street.counts(), 0); }
224+
street.dequeue(0);
225+
THEN("The input flow is one after dequeue") { CHECK_EQ(street.counts(), 1); }
226+
}
227+
}
228+
}
200229
}
201230

202231
TEST_CASE("Road") {

0 commit comments

Comments
 (0)