EventManager.cpp 19.1 KB
Newer Older
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1 2 3
/**
 * \file        EventManager.cpp
 * \date        Jul 4, 2014
4 5
 * \version     v0.7
 * \copyright   <2009-2015> Forschungszentrum Jülich GmbH. All rights reserved.
Ulrich Kemloh's avatar
Ulrich Kemloh committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * \section License
 * This file is part of JuPedSim.
 *
 * JuPedSim is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * JuPedSim is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JuPedSim. If not, see <http://www.gnu.org/licenses/>.
 *
 * \section Description
 *
 *
 **/


#include <string>
#include <cstdio>
#include <cstdlib>
#include <iostream>
33
#include <algorithm>
Ulrich Kemloh's avatar
Ulrich Kemloh committed
34 35 36 37
#include <fstream>
#include <vector>
#include <math.h>
#include <stdio.h>
38
#include "../pedestrian/Pedestrian.h"
Ulrich Kemloh's avatar
Ulrich Kemloh committed
39
#include "../pedestrian/Knowledge.h"
40
#include "../mpi/LCGrid.h"
Ulrich Kemloh's avatar
Ulrich Kemloh committed
41 42 43 44 45 46 47 48
#include "../geometry/Building.h"
#include "../geometry/SubRoom.h"
#include "../geometry/Transition.h"
#include "../geometry/Point.h"
#include "../tinyxml/tinyxml.h"
#include "../IO/OutputHandler.h"
#include "../IO/IODispatcher.h"
#include "../routing/RoutingEngine.h"
49 50 51 52 53 54
#include "../routing/GlobalRouter.h"
#include "../routing/QuickestPathRouter.h"
#include "../routing/MeshRouter.h"
#include "../routing/DummyRouter.h"
#include "../routing/SafestPathRouter.h"
#include "../routing/CognitiveMapRouter.h"
55
#include "EventManager.h"
Ulrich Kemloh's avatar
Ulrich Kemloh committed
56
#include "Event.h"
57

Ulrich Kemloh's avatar
Ulrich Kemloh committed
58 59 60
using std::map;
using std::cout;
using std::endl;
61

62

Ulrich Kemloh's avatar
Ulrich Kemloh committed
63 64 65 66 67 68
EventManager::EventManager(Building *_b)
{
     _building = _b;
     _eventCounter = 0;
     _dynamic = false;
     _lastUpdateTime = 0;
69 70
     _projectFilename=_building->GetProjectFilename();
     _projectRootDir=_building->GetProjectRootDir();
71 72
     _updateFrequency =1 ;//seconds
     _updateRadius =2;//meters
Ulrich Kemloh's avatar
Ulrich Kemloh committed
73

74
     _file = fopen("../events/events.txt", "r");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
75
     if (!_file) {
76 77
          Log->Write("INFO:\tFiles 'events.txt' missing. "
                    "Realtime interaction with the simulation not possible.");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
78
     } else {
79
          Log->Write("INFO:\tFile 'events.txt' will be monitored for new events.");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
80 81
          _dynamic = true;
     }
82

Ulrich Kemloh's avatar
Ulrich Kemloh committed
83 84 85 86 87
     //generate random number between 0 and 1 uniformly distributed
     _rdDistribution = std::uniform_real_distribution<double> (0,1);
     std::random_device rd;
     _rdGenerator=std::mt19937(rd());

88 89
     //save the first graph
     CreateRoutingEngine(_b, true);
90 91
}

92 93 94 95
EventManager::~EventManager()
{
     if(_file)
          fclose(_file);
96 97

     _eventEngineStorage.clear();
98 99
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
100
bool EventManager::ReadEventsXml()
Ulrich Kemloh's avatar
Ulrich Kemloh committed
101
{
102
     Log->Write("INFO: \tLooking for pre-defined events in other files");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
103 104
     //get the geometry filename from the project file
     TiXmlDocument doc(_projectFilename);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
105
     if (!doc.LoadFile()) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
106 107
          Log->Write("ERROR: \t%s", doc.ErrorDesc());
          Log->Write("ERROR: \t could not parse the project file.");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
108
          return false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
109 110 111
     }

     TiXmlElement* xMainNode = doc.RootElement();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
112
     string eventfile = "";
Ulrich Kemloh's avatar
Ulrich Kemloh committed
113
     if (xMainNode->FirstChild("events_file")) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
114
          eventfile = _projectRootDir
115
                    + xMainNode->FirstChild("events_file")->FirstChild()->Value();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
116 117
          Log->Write("INFO: \tevents <" + eventfile + ">");
     } else {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
118
          Log->Write("INFO: \tNo events found");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
119
          return true;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
120 121 122 123
     }

     Log->Write("INFO: \tParsing the event file");
     TiXmlDocument docEvent(eventfile);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
