SUMO - Simulation of Urban MObility
MSLaneSpeedTrigger.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Changes the speed allowed on a set of lanes
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <string>
40 #include <utils/xml/XMLSubSys.h>
43 #include <microsim/MSLane.h>
44 #include <microsim/MSNet.h>
45 #include <microsim/MSEdge.h>
46 #include "MSLaneSpeedTrigger.h"
47 
48 #ifdef HAVE_INTERNAL
49 #include <microsim/MSGlobals.h>
50 #include <mesosim/MELoop.h>
51 #include <mesosim/MESegment.h>
52 #endif
53 
54 #ifdef CHECK_MEMORY_LEAKS
55 #include <foreign/nvwa/debug_new.h>
56 #endif // CHECK_MEMORY_LEAKS
57 
58 
59 // ===========================================================================
60 // method definitions
61 // ===========================================================================
63  const std::vector<MSLane*>& destLanes,
64  const std::string& file) :
65  MSTrigger(id),
66  SUMOSAXHandler(file),
67  myDestLanes(destLanes),
68  myCurrentSpeed(destLanes[0]->getSpeedLimit()),
69  myDefaultSpeed(destLanes[0]->getSpeedLimit()),
70  myAmOverriding(false),
71  mySpeedOverrideValue(destLanes[0]->getSpeedLimit()),
72  myDidInit(false) {
73  if (file != "") {
74  if (!XMLSubSys::runParser(*this, file)) {
75  throw ProcessError();
76  }
77  if (!myDidInit) {
78  init();
79  }
80  }
81 }
82 
83 void
85  // set it to the right value
86  // assert there is at least one
87  if (myLoadedSpeeds.size() == 0) {
88  myLoadedSpeeds.push_back(std::make_pair(100000, myCurrentSpeed));
89  }
90  // set the process to the begin
92  // pass previous time steps
94  while ((*myCurrentEntry).first < now && myCurrentEntry != myLoadedSpeeds.end()) {
95  processCommand(true, now);
96  }
97 
98  // add the processing to the event handler
101  (*myCurrentEntry).first, MSEventControl::NO_CHANGE);
102  myDidInit = true;
103 }
104 
105 
107 
108 
109 SUMOTime
111  return processCommand(true, currentTime);
112 }
113 
114 
115 SUMOTime
116 MSLaneSpeedTrigger::processCommand(bool move2next, SUMOTime currentTime) {
117  UNUSED_PARAMETER(currentTime);
118  std::vector<MSLane*>::iterator i;
119  const SUMOReal speed = getCurrentSpeed();
120  for (i = myDestLanes.begin(); i != myDestLanes.end(); ++i) {
121 #ifdef HAVE_INTERNAL
123  MESegment* first = MSGlobals::gMesoNet->getSegmentForEdge((*i)->getEdge());
124  while (first != 0) {
125  first->setSpeed(speed, currentTime, -1);
126  first = first->getNextSegment();
127  }
128  continue;
129  }
130 #endif
131  (*i)->setMaxSpeed(speed);
132  }
133  if (!move2next) {
134  // changed from the gui
135  return 0;
136  }
137  if (myCurrentEntry != myLoadedSpeeds.end()) {
138  ++myCurrentEntry;
139  }
140  if (myCurrentEntry != myLoadedSpeeds.end()) {
141  return ((*myCurrentEntry).first) - ((*(myCurrentEntry - 1)).first);
142  } else {
143  return 0;
144  }
145 }
146 
147 
148 void
150  const SUMOSAXAttributes& attrs) {
151  // check whether the correct tag is read
152  if (element != SUMO_TAG_STEP) {
153  return;
154  }
155  // extract the values
156  bool ok = true;
157  SUMOTime next = attrs.getSUMOTimeReporting(SUMO_ATTR_TIME, getID().c_str(), ok);
158  SUMOReal speed = attrs.getOpt<SUMOReal>(SUMO_ATTR_SPEED, getID().c_str(), ok, -1);
159  // check the values
160  if (next < 0) {
161  WRITE_ERROR("Wrong time in vss '" + getID() + "'.");
162  return;
163  }
164  if (speed < 0) {
165  speed = myDefaultSpeed;
166  }
167  // set the values for the next step if they are valid
168  if (myLoadedSpeeds.size() != 0 && myLoadedSpeeds.back().first == next) {
169  WRITE_WARNING("Time " + time2string(next) + " was set twice for vss '" + getID() + "'; replacing first entry.");
170  myLoadedSpeeds.back().second = speed;
171  } else {
172  myLoadedSpeeds.push_back(std::make_pair(next, speed));
173  }
174 }
175 
176 
177 void
179  if (element == SUMO_TAG_VSS && !myDidInit) {
180  init();
181  }
182 }
183 
184 
185 SUMOReal
187  return myDefaultSpeed;
188 }
189 
190 
191 void
193  myAmOverriding = val;
194  processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
195 }
196 
197 
198 void
200  mySpeedOverrideValue = val;
201  processCommand(false, MSNet::getInstance()->getCurrentTimeStep());
202 }
203 
204 
205 SUMOReal
207  if (myCurrentEntry != myLoadedSpeeds.begin()) {
208  return (*(myCurrentEntry - 1)).second;
209  } else {
210  return (*myCurrentEntry).second;
211  }
212 }
213 
214 
215 SUMOReal
217  if (myAmOverriding) {
218  return mySpeedOverrideValue;
219  } else {
221  // ok, maybe the first shall not yet be the valid one
222  if (myCurrentEntry == myLoadedSpeeds.begin() && (*myCurrentEntry).first > now) {
223  return myDefaultSpeed;
224  }
225  // try the loaded
226  if (myCurrentEntry != myLoadedSpeeds.end() && (*myCurrentEntry).first <= now) {
227  return (*myCurrentEntry).second;
228  } else {
229  // we have run past the end of the loaded steps or the current step is not yet active:
230  // -> use the value of the previous step
231  return (*(myCurrentEntry - 1)).second;
232  }
233  }
234 }
235 
236 
237 /****************************************************************************/
238 
void setOverridingValue(SUMOReal val)
std::vector< MSLane * > myDestLanes
long long int SUMOTime
Definition: SUMOTime.h:43
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
SUMOTime execute(SUMOTime currentTime)
Executes a switch command.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:162
SAX-handler base for SUMO-files.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:255
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:114
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
SUMOReal getCurrentSpeed() const
Returns the current speed.
const std::string & getID() const
Returns the id.
Definition: Named.h:65
SUMOReal myDefaultSpeed
The original speed allowed on the lanes.
An abstract device that changes the state of the micro simulation.
Definition: MSTrigger.h:48
Encapsulated SAX-Attributes.
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:389
A wrapper for a Command function.
SUMOReal getDefaultSpeed() const
bool myAmOverriding
The information whether the read speed shall be overridden.
std::vector< std::pair< SUMOTime, SUMOReal > >::iterator myCurrentEntry
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
void setOverriding(bool val)
virtual SUMOTime addEvent(Command *operation, SUMOTime execTimeStep, AdaptType type)
Adds an Event.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
MSLaneSpeedTrigger(const std::string &id, const std::vector< MSLane * > &destLanes, const std::string &file)
Constructor.
SUMOReal mySpeedOverrideValue
The speed to use if overriding the read speed.
SUMOTime processCommand(bool move2next, SUMOTime currentTime)
virtual ~MSLaneSpeedTrigger()
Destructor.
std::vector< std::pair< SUMOTime, SUMOReal > > myLoadedSpeeds
bool myDidInit
The information whether init was called.
A variable speed sign.
#define SUMOReal
Definition: config.h:214
static const bool gUseMesoSim
Definition: MSGlobals.h:102
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue, bool report=true) const
Tries to read given attribute assuming it is an int.
virtual void myEndElement(int element)
Called on the closing of a tag;.