SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00010 // } 00011 /****************************************************************************/ 00012 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00013 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00014 /****************************************************************************/ 00015 // 00016 // This file is part of SUMO. 00017 // SUMO is free software: you can redistribute it and/or modify 00018 // it under the terms of the GNU General Public License as published by 00019 // the Free Software Foundation, either version 3 of the License, or 00020 // (at your option) any later version. 00021 // 00022 /****************************************************************************/ 00023 00024 00025 // =========================================================================== 00026 // included modules 00027 // =========================================================================== 00028 #ifdef _MSC_VER 00029 #include <windows_config.h> 00030 #else 00031 #include <config.h> 00032 #endif 00033 00034 #include "NLDiscreteEventBuilder.h" 00035 #include <utils/xml/SUMOXMLDefinitions.h> 00036 #include <microsim/MSNet.h> 00037 #include <microsim/actions/Command_SaveTLSState.h> 00038 #include <microsim/actions/Command_SaveTLSSwitches.h> 00039 #include <microsim/actions/Command_SaveTLSSwitchStates.h> 00040 #include <microsim/MSEventControl.h> 00041 #include <microsim/traffic_lights/MSTLLogicControl.h> 00042 #include <microsim/traffic_lights/MSTrafficLightLogic.h> 00043 #include <utils/common/FileHelpers.h> 00044 #include <utils/common/UtilExceptions.h> 00045 #include <utils/iodevices/OutputDevice.h> 00046 00047 #ifdef CHECK_MEMORY_LEAKS 00048 #include <foreign/nvwa/debug_new.h> 00049 #endif // CHECK_MEMORY_LEAKS 00050 00051 00052 // =========================================================================== 00053 // method definitions 00054 // =========================================================================== 00055 NLDiscreteEventBuilder::NLDiscreteEventBuilder(MSNet& net) 00056 : myNet(net) { 00057 myActions["SaveTLSStates"] = EV_SAVETLSTATE; 00058 myActions["SaveTLSSwitchTimes"] = EV_SAVETLSWITCHES; 00059 myActions["SaveTLSSwitchStates"] = EV_SAVETLSWITCHSTATES; 00060 } 00061 00062 00063 NLDiscreteEventBuilder::~NLDiscreteEventBuilder() {} 00064 00065 00066 void 00067 NLDiscreteEventBuilder::addAction(const SUMOSAXAttributes& attrs, 00068 const std::string& basePath) { 00069 bool ok = true; 00070 const std::string type = attrs.getOptStringReporting(SUMO_ATTR_TYPE, 0, ok, ""); 00071 // check whether the type was given 00072 if (type == "" || !ok) { 00073 throw InvalidArgument("An action's type is not given."); 00074 } 00075 // get the numerical representation 00076 KnownActions::iterator i = myActions.find(type); 00077 if (i == myActions.end()) { 00078 throw InvalidArgument("The action type '" + type + "' is not known."); 00079 } 00080 // build the action 00081 switch ((*i).second) { 00082 case EV_SAVETLSTATE: 00083 buildSaveTLStateCommand(attrs, basePath); 00084 break; 00085 case EV_SAVETLSWITCHES: 00086 buildSaveTLSwitchesCommand(attrs, basePath); 00087 break; 00088 case EV_SAVETLSWITCHSTATES: 00089 buildSaveTLSwitchStatesCommand(attrs, basePath); 00090 break; 00091 } 00092 } 00093 00094 00095 void 00096 NLDiscreteEventBuilder::buildSaveTLStateCommand(const SUMOSAXAttributes& attrs, 00097 const std::string& basePath) { 00098 bool ok = true; 00099 const std::string dest = attrs.getOptStringReporting(SUMO_ATTR_DEST, 0, ok, ""); 00100 const std::string source = attrs.getOptStringReporting(SUMO_ATTR_SOURCE, 0, ok, ""); 00101 // check the parameter 00102 if (dest == "" || !ok) { 00103 throw InvalidArgument("Incomplete description of an 'SaveTLSState'-action occured."); 00104 } 00105 if (source == "") { 00106 const std::vector<std::string> ids = myNet.getTLSControl().getAllTLIds(); 00107 for (std::vector<std::string>::const_iterator tls = ids.begin(); tls != ids.end(); ++tls) { 00108 const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(*tls); 00109 new Command_SaveTLSState(logics, OutputDevice::getDevice(dest, basePath)); 00110 } 00111 } else { 00112 // get the logic 00113 if (!myNet.getTLSControl().knows(source)) { 00114 throw InvalidArgument("The traffic light logic to save (" + source + ") is not known."); 00115 } 00116 const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(source); 00117 // build the action 00118 new Command_SaveTLSState(logics, OutputDevice::getDevice(dest, basePath)); 00119 } 00120 } 00121 00122 00123 void 00124 NLDiscreteEventBuilder::buildSaveTLSwitchesCommand(const SUMOSAXAttributes& attrs, 00125 const std::string& basePath) { 00126 bool ok = true; 00127 const std::string dest = attrs.getOptStringReporting(SUMO_ATTR_DEST, 0, ok, ""); 00128 const std::string source = attrs.getOptStringReporting(SUMO_ATTR_SOURCE, 0, ok, ""); 00129 // check the parameter 00130 if (dest == "" || !ok) { 00131 throw InvalidArgument("Incomplete description of an 'SaveTLSSwitchTimes'-action occured."); 00132 } 00133 if (source == "") { 00134 const std::vector<std::string> ids = myNet.getTLSControl().getAllTLIds(); 00135 for (std::vector<std::string>::const_iterator tls = ids.begin(); tls != ids.end(); ++tls) { 00136 const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(*tls); 00137 new Command_SaveTLSSwitches(logics, OutputDevice::getDevice(dest, basePath)); 00138 } 00139 } else { 00140 // get the logic 00141 if (!myNet.getTLSControl().knows(source)) { 00142 throw InvalidArgument("The traffic light logic to save (" + source + ") is not known."); 00143 } 00144 const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(source); 00145 // build the action 00146 new Command_SaveTLSSwitches(logics, OutputDevice::getDevice(dest, basePath)); 00147 } 00148 } 00149 00150 00151 void 00152 NLDiscreteEventBuilder::buildSaveTLSwitchStatesCommand(const SUMOSAXAttributes& attrs, 00153 const std::string& basePath) { 00154 bool ok = true; 00155 const std::string dest = attrs.getOptStringReporting(SUMO_ATTR_DEST, 0, ok, ""); 00156 const std::string source = attrs.getOptStringReporting(SUMO_ATTR_SOURCE, 0, ok, ""); 00157 // check the parameter 00158 if (dest == "" || !ok) { 00159 throw InvalidArgument("Incomplete description of an 'SaveTLSSwitchStates'-action occured."); 00160 } 00161 if (source == "") { 00162 const std::vector<std::string> ids = myNet.getTLSControl().getAllTLIds(); 00163 for (std::vector<std::string>::const_iterator tls = ids.begin(); tls != ids.end(); ++tls) { 00164 const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(*tls); 00165 new Command_SaveTLSSwitchStates(logics, OutputDevice::getDevice(dest, basePath)); 00166 } 00167 } else { 00168 // get the logic 00169 if (!myNet.getTLSControl().knows(source)) { 00170 throw InvalidArgument("The traffic light logic to save (" + source + ") is not known."); 00171 } 00172 const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(source); 00173 // build the action 00174 new Command_SaveTLSSwitchStates(logics, OutputDevice::getDevice(dest, basePath)); 00175 } 00176 } 00177 00178 00179 00180 /****************************************************************************/