124 125
     if (!docEvent.LoadFile()) {
          Log->Write("ERROR: \t%s", docEvent.ErrorDesc());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
126
          Log->Write("ERROR: \t could not parse the event file.");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
127
          return false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
128 129 130
     }

     TiXmlElement* xRootNode = docEvent.RootElement();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
131
     if (!xRootNode) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
132
          Log->Write("ERROR:\tRoot element does not exist.");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
133
          return false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
134 135
     }

Ulrich Kemloh's avatar
Ulrich Kemloh committed
136
     if (xRootNode->ValueStr() != "JPScore") {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
137
          Log->Write("ERROR:\tRoot element value is not 'JPScore'.");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
138
          return false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
139 140 141
     }

     TiXmlNode* xEvents = xRootNode->FirstChild("events");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
142
     if (!xEvents) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
143
          Log->Write("ERROR:\tNo events found.");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
144
          return false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
145
     }
146
     _updateFrequency = xmltoi(xEvents->ToElement()->Attribute("update_frequency"), 1);
147 148 149
     _updateRadius = xmltoi(xEvents->ToElement()->Attribute("update_radius"), 2);

     //Pedestrian::SetColorMode(BY_SPOTLIGHT);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
150

Ulrich Kemloh's avatar
Ulrich Kemloh committed
151 152
     for (TiXmlElement* e = xEvents->FirstChildElement("event"); e;
               e = e->NextSiblingElement("event")) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
153 154 155 156 157 158

          int id = atoi(e->Attribute("id"));
          double zeit = atoi(e->Attribute("time"));
          string state (e->Attribute("state"));
          string type (e->Attribute("type"));
          _events.push_back(Event(id,zeit,type,state));
Ulrich Kemloh's avatar
Ulrich Kemloh committed
159
     }
160
     Log->Write("INFO: \tEvents were initialized");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
161
     return true;
162 163
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
164
void EventManager::ListEvents()
165
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
166
     for(const auto& event: _events)
167
     {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
168
          Log->Write("INFO:\t " + event.GetDescription());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
169
     }
170 171
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
172 173 174 175 176 177 178 179 180 181
void EventManager::ReadEventsTxt(double time)
{
     rewind(_file);
     char cstring[256];
     int lines = 0;
     do {
          if (fgets(cstring, 30, _file) == NULL) {
               Log->Write("WARNING: \tCould not read the event file");
               return;
          }
182
          if (cstring[0] != '#') {// skip comments
Ulrich Kemloh's avatar
Ulrich Kemloh committed
183 184
               lines++;
               if (lines > _eventCounter) {
185
                    Log->Write("INFO:\tEvent: after %.2f sec: ", time);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
186 187 188 189 190
                    GetEvent(cstring);
                    _eventCounter++;
               }
          }
     } while (feof(_file) == 0);
191 192
}

193 194
bool EventManager::UpdateAgentKnowledge(Building* _b)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
195
     //#pragma omp parallel
196 197 198 199
     for(auto&& ped:_b->GetAllPedestrians())
     {
          for (auto&& door: _b->GetAllTransitions())
          {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
200
               if(door.second->DistTo(ped->GetPos())<0.5)//distance to door to register its state
201 202 203
               {
                    //actualize the information about the newly closed door
                    if(door.second->IsOpen()==false)
204
                    {
205
                         ped->AddKnownClosedDoor(door.first, Pedestrian::GetGlobalTime());
206 207
                         ped->SetNewEventFlag(true);

208
                    }
209 210 211 212
               }
          }
     }

213 214 215 216
     //collect the peds that are allowed to forward the information.
     vector<Pedestrian*> informant;
     for(auto&& ped:_b->GetAllPedestrians())
     {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
217 218
          if (ped->GetNewEventFlag())
               informant.push_back(ped);
219
     }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
220

221 222

     for(auto&& ped1:informant)
