123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- /**
- * @file Coordinator.cpp
- * @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/>.
- */
- #include <devs/Coordinator.hpp>
- #include <devs/Simulator.hpp>
- #include <cassert>
- namespace paradevs { namespace devs {
- Coordinator::Coordinator(const std::string& name) : Model(name)
- { }
- Coordinator::~Coordinator()
- {
- for (unsigned int i = 0; i < _child_list.size(); i++)
- { delete _child_list[i]; }
- }
- common::Time Coordinator::i_message(common::Time t)
- {
- std::cout << "[" << get_name() << "] at " << t << ": BEFORE - i_message => "
- << "tl = " << _tl << " ; tn = " << _tn << std::endl;
- assert(_child_list.size() > 0);
- for (unsigned int i = 0; i < _child_list.size(); i++) {
- Model* model = _child_list[i];
- _event_table.init(model->i_message(_tn), model);
- }
- _tl = t;
- _tn = _event_table.get_current_time();
- std::cout << "[" << get_name() << "] at " << t << ": AFTER - i_message => "
- << "tl = " << _tl << " ; tn = " << _tn << std::endl;
- return _tn;
- }
- common::Time Coordinator::s_message(common::Time t)
- {
- std::cout << "[" << get_name() << "] at " << t << ": BEFORE - s_message => "
- << "tl = " << _tl << " ; tn = " << _tn << std::endl;
- std::cout << "[" << get_name() << "]: " << _event_table.to_string()
- << std::endl;
- assert(t == _tn);
- Model* current = dynamic_cast < devs::Model* >(
- _event_table.get_current_model());
- common::Time tn = current->s_message(_tn);
- _event_table.put(tn, current);
- _tl = t;
- _tn = _event_table.get_current_time();
- std::cout << "[" << get_name() << "] at " << t << ": AFTER - s_message => "
- << "tl = " << _tl << " ; tn = " << _tn << std::endl;
- std::cout << "[" << get_name() << "]: " << _event_table.to_string()
- << std::endl;
- std::cout << "**************" << std::endl;
- return _tn;
- }
- common::Time Coordinator::x_message(const common::Message& xmsg, common::Time t)
- {
- std::cout << "[" << get_name() << "] at " << t << ": BEFORE - x_message on "
- << xmsg.get_port_name() << " => "
- << "tl = " << _tl << " ; tn = " << _tn << std::endl;
- std::cout << "[" << get_name() << "] " << _link_list.to_string()
- << std::endl;
- assert(_tl <= t and t <= _tn);
- std::pair < common::Links::iterator, common::Links::iterator > result =
- _link_list.equal_range(common::Node(xmsg.get_port_name(), this));
- for (common::Links::iterator it_r = result.first; it_r != result.second;
- ++it_r) {
- _tn = dynamic_cast < devs::Simulator* >(
- (*it_r).second.get_model())->x_message(
- common::Message(it_r->second.get_port_name(),
- it_r->second.get_model(),
- xmsg.get_content()), t);
- _event_table.put(
- _tn,
- dynamic_cast < devs::Simulator* >(it_r->second.get_model()));
- }
- _tl = t;
- _tn = _event_table.get_current_time();
- std::cout << "[" << get_name() << "] at " << t << ": AFTER - x_message => "
- << "tl = " << _tl << " ; tn = " << _tn << std::endl;
- return _tn;
- }
- common::Time Coordinator::y_message(common::Messages messages, common::Time t)
- {
- std::cout << "[" << get_name() << "] at " << t << ": BEFORE - y_message => "
- << "tl = " << _tl << " ; tn = " << _tn << std::endl;
- std::cout << "[" << get_name() << "] at " << t << ": "
- << messages.to_string() << std::endl;
- if (not messages.empty()) {
- bool internal = false;
- while (not messages.empty()) {
- common::Message ymsg = messages.back();
- messages.pop_back();
- std::pair < common::Links::iterator ,
- common::Links::iterator > result_model =
- _link_list.equal_range(common::Node(ymsg.get_port_name(),
- ymsg.get_model()));
- for (common::Links::iterator it = result_model.first;
- it != result_model.second; ++it) {
- // event on output port of coupled model
- if (it->second.get_model() == this) {
- common::Messages ymessages;
- ymessages.push_back(
- common::Message(it->second.get_port_name(),
- it->second.get_model(),
- ymsg.get_content()));
- dynamic_cast < devs::Coordinator* >(
- get_parent())->y_message(ymessages, t);
- } else { // event on input port of internal model
- common::Message message(
- it->second.get_port_name(),
- it->second.get_model(),
- ymsg.get_content());
- common::Time tn = dynamic_cast < devs::Model* >(
- it->second.get_model())->x_message(message, t);
- _event_table.put(
- tn,
- dynamic_cast < devs::Model* >(it->second.get_model()));
- internal = true;
- }
- }
- }
- if (internal) {
- _tl = t;
- _tn = _event_table.get_current_time();
- }
- }
- std::cout << "[" << get_name() << "] at " << t << ": AFTER - y_message => "
- << "tl = " << _tl << " ; tn = " << _tn << std::endl;
- return _tn;
- }
- void Coordinator::observation(std::ostream& file) const
- {
- for (unsigned i = 0; i < _child_list.size(); i++) {
- _child_list[i]->observation(file);
- }
- }
- void Coordinator::add_child(Model* child)
- {
- _child_list.push_back(child);
- child->set_parent(this);
- }
- void Coordinator::add_link(const common::Node& source,
- const common::Node& destination)
- {
- _link_list.insert(std::pair < common::Node, common::Node >(source,
- destination));
- }
- } } // namespace paradevs devs
|