Esempio n. 1
0
        void externalTransition(
            const vle::devs::ExternalEventList& events,
            const vle::devs::Time& time)
        {
            vle::devs::ExternalEventList::const_iterator it = events.begin();

            while (it != events.end()) {
                if ((*it)->onPort("start")) {
                    if (mPhase == INIT) {
                        mActivities.starting(time);
                        if (mActivities.startingActivities().empty()) {
                            mPhase = WAIT;
                        } else {
                            mPhase = SEND;
                        }
                    }
                } else if ((*it)->onPort("done")) {
                    Activity* a = Activity::build(Activity::get(*it));

                    TraceModel(vle::fmt(" [%1%:%2%] at %3% -> %4% DONE") %
                               getModel().getParentName() % getModelName() %
                               time % a->name());

                    mDoneActivities.push_back(a);
                }
                ++it;
            }
            if (mSigma > 0) {
                mSigma -= (time - mLastTime);
                mLastTime = time;
            }
        }
Esempio n. 2
0
        void externalTransition(
            const vle::devs::ExternalEventList& events,
            const vle::devs::Time& time)
        {
            vle::devs::ExternalEventList::const_iterator it = events.begin();

            while (it != events.end()) {
                if ((*it)->onPort("available")) {
                    bool available =
                        (*it)->getBooleanAttributeValue("available");
                    unsigned int number =
                        (*it)->getIntegerAttributeValue("number");
                    std::string type = (*it)->getStringAttributeValue("type");

                    if (available) {
                        mAvailableResourceNumber += number;
                    } else {
                        mUnavailableResources[type] =
                            mResourceConstraints->quantity(type) - number;
                    }
                    ++mResponseNumber;

                    TraceModel(
                        vle::fmt(" [%1%:%2%] at %3% -> available: %4% / %5%")
                        % getModel().getParentName() % getModelName() %
                        time % mAvailableResourceNumber %
                        mResourceConstraints->quantity());

                    if (mAvailableResourceNumber ==
                        mResourceConstraints->quantity()) {
                        mPhase = SEND_ASSIGN;
                    } else {
                        if (mResponseNumber == mResourceConstraints->size()) {
                            clearDemand();
                            mPhase = SEND_UNAVAILABLE;
                        } else {
                            mPhase = WAIT_AVAILABLE;
                        }
                    }
                } else if ((*it)->onPort("demand")) {
                    mResourceConstraints =
                        ResourceConstraints::build(Resources::get(*it));

                    TraceModel(vle::fmt(" [%1%:%2%] at %3% -> demand: %4%") %
                               getModel().getParentName() % getModelName() %
                               time % mResourceConstraints->quantity());

                    mPhase = SEND_DEMAND;
                } else if ((*it)->onPort("release")) {
                    mReleasedResources = Resources::build(Resources::get(*it));

                    TraceModel(vle::fmt(" [%1%:%2%] at %3% -> release: %4%") %
                               getModel().getParentName() % getModelName() %
                               time % *mReleasedResources);

                    mPhase = SEND_RELEASE;
                }
                ++it;
            }
        }
Esempio n. 3
0
  void XRay::externalTransition(
                                  const vd::ExternalEventList& event,
                                  const vd::Time& time)
  {
      
      if (mPhase == RECEIVE) {
          if (!mapResult.empty())
              mapResult.clear();
          for (vd::ExternalEventList::const_iterator it = event.begin();
                      it != event.end(); ++it) {

              double randValue = rand().getDouble();
              if ((*it) -> getPortName() == "status") {

                  std::string value = 
                        (*it)-> getStringAttributeValue ("value");
                  std::string modelName = 
	                      (*it)-> getStringAttributeValue ("modelName");

                  if (value == "S" || value == "R") {
                      // probability of a wrong interpretation
                      if (randValue > mProbabilityRightSR) {
                        value = "I";
                      }
                  } else if (value == "I") {
                      if (randValue > mProbabilityRightI) {
                        value = "S";
                      }
                  }
                  mapResult[modelName] = value;
              }
          }
          mCurrentTime = vd::Time(time);
      int nbInfected = 0;
      //int nbNonInfected = 0;
      std::map<std::string, std::string>::iterator node;
      for (node=mapResult.begin(); node!=mapResult.end();node++){
          if (node->second == "I")
              nbInfected++;
          //else
          //    nbNonInfected++;
      }
      //std::cout<<"xray recieve: "<<nbNonInfected<<" non infecteds and: "<< nbInfected
      //    <<" infecteds at time: "<<time.getValue()<<"\n";
      double tempPrev = ((double) nbInfected) / mSampleSize;
      //std::cout<<"tempPrev: "<<tempPrev<<"\n";
      mIncidence = (tempPrev - mPrevalence)/mObservationTimeStep;
      mPrevalence = tempPrev;
      }
  }
    /**
     * @brief Handles external event and registers if necesserary
     * updates of external events (ExtUp), perturabations and discontinuities.
     * @param t, the current time
     * @param ext, the list of external event
     */
    void handleExtEvt(const vd::Time& /*t*/, const vd::ExternalEventList& ext)
    {

        vd::ExternalEventList::const_iterator itb = ext.begin();
        vd::ExternalEventList::const_iterator ite = ext.end();
        for (; itb != ite; itb++) {
            //get port name
            const std::string& portName = itb->getPortName();
            //find internal variable
            Variables::iterator itf = vars().find(portName);
            if (itf == vars().end()) {
                throw utils::InternalError(vle::utils::format(
                        "[%s] Unrecognised variable '%s'",
                        getModelName().c_str(), portName.c_str()));
            }
            if (itb->attributes()->isMap() and
                    itb->attributes()->toMap().exist("value")) {
                itf->second->setVal(
                        itb->attributes()->toMap().getDouble("value"));
            } else {
                itf->second->setVal(itb->attributes()->toDouble().value());
            }
        }
    }
