Sampling

From VLE

Jump to: navigation, search

Code explanation

Model's attributs

private:
   enum State { PASSIVE, RESPOND, CONFLICT };
 
    // parameter
    double mDuration;
 
    // state
    double mSampling;
    double mSigma;
    State mState;
    vd::Time mLastTime;

As the model Generator,mDuration is a parameter that will allow us to store the time step between the reception of an event and its replication. The attributemSampling store entrance to replicate. In our example, an entry is represented by a real. mSigma will allow us to calculate the remaining time between each event received to continue to replicate the first entry received. mState will serve to situate us in the state of the model : if it's waiting for input or if it's in the process of replicating. And finally, mLastTime, as in the model Generator used to store the date of the last event.


Constructor / Destructor

Sampling(const vd::DynamicsInit& init, const vd::InitEventList& events)    
    : vd::Dynamics(init, events)
{ mDuration = vv::toDouble(events.get("duration")); }
 
virtual ~Sampling()
{ }


There are exactly the same as for the model Generator, the constructor retrieves the duration parameter, and the destructor is empty but obligatory.


Init

virtual vd::Time init(const vd::Time& time)
{
  mSigma = mDuration;
  mLastTime = time;
  mState = PASSIVE;
  return vd::Time::infinity;
}


We init the attribute mSigma at parameter duration, mLastime at the start. At the begining of the simulation, we wait an input so we init mState to PASSIVE. Finaly, we return infinity to wait indefinitely until a reception of an entry.


Output

virtual void output(const vd::Time& /* time */,
    vd::ExternalEventList& output) const
{
    vd::ExternalEvent* event = new vd::ExternalEvent("out");
 
    event << vd::attribute("in", mSampling);
    output.addEvent(event);
 
}


We want replicate what we have recieved in entry. First we create an event nammed event which will be sent on the port out. We add at this new event the attribute mStorage (which contains what must be replicated) and which will be send on the port out. Finaly we add the event which will be sent by the output function.


TimeAdvance (Ta)

virtual vd::Time timeAdvance() const
{
  switch (mState) {
  case PASSIVE : {
    return vd::Time::infinity;
  }
  case RESPOND : {
    return mSigma;
  }
  case CONFLICT : {
    return 0.0;
  }
  default :
    return vd::Time::infinity;
  }
}

If we waiting en event in model's entry, we wait indefinitely. If we replicating an entry (RESPOND state), we return the time remaining before sending the replicated entry. If we are in the state CONFLICT (we receive a new entry and we want return one in the same time), we return 0 for directly send the news updated entry.


InternalTransition

virtual void internalTransition(const vd::Time& time)
{
  mLastTime = time;
  mSigma = mDuration;
  mState = PASSIVE;
}

The internal transition takes place only during the replication of the entry. We set mLastTime to time to can update mSigma is the next transition is an externalTransition. We set back mSigma to mDuration and mState to PASSIVE because we begining a new waiting of entry.


ExternalTransition

virtual void externalTransition(const vd::ExternalEventList& events,
    const vd::Time&  time )
{
  switch (mState) {
  case PASSIVE : {
    vd::ExternalEventList::const_iterator it = events.begin();
    while (it != events.end()) {
      mSampling = (*it)->getDoubleAttributeValue("in");
      ++it;
    }
    mSigma = mDuration;
    mLastTime = time;
    mState = RESPOND;
    break;
  }
  case RESPOND : {
    vd::ExternalEventList::const_iterator it = events.begin();
    while (it != events.end()) {
      mSampling = (*it)->getDoubleAttributeValue("in");
      ++it;
    }
    mSigma -= time - mLastTime;
    mLastTime = time;
    break;
  }
  case CONFLICT : {
    vd::ExternalEventList::const_iterator it = events.begin();
    while (it != events.end()) {
      mSampling = (*it)->getDoubleAttributeValue("in");
      ++it;
    }
    mSigma = 0;
    mLastTime=time;
    break;
  }
  }
}


This page was last modified on 16 July 2010, at 09:37. This page has been accessed 574 times.