123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- /**
- * @file pdevs/Coordinator.hpp
- * @author The PARADEVS Development Team
- * See the AUTHORS or Authors.txt file
- */
- /*
- * PARADEVS - the multimodeling and simulation environment
- * This file is a part of the PARADEVS environment
- *
- * Copyright (C) 2013 ULCO http://www.univ-litoral.fr
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef PDEVS_COORDINATOR
- #define PDEVS_COORDINATOR 1
- #include <common/Coordinator.hpp>
- #include <common/Parameters.hpp>
- #include <common/Trace.hpp>
- #include <cassert>
- #include <iostream>
- namespace paradevs { namespace pdevs {
- template < class Time, class Scheduler, class GraphManager,
- class Parameters = common::NoParameters,
- class GraphParameters = common::NoParameters >
- class Coordinator : public common::Coordinator < Time >
- {
- typedef Coordinator < Time, Scheduler, GraphManager,
- Parameters, GraphParameters > type;
- public:
- typedef Parameters parameters_type;
- typedef GraphParameters graph_parameters_type;
- Coordinator(const std::string& name,
- const Parameters& /* parameters */,
- const GraphParameters& graph_parameters) :
- common::Coordinator < Time >(name),
- _graph_manager(this, graph_parameters)
- { }
- virtual ~Coordinator()
- { }
- typename Time::type start(typename Time::type t)
- {
- #ifdef WITH_TRACE
- common::Trace < Time >::trace()
- << common::TraceElement < Time >(type::get_name(), t,
- common::I_MESSAGE)
- << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
- << type::_tn;
- common::Trace < Time >::trace().flush();
- #endif
- assert(_graph_manager.children().size() > 0);
- for (auto & child : _graph_manager.children()) {
- _event_table.init(child->start(
- Coordinator < Time, Scheduler,
- GraphManager, Parameters,
- GraphParameters >::_tn), child);
- }
- type::_tl = t;
- type::_tn = _event_table.get_current_time();
- #ifdef WITH_TRACE
- common::Trace < Time >::trace()
- << common::TraceElement < Time >(type::get_name(), t,
- common::I_MESSAGE)
- << ": AFTER => " << "tl = " << type::_tl
- << " ; tn = " << type::_tn;
- common::Trace < Time >::trace().flush();
- #endif
- return type::_tn;
- }
- /**************************************************
- * when *-message(t)
- * calculate IMM (models with tn = t in scheduler
- * calculate INF from IMM
- * for each e in IMM U INF
- * calculate influencer
- * ...
- * send done to parent
- **************************************************/
- void output(typename Time::type t)
- {
- #ifdef WITH_TRACE
- common::Trace < Time >::trace()
- << common::TraceElement < Time >(type::get_name(), t,
- common::OUTPUT)
- << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
- << type::_tn << " ; scheduler = " << _event_table.to_string();
- common::Trace < Time >::trace().flush();
- #endif
- assert(t == type::_tn);
- common::Models < Time > IMM = _event_table.get_current_models(t);
- #ifdef WITH_TRACE
- common::Trace < Time >::trace()
- << common::TraceElement < Time >(type::get_name(), t,
- common::OUTPUT)
- << ": IMM = " << IMM.to_string();
- common::Trace < Time >::trace().flush();
- #endif
- for (auto & model : IMM) {
- model->output(t);
- }
- #ifdef WITH_TRACE
- common::Trace < Time >::trace()
- << common::TraceElement < Time >(type::get_name(), t,
- common::OUTPUT)
- << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
- << type::_tn << " ; scheduler = " << _event_table.to_string();
- common::Trace < Time >::trace().flush();
- #endif
- }
- /*******************************************************************
- * when x-message(t)
- * receivers = { r | r in children, N in Ir, Z(N,r)(x) isn't empty
- * for each r in receivers
- * send x-message(Z(N,r)(x), t) with input value Z(N,r)(x) to r
- * for each r in IMM and not in receivers
- * send x-message(empty, t) to r
- * sort event list acocrding to tn
- * tl = t
- * tn = min(tn_d | d in D)
- *******************************************************************/
- typename Time::type transition(typename Time::type t)
- {
- #ifdef WITH_TRACE
- common::Trace < Time >::trace()
- << common::TraceElement < Time >(type::get_name(), t,
- common::S_MESSAGE)
- << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
- << type::_tn << " ; scheduler = " << _event_table.to_string();
- common::Trace < Time >::trace().flush();
- #endif
- assert(t >= type::_tl and t <= type::_tn);
- common::Models < Time > receivers = _event_table.get_current_models(t);
- add_models_with_inputs(receivers);
- #ifdef WITH_TRACE
- common::Trace < Time >::trace()
- << common::TraceElement < Time >(type::get_name(), t,
- common::S_MESSAGE)
- << ": receivers = " << receivers.to_string();
- common::Trace < Time >::trace().flush();
- #endif
- for (auto & model : receivers) {
- _event_table.put(model->transition(t), model);
- }
- update_event_table(t);
- type::_tl = t;
- type::_tn = _event_table.get_current_time();
- type::clear_bag();
- #ifdef WITH_TRACE
- common::Trace < Time >::trace()
- << common::TraceElement < Time >(type::get_name(), t,
- common::S_MESSAGE)
- << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
- << type::_tn << " ; scheduler = " << _event_table.to_string();
- common::Trace < Time >::trace().flush();
- #endif
- return type::_tn;
- }
- void post_event(typename Time::type t,
- const common::ExternalEvent < Time >& event)
- {
- #ifdef WITH_TRACE
- common::Trace < Time >::trace()
- << common::TraceElement < Time >(type::get_name(), t,
- common::POST_EVENT)
- << ": BEFORE => " << event.to_string();
- common::Trace < Time >::trace().flush();
- #endif
- type::add_event(event);
- _graph_manager.post_event(t, event);
- update_event_table(t);
- type::_tn = _event_table.get_current_time();
- #ifdef WITH_TRACE
- common::Trace < Time >::trace()
- << common::TraceElement < Time >(type::get_name(), t,
- common::POST_EVENT)
- << ": AFTER => " << event.to_string();
- common::Trace < Time >::trace().flush();
- #endif
- }
- /*******************************************************************
- * when y-message(y_d, t) with output y_d from d
- *******************************************************************/
- typename Time::type dispatch_events(common::Bag < Time > bag,
- typename Time::type t)
- {
- #ifdef WITH_TRACE
- common::Trace < Time >::trace()
- << common::TraceElement < Time >( type::get_name(), t,
- common::Y_MESSAGE)
- << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
- << type::_tn << " ; bag = " << bag.to_string()
- << " ; " << _event_table.to_string();
- common::Trace < Time >::trace().flush();
- #endif
- _graph_manager.dispatch_events(bag, t);
- update_event_table(t);
- type::_tn = _event_table.get_current_time();
- #ifdef WITH_TRACE
- common::Trace < Time >::trace()
- << common::TraceElement < Time >(type::get_name(), t,
- common::Y_MESSAGE)
- << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn
- << " ; " << _event_table.to_string();
- common::Trace < Time >::trace().flush();
- #endif
- return type::_tn;
- }
- void observation(std::ostream& file) const
- {
- for (auto & child : _graph_manager.children()) {
- child->observation(file);
- }
- }
- void add_models_with_inputs(common::Models < Time >& receivers)
- {
- for (auto & model : _graph_manager.children()) {
- if (model->event_number() > 0) {
- if (std::find(receivers.begin(), receivers.end(), model) ==
- receivers.end()) {
- receivers.push_back(model);
- }
- }
- }
- }
- void update_event_table(typename Time::type t)
- {
- for (auto & model : _graph_manager.children()) {
- if (model->event_number() > 0) {
- _event_table.put(t, model);
- }
- }
- }
- private:
- GraphManager _graph_manager;
- Scheduler _event_table;
- };
- } } // namespace paradevs pdevs
- #endif
|