LCOV - code coverage report
Current view: top level - app/src/data/SUMO - Network_stringify.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 21 41 51.2 %
Date: 2023-08-17 16:45:52 Functions: 7 14 50.0 %

          Line data    Source code
       1             : #include "data/SUMO/Network.hpp"
       2             : #include "utils/invertMap.hpp"
       3             : 
       4             : using namespace std;
       5             : using namespace rapidxml;
       6             : using namespace SUMO;
       7             : using namespace utils::stringify;
       8             : 
       9             : typedef Network::Junction          Junction;
      10             : typedef Network::Edge              Edge;
      11             : typedef Network::Edge::Lane        Lane;
      12             : typedef Network::TrafficLightLogic TrafficLightLogic;
      13             : typedef Network::Connection        Connection;
      14             : 
      15             : // clang-format off
      16             : const unordered_map<string, Edge::Function> str2function = {
      17             :     {"internal"     , Edge::Function::INTERNAL},
      18             :     {"connector"    , Edge::Function::CONNECTOR},
      19             :     {"crossing"     , Edge::Function::CROSSING},
      20             :     {"walkingarea"  , Edge::Function::WALKINGAREA},
      21             :     {"normal"       , Edge::Function::NORMAL}
      22             : };
      23             : const unordered_map<Edge::Function, string> function2str = utils::invertMap(str2function);
      24             : 
      25             : const unordered_map<string, Junction::Type> str2junctionType = {
      26             :     {"priority"                     , Junction::Type::PRIORITY                   },
      27             :     {"traffic_light"                , Junction::Type::TRAFFIC_LIGHT              },
      28             :     {"right_before_left"            , Junction::Type::RIGHT_BEFORE_LEFT          },
      29             :     {"left_before_right"            , Junction::Type::LEFT_BEFORE_RIGHT          },
      30             :     {"unregulated"                  , Junction::Type::UNREGULATED                },
      31             :     {"traffic_light_unregulated"    , Junction::Type::TRAFFIC_LIGHT_UNREGULATED  },
      32             :     {"priority_stop"                , Junction::Type::PRIORITY_STOP              },
      33             :     {"allway_stop"                  , Junction::Type::ALLWAY_STOP                },
      34             :     {"rail_signal"                  , Junction::Type::RAIL_SIGNAL                },
      35             :     {"zipper"                       , Junction::Type::ZIPPER                     },
      36             :     {"rail_crossing"                , Junction::Type::RAIL_CROSSING              },
      37             :     {"traffic_light_right_on_red"   , Junction::Type::TRAFFIC_LIGHT_RIGHT_ON_RED },
      38             :     {"dead_end"                     , Junction::Type::DEAD_END                   },
      39             : 
      40             :     {"internal"                     , Junction::Type::INTERNAL                   },
      41             : 
      42             :     {"unknown"                      , Junction::Type::UNKNOWN                    },
      43             :     {"district"                     , Junction::Type::DISTRICT                   }
      44             : };
      45             : const unordered_map<Junction::Type, string> junctionType2str = utils::invertMap(str2junctionType);
      46             : 
      47             : const unordered_map<string, TrafficLightLogic::Type> str2tlType = {
      48             :     {"static"       , TrafficLightLogic::Type::STATIC},
      49             :     {"actuated"     , TrafficLightLogic::Type::ACTUATED},
      50             :     {"delay_based"  , TrafficLightLogic::Type::DELAY_BASED}
      51             : };
      52             : const unordered_map<TrafficLightLogic::Type, string> tlType2str = utils::invertMap(str2tlType);
      53             : 
      54             : const unordered_map<string, TrafficLightLogic::Phase::State> str2tlState = {
      55             :     {"r", TrafficLightLogic::Phase::State::RED},
      56             :     {"y", TrafficLightLogic::Phase::State::YELLOW_STOP},
      57             :     {"g", TrafficLightLogic::Phase::State::GREEN_NOPRIORITY},
      58             :     {"G", TrafficLightLogic::Phase::State::GREEN_PRIORITY},
      59             :     {"s", TrafficLightLogic::Phase::State::GREEN_RIGHT},
      60             :     {"u", TrafficLightLogic::Phase::State::YELLOW_START},
      61             :     {"o", TrafficLightLogic::Phase::State::OFF_YIELD},
      62             :     {"O", TrafficLightLogic::Phase::State::OFF}
      63             : };
      64             : const unordered_map<TrafficLightLogic::Phase::State, string> tlState2str = utils::invertMap(str2tlState);
      65             : 
      66             : const unordered_map<string, Connection::Direction> str2connDir = {
      67             :     {"invalid"  , Connection::Direction::INVALID},
      68             :     {"s"        , Connection::Direction::STRAIGHT},
      69             :     {"t"        , Connection::Direction::TURN},
      70             :     {"l"        , Connection::Direction::LEFT},
      71             :     {"r"        , Connection::Direction::RIGHT},
      72             :     {"L"        , Connection::Direction::PARTIALLY_LEFT},
      73             :     {"R"        , Connection::Direction::PARTIALLY_RIGHT}
      74             : };
      75             : const unordered_map<Connection::Direction, string> connDir2str = utils::invertMap(str2connDir);
      76             : 
      77             : const unordered_map<string, Connection::State> str2connState = {
      78             :     {"-", Connection::State::DEAD_END},
      79             :     {"=", Connection::State::EQUAL},
      80             :     {"m", Connection::State::MINOR_LINK},
      81             :     {"M", Connection::State::MAJOR_LINK},
      82             :     {"O", Connection::State::CONTROLLER_OFF},
      83             :     {"o", Connection::State::YELLOW_FLASHING},
      84             :     {"y", Connection::State::YELLOW_MINOR_LINK},
      85             :     {"Y", Connection::State::YELLOW_MAJOR_LINK},
      86             :     {"r", Connection::State::RED},
      87             :     {"g", Connection::State::GREEN_MINOR},
      88             :     {"G", Connection::State::GREEN_MAJOR}
      89             : };
      90             : const unordered_map<Connection::State, string> connState2str = utils::invertMap(str2connState);
      91             : // clang-format on
      92             : 
      93       23606 : Edge::Function stringify<Edge::Function>::fromString(const string &s) {
      94       23606 :     auto it = str2function.find(s);
      95       23606 :     if(it != str2function.end())
      96       23606 :         return it->second;
      97             :     else
      98             :         return Edge::Function::NORMAL;
      99             : }
     100             : 
     101       19304 : string stringify<Edge::Function>::toString(const Edge::Function &t) {
     102       19304 :     return function2str.at(t);
     103             : }
     104             : 
     105        8487 : Junction::Type stringify<Junction::Type>::fromString(const string &s) {
     106        8487 :     return str2junctionType.at(s);
     107             : }
     108             : 
     109           0 : string stringify<Junction::Type>::toString(const Junction::Type &t) {
     110           0 :     return junctionType2str.at(t);
     111             : }
     112             : 
     113        1016 : TrafficLightLogic::Type stringify<TrafficLightLogic::Type>::fromString(const string &s) {
     114        1016 :     return str2tlType.at(s);
     115             : }
     116             : 
     117           0 : string stringify<TrafficLightLogic::Type>::toString(const TrafficLightLogic::Type &t) {
     118           0 :     return tlType2str.at(t);
     119             : }
     120             : 
     121       18562 : TrafficLightLogic::Phase::State stringify<TrafficLightLogic::Phase::State>::fromString(const string &s) {
     122           0 :     return str2tlState.at(s);
     123             : }
     124             : 
     125           0 : string stringify<TrafficLightLogic::Phase::State>::toString(const TrafficLightLogic::Phase::State &t) {
     126           0 :     return tlState2str.at(t);
     127             : }
     128             : 
     129             : vector<TrafficLightLogic::Phase::State>
     130        3652 : stringify<vector<TrafficLightLogic::Phase::State>>::fromString(const string &s) {
     131        3652 :     vector<TrafficLightLogic::Phase::State> ret;
     132        3652 :     ret.reserve(s.size());
     133       22214 :     for(const char &c: s) {
     134       18562 :         ret.emplace_back(stringify<TrafficLightLogic::Phase::State>::fromString(string(1, c)));
     135             :     }
     136        3652 :     return ret;
     137             : }
     138             : 
     139           0 : string stringify<vector<TrafficLightLogic::Phase::State>>::toString(const vector<TrafficLightLogic::Phase::State> &t) {
     140           0 :     char *arr = new char[t.size() + 1];
     141           0 :     for(size_t i = 0; i < t.size(); ++i) {
     142           0 :         string s = stringify<TrafficLightLogic::Phase::State>::toString(t[i]);
     143           0 :         if(s.size() != 1)
     144           0 :             throw logic_error("Stringification of tlLogic::Phase::State should always have only 1 char");
     145           0 :         arr[i] = s[0];
     146             :     }
     147           0 :     arr[t.size()] = '\0';
     148           0 :     return string(arr);
     149             : }
     150             : 
     151       55603 : Connection::Direction stringify<Connection::Direction>::fromString(const string &s) {
     152       55603 :     return str2connDir.at(s);
     153             : }
     154             : 
     155           0 : string stringify<Connection::Direction>::toString(const Connection::Direction &t) {
     156           0 :     return connDir2str.at(t);
     157             : }
     158             : 
     159       55603 : Connection::State stringify<Connection::State>::fromString(const string &s) {
     160       55603 :     return str2connState.at(s);
     161             : }
     162             : 
     163           0 : string stringify<Connection::State>::toString(const Connection::State &t) {
     164           0 :     return connState2str.at(t);
     165             : }

Generated by: LCOV version 1.14