SamplingFrom VLECode explanationModel'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.
Sampling(const vd::DynamicsInit& init, const vd::InitEventList& events) : vd::Dynamics(init, events) { mDuration = vv::toDouble(events.get("duration")); } virtual ~Sampling() { }
virtual vd::Time init(const vd::Time& time) { mSigma = mDuration; mLastTime = time; mState = PASSIVE; return vd::Time::infinity; }
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); }
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.
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; } } }
|