User Manual, Developers Guide and API Documentation

TimeDependent.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include <WNS/simulator/ISimulator.hpp>
00029 #include <WNS/events/scheduler/Interface.hpp>
00030 
00031 #include <WNS/distribution/TimeDependent.hpp>
00032 
00033 
00034 using namespace wns::distribution;
00035 
00036 STATIC_FACTORY_REGISTER_WITH_CREATOR(
00037     TimeDependent,
00038     Distribution,
00039     "wns.distribution.TimeDependent",
00040     wns::PyConfigViewCreator);
00041 STATIC_FACTORY_REGISTER_WITH_CREATOR(
00042     TimeDependent,
00043     Distribution,
00044     "wns.distribution.TimeDependent",
00045     wns::distribution::RNGConfigCreator);
00046 
00047 
00048 TimeDependent::DistributionEvent::DistributionEvent(
00049     TimeDependent* td,
00050     const wns::pyconfig::View& view) :
00051     target_(td),
00052     config_(view)
00053 {
00054     assure(this->target_, "No valid target set (NULL)");
00055 }
00056 
00057 TimeDependent::DistributionEvent::~DistributionEvent()
00058 {
00059     this->target_ = NULL;
00060 }
00061 
00062 void
00063 TimeDependent::DistributionEvent::operator()()
00064 {
00065     assure(this->target_, "No target set");
00066     this->target_->setDistribution(config_);
00067     this->target_->queueNextDistribution();
00068 }
00069 
00070 TimeDependent::TimeDependent(const wns::pyconfig::View& view) :
00071     Distribution(),
00072     distribution_(NULL),
00073     events_(),
00074     config_(view)
00075 {
00076     init();
00077 }
00078 
00079 TimeDependent::TimeDependent(wns::rng::RNGen* rng, const wns::pyconfig::View& view) :
00080     Distribution(rng),
00081     distribution_(NULL),
00082     events_(),
00083     config_(view)
00084 {
00085     init();
00086 }
00087 
00088 void
00089 TimeDependent::init()
00090 {
00091     for(int ii = 0; ii < config_.len("eventList"); ++ii)
00092     {
00093         wns::pyconfig::View eventView = config_.get("eventList", ii);
00094         DistributionEvent de (this, eventView.get("distribution"));
00095         wns::simulator::Time activationTime =
00096             eventView.get<wns::simulator::Time>("activationTime");
00097         assure(this->events_.find(activationTime) == this->events_.end(),
00098                "Event for this time is already scheduled. Not possible.");
00099         this->events_.insert(std::make_pair(activationTime, de));
00100     }
00101 
00102     // if the first Event is scheduled for 0.0, install distribution right
00103     // away
00104     if(this->events_.begin()->first == 0.0)
00105     {
00106         DistributionEvent ev = this->events_.begin()->second;
00107         this->events_.erase(this->events_.begin());
00108         (ev)();
00109     } else {
00110         this->queueNextDistribution();
00111     }
00112 }
00113 
00114 TimeDependent::~TimeDependent()
00115 {
00116     this->removeDistribution();
00117 }
00118 
00119 double
00120 TimeDependent::operator()()
00121 {
00122     assure(this->distribution_, "No distribution set!");
00123     return (*distribution_)();
00124 }
00125 
00126 std::string
00127 TimeDependent::paramString() const
00128 {
00129     std::ostringstream tmp;
00130     tmp << "TimeDependent()";
00131     return tmp.str();
00132 }
00133 
00134 void
00135 TimeDependent::queueNextDistribution()
00136 {
00137     if(!this->events_.empty())
00138     {
00139         EventContainer::iterator itr = this->events_.begin();
00140         wns::simulator::getEventScheduler()->schedule(itr->second, itr->first);
00141         this->events_.erase(itr);
00142     }
00143 }
00144 
00145 void
00146 TimeDependent::setDistribution(const wns::pyconfig::View& distConfig)
00147 {
00148     this->removeDistribution();
00149     wns::distribution::RNGDistributionCreator* dc =
00150         wns::distribution::RNGDistributionFactory::creator(distConfig.get<std::string>("__plugin__"));
00151     this->distribution_ = dc->create(getRNG(), distConfig);
00152 }
00153 
00154 void
00155 TimeDependent::removeDistribution()
00156 {
00157     if(this->distribution_)
00158     {
00159         delete this->distribution_;
00160         this->distribution_ = NULL;
00161     }
00162 }
00163 
00164 /*
00165   Local Variables:
00166   mode: c++
00167   fill-column: 80
00168   c-basic-offset: 8
00169   c-comment-only-line-offset: 0
00170   c-tab-always-indent: t
00171   indent-tabs-mode: t
00172   tab-width: 8
00173   End:
00174 */

Generated on Sat May 26 03:31:36 2012 for openWNS by  doxygen 1.5.5