123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- /**
- * @file tests/devs/models.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 <http://www.gnu.org/licenses/>.
- */
- #ifndef TESTS_DEVS_MODELS_HPP
- #define TESTS_DEVS_MODELS_HPP
- #include <artis-star/common/time/DoubleTime.hpp>
- #include <artis-star/common/utils/Trace.hpp>
- #include <artis-star/kernel/devs/Dynamics.hpp>
- #include <chrono>
- #include <iostream>
- namespace artis {
- namespace tests {
- namespace devs {
- struct data {
- double x;
- double y;
- data()
- : x(0), y(0) {}
- data(double _x, double _y)
- : x(_x), y(_y) {}
- };
- class A : public artis::devs::Dynamics<common::DoubleTime, A> {
- public:
- A(const std::string &name,
- const artis::devs::Context<common::DoubleTime, A, artis::common::NoParameters> &context)
- :
- artis::devs::Dynamics<common::DoubleTime, A>(name, context) {
- }
- ~A() override = default;
- void dint(typename common::DoubleTime::type t) override {
- #ifndef WITH_TRACE
- (void)t;
- #else
- common::Trace<common::DoubleTime>::trace()
- << common::TraceElement<common::DoubleTime>(get_name(), t,
- common::FormalismType::DEVS,
- common::FunctionType::DELTA_INT,
- common::LevelType::USER);
- common::Trace<common::DoubleTime>::trace().flush();
- #endif
- if (_phase == WAIT) {
- ++_value.x;
- --_value.y;
- _phase = SEND;
- } else if (_phase == SEND) {
- _phase = WAIT;
- }
- }
- void
- dext(typename common::DoubleTime::type t, typename common::DoubleTime::type /* e */,
- const common::ExternalEvent<common::DoubleTime> &msg) override {
- #ifndef WITH_TRACE
- (void)t;
- (void)msgs;
- #else
- common::Trace<common::DoubleTime>::trace()
- << common::TraceElement<common::DoubleTime>(get_name(), t,
- common::FormalismType::DEVS,
- common::FunctionType::DELTA_EXT,
- common::LevelType::USER)
- << "event = " << msg.to_string();
- common::Trace<common::DoubleTime>::trace().flush();
- #endif
- _phase = SEND;
- }
- void start(typename common::DoubleTime::type t) override {
- #ifndef WITH_TRACE
- (void)t;
- #else
- common::Trace<common::DoubleTime>::trace()
- << common::TraceElement<common::DoubleTime>(get_name(), t,
- common::FormalismType::DEVS,
- common::FunctionType::START,
- common::LevelType::USER);
- common::Trace<common::DoubleTime>::trace().flush();
- #endif
- _phase = SEND;
- }
- typename common::DoubleTime::type
- ta(typename common::DoubleTime::type t) const override {
- #ifndef WITH_TRACE
- (void)t;
- #else
- common::Trace<common::DoubleTime>::trace()
- << common::TraceElement<common::DoubleTime>(get_name(), t,
- common::FormalismType::DEVS,
- common::FunctionType::TA,
- common::LevelType::USER);
- common::Trace<common::DoubleTime>::trace().flush();
- #endif
- if (_phase == WAIT) {
- return 1;
- } else {
- return 0;
- }
- }
- common::ExternalEvent<common::DoubleTime>
- lambda(typename common::DoubleTime::type t) const override {
- #ifndef WITH_TRACE
- (void)t;
- #endif
- if (_phase == SEND) {
- artis::common::ExternalEvent<common::DoubleTime> msg(_value);
- #ifdef WITH_TRACE
- common::Trace<common::DoubleTime>::trace()
- << common::TraceElement<common::DoubleTime>(get_name(), t,
- common::FormalismType::DEVS,
- common::FunctionType::LAMBDA,
- common::LevelType::USER)
- << "event = " << msg.to_string();
- common::Trace<common::DoubleTime>::trace().flush();
- #endif
- return msg;
- } else {
- #ifdef WITH_TRACE
- common::Trace<common::DoubleTime>::trace()
- << common::TraceElement<common::DoubleTime>(get_name(), t,
- common::FormalismType::DEVS,
- common::FunctionType::LAMBDA,
- common::LevelType::USER)
- << "no event";
- common::Trace<common::DoubleTime>::trace().flush();
- #endif
- return artis::common::ExternalEvent<common::DoubleTime>::Void;
- }
- }
- private:
- enum Phase {
- WAIT, SEND
- };
- Phase _phase;
- data _value;
- };
- class B : public artis::devs::Dynamics<common::DoubleTime, B> {
- public:
- B(const std::string &name,
- const artis::devs::Context<common::DoubleTime, B, artis::common::NoParameters> &context)
- :
- artis::devs::Dynamics<common::DoubleTime, B>(name, context),
- _value(0) {
- }
- ~B() override = default;
- void dint(typename common::DoubleTime::type t) override {
- #ifndef WITH_TRACE
- (void)t;
- #else
- common::Trace<common::DoubleTime>::trace()
- << common::TraceElement<common::DoubleTime>(get_name(), t,
- common::FormalismType::DEVS,
- common::FunctionType::DELTA_INT,
- common::LevelType::USER);
- common::Trace<common::DoubleTime>::trace().flush();
- #endif
- if (_phase == SEND) {
- _phase = WAIT;
- }
- }
- void
- dext(typename common::DoubleTime::type t, typename common::DoubleTime::type /* e */,
- const common::ExternalEvent<common::DoubleTime> &msg) override {
- #ifndef WITH_TRACE
- (void)t;
- (void)msgs;
- #else
- common::Trace<common::DoubleTime>::trace()
- << common::TraceElement<common::DoubleTime>(get_name(), t,
- common::FormalismType::DEVS,
- common::FunctionType::DELTA_EXT,
- common::LevelType::USER)
- << "event = " << msg.to_string();
- common::Trace<common::DoubleTime>::trace().flush();
- #endif
- _phase = SEND;
- }
- void start(typename common::DoubleTime::type t) override {
- #ifndef WITH_TRACE
- (void)t;
- #else
- common::Trace<common::DoubleTime>::trace()
- << common::TraceElement<common::DoubleTime>(get_name(), t,
- common::FormalismType::DEVS,
- common::FunctionType::START,
- common::LevelType::USER);
- common::Trace<common::DoubleTime>::trace().flush();
- #endif
- _phase = WAIT;
- }
- typename common::DoubleTime::type ta(
- typename common::DoubleTime::type t) const override {
- #ifndef WITH_TRACE
- (void)t;
- #else
- common::Trace<common::DoubleTime>::trace()
- << common::TraceElement<common::DoubleTime>(get_name(), t,
- common::FormalismType::DEVS,
- common::FunctionType::TA,
- common::LevelType::USER);
- common::Trace<common::DoubleTime>::trace().flush();
- #endif
- if (_phase == WAIT) {
- return common::DoubleTime::infinity;
- } else {
- return 0;
- }
- }
- common::ExternalEvent<common::DoubleTime> lambda(
- typename common::DoubleTime::type t) const override {
- #ifndef WITH_TRACE
- (void)t;
- #endif
- if (_phase == SEND) {
- artis::common::ExternalEvent<common::DoubleTime> msg(_value);
- #ifdef WITH_TRACE
- common::Trace<common::DoubleTime>::trace()
- << common::TraceElement<common::DoubleTime>(get_name(), t,
- common::FormalismType::DEVS,
- common::FunctionType::LAMBDA,
- common::LevelType::USER)
- << "event = " << msg.to_string();
- common::Trace<common::DoubleTime>::trace().flush();
- #endif
- return msg;
- } else {
- #ifdef WITH_TRACE
- common::Trace<common::DoubleTime>::trace()
- << common::TraceElement<common::DoubleTime>(get_name(), t,
- common::FormalismType::DEVS,
- common::FunctionType::LAMBDA,
- common::LevelType::USER)
- << "no event";
- common::Trace<common::DoubleTime>::trace().flush();
- #endif
- return artis::common::ExternalEvent<common::DoubleTime>::Void;
- }
- }
- private:
- enum Phase {
- WAIT, SEND
- };
- Phase _phase;
- double _value;
- };
- }
- }
- } // namespace artis tests devs
- #endif
|