diff --git a/Simulation.cpp b/Simulation.cpp index 28f82698e50279270e34e6a9a40e56fe8b644085..84f4964987ba8be381dd66112eb638e1c364862f 100644 --- a/Simulation.cpp +++ b/Simulation.cpp @@ -275,7 +275,11 @@ bool Simulation::InitArgs() if (!_em->ReadEventsXml()) { Log->Write("ERROR: \tCould not initialize events handling"); } - _em->ListEvents(); + if (!_em->ReadSchedule()) { + Log->Write("ERROR: \tCould not initialize schedule handling"); + } + + _em->ListEvents(); //_building->SaveGeometry("test.sav.xml"); diff --git a/events/EventManager.cpp b/events/EventManager.cpp index 899273427301ed63a63b369033a73a0e0a3396f5..8bbff0432aa7bcda1dca649c2b6a934549314b5c 100644 --- a/events/EventManager.cpp +++ b/events/EventManager.cpp @@ -794,3 +794,105 @@ void EventManager::CreateSomeEngines() exit(0); } +bool EventManager::ReadSchedule() +{ + Log->Write("INFO: \tReading schedule"); + //get the geometry filename from the project file + TiXmlDocument doc(_projectFilename); + if (!doc.LoadFile()) { + Log->Write("ERROR: \t%s", doc.ErrorDesc()); + Log->Write("ERROR: \t could not parse the project file."); + return false; + } + + TiXmlElement* xMainNode = doc.RootElement(); + + string scheduleFile = ""; + if (xMainNode->FirstChild("schedule_file")) { + scheduleFile = _projectRootDir + + xMainNode->FirstChild("schedule_file")->FirstChild()->Value(); + Log->Write("INFO: \tevents <" + scheduleFile + ">"); + } else { + Log->Write("INFO: \tNo events found"); + return true; + } + + + Log->Write("INFO: \tParsing the schedule file"); + TiXmlDocument docSchedule(scheduleFile); + if (!docSchedule.LoadFile()) { + Log->Write("ERROR: \t%s", docSchedule.ErrorDesc()); + Log->Write("ERROR: \t could not parse the schedule file."); + return false; + } + + TiXmlElement* xRootNode = docSchedule.RootElement(); + if (!xRootNode) { + Log->Write("ERROR:\tRoot element does not exist."); + return false; + } + + if (xRootNode->ValueStr() != "JPScore") { + Log->Write("ERROR:\tRoot element value is not 'JPScore'."); + return false; + } + + // Read groups + TiXmlNode* xGroups = xRootNode->FirstChild("groups"); + if (!xGroups) { + Log->Write("ERROR:\tNo groups found."); + return false; + } + + for (TiXmlElement* e = xGroups->FirstChildElement("group"); e; + e = e->NextSiblingElement("group")){ + int id = atoi(e->Attribute("id")); + std::vector<int> member; + for (TiXmlElement* xmember = e->FirstChildElement("member"); xmember; + xmember = xmember->NextSiblingElement("member")){ + int tId = atoi(xmember->Attribute("t_id")); + member.push_back(tId); + } + groupDoor[id] = member; + } + + // Read times + TiXmlNode* xTimes = xRootNode->FirstChild("times"); + if (!xTimes) { + Log->Write("ERROR:\tNo times found."); + return false; + } + + for (TiXmlElement* e = xTimes->FirstChildElement("time"); e; + e = e->NextSiblingElement("time")) { + int id = atoi(e->Attribute("group_id")); + int closing_time = atoi(e->Attribute("closing_time")); + + std::vector<int> timeOpen; + std::vector<int> timeClose; + + for (TiXmlElement* time = e->FirstChildElement("t"); time; + time = time->NextSiblingElement("t")) { + int t = atoi(time->Attribute("t")); + timeOpen.push_back(t); + timeClose.push_back(t+closing_time); + } + + for (auto door : groupDoor[id]){ + for (auto open : timeOpen){ + Event event(door, open, "door", "open"); + _events.push_back(event); + } + + for (auto close : timeClose){ + Event event(door, close, "door", "temp_close"); + _events.push_back(event); + } + } + } + + Log->Write("INFO: \tSchedule initialized"); + + return true; +} + diff --git a/events/EventManager.h b/events/EventManager.h index c43d86a62fa46c89101d574a1e024c9707872c13..c3366d26ea007b1ed08b32a2b659cc9262605629 100644 --- a/events/EventManager.h +++ b/events/EventManager.h @@ -67,6 +67,10 @@ public: */ void ReadEventsTxt(double time); + /** + * Reads the schedule file + */ + bool ReadSchedule(); //process the event using the current time stamp //from the pedestrian class @@ -163,4 +167,5 @@ private: std::mt19937 _rdGenerator; std::uniform_real_distribution<double> _rdDistribution; // std::uniform_real_distribution<double> d(0, 1); + std::map<int, std::vector<int>> groupDoor; };