Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 42 additions & 39 deletions src/dsm/headers/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
m_streets.emplace(
index, std::make_unique<Street>(index, std::make_pair(srcId, dstId)));
assert(index == srcId * n + dstId);
if (!isAdj) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 18.4 rule Note

MISRA 18.4 rule
m_streets[index]->setLength(value);
}
m_streets[index]->setMaxSpeed(defaultSpeed);
Expand Down Expand Up @@ -252,11 +252,15 @@
}
} else if (fileExt == "csv") {
std::ifstream ifs{fileName};
assert((void("Coordinates file not found."), ifs.is_open()));
if (!ifs.is_open()) {
throw std::invalid_argument(buildLog("Cannot find file: " + fileName));
}
// Check if the first line is nodeId;lat;lon
std::string line;
std::getline(ifs, line);
assert((void("Invalid file format."), line == "nodeId;lat;lon"));
if (line != "nodeId;lat;lon") {
throw std::invalid_argument(buildLog("Invalid file format."));
}
double dLat, dLon;
while (!ifs.eof()) {
std::getline(ifs, line);
Expand Down Expand Up @@ -304,10 +308,16 @@
std::getline(iss, lat, ';');
std::getline(iss, lon, ';');
std::getline(iss, highway, ';');
Id nodeId{static_cast<Id>(std::stoul(id))};
m_nodes.emplace(nodeIndex,
std::make_unique<Intersection>(
nodeIndex, std::make_pair(std::stod(lat), std::stod(lon))));
auto const nodeId{static_cast<Id>(std::stoul(id))};
if (highway.find("traffic_signals") != std::string::npos) {
addNode<TrafficLight>(
nodeIndex, 60, std::make_pair(std::stod(lat), std::stod(lon)));
} else if (highway.find("roundabout") != std::string::npos) {
addNode<Roundabout>(nodeIndex, std::make_pair(std::stod(lat), std::stod(lon)));
} else {
addNode<Intersection>(nodeIndex,
std::make_pair(std::stod(lat), std::stod(lon)));
}
m_nodeMapping.emplace(std::make_pair(nodeId, nodeIndex));
++nodeIndex;
}
Expand All @@ -321,9 +331,8 @@
if (fileExt == "csv") {
std::ifstream file{fileName};
if (!file.is_open()) {
std::string errrorMsg{"Error at line " + std::to_string(__LINE__) + " in file " +
__FILE__ + ": " + "File not found"};
throw std::invalid_argument(errrorMsg);
throw std::invalid_argument(
buildLog(std::format("File \'{}\' not found", fileName)));
}
std::string line;
std::getline(file, line); // skip first line
Expand All @@ -333,54 +342,48 @@
continue;
}
std::istringstream iss{line};
std::string sourceId, targetId, length, oneway, lanes, highway, maxspeed, bridge;
// u;v;length;oneway;highway;maxspeed;bridge
std::string sourceId, targetId, length, oneway, lanes, highway, maxspeed, name;

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
// u;v;length;oneway;highway;maxspeed;name
std::getline(iss, sourceId, ';');

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule

Check notice

Code scanning / Cppcheck (reported by Codacy)

Return value of function std::stod() is not used. Note

Return value of function std::stod() is not used.
std::getline(iss, targetId, ';');
std::getline(iss, length, ';');
std::getline(iss, oneway, ';');
std::getline(iss, lanes, ';');
std::getline(iss, highway, ';');
std::getline(iss, maxspeed, ';');
std::getline(iss, bridge, ';');
try {
std::stod(maxspeed);
} catch (const std::invalid_argument& e) {
maxspeed = "30";
std::getline(iss, name, ';');
if (maxspeed.empty()) {
maxspeed = "30"; // Default to 30 km/h if no maxspeed is provided
} else {
try {
std::stod(maxspeed);
} catch (const std::invalid_argument& e) {
maxspeed = "30"; // Default to 30 km/h if maxspeed is invalid
}
}

uint8_t numLanes;
if (lanes.empty()) {
numLanes = 1; // Default to 1 lane if no value is provided
lanes = "1"; // Default to 1 lane if no value is provided
} else {
try {
// Convert lanes to a double first, then cast to uint8_t
double lanesVal = std::stod(lanes);
if (lanesVal < 1 || std::isnan(lanesVal)) {
numLanes = 1; // Default to 1 if lanes is invalid
} else {
numLanes = static_cast<uint8_t>(lanesVal); // Cast to uint8_t
}
} catch (const std::invalid_argument&) {
numLanes = 1; // Default to 1 if conversion fails
std::stoul(lanes);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule

Check notice

Code scanning / Cppcheck (reported by Codacy)

Return value of function std::stoul() is not used. Note

Return value of function std::stoul() is not used.
} catch (const std::invalid_argument& e) {
lanes = "1"; // Default to 1 lane if lanes is invalid
}
}

Id streetId = std::stoul(sourceId) + std::stoul(targetId) * m_nodes.size();
m_streets.emplace(
streetId,
std::make_unique<Street>(streetId,
1,
std::stod(maxspeed),
std::stod(length),
std::make_pair(m_nodeMapping[std::stoul(sourceId)],
m_nodeMapping[std::stoul(targetId)]),
numLanes));
addEdge<Street>(streetId,
std::stod(length) / 5,

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note

MISRA 12.3 rule
std::stod(maxspeed),
std::stod(length),
std::make_pair(m_nodeMapping[std::stoul(sourceId)],
m_nodeMapping[std::stoul(targetId)]),
std::stoul(lanes),
name);
}
} else {
std::string errrorMsg{"Error at line " + std::to_string(__LINE__) + " in file " +
__FILE__ + ": " + "File extension not supported"};
throw std::invalid_argument(errrorMsg);
throw std::invalid_argument(buildLog("File extension not supported"));
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/dsm/headers/TrafficLight.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ namespace dsm {
TrafficLight(Id id, Delay cycleTime)
: Intersection{id}, m_cycleTime{cycleTime}, m_counter{0} {}

TrafficLight(Id id, Delay cycleTime, std::pair<double, double> coords)
: Intersection{id, std::move(coords)}, m_cycleTime{cycleTime}, m_counter{0} {}

TrafficLight(Node const& node, Delay const cycleTime, Delay const counter = 0)
: Intersection{node}, m_cycleTime{cycleTime}, m_counter{counter} {}

Expand Down
40 changes: 34 additions & 6 deletions test/Test_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,53 @@
WHEN("We import nodes and edges from OSM") {
graph.importOSMNodes("./data/nodes.csv");
graph.importOSMEdges("./data/edges.csv");
std::ifstream fNodes{"./data/nodes.csv"};
// get number of lines
std::string line;
int nNodes{-1}; // -1 because of the header
while (std::getline(fNodes, line)) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note test

MISRA 14.4 rule
++nNodes;
}
fNodes.close();
std::ifstream fEdges{"./data/edges.csv"};
int nEdges{-1}; // -1 because of the header
while (std::getline(fEdges, line)) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note test

MISRA 14.4 rule
++nEdges;
}
fEdges.close();
THEN("Sizes are correct") {
CHECK_EQ(graph.nNodes(), 25);
CHECK_EQ(graph.nEdges(), 60);
CHECK_EQ(graph.nNodes(), nNodes);
CHECK_EQ(graph.nEdges(), nEdges);
}
THEN("We are able to build the adjacency matrix") {
graph.buildAdj();
CHECK_EQ(graph.adjMatrix().size(), 60);
CHECK_EQ(graph.adjMatrix().size(), nEdges);
}
}
WHEN("We import many nodes and edges from OSM") {
graph.importOSMNodes("./data/nodes_big.csv");
graph.importOSMEdges("./data/edges_big.csv");
std::ifstream fNodes{"./data/nodes_big.csv"};
// get number of lines
std::string line;
int nNodes{-1}; // -1 because of the header
while (std::getline(fNodes, line)) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note test

MISRA 14.4 rule
++nNodes;
}
fNodes.close();
std::ifstream fEdges{"./data/edges_big.csv"};
int nEdges{-1}; // -1 because of the header
while (std::getline(fEdges, line)) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.4 rule Note test

MISRA 14.4 rule
++nEdges;
}
fEdges.close();
THEN("Sizes are correct") {
CHECK_EQ(graph.nNodes(), 4077);
CHECK_EQ(graph.nEdges(), 8875);
CHECK_EQ(graph.nNodes(), nNodes);
CHECK_EQ(graph.nEdges(), nEdges);
}
THEN("We are able to build the adjacency matrix") {
graph.buildAdj();
CHECK_EQ(graph.adjMatrix().size(), 8875);
CHECK_EQ(graph.adjMatrix().size(), nEdges);
}
}
}
Expand Down
Loading
Loading