Esempio n. 5
0
    void externalTransition(
        const vle::devs::ExternalEventList& events, const vle::devs::Time& time)
    {
        vle::devs::ExternalEventList::const_iterator it = events.begin();

        std::cout.precision(12);
        std::cout << time << " - [" << getModelName()
                  << "] externalTransition: " << mPhase << std::endl;

        while (it != events.end()) {
            if ((*it)->onPort("transport")) {
                Transport* transport = new Transport(
                    vle::value::toMapValue(
                        (*it)->getAttributeValue("transport")));

                std::cout << time << " - [" << getModelName()
                          << "] DECISION TRANSPORT: " << transport->toString()
                          << " => " << mPhase << std::endl;

                transport->arrived(time);
                mTransports.push_back(transport);
            } else if ((*it)->onPort("loaded")) {
                TransportID transportID =
                    (*it)->getIntegerAttributeValue("id");

                std::cout << time << " - [" << getModelName()
                          << "] DECISION LOADED: transport -> " << transportID
                          << std::endl;

                removeWaitingTransport(transportID);
                mPhase = SEND_DEPART;
            }
            ++it;
        }
        updateSigma(time);
    }
Esempio n. 6
0
	virtual void externalTransition(const vd::ExternalEventList& eventList, const vd::Time& time) {
		TraceAlways(vu::DateTime::simpleCurrentDate() + " - MMSExecute - externalTransition at time : " + time.toString());
		//Update input port 
	    vd::ExternalEventList::const_iterator it = eventList.begin();
	    for (;it != eventList.end(); it++)
	    {
			const vd::ExternalEvent& event = *(*it);
			if (event.onPort("time"))
		    {
				TraceModel(vu::DateTime::simpleCurrentDate() + " - Trying to update \"time\" port");
				if(event.existAttributeValue("time"))
				{
				    const vv::Value& val = event.getAttributeValue("time");
						
					this->time = Long_fromValue(&val);
					if(val.isNull() == false)
					{
						TraceModel(vu::DateTime::simpleCurrentDate() + " - Update \"time\" input port to \"" + val.writeToString() + "\" at time " + time.toString());
					}
					else
					{
						TraceModel(vu::DateTime::simpleCurrentDate() + " - NULL value for  \"time\" input port at time " + time.toString());
					}
							
				}
				else
				{
					TraceModel(vu::DateTime::simpleCurrentDate() + " - Unknown attribute value \"time\" for the port \"time\"");
				}
		    }
			if (event.onPort("timestep"))
		    {
				TraceModel(vu::DateTime::simpleCurrentDate() + " - Trying to update \"timestep\" port");
				if(event.existAttributeValue("timestep"))
				{
				    const vv::Value& val = event.getAttributeValue("timestep");
						
					this->timestep = Long_fromValue(&val);
					if(val.isNull() == false)
					{
						TraceModel(vu::DateTime::simpleCurrentDate() + " - Update \"timestep\" input port to \"" + val.writeToString() + "\" at time " + time.toString());
					}
					else
					{
						TraceModel(vu::DateTime::simpleCurrentDate() + " - NULL value for  \"timestep\" input port at time " + time.toString());
					}
							
				}
				else
				{
					TraceModel(vu::DateTime::simpleCurrentDate() + " - Unknown attribute value \"timestep\" for the port \"timestep\"");
				}
		    }
			if (event.onPort("cycle"))
		    {
				TraceModel(vu::DateTime::simpleCurrentDate() + " - Trying to update \"cycle\" port");
				if(event.existAttributeValue("cycle"))
				{
				    const vv::Value& val = event.getAttributeValue("cycle");
						
					this->cycle = Long_fromValue(&val);
					if(val.isNull() == false)
					{
						TraceModel(vu::DateTime::simpleCurrentDate() + " - Update \"cycle\" input port to \"" + val.writeToString() + "\" at time " + time.toString());
					}
					else
					{
						TraceModel(vu::DateTime::simpleCurrentDate() + " - NULL value for  \"cycle\" input port at time " + time.toString());
					}
							
				}
				else
				{
					TraceModel(vu::DateTime::simpleCurrentDate() + " - Unknown attribute value \"cycle\" for the port \"cycle\"");
				}
		    }
			if (event.onPort("NID"))
		    {
				TraceModel(vu::DateTime::simpleCurrentDate() + " - Trying to update \"NID\" port");
				if(event.existAttributeValue("NID"))
				{
				    const vv::Value& val = event.getAttributeValue("NID");
						
					this->NID = Long_fromValue(&val);
					if(val.isNull() == false)
					{
						TraceModel(vu::DateTime::simpleCurrentDate() + " - Update \"NID\" input port to \"" + val.writeToString() + "\" at time " + time.toString());
					}
					else
					{
						TraceModel(vu::DateTime::simpleCurrentDate() + " - NULL value for  \"NID\" input port at time " + time.toString());
					}
							
				}
				else
				{
					TraceModel(vu::DateTime::simpleCurrentDate() + " - Unknown attribute value \"NID\" for the port \"NID\"");
				}
		    }
		}
	}
