/**
* @file kernel/sss/Simulator.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 SSS_SIMULATOR
#define SSS_SIMULATOR 1
#include
#include
#include
#include
#include
#include
namespace artis {
namespace sss {
template
class Simulator : public common::Simulator,
public sss::Model {
typedef Simulator type;
public:
Simulator(const std::string& name, const typename Time::type& time_step,
const Parameters& parameters)
:
common::Model(name),
common::Simulator(name),
sss::Model(name),
_dynamics(name, parameters),
_time_step(time_step) { }
~Simulator() { }
virtual bool is_atomic() const { return common::Simulator::is_atomic(); }
virtual void restore(const common::context::State& state)
{
common::Simulator::restore(state);
_dynamics.restore(state);
}
virtual void save(context::State& state) const
{
common::Simulator::save(state);
_dynamics.save(state);
}
virtual std::string to_string(int level) const { return common::Simulator::to_string(level); }
virtual void finish(const typename Time::type& t)
{
#ifndef WITH_TRACE
(void) t;
#endif
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::FormalismType::SSS,
common::FunctionType::FINISH,
common::LevelType::FORMALISM);
common::Trace::trace().flush();
#endif
}
typename Time::type start(const typename Time::type& t)
{
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::FunctionType::I_MESSAGE)
<< ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
<< type::_tn;
common::Trace::trace().flush();
#endif
_dynamics.start(t);
type::_tl = t;
type::_tn = t;
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::FunctionType::I_MESSAGE)
<< ": AFTER => " << "tl = " << type::_tl << " ; tn = "
<< type::_tn;
common::Trace::trace().flush();
#endif
return type::_tn;
}
void observation(std::ostream& file) const
{
_dynamics.observation(file);
}
void output(const typename Time::type& t)
{
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::FunctionType::OUTPUT) << ": BEFORE";
common::Trace::trace().flush();
#endif
if (t == type::_tn) {
common::Bag bag = _dynamics.lambda(t);
if (not bag.empty()) {
for (auto& event : bag) {
event.set_model(this);
}
dynamic_cast < common::Coordinator* >(
type::get_parent())->dispatch_events(bag, t);
}
}
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::FunctionType::OUTPUT) << ": AFTER";
common::Trace::trace().flush();
#endif
}
void post_event(const typename Time::type& t,
const common::ExternalEvent& event)
{
#ifndef WITH_TRACE
(void)t;
#endif
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::FunctionType::POST_EVENT)
<< ": BEFORE => " << event.to_string();
common::Trace::trace().flush();
#endif
type::add_event(event);
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::FunctionType::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::FunctionType::S_MESSAGE)
<< ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
<< type::_tn;
common::Trace::trace().flush();
#endif
assert(t == type::_tn);
if (type::is_marked()) {
if (type::is_send()) {
type::_tl = t;
type::_tn = t + _time_step;
}
} else {
_dynamics.transition(type::get_bag(), t);
type::clear_bag();
}
#ifdef WITH_TRACE
common::Trace::trace()
<< common::TraceElement(type::get_name(), t,
common::FunctionType::S_MESSAGE)
<< ": AFTER => " << "tl = " << type::_tl << " ; tn = "
<< type::_tn;
common::Trace::trace().flush();
#endif
return type::_tn;
}
virtual void update_buffer(typename Time::type time) { _dynamics.update_buffer(time); }
private :
Dynamics _dynamics;
typename Time::type _time_step;
};
}
} // namespace artis sss
#endif