MSNet.h

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // The simulated network and simulation perfomer
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 #ifndef MSNet_h
00020 #define MSNet_h
00021 
00022 
00023 // ===========================================================================
00024 // included modules
00025 // ===========================================================================
00026 #ifdef _MSC_VER
00027 #include <windows_config.h>
00028 #else
00029 #include <config.h>
00030 #endif
00031 
00032 #include <typeinfo>
00033 #include <vector>
00034 #include <map>
00035 #include <string>
00036 #include <fstream>
00037 #include <iostream>
00038 #include <cmath>
00039 #include <iomanip>
00040 #include "MSVehicleControl.h"
00041 #include "MSPersonControl.h"
00042 #include "MSEventControl.h"
00043 #include <utils/geom/Boundary.h>
00044 #include <utils/geom/Position2D.h>
00045 #include <utils/common/SUMOTime.h>
00046 #include <microsim/trigger/MSBusStop.h>
00047 #include <utils/common/UtilExceptions.h>
00048 
00049 #ifdef _MESSAGES
00050 #include <utils/common/NamedObjectCont.h>
00051 #endif
00052 
00053 // ===========================================================================
00054 // class declarations
00055 // ===========================================================================
00056 class MSEdge;
00057 class MSEdgeControl;
00058 class MSJunctionControl;
00059 class MSEmitControl;
00060 class MSRouteLoaderControl;
00061 class MSVehicle;
00062 class MSRoute;
00063 class MSLane;
00064 class MSTLLogicControl;
00065 class MSTrigger;
00066 class MSDetectorControl;
00067 class ShapeContainer;
00068 class BinaryInputDevice;
00069 class MSRouteLoader;
00070 class MSEdgeWeightsStorage;
00071 class SUMOVehicle;
00072 #ifdef _MESSAGES
00073 class MSMessageEmitter;
00074 #endif
00075 
00076 
00077 // ===========================================================================
00078 // class definitions
00079 // ===========================================================================
00084 class MSNet {
00085 public:
00089     enum SimulationState {
00091         SIMSTATE_RUNNING,
00093         SIMSTATE_END_STEP_REACHED,
00095         SIMSTATE_NO_FURTHER_VEHICLES,
00097         SIMSTATE_CONNECTION_CLOSED,
00099         SIMSTATE_ERROR_IN_SIM,
00101         SIMSTATE_TOO_MANY_VEHICLES
00102     };
00103 
00104 
00105 public:
00110     static MSNet* getInstance() throw(ProcessError);
00111 
00112 
00127     MSNet(MSVehicleControl *vc, MSEventControl *beginOfTimestepEvents,
00128           MSEventControl *endOfTimestepEvents, MSEventControl *emissionEvents,
00129           ShapeContainer *shapeCont=0) throw(ProcessError);
00130 
00131 
00133     virtual ~MSNet() throw();
00134 
00135 
00147     void closeBuilding(MSEdgeControl *edges, MSJunctionControl *junctions,
00148                        MSRouteLoaderControl *routeLoaders, MSTLLogicControl *tlc,
00149                        std::vector<SUMOTime> stateDumpTimes, std::vector<std::string> stateDumpFiles) throw();
00150 
00151 
00155     static void clearAll();
00156 
00157 
00165     int simulate(SUMOTime start, SUMOTime stop);
00166 
00167 
00171     void simulationStep();
00172 
00173 
00181     void closeSimulation(SUMOTime start);
00182 
00183 
00189     SimulationState simulationState(SUMOTime stopTime) const throw();
00190 
00191 
00195     static std::string getStateMessage(SimulationState state) throw();
00196 
00197 
00201     SUMOTime getCurrentTimeStep() const;
00202 
00203 
00207     void writeOutput();
00208 
00209 
00213     bool logSimulationDuration() const throw();
00214 
00215 
00217 
00218 
00223     void preSimStepOutput() const throw();
00224 
00225 
00230     void postSimStepOutput() const throw();
00231     //}
00232 
00233 
00234 
00235 #ifdef HAVE_MESOSIM
00238 
00239 
00243     void saveState(std::ostream &os) throw();
00244 
00245 
00250     unsigned int loadState(BinaryInputDevice &bis) throw();
00252 #endif
00253 
00256 
00262     MSVehicleControl &getVehicleControl() throw() {
00263         return *myVehicleControl;
00264     }
00265 
00266 
00275     MSPersonControl &getPersonControl() throw();
00276 
00277 
00283     MSEdgeControl &getEdgeControl() throw() {
00284         return *myEdges;
00285     }
00286 
00287 
00293     MSEmitControl &getEmitControl() throw() {
00294         return *myEmitter;
00295     }
00296 
00297 
00303     MSDetectorControl &getDetectorControl() throw() {
00304         return *myDetectorControl;
00305     }
00306 
00307 
00313     MSTLLogicControl &getTLSControl() throw() {
00314         return *myLogics;
00315     }
00316 
00317 
00323     MSJunctionControl &getJunctionControl() throw() {
00324         return *myJunctions;
00325     }
00326 
00327 
00333     MSEventControl &getBeginOfTimestepEvents() throw() {
00334         return *myBeginOfTimestepEvents;
00335     }
00336 
00337 
00343     MSEventControl &getEndOfTimestepEvents() throw() {
00344         return *myEndOfTimestepEvents;
00345     }
00346 
00347 
00353     MSEventControl &getEmissionEvents() throw() {
00354         return *myEmissionEvents;
00355     }
00356 
00357 
00363     ShapeContainer &getShapeContainer() throw() {
00364         return *myShapeContainer;
00365     }
00366 
00367 
00373     MSEdgeWeightsStorage &getWeightsStorage() throw();
00375 
00376 
00377 
00380 
00392     bool addBusStop(MSBusStop* busStop) throw() {
00393         if (myBusStopDict.find(busStop->getID()) == myBusStopDict.end()) {
00394             myBusStopDict[busStop->getID()] = busStop;
00395             return true;
00396         }
00397         return false;
00398     }
00399 
00400 
00405     MSBusStop *getBusStop(const std::string &id) throw() {
00406         BusStopDictType::iterator it = myBusStopDict.find(id);
00407         if (it == myBusStopDict.end()) {
00408             return 0;
00409         }
00410         return it->second;
00411     }
00412 
00413 
00419     std::string getBusStopID(const MSLane* lane, const SUMOReal pos) throw() {
00420         for (BusStopDictType::iterator it = myBusStopDict.begin(); it != myBusStopDict.end(); ++it) {
00421             MSBusStop *stop = it->second;
00422             if (&stop->getLane() == lane && fabs(stop->getEndLanePosition() - pos) < POSITION_EPS) {
00423                 return stop->getID();
00424             }
00425         }
00426         return "";
00427     }
00429 
00430 
00431 
00434 
00436     enum VehicleState {
00438         VEHICLE_STATE_BUILT,
00440         VEHICLE_STATE_DEPARTED,
00442         VEHICLE_STATE_STARTING_TELEPORT,
00444         VEHICLE_STATE_ENDING_TELEPORT,
00446         VEHICLE_STATE_ARRIVED
00447     };
00448 
00449 
00453     class VehicleStateListener {
00454     public:
00456         VehicleStateListener() throw() { }
00457 
00459         virtual ~VehicleStateListener() throw() { }
00460 
00465         virtual void vehicleStateChanged(const MSVehicle * const vehicle, VehicleState to) throw() = 0;
00466 
00467     };
00468 
00469 
00473     void addVehicleStateListener(VehicleStateListener *listener) throw();
00474 
00475 
00479     void removeVehicleStateListener(VehicleStateListener *listener) throw();
00480 
00481 
00487     void informVehicleStateListener(const MSVehicle * const vehicle, VehicleState to) throw();
00489 
00490 
00500     class EdgeWeightsProxi {
00501     public:
00506         EdgeWeightsProxi(const MSEdgeWeightsStorage &vehKnowledge,
00507                          const MSEdgeWeightsStorage &netKnowledge)
00508                 : myVehicleKnowledge(vehKnowledge), myNetKnowledge(netKnowledge) {}
00509 
00510 
00512         ~EdgeWeightsProxi() {}
00513 
00514 
00522         SUMOReal getTravelTime(const MSEdge * const e, const SUMOVehicle * const v, SUMOReal t) const;
00523 
00524 
00532         SUMOReal getEffort(const MSEdge * const e, const SUMOVehicle * const v, SUMOReal t) const;
00533 
00534     private:
00536         const MSEdgeWeightsStorage &myVehicleKnowledge;
00537 
00539         const MSEdgeWeightsStorage &myNetKnowledge;
00540 
00541     };
00542 
00543 
00544 #ifdef _MESSAGES
00546     typedef NamedObjectCont< MSMessageEmitter* > MsgEmitterDict;
00547 
00548     // TODO
00555     MSMessageEmitter *getMsgEmitter(const std::string& whatemit);
00556 
00561     void createMsgEmitter(std::string& id,
00562                           std::string& file,
00563                           const std::string& base,
00564                           std::string& whatemit,
00565                           bool reverse,
00566                           bool table,
00567                           bool xy,
00568                           SUMOReal step);
00569 #endif
00570 
00571 protected:
00573     static MSNet* myInstance;
00574 
00576     MSRouteLoaderControl *myRouteLoaders;
00577 
00579     SUMOTime myStep;
00580 
00581 
00582 
00585 
00587     MSVehicleControl *myVehicleControl;
00589     MSPersonControl *myPersonControl;
00591     MSEdgeControl* myEdges;
00593     MSJunctionControl* myJunctions;
00595     MSTLLogicControl *myLogics;
00597     MSEmitControl* myEmitter;
00599     MSDetectorControl *myDetectorControl;
00601     MSEventControl *myBeginOfTimestepEvents;
00603     MSEventControl *myEndOfTimestepEvents;
00605     MSEventControl *myEmissionEvents;
00607     ShapeContainer *myShapeContainer;
00609     MSEdgeWeightsStorage *myEdgeWeights;
00611 
00612 
00613 
00616 
00618     bool myLogExecutionTime;
00619 
00621     bool myLogStepNumber;
00622 
00624     long mySimStepBegin, mySimStepEnd, mySimStepDuration;
00625 
00627     long mySimBeginMillis;
00628 
00630     long myVehiclesMoved;
00631     //}
00632 
00633 
00634 
00637 
00639     std::vector<SUMOTime> myStateDumpTimes;
00641     std::vector<std::string> myStateDumpFiles;
00643 
00644 
00646     int myTooManyVehicles;
00647 
00648 
00650     typedef std::map< std::string, MSBusStop* > BusStopDictType;
00652     BusStopDictType myBusStopDict;
00653 
00655     std::vector<VehicleStateListener*> myVehicleStateListeners;
00656 
00657 
00658 #ifdef _MESSAGES
00660     MsgEmitterDict myMsgEmitter;
00661 
00663     std::vector<MSMessageEmitter*> msgEmitVec;
00664 #endif
00665 
00666 
00667 private:
00669     MSNet(const MSNet&);
00670 
00672     MSNet& operator=(const MSNet&);
00673 
00674 
00675 };
00676 
00677 
00678 #endif
00679 
00680 /****************************************************************************/
00681 

Generated on Wed May 5 00:06:32 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6