Esempio n. 7
0
    virtual void externalTransition(const vd::ExternalEventList &events,
                                    vd::Time time) override
    {
        double val, shifting_factor;
        int cnt;

        if (events.size() > 1)
            Trace(context(), 6, "Warning: %s got multiple events at date: %f\n",
                  getModelName().c_str(), time);

        auto it = events.begin();
        while (it != events.end()) {
            val = it->getMap().getDouble("d_val");
            if (INIT == m_state) {
                init_step_number_and_offset(val);
                update_thresholds();
                m_state = RESPONSE;
            } else {
                cnt = 0;
                if ((val > m_upthreshold) || (val < m_downthreshold))
                    Trace(context(), 6, "%s: treating out of bonds val: %f "
                          "(quantizer interval : [%f,%f] at date: %f",
                          getModelName().c_str(), val, m_downthreshold,
                          m_upthreshold, time);

                while ((val >= m_upthreshold) || (val <= m_downthreshold)) {
                    cnt++;
                    if (val >= m_upthreshold) {
                        m_step_number++;
                    } else {
                        m_step_number--;
                    }
                    switch (m_adapt_state) {
                    case IMPOSSIBLE:
                        update_thresholds();
                        break;
                    case POSSIBLE:
                        if (val >= m_upthreshold) {
                            store_change(m_step_size, time);
                        } else {
                            store_change(-m_step_size, time);
                        }
                        shifting_factor = shift_quanta();
                        if (0 > shifting_factor)
                            throw vu::ModellingError(
                                "Bad shifting value (value : %f, "
                                "should be strictly positive)\n",
                                 shifting_factor);
                        if (1 < shifting_factor)
                            throw vu::ModellingError(
                                "Bad shifting value ( value : %f, "
                                "should be less than 1)\n",
                                shifting_factor);;
                        if ((0 != shifting_factor) && (1 != shifting_factor)) {
                            if (val >= m_upthreshold) {
                                update_thresholds(shifting_factor, DOWN);
                            } else {
                                update_thresholds(shifting_factor, UP);
                            }
                            Trace(context(), 6,
                                  "Quantifier %s new quantas while treating new val %f at date %f",
                                  getModelName().c_str(), val, time);

                            Trace(context(), 6,
                                  "Quantizer interval:  [%f, %f], amplitude: %f "
                                  "(default amplitude: %f)", m_downthreshold,
                                  m_upthreshold, (m_upthreshold - m_downthreshold),
                                  (2 * m_step_size));

                            Trace(context(), 6,
                                  "Quantifier %s shifting : %f",
                                  getModelName().c_str(), shifting_factor);

                            m_adapt_state = DONE;
                        } else {
                            update_thresholds();
                        }
                        break;
                    case DONE: // equiv to reinit
                        init_step_number_and_offset(val);
                        // archive.resize(0);
                        m_adapt_state = POSSIBLE;
                        update_thresholds();
                        break;
                    }
                }

                if (cnt > 1)
                    Trace(context(), 6, "Warning : in %s multiple quanta change"
                          " at date : %f %d\n", getModelName().c_str(), time, cnt);

                if (0 == cnt)
                    Trace(context(), 6, "Warning : in %s useless ext transition"
                          "call: no quanta change! input val %f (quantizer "
                          "interval : [%f,%f] at date %f\n", getModelName().c_str(),
                          val, m_downthreshold, m_upthreshold, time);
            }
            ++it;
        }
        m_state = RESPONSE;
    }