223 224 225 226 227
     {
          vector<Pedestrian*> neighbourhood;
          _b->GetGrid()->GetNeighbourhood(ped1,neighbourhood);
          for(auto&& ped2:neighbourhood)
          {
228
               if( (ped1->GetPos()-ped2->GetPos()).Norm()<_updateRadius)
229 230
               {
                    //maybe same room and subroom ?
231 232
                    vector<SubRoom*> empty;
                    if(_b->IsVisible(ped1->GetPos(),ped2->GetPos(),empty))
Ulrich Kemloh's avatar
Ulrich Kemloh committed
233
                    {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
234 235 236 237 238 239
                         //SynchronizeKnowledge(ped1, ped2);  //ped1->SetSpotlight(true);
                         if(MergeKnowledge(ped1, ped2))
                         {
                              //p2 is now an informant
                              ped2->SetNewEventFlag(true);
                         }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
240
                    }
241 242 243 244
               }
          }
     }

Ulrich Kemloh's avatar
Ulrich Kemloh committed
245 246 247
     //TODO: what happen when they all have the new event flag ? reset maybe?
     if(informant.size()==_b->GetAllPedestrians().size())
     {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
248

Ulrich Kemloh's avatar
Ulrich Kemloh committed
249 250 251 252
          for(auto&& ped:_b->GetAllPedestrians())
               ped->SetNewEventFlag(false);
     }

253
     // information speed to fast
Ulrich Kemloh's avatar
Ulrich Kemloh committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267
     //     for(auto&& ped1:_b->GetAllPedestrians())
     //     {
     //          vector<Pedestrian*> neighbourhood;
     //          _b->GetGrid()->GetNeighbourhood(ped1,neighbourhood);
     //          for(auto&& ped2:neighbourhood)
     //          {
     //               if( (ped1->GetPos()-ped2->GetPos()).Norm()<_updateRadius)
     //               {
     //                    //maybe same room and subroom ?
     //                    if(_b->IsVisible(ped1->GetPos(),ped2->GetPos()))
     //                    MergeKnowledge(ped1, ped2);  //ped1->SetSpotlight(true);
     //               }
     //          }
     //     }
268

269 270 271 272 273 274 275 276 277
     //update the routers based on the configurations
     //#pragma omp parallel
     for(auto&& ped:_b->GetAllPedestrians())
     {
          if(UpdateRoute(ped)==false)
          {
               //Clear the memory and attempt to reroute
               //this can happen if all doors are known to be closed
               ped->ClearKnowledge();
278
               //Log->Write("ERROR: \t clearing ped knowledge");
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
               if(UpdateRoute(ped)==false)
               {
                    Log->Write("ERROR: \t cannot reroute the pedestrian. unknown problem");
                    //return false;
                    exit(EXIT_FAILURE);
               }
          }
     }
     return true;
}

