#include <config.h>
#include <string>
#include <utils/options/OptionsCont.h>
#include <utils/common/MsgHandler.h>
#include <utils/common/StringTokenizer.h>
#include <utils/common/UtilExceptions.h>
#include <utils/common/StringUtils.h>
#include <utils/common/ToString.h>
#include <utils/xml/SUMOSAXHandler.h>
#include <utils/xml/SUMOXMLDefinitions.h>
#include "RODFDetectorHandler.h"
#include "RODFNet.h"
RODFDetectorHandler::RODFDetectorHandler(RODFNet* optNet, bool ignoreErrors, RODFDetectorCon& con,
const std::string& file)
: SUMOSAXHandler(file),
myNet(optNet), myIgnoreErrors(ignoreErrors), myContainer(con) {}
RODFDetectorHandler::~RODFDetectorHandler() {}
void
RODFDetectorHandler::myStartElement(int element,
const SUMOSAXAttributes& attrs) {
if (element == SUMO_TAG_DETECTOR_DEFINITION || element == SUMO_TAG_E1DETECTOR || element == SUMO_TAG_INDUCTION_LOOP) {
try {
bool ok = true;
std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
if (!ok) {
throw ProcessError();
}
std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, id.c_str(), ok);
if (!ok) {
throw ProcessError();
}
ROEdge* edge = myNet->getEdge(SUMOXMLDefinitions::getEdgeIDFromLane(lane));
int laneIndex = SUMOXMLDefinitions::getIndexFromLane(lane);
if (edge == nullptr || laneIndex >= edge->getNumLanes()) {
throw ProcessError("Unknown lane '" + lane + "' for detector '" + id + "' in '" + getFileName() + "'.");
}
double pos = attrs.get<double>(SUMO_ATTR_POSITION, id.c_str(), ok);
std::string mml_type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
if (!ok) {
throw ProcessError();
}
RODFDetectorType type = TYPE_NOT_DEFINED;
if (mml_type == "between") {
type = BETWEEN_DETECTOR;
} else if (mml_type == "source" || mml_type == "highway_source") {
type = SOURCE_DETECTOR;
} else if (mml_type == "sink") {
type = SINK_DETECTOR;
}
RODFDetector* detector = new RODFDetector(id, lane, pos, type);
if (!myContainer.addDetector(detector)) {
delete detector;
throw ProcessError(TLF("Could not add detector '%' (probably the id is already used).", id));
}
} catch (ProcessError& e) {
if (myIgnoreErrors) {
WRITE_WARNING(e.what());
} else {
throw;
}
}
}
}