/**
* @file kernel/dtss/Coordinator.hpp
* @author The ARTIS Development Team
* See the AUTHORS or Authors.txt file
*/
/*
* ARTIS - the multimodeling and simulation environment
* This file is a part of the ARTIS environment
*
* Copyright (C) 2013-2019 ULCO http://www.univ-littoral.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 .
*/
#ifndef DTSS_COORDINATOR
#define DTSS_COORDINATOR 1
#include
#include
#include
#include
namespace artis {
namespace dtss {
template
class Parameters {
public:
Parameters(typename Time::type time_step)
:_time_step(time_step) { }
typename Time::type _time_step;
};
template,
class GraphParameters = common::NoParameters>
class Coordinator : public common::Coordinator {
typedef Coordinator type;
public:
typedef Parameters parameters_type;
typedef GraphParameters graph_parameters_type;
Coordinator(const std::string& name, const Parameters& parameters, const GraphParameters& graph_parameters)
:
common::Model(name),
common::Coordinator(name),
_graph_manager(this, parameters, graph_parameters),
_time_step(parameters._time_step) { }
virtual ~Coordinator() { }
common::GraphManager& get_graph_manager() { return _graph_manager; }
const common::GraphManager& get_graph_manager() const { return _graph_manager; }
typename Time::type start(const typename Time::type& t)
{
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::TraceType::I_MESSAGE)
<< ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
<< type::_tn;
common::Trace::trace().flush();
#endif
assert(_graph_manager.children().size() > 0);
for (auto& child : _graph_manager.children()) {
child->start(t);
}
type::_tl = t;
type::_tn = t;
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::TraceType::I_MESSAGE)
<< ": AFTER => " << "tl = " << type::_tl << " ; tn = "
<< type::_tn;
common::Trace::trace().flush();
#endif
return type::_tn;
}
typename Time::type dispatch_events(const common::Bag& bag,
const typename Time::type& t)
{
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::TraceType::Y_MESSAGE)
<< ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
<< type::_tn << " ; bag = " << bag.to_string();
common::Trace::trace().flush();
#endif
_graph_manager.dispatch_events(bag, t);
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::TraceType::Y_MESSAGE)
<< ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
<< type::_tn;
common::Trace::trace().flush();
#endif
return type::_tn;
}
common::Value observe(const typename Time::type& /* t */,
unsigned int /* index */) const
{
assert(false);
return common::Value();
}
void output(const typename Time::type& t)
{
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::TraceType::OUTPUT)
<< ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
<< type::_tn;
common::Trace::trace().flush();
#endif
if (t == type::_tn) {
for (auto& model : _graph_manager.children()) {
model->output(t);
}
}
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::TraceType::OUTPUT)
<< ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
common::Trace::trace().flush();
#endif
}
void post_event(const typename Time::type& t,
const common::ExternalEvent& event)
{
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::TraceType::POST_EVENT)
<< ": BEFORE => " << event.to_string();
common::Trace::trace().flush();
#endif
if (t == type::_tn) {
_graph_manager.post_event(t, event);
} else {
_policy(t, event, type::_tl, type::_tn);
}
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::TraceType::POST_EVENT)
<< ": AFTER => " << event.to_string();
common::Trace::trace().flush();
#endif
}
typename Time::type transition(const typename Time::type& t)
{
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::TraceType::S_MESSAGE)
<< ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
<< type::_tn;
common::Trace::trace().flush();
#endif
if (t == type::_tn) {
for (auto& event : _policy.bag()) {
post_event(t, event);
}
_policy.clear();
for (auto& model : _graph_manager.children()) {
model->transition(t);
}
type::_tl = t;
type::_tn = t + _time_step;
}
type::clear_bag();
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::TraceType::S_MESSAGE)
<< ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn;
common::Trace::trace().flush();
#endif
return type::_tn;
}
private:
GraphManager _graph_manager;
typename Time::type _time_step;
Policy _policy;
};
}
} // namespace artis dtss
#endif