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