bool EventManager::UpdateRoute(Pedestrian* ped)
{
     //create the key as string.
     //map are sorted by default
294
     string key= ped->GetKnowledgeAsString();
295 296 297 298 299 300 301 302 303 304 305
     //get the router engine corresponding to the actual configuration
     bool status=true;
     if (_eventEngineStorage.count(key)>0)
     {
          RoutingEngine* engine=_eventEngineStorage[key];
          //retrieve the old strategy
          RoutingStrategy strategy=ped->GetRouter()->GetStrategy();
          //retrieve the new router
          Router*rout =engine->GetRouter(strategy);
          //check for validity
          ped->SetRouter(rout);
306 307
          //clear all previous routes
          ped->ClearMentalMap();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
308
          //ped->ClearKnowledge();
309 310 311 312 313
          //overwrite/update the pedestrian router
          if(!rout) status= false;
     }
     else
     {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
314
          //Log->Write("WARNING: \t unknown configuration <%s>", key.c_str());
315
          //Log->Write("WARNING: \t  [%d] router available", _eventEngineStorage.size());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
316 317
          //Log->Write("       : \t trying to create");
          //CreateRoutingEngine(_building);
318 319 320 321 322
          status= false;
     }
     return status;
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
323
bool EventManager::SynchronizeKnowledge(Pedestrian* p1, Pedestrian* p2)
324
{
325 326
     auto const & old_info1 = p1->GetKnownledge();
     auto const & old_info2 = p2->GetKnownledge();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
327
     map<int, Knowledge> merge_info;
328 329 330 331 332 333 334

     //collect the most recent knowledge
     for (auto&& info1 : old_info1)
     {
          merge_info[info1.first] = info1.second;
     }

335
     for (auto&& info2 : old_info2)
336 337
     {
          //update infos according to a newest time
338
          if (merge_info.count(info2.first) > 0)
339
          {
340
               if (info2.second.GetTime() > merge_info[info2.first].GetTime())
341
               {
342
                    merge_info[info2.first] = info2.second;
343 344
               }
          }
345
          else //the info was not present, just add
346
          {
347
               merge_info[info2.first] = info2.second;
348 349 350 351 352 353 354 355
          }
     }

     //synchronize the knowledge
     p1->ClearKnowledge();
     p2->ClearKnowledge();
     for (auto&& info : merge_info)
     {
356 357
          p1->AddKnownClosedDoor(info.first, info.second.GetTime());
          p2->AddKnownClosedDoor(info.first, info.second.GetTime());
358
     }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
     return true;
}

bool EventManager::MergeKnowledge(Pedestrian* p1, Pedestrian* p2)
{
     auto const & old_info1 = p1->GetKnownledge();
        auto const & old_info2 = p2->GetKnownledge();
        map<int, Knowledge> merge_info;

        //collect the most recent knowledge
        for (auto&& info1 : old_info1)
        {
             merge_info[info1.first] = info1.second;
        }

        for (auto&& info2 : old_info2)
        {
             //update infos according to a newest time
             if (merge_info.count(info2.first) > 0)
             {
                  if (info2.second.GetTime() > merge_info[info2.first].GetTime())
                  {
                       merge_info[info2.first] = info2.second;
                  }
             }
             else //the info was not present, just add
             {
                  merge_info[info2.first] = info2.second;
             }
        }

        //synchronize the knowledge
        //accept the information with a certain probability
        //cout<<_rdDistribution(_rdGenerator)<<endl;
        if(_rdDistribution(_rdGenerator)< (1-p1->GetRiskTolerance()))
        {
             //p1->ClearKnowledge();
             p2->ClearKnowledge();
             for (auto&& info : merge_info)
             {
                  //p1->AddKnownClosedDoor(info.first, info.second.GetTime());
                  p2->AddKnownClosedDoor(info.first, info.second.GetTime());
             }
             //p2->SetSpotlight(false);
             return true;
        }
        else
        {
             cout<<"refusing the information:"<<p2->GetID()<<endl;
             //Pedestrian::SetColorMode(BY_SPOTLIGHT);
             //p2->SetSpotlight(true);
             //exit(0);
             return false;
        }
413 414 415 416
}

void EventManager::ProcessEvent()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
417
     if (_events.size() == 0) return;
418 419 420 421

     int current_time = Pedestrian::GetGlobalTime();

     if ( (current_time != _lastUpdateTime) &&
422
               ((current_time % _updateFrequency) == 0))
423 424 425 426 427 428
     {
          //update knowledge about closed doors
          //share the information between the pedestrians
          UpdateAgentKnowledge(_building);
          //actualize based on the new knowledge
          _lastUpdateTime = current_time;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
429
          //cout<<"updating..."<<current_time<<endl<<endl;
430 431 432 433 434
     }

     //update the building state
     // the time is needed as double
     double current_time_d = Pedestrian::GetGlobalTime();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
435 436

     for(const auto& event: _events)
437
     {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
438
          if (fabs(event.GetTime() - current_time_d) < J_EPS_EVENT) {
439
               //Event with current time stamp detected
440
               Log->Write("INFO:\tEvent: after %.2f sec: ", current_time_d);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
441 442
               if (event.GetState().compare("close") == 0) {
                    CloseDoor(event.GetId());
443
               } else {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
444
                    OpenDoor(event.GetId());
445 446
               }
          }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
447

448 449 450 451 452 453 454
     }

     if (_dynamic)
          ReadEventsTxt(current_time);
}


455

