/**
* @file artis/kernel/AbstractModel.hpp
* @author See the AUTHORS file
*/
/*
* Copyright (C) 2012-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 __ARTIS_KERNEL_ABSTRACT_ATOMIC_MODEL_HPP
#define __ARTIS_KERNEL_ABSTRACT_ATOMIC_MODEL_HPP
#include
#include
#include
#include
#include
#include
#include
namespace artis {
namespace kernel {
template
class AbstractAtomicModel : public AbstractModel,
public States,
public Internals,
public Externals {
typedef AbstractModel type;
using Externals::restore;
using Internals::restore;
using States::restore;
using Externals::save;
using Internals::save;
using States::save;
public:
AbstractAtomicModel(const type* parent = 0)
:type(parent)
{
#ifdef WITH_TRACE
utils::Trace::trace()
<< utils::TraceElement(
true,
boost::core::demangle(typeid(T).name()),
utils::DoubleTime::null,
utils::CONSTRUCT);
utils::Trace::trace().flush();
#endif
}
virtual ~AbstractAtomicModel()
{
#ifdef WITH_TRACE
trace_element(true, utils::DoubleTime::null,
utils::DESTRUCT);
#endif
}
virtual const Node* atomic(unsigned int /* index */) const { return this; }
virtual void after(typename U::type t)
{
#ifdef WITH_TRACE
trace_element(true, t, utils::AFTER_COMPUTE);
trace_internals(true, t, utils::AFTER_COMPUTE);
#else
(void) t;
#endif
}
virtual void before(typename U::type t)
{
#ifdef WITH_TRACE
trace_element(true, t, utils::BEFORE_COMPUTE);
trace_internals(true, t, utils::BEFORE_COMPUTE);
trace_externals(true, t, utils::BEFORE_COMPUTE);
#else
(void) t;
#endif
}
virtual bool check(typename U::type t) const { return Externals::check(t); }
virtual void compute(typename U::type t, bool update) = 0;
virtual const Any& get(typename U::type t, unsigned int index) const
{
if (type::last_time != t) {
throw utils::InvalidGet("Variable not computed");
}
return Internals::get(index);
}
template
W get(typename U::type t, unsigned int index) const
{
if (type::last_time != t) {
throw utils::InvalidGet("Variable not computed");
}
Any value = Internals::get(index);
return static_cast < const T* >(this)->*(value.get());
}
virtual std::string get(const ValueTypeID& value_type, typename U::type t,
unsigned int index) const
{
if (type::last_time != t) {
throw utils::InvalidGet("Variable not computed");
}
Any value = Internals::get(index);
switch (value_type) {
case DOUBLE: {
std::ostringstream ss;
ss << std::setprecision(10)
<< static_cast < const T* >(this)->*(
value.get());
return ss.str();
}
case INT:
return std::to_string(
static_cast < const T* >(this)->*(value.get()));
case BOOL:
return std::to_string(
static_cast < const T* >(this)->*(value.get()));
default:
return "NA";
}
}
virtual void init(typename U::type t, const V& parameters) = 0;
virtual bool is_atomic() const { return true; }
bool is_computed(typename U::type t, unsigned int /* index */) const { return type::last_time == t; }
bool is_stable(typename U::type t) const { return type::last_time == t; }
virtual bool is_updated() const { return Externals::updated; }
virtual void restore(const context::State& state)
{
Externals::restore(this, state);
Internals::restore(this, state);
States::restore(this, state);
type::last_time = state.last_time();
}
virtual void save(context::State& state) const
{
Externals::save(this, state);
Internals::save(this, state);
States::save(this, state);
state.last_time(type::last_time);
}
virtual void stable() { Externals::updated = false; }
virtual void trace_element(typename U::type t,
utils::TraceType type = utils::NONE,
std::string comment = "") const { trace_element(false, t, type, comment); }
virtual void trace_internals(typename U::type t,
utils::TraceType type) const { trace_internals(false, t, type); }
virtual void trace_externals(typename U::type t,
utils::TraceType type = utils::NONE) const { trace_externals(false, t, type); }
virtual void trace_model(typename U::type t,
utils::TraceType type = utils::NONE) const { trace_model(false, t, type); }
private:
void trace_element(bool from_kernel, typename U::type t,
utils::TraceType type = utils::NONE,
std::string comment = "") const
{
utils::Trace::trace()
<< utils::TraceElement(
from_kernel,
AbstractAtomicModel::path(this),
t, type)
<< comment;
utils::Trace::trace().flush();
}
void trace_internals(bool from_kernel, typename U::type t,
utils::TraceType type) const
{
for (size_t i = 0; i < Internals::size(); ++i) {
if (not Internals::get(i).is_null()) {
utils::Trace::trace()
<< utils::TraceElement(
from_kernel,
AbstractAtomicModel::path(this),
t, type)
<< utils::KernelInfo(
Internals::name(i), true,
Internals::get(i).to_string(
dynamic_cast < const T* >(this))
);
utils::Trace::trace().flush();
}
}
}
void trace_externals(bool from_kernel, typename U::type t,
utils::TraceType type = utils::NONE) const
{
for (size_t i = 0; i < Externals::size(); ++i) {
if (not Externals::get(i).is_null()) {
utils::Trace::trace()
<< utils::TraceElement(
from_kernel,
AbstractAtomicModel::path(this),
t, type)
<< utils::KernelInfo(
Externals::name(i), false,
Externals::get(i).to_string(
dynamic_cast < const T* >(this))
);
utils::Trace::trace().flush();
}
}
}
void trace_model(bool from_kernel, typename U::type t,
utils::TraceType type = utils::NONE) const
{
trace_element(from_kernel, t, type);
trace_internals(from_kernel, t, type);
trace_externals(from_kernel, t, type);
}
};
}
}
#endif