123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- /**
- * @file common/Model.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-2022 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 <http://www.gnu.org/licenses/>.
- */
- #ifndef COMMON_MODEL
- #define COMMON_MODEL
- #include "artis-star/common/event/Bag.hpp"
- #include "artis-star/common/event/ExternalEvent.hpp"
- #include "artis-star/common/event/InternalEvent.hpp"
- #include <artis-star/common/Scheduler.hpp>
- #include "artis-star/common/event/Value.hpp"
- #include <artis-star/common/context/State.hpp>
- #include <algorithm>
- #include <cassert>
- #include <map>
- #include <sstream>
- namespace artis::common {
- namespace event {
- template<typename Time>
- class ExternalEvent;
- template<typename Time>
- class InternalEvent;
- template<typename Time>
- class Bag;
- }
- struct Port {
- unsigned int index;
- std::string name;
- };
- typedef std::vector<unsigned int> Ports;
- typedef std::map<unsigned int, std::string> PortMap;
- template<typename Time>
- class Model {
- public:
- Model(const std::string &name)
- :
- _tl(0), _tn(0), _parent(0), _name(name), _inputs(0) {}
- virtual ~Model() {
- if (_inputs) {
- delete _inputs;
- }
- }
- // structure
- void add_in_port(const Port &port) {
- assert(not exist_in_port(port.index));
- _in_ports.push_back(port.index);
- _in_port_map[port.index] = port.name;
- }
- void add_out_port(const Port &port) {
- assert(not exist_out_port(port.index));
- _out_ports.push_back(port.index);
- _out_port_map[port.index] = port.name;
- }
- void delete_in_port(const Port &port) {
- assert(not exist_in_port(port.index));
- _in_ports.erase(std::find(_in_ports.begin(), _in_ports.end(),
- port.index));
- _in_port_map.erase(port.index);
- }
- void delete_out_port(const Port &port) {
- assert(not exist_out_port(port.index));
- _out_ports.erase(std::find(_out_ports.begin(), _out_ports.end(),
- port.index));
- _out_port_map.erase(port.index);
- }
- bool exist_in_port(unsigned int port_index) const {
- return _in_port_map.find(port_index) != _in_port_map.end();
- }
- bool exist_out_port(unsigned int port_index) const {
- return _out_port_map.find(port_index) != _out_port_map.end();
- }
- std::string get_full_name() const {
- if (_parent) {
- return _parent->get_full_name() + ":" + _name;
- } else {
- return _name;
- }
- }
- std::string get_in_port_name(unsigned int port_index) const {
- assert(exist_in_port(port_index));
- return _in_port_map.find(port_index)->second;
- }
- size_t get_in_port_number() const { return _in_port_map.size(); }
- std::string get_out_port_name(unsigned int port_index) const {
- assert(exist_out_port(port_index));
- return _out_port_map.find(port_index)->second;
- }
- size_t get_out_port_number() const { return _out_port_map.size(); }
- const std::string &get_name() const { return _name; }
- Model<Time> *get_parent() const { return _parent; }
- virtual const Model<Time> *get_sub_model(unsigned int index) const {
- (void) index;
- assert(false);
- return nullptr;
- }
- virtual const Model<Time> *get_sub_model(unsigned int index,
- unsigned int rank) const {
- (void) index;
- (void) rank;
- assert(false);
- return nullptr;
- }
- virtual unsigned int get_sub_model_number(unsigned int index) const {
- (void) index;
- assert(false);
- return 0;
- }
- virtual bool is_atomic() const = 0;
- virtual bool is_remote() const { return false; }
- virtual std::string observable_name(unsigned int observable_index) const {
- (void) observable_index;
- assert(false);
- return std::string();
- }
- std::string path() const {
- return (_parent != nullptr ? _parent->path() : "") + ":" + get_name();
- }
- virtual void restore(const common::context::State<Time> &state) {
- _tl = state.last_time();
- _tn = state.next_time();
- }
- virtual void save(common::context::State<Time> &state) const {
- state.last_time(_tl);
- state.next_time(_tn);
- }
- void set_parent(Model<Time> *parent) { _parent = parent; }
- virtual std::string to_string(int /* level */) const = 0;
- // event
- void add_event(const common::event::ExternalEvent<Time> &message) {
- if (_inputs == 0) {
- _inputs = new common::event::Bag<Time>;
- }
- _inputs->push_back(message);
- }
- void clear_bag() {
- if (_inputs) {
- delete _inputs;
- _inputs = 0;
- }
- }
- unsigned int event_number() const {
- return _inputs ? _inputs->size() : 0;
- }
- const common::event::Bag<Time> &get_bag() {
- if (_inputs == 0) {
- _inputs = new common::event::Bag<Time>;
- }
- return *_inputs;
- }
- // time
- typename Time::type get_tl() const { return _tl; }
- typename Time::type get_tn() const { return _tn; }
- // devs methods
- virtual void finish(const typename Time::type & /* t */) = 0;
- virtual common::event::Value observe(const typename Time::type & /* t */,
- unsigned int /* index */) const = 0;
- virtual void output(const typename Time::type & /* t */) = 0;
- virtual void post_event(const typename Time::type & /* t */,
- const common::event::ExternalEvent<Time> & /* event */) = 0;
- virtual typename Time::type start(const typename Time::type & /* t */) = 0;
- virtual typename Time::type transition(const typename Time::type & /* t */) = 0;
- virtual typename Time::type lookahead(const typename Time::type &t) const { return t; }
- // scheduler
- void handle(SchedulerHandle handle) { _handle.handle(handle); }
- const SchedulerHandle &handle() const { return _handle.handle(); }
- protected:
- typename Time::type _tl;
- typename Time::type _tn;
- private :
- Model<Time> *_parent;
- std::string _name;
- Ports _in_ports;
- PortMap _in_port_map;
- Ports _out_ports;
- PortMap _out_port_map;
- common::event::Bag<Time> *_inputs;
- SchedulerHandle _handle;
- };
- template<typename Time>
- class ModelMap : public std::map<unsigned int, Model<Time> *> {
- public:
- ModelMap() {}
- virtual ~ModelMap() {}
- std::string to_string() const {
- std::ostringstream ss;
- ss << "{ ";
- for (typename ModelMap<Time>::const_iterator it = ModelMap<Time>::begin(); it != ModelMap<Time>::end(); ++it) {
- ss << it->second->get_name() << " ";
- }
- ss << "}";
- return ss.str();
- }
- };
- template<typename Time>
- class Models : public std::vector<Model<Time> *> {
- public:
- Models() {}
- virtual ~Models() {}
- std::string to_string() const {
- std::ostringstream ss;
- ss << "{ ";
- for (typename Models<Time>::const_iterator it = Models<Time>::begin(); it != Models<Time>::end(); ++it) {
- ss << (*it)->get_name() << " ";
- }
- ss << "}";
- return ss.str();
- }
- };
- template<typename Time>
- class ModelsMap : public std::map<unsigned int, Models<Time> > {
- public:
- ModelsMap() {}
- virtual ~ModelsMap() {}
- std::string to_string() const {
- std::ostringstream ss;
- ss << "{ ";
- for (typename ModelsMap<Time>::const_iterator it = ModelsMap<Time>::begin(); it != ModelsMap<Time>::end(); ++it) {
- ss << it->second.to_string() << " ";
- }
- ss << "}";
- return ss.str();
- }
- };
- } // namespace artis common
- #endif
|