123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- /**
- * @file kernel/pdevs/GraphManager.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-2015 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_GRAPH_MANANGER
- #define PDEVS_GRAPH_MANANGER 1
- #include <paradevs/common/Coordinator.hpp>
- #include <paradevs/common/Links.hpp>
- #include <paradevs/common/Model.hpp>
- #include <paradevs/common/Parameters.hpp>
- #include <paradevs/common/utils/String.hpp>
- #include <sstream>
- namespace paradevs { namespace pdevs {
- template < class Time, class SchedulerHandle,
- class GraphParameters = common::NoParameters >
- class GraphManager
- {
- public:
- GraphManager(common::Coordinator < Time, SchedulerHandle >* coordinator,
- const GraphParameters& /* parameters */) :
- _coordinator(coordinator)
- { }
- virtual ~GraphManager()
- { }
- void add_child(common::Model < Time, SchedulerHandle >* child)
- {
- _child_list.push_back(child);
- child->set_parent(_coordinator);
- }
- void add_link(common::Model < Time, SchedulerHandle >* src_model,
- const std::string& src_port_name,
- common::Model < Time, SchedulerHandle >* dst_model,
- const std::string& dst_port_name)
- {
- assert((src_model != _coordinator and
- dst_model != _coordinator and
- src_model->exist_out_port(src_port_name) and
- dst_model->exist_in_port(dst_port_name)) or
- (src_model == _coordinator and
- dst_model != _coordinator and
- src_model->exist_in_port(src_port_name) and
- dst_model->exist_in_port(dst_port_name)) or
- (src_model != _coordinator and
- dst_model == _coordinator and
- src_model->exist_out_port(src_port_name) and
- dst_model->exist_out_port(dst_port_name)));
- _link_list.add(src_model, src_port_name, dst_model, dst_port_name);
- }
- const common::Models < Time, SchedulerHandle >& children() const
- { return _child_list; }
- void dispatch_events(common::Bag < Time, SchedulerHandle > bag,
- typename Time::type t)
- {
- for (auto & ymsg : bag) {
- typename common::Links < Time,
- SchedulerHandle >::Result result_model =
- _link_list.find(ymsg.get_model(),
- ymsg.get_port_name());
- for (typename common::Links < Time, SchedulerHandle >::
- const_iterator it = result_model.first;
- it != result_model.second; ++it) {
- // event on output port of coupled Model
- if (it->second.get_model() == _coordinator) {
- common::Bag < Time, SchedulerHandle > ymessages;
- ymessages.push_back(
- common::ExternalEvent <
- Time, SchedulerHandle >(it->second,
- ymsg.get_content()));
- dynamic_cast < common::Coordinator <
- Time, SchedulerHandle >* >(
- _coordinator->get_parent())->dispatch_events(
- ymessages, t);
- } else { // event on input port of internal model
- it->second.get_model()->post_event(
- t, common::ExternalEvent <
- Time, SchedulerHandle >(
- it->second, ymsg.get_content()));
- }
- }
- }
- }
- bool exist_link(common::Model < Time, SchedulerHandle >* src_model,
- const std::string& src_port_name,
- common::Model < Time, SchedulerHandle >* dst_model,
- const std::string& dst_port_name) const
- {
- return _link_list.exist(src_model, src_port_name, dst_model,
- dst_port_name);
- }
- common::Coordinator < Time, SchedulerHandle >* get_coordinator() const
- { return _coordinator; }
- void post_event(typename Time::type t,
- const common::ExternalEvent < Time,
- SchedulerHandle >& event)
- {
- typename common::Links < Time, SchedulerHandle >::Result result =
- _link_list.find(_coordinator, event.get_port_name());
- for (typename common::Links < Time,
- SchedulerHandle >::const_iterator it_r =
- result.first; it_r != result.second; ++it_r) {
- it_r->second.get_model()->post_event(
- t, common::ExternalEvent <
- Time, SchedulerHandle >(it_r->second,
- event.get_content()));
- }
- }
- virtual std::string to_string(int level) const
- {
- std::ostringstream ss;
- ss << common::spaces(level * 2) << "Childs :" << std::endl;
- for (auto & child : _child_list) {
- ss << child->to_string(level + 1);
- }
- ss << _link_list.to_string(level);
- return ss.str();
- }
- private:
- common::Links < Time, SchedulerHandle > _link_list;
- common::Models < Time, SchedulerHandle > _child_list;
- common::Coordinator < Time, SchedulerHandle >* _coordinator;
- };
- } } // namespace paradevs pdevs
- #endif
|