Ulrich Kemloh's avatar
Ulrich Kemloh committed
456
//close the door if it was open and relaunch the routing procedure
Ulrich Kemloh's avatar
Ulrich Kemloh committed
457 458 459
void EventManager::CloseDoor(int id)
{
     Transition *t = _building->GetTransition(id);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
460 461
     if (t->IsOpen())
     {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
462
          t->Close();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
463
          Log->Write("INFO:\tClosing door %d ", id);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
464 465 466 467 468
          //Create and save a graph corresponding to the actual state of the building.
          if(CreateRoutingEngine(_building)==false)
          {
               Log->Write("ERROR: \tcannot create a routing engine with the new event");
          }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
469
     } else {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
470
          Log->Write("WARNING: \tdoor %d is already close", id);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
471
     }
472 473 474

}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
475
//open the door if it was open and relaunch the routing procedure
Ulrich Kemloh's avatar
Ulrich Kemloh committed
476 477 478
void EventManager::OpenDoor(int id)
{
     Transition *t = _building->GetTransition(id);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
479 480
     if (!t->IsOpen())
     {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
481
          t->Open();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
482
          Log->Write("INFO:\tOpening door %d ", id);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
483 484 485 486 487
          //Create and save a graph corresponding to the actual state of the building.
          if(CreateRoutingEngine(_building)==false)
          {
               Log->Write("ERROR: \tcannot create a routing engine with the new event");
          }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
488
     } else {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
489
          Log->Write("WARNING: \tdoor %d is already open", id);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
490
     }
491 492
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
void EventManager::GetEvent(char* c)
{
     int split = 0;
     string type = "";
     string id = "";
     string state = "";
     for (int i = 0; i < 20; i++) {
          if (!c[i]) {
               break;
          } else if (c[i] == ' ') {
               split++;
          } else if (c[i] == '\n') {

          } else {
               if (split == 0) {
                    type += c[i];
               } else if (split == 1) {
                    id += c[i];
               } else if (split == 2) {
                    state += c[i];
               }
          }
     }
     if (state.compare("close") == 0) {
          CloseDoor(atoi(id.c_str()));
     } else {
          OpenDoor(atoi(id.c_str()));
     }
521
}
522 523 524 525

bool EventManager::CreateRoutingEngine(Building* _b, int first_engine)
{
     std::vector<int> closed_doors;
526
     closed_doors.clear();
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

     for(auto&& t:_b->GetAllTransitions())
     {
          if(t.second->IsOpen()==false)
               closed_doors.push_back(t.second->GetID());
     }
     std::sort(closed_doors.begin(), closed_doors.end());

     //create the key as string.
     string key="";
     for(int door:closed_doors)
     {
          if(key.empty())
               key.append(std::to_string(door));
          else
               key.append(":"+std::to_string(door));
     }

     //the first (default) engine was created in the simulation
     // collect the defined routers
     if(first_engine)
     {
          RoutingEngine* engine=_b->GetRoutingEngine();
          _eventEngineStorage[key]=engine;

          for(auto&& rout: engine->GetAvailableRouters())
          {
               _availableRouters.push_back(rout->GetStrategy());
          }
          Log->Write("INFO: \tAdding a new routing Engine with the key: "+key+"\n");
          return true;
     }

     // the engine was not created
     // create a new one with the actual configuration
     if (_eventEngineStorage.count(key)==0)
     {
          //std::shared_ptr<RoutingEngine> engine = std::shared_ptr<RoutingEngine>(new RoutingEngine());
          //engine.get()->Init(_b);
          //_eventEngineStorage[key]=engine.get();

          //populate the engine with the routers defined in the ini file
          //and initialize
          RoutingEngine* engine= new RoutingEngine();
          for(auto&& rout:_availableRouters)
          {
               engine->AddRouter(CreateRouter(rout));
          }

          if(engine->Init(_b)==false)
               return false;

          //save the configuration
          _eventEngineStorage[key]=engine;
          Log->Write("INFO: \tAdding a new routing Engine with the key: "+key+"\n");
     }
     else
     {
          Log->Write("INFO: \tA routing already exits with the key: "+key+"\n");
     }

     return true;
}

Router * EventManager::CreateRouter(const RoutingStrategy& strategy)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
593
     Router * rout=nullptr;
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

     switch(strategy)
     {
     case ROUTING_LOCAL_SHORTEST:
          rout = new GlobalRouter(ROUTING_LOCAL_SHORTEST, ROUTING_LOCAL_SHORTEST);
          break;

     case ROUTING_GLOBAL_SHORTEST:
          rout = new GlobalRouter(ROUTING_GLOBAL_SHORTEST, ROUTING_GLOBAL_SHORTEST);
          break;

     case ROUTING_QUICKEST:
          rout = new QuickestPathRouter(ROUTING_QUICKEST, ROUTING_QUICKEST);
          break;

     case ROUTING_NAV_MESH:
          rout = new MeshRouter(ROUTING_NAV_MESH, ROUTING_NAV_MESH);
          break;

     case ROUTING_DUMMY:
          rout = new DummyRouter(ROUTING_DUMMY, ROUTING_DUMMY);
          break;

     case ROUTING_SAFEST:
          rout = new SafestPathRouter(ROUTING_SAFEST, ROUTING_SAFEST);
          break;

     case ROUTING_COGNITIVEMAP:
          rout = new CognitiveMapRouter(ROUTING_COGNITIVEMAP, ROUTING_COGNITIVEMAP);
          break;

     default:
          Log->Write("ERROR: \twrong value for routing strategy [%d]!!!\n", strategy );
627
          exit(EXIT_FAILURE);
628 629 630 631 632
          break;

     }
     return rout;
}
633 634