EventManager.cpp 5.9 KB
Newer Older
1 2 3 4
#include <cstdio>
#include <cstdlib> 
#include <iostream>
#include <string>
5
#include <math.h>
6
#include "EventManager.h"
7 8
#include "../tinyxml/tinyxml.h"
#include "../IO/OutputHandler.h"
9 10
#include "../routing/RoutingEngine.h"
#include "../pedestrian/Pedestrian.h"
11 12 13

using namespace std;

14 15 16 17
/*******************
 Konstruktoren
 ******************/

18
EventManager::EventManager(Building *_b){
19
    _event_times=vector<double>();
20 21 22
    _event_types=vector<string>();
    _event_states=vector<string>();
    _event_ids=vector<int>();
23
    _projectFilename = "";
24
    _building = _b;
25
    _deltaT=NULL;
26
}
27

28 29 30 31 32 33
/*******************
 Dateien einlesen
 ******************/
void EventManager::SetProjectFilename(const std::string &filename){
    _projectFilename=filename;
}
34

35 36 37 38 39 40 41 42 43 44 45 46 47
void EventManager::SetProjectRootDir(const std::string &filename){
    _projectRootDir= filename;
}

void EventManager::readEventsXml(){
    printf("INFO: \tReading events\n ");
    //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");
        printf("ERROR: \t%s\nRROR: \t could not parse the project file\n",doc.ErrorDesc());
        exit(EXIT_FAILURE);
48 49
    }

50 51 52 53 54 55 56 57
    //Log->Write("INFO: \tParsing the event file");
    printf("INFO: \tParsing the event file\n");
    TiXmlElement* xMainNode = doc.RootElement();
    string eventfile="";
    if(xMainNode->FirstChild("events")){
        eventfile=_projectRootDir+xMainNode->FirstChild("events")->FirstChild()->Value();
        //Log->Write("INFO: \tevents <"+eventfile+">");
        //printf("INFO: \t events <%s>\n", eventfile);
58
    }
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    TiXmlDocument docEvent(eventfile);
    if(!docEvent.LoadFile()){
        //Log->Write("EROOR: \t%s",docEvent.ErrorDesc());
        //Log->Write("ERROR: \t could not parse the event file");
        printf("EROOR: \t%s\nERROR: \t could not parse the event file\n",docEvent.ErrorDesc());
        exit(EXIT_FAILURE);
    }

    TiXmlElement* xRootNode = docEvent.RootElement();
    if(!xRootNode){
        //Log->Write("ERROR:\tRoot element does not exist.");
        printf("ERROR:\tRoot element does not exist.\n");
        exit(EXIT_FAILURE);
    }

    if( xRootNode->ValueStr () != "JPScore" ) {
        //Log->Write("ERROR:\tRoot element value is not 'geometry'.");
        printf("ERROR:\tRoot element value is not 'JPScore'.");
        exit(EXIT_FAILURE);
    }

    TiXmlNode* xEvents = xRootNode->FirstChild("events");
82
    if(!xEvents){
83 84 85
        //Log->Write("ERROR:\tNo events found.");
        printf("ERROR:\tNo events found.\n");
        exit(EXIT_FAILURE);
86 87 88
    }
    
    for(TiXmlElement* e = xEvents->FirstChildElement("event"); e; e= e->NextSiblingElement("event")){
89
        _event_times.push_back(atoi(e->Attribute("time")));
90 91 92
        _event_types.push_back(e->Attribute("type"));
        _event_states.push_back(e->Attribute("state"));
        _event_ids.push_back(atoi(e->Attribute("id")));
93 94 95 96 97
    }
    printf("INFO: \tEvents were read\n");
}

void EventManager::listEvents(){
98
    if(_event_times.size()==0){
99 100 101 102 103 104 105 106
        //Log->Write("INFO: \tNo events in the events.xml");
        printf("INFO: \tNo events in the events.xml\n");
    }
    else{
        int i;
        for(i=0;i<_event_times.size();i++){
           // Log->Write("INFO: \tEvent "+(i+1)+" after "+_event_times[i]+" sec.: "+_event_values[i]);

107
            cout <<"INFO: \tEvent "<<i+1<<" after "<< _event_times[i] << "sec.: "<< _event_types[i] << " " << _event_states[i] << " " << _event_ids[i] << endl;
108 109 110 111 112 113 114 115 116
        }
    }

}

/***********
 Update
 **********/

117 118 119 120 121 122 123
void EventManager::Update_Events(double time, double d){
    //1. pruefen ob in _event_times der zeitstempel time zu finden ist. Wenn ja zu 2. sonst zu 3.
    //2. Event aus _event_times und _event_values verarbeiten (Tuere schliessen/oeffnen, neues Routing)
    //   Dann pruefen, ob eine neue Zeile in der .txt Datei steht
    //3. .txt Datei auf neue Zeilen pruefen. Wenn es neue gibt diese Events verarbeiten ( Tuere schliessen/oeffnen,
    //   neues Routing) ansonsten fertig
    int i;
124
    _deltaT=d;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    for(i=0;i<_event_times.size();i++){
        if(fabs(_event_times[i]-time)<0.0000001){
            //Event findet statt
            printf("%f: Event zum Zeitpunkt %f findet statt: ",time,_event_times[i]);
            cout << _event_types[i] << " " << _event_ids[i] << " " << _event_states[i] << endl;
            if(_event_states[i].compare("close")==0){
                closeDoor(_event_ids[i]);
            }
            else{
                openDoor(_event_ids[i]);
            }
        }
    }
}

/***************
 Eventhandling
 **************/
void EventManager::closeDoor(int id){
    //pruefen ob entsprechende Tuer schon zu ist, wenn nicht dann schliessen und neues Routing berechnen
    Transition *t=_building->GetTransition(id);
    if(t->IsOpen()){
        t->Close();
        cout << "Door " << id << " closed." << endl;
149
        changeRouting(id,"close");
150 151 152 153 154 155 156 157 158 159
    }

}

void EventManager::openDoor(int id){
    //pruefen ob entsprechende Tuer schon offen ist, wenn nicht dann oeffnen und neues Routing berechnen
    Transition *t=_building->GetTransition(id);
    if(!t->IsOpen()){
        t->Open();
        cout << "Door " << id << " opened." << endl;
160 161 162 163
        changeRouting(id,"open");
    }
    else{
        cout << "Door " << id << " is already open yet." << endl;
164 165 166
    }
}

167 168 169 170 171 172 173 174 175 176 177 178 179 180
void EventManager::changeRouting(int id, string state){
    RoutingEngine* routingEngine= _building->GetRoutingEngine();
    routingEngine->Init(_building);
    _building->InitPhiAllPeds(_deltaT);
    vector<Pedestrian*> _allPedestrians=_building->GetAllPedestrians();
    cout << "alle Pedestrians" <<endl;
    unsigned int nSize = _allPedestrians.size();
    cout << nSize << endl;
    for (int p = 0; p < nSize; ++p) {
        _allPedestrians[p]->ClearMentalMap();

        cout << p << ": " << _allPedestrians[p]->FindRoute() << endl;
    }
    cout << "fertig" << endl;
181

182
}