Parcourir la source

Add new formalism tests: dsde

Eric Ramat il y a 5 ans
Parent
commit
2a8cc8c624

+ 1 - 0
src/tests/CMakeLists.txt

@@ -1,3 +1,4 @@
+ADD_SUBDIRECTORY(dsde)
 ADD_SUBDIRECTORY(dtss)
 ADD_SUBDIRECTORY(mixed)
 ADD_SUBDIRECTORY(mpi)

+ 12 - 0
src/tests/dsde/CMakeLists.txt

@@ -0,0 +1,12 @@
+add_definitions(-DWITH_TRACE)
+
+INCLUDE_DIRECTORIES(
+        ${CMAKE_SOURCE_DIR}/src
+        ${ARTIS_INCLUDE_DIRS}
+        ${Boost_INCLUDE_DIRS})
+
+LINK_DIRECTORIES()
+
+ADD_EXECUTABLE(dsde-tests graph_manager.hpp models.hpp tests.cpp)
+
+TARGET_LINK_LIBRARIES(dsde-tests)

+ 168 - 0
src/tests/dsde/graph_manager.hpp

@@ -0,0 +1,168 @@
+/**
+ * @file tests/dsde/graph_manager.cpp
+ * @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_DSDE_GRAPH_MANAGER_HPP
+#define TESTS_DSDE_GRAPH_MANAGER_HPP
+
+#include <artis-star/kernel/dsde/Coordinator.hpp>
+#include <artis-star/kernel/dsde/GraphManager.hpp>
+#include <artis-star/kernel/pdevs/Simulator.hpp>
+
+#include <tests/dsde/models.hpp>
+
+namespace artis {
+    namespace tests {
+        namespace dsde {
+
+            class FlatGraphManager :
+                    public artis::dsde::GraphManager<common::DoubleTime> {
+            public:
+                enum submodels {
+                    BEEP = 1
+                };
+
+                FlatGraphManager(common::Coordinator<common::DoubleTime>* coordinator,
+                        const artis::common::NoParameters& parameters,
+                        const artis::common::NoParameters& graph_parameters)
+                        :
+                        artis::dsde::GraphManager<common::DoubleTime>(coordinator, parameters,
+                                graph_parameters),
+                        _beep("beep", parameters)
+                {
+                    add_child(BEEP, &_beep);
+                }
+
+                ~FlatGraphManager() override = default;
+
+            private:
+                artis::pdevs::Simulator<common::DoubleTime, Beep> _beep;
+            };
+
+            class Executive :
+                    public artis::dsde::Executive<common::DoubleTime, Executive> {
+            public:
+                Executive(
+                        const artis::dsde::ExecutiveContext<common::DoubleTime, Executive, artis::common::NoParameters, artis::common::NoParameters>& context,
+                        artis::dsde::GraphManager<common::DoubleTime, artis::common::NoParameters, artis::common::NoParameters>& graph_manager)
+                        :
+                        artis::dsde::Executive<common::DoubleTime, Executive>(context,
+                                graph_manager), _index(0), _up(true) { }
+
+                ~Executive() override = default;
+
+                void dint(typename common::DoubleTime::type t) override
+                {
+
+#ifndef WITH_TRACE
+                    (void)t;
+#endif
+
+#ifdef WITH_TRACE
+                    common::Trace<common::DoubleTime>::trace()
+                            << common::TraceElement<common::DoubleTime>(get_name(), t,
+                                    common::FormalismType::DSDE,
+                                    common::FunctionType::DELTA_INT,
+                                    common::LevelType::USER);
+                    common::Trace<common::DoubleTime>::trace().flush();
+#endif
+
+                    if (_up) {
+                        ++_index;
+                    } else {
+                        --_index;
+                    }
+                    if (_index == 4 and _up) {
+                        _up = false;
+                        --_index;
+                    }
+                    if (_up) {
+                        graph_manager().add_dynamic_child(_index,
+                                new artis::pdevs::Simulator<common::DoubleTime, Beep>(
+                                        "beep" + std::to_string(_index),
+                                        artis::common::NoParameters()));
+                        graph_manager().add_link(FlatGraphManager::BEEP, Beep::OUT, _index,
+                                Beep::IN);
+                    } else {
+                        if (_index == 3) {
+                            graph_manager().remove_dynamic_child(t, _index);
+                        } else if (_index == 2) {
+                            graph_manager().remove_link(FlatGraphManager::BEEP, Beep::OUT, _index,
+                                    Beep::IN);
+                        }
+                    }
+                }
+
+                typename common::DoubleTime::type
+                start(typename common::DoubleTime::type t) override
+                {
+
+#ifndef WITH_TRACE
+                    (void)t;
+#endif
+
+#ifdef WITH_TRACE
+                    common::Trace<common::DoubleTime>::trace()
+                            << common::TraceElement<common::DoubleTime>(get_name(), t,
+                                    common::FormalismType::DSDE,
+                                    common::FunctionType::START,
+                                    common::LevelType::USER);
+                    common::Trace<common::DoubleTime>::trace().flush();
+#endif
+
+                    _index = 1;
+                    _up = true;
+                    return 1;
+                }
+
+                typename common::DoubleTime::type
+                ta(typename common::DoubleTime::type t) const override
+                {
+#ifndef WITH_TRACE
+                    (void)t;
+#endif
+
+#ifdef WITH_TRACE
+                    common::Trace<common::DoubleTime>::trace()
+                            << common::TraceElement<common::DoubleTime>(get_name(), t,
+                                    common::FormalismType::DSDE,
+                                    common::FunctionType::TA,
+                                    common::LevelType::USER);
+                    common::Trace<common::DoubleTime>::trace().flush();
+#endif
+
+                    return 2;
+                }
+
+            private:
+                int _index;
+                bool _up;
+            };
+
+        }
+    }
+} // namespace artis tests dsde
+
+#endif

+ 212 - 0
src/tests/dsde/models.hpp

@@ -0,0 +1,212 @@
+/**
+ * @file tests/dsde/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_DSDE_MODELS_HPP
+#define TESTS_DSDE_MODELS_HPP
+
+#include <artis-star/common/time/DoubleTime.hpp>
+#include <artis-star/common/utils/Trace.hpp>
+
+#include <artis-star/kernel/dsde/Coordinator.hpp>
+#include <artis-star/kernel/dsde/Executive.hpp>
+#include <artis-star/kernel/pdevs/Dynamics.hpp>
+
+#include <tests/dsde/graph_manager.hpp>
+
+namespace artis {
+    namespace tests {
+        namespace dsde {
+
+            class Beep :
+                    public artis::pdevs::Dynamics<common::DoubleTime, Beep> {
+            public:
+                enum inputs {
+                    IN
+                };
+                enum outputs {
+                    OUT
+                };
+
+                Beep(const std::string& name,
+                        const artis::pdevs::Context<common::DoubleTime, Beep, artis::common::NoParameters>& context)
+                        :
+                        artis::pdevs::Dynamics<common::DoubleTime, Beep>(name, context), _value(0)
+                {
+                    input_ports({{IN, "in"}});
+                    output_ports({{OUT, "out"}});
+                }
+
+                ~Beep() override = default;
+
+                void dint(typename common::DoubleTime::type t) override
+                {
+
+#ifndef WITH_TRACE
+                    (void)t;
+#endif
+
+#ifdef WITH_TRACE
+                    common::Trace<common::DoubleTime>::trace()
+                            << common::TraceElement<common::DoubleTime>(get_name(), t,
+                                    common::FormalismType::PDEVS,
+                                    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::Bag<common::DoubleTime>& bag) override
+                {
+
+#ifndef WITH_TRACE
+                    (void)t;
+                    (void)bag;
+#endif
+
+#ifdef WITH_TRACE
+                    common::Trace<common::DoubleTime>::trace()
+                            << common::TraceElement<common::DoubleTime>(get_name(), t,
+                                    common::FormalismType::PDEVS,
+                                    common::FunctionType::DELTA_EXT,
+                                    common::LevelType::USER)
+                            << "messages = " << bag.to_string();
+                    common::Trace<common::DoubleTime>::trace().flush();
+#endif
+
+                    _phase = SEND;
+                }
+
+                void dconf(typename common::DoubleTime::type t,
+                        typename common::DoubleTime::type /* e */,
+                        const common::Bag<common::DoubleTime>& bag) override
+                {
+
+#ifndef WITH_TRACE
+                    (void)t;
+                    (void)bag;
+#endif
+
+#ifdef WITH_TRACE
+                    common::Trace<common::DoubleTime>::trace()
+                            << common::TraceElement<common::DoubleTime>(get_name(), t,
+                                    common::FormalismType::PDEVS,
+                                    common::FunctionType::DELTA_CONF,
+                                    common::LevelType::USER)
+                            << "messages = " << bag.to_string();
+                    common::Trace<common::DoubleTime>::trace().flush();
+#endif
+
+                }
+
+                typename common::DoubleTime::type
+                start(typename common::DoubleTime::type t) override
+                {
+
+#ifndef WITH_TRACE
+                    (void)t;
+#endif
+
+#ifdef WITH_TRACE
+                    common::Trace<common::DoubleTime>::trace()
+                            << common::TraceElement<common::DoubleTime>(get_name(), t,
+                                    common::FormalismType::PDEVS,
+                                    common::FunctionType::START,
+                                    common::LevelType::USER);
+                    common::Trace<common::DoubleTime>::trace().flush();
+#endif
+
+                    _phase = WAIT;
+                    return 0;
+                }
+
+                typename common::DoubleTime::type
+                ta(typename common::DoubleTime::type t) const override
+                {
+
+#ifndef WITH_TRACE
+                    (void)t;
+#endif
+
+#ifdef WITH_TRACE
+                    common::Trace<common::DoubleTime>::trace()
+                            << common::TraceElement<common::DoubleTime>(get_name(), t,
+                                    common::FormalismType::PDEVS,
+                                    common::FunctionType::TA,
+                                    common::LevelType::USER);
+                    common::Trace<common::DoubleTime>::trace().flush();
+#endif
+
+                    if (_phase == WAIT) {
+                        return (rand() % 10) / 2. + 1;
+                    } else {
+                        return 0;
+                    }
+                }
+
+                common::Bag<common::DoubleTime>
+                lambda(typename common::DoubleTime::type t) const override
+                {
+
+#ifndef WITH_TRACE
+                    (void)t;
+#endif
+                    common::Bag<common::DoubleTime> bag;
+
+                    bag.push_back(artis::common::ExternalEvent<common::DoubleTime>(OUT, _value));
+
+#ifdef WITH_TRACE
+                    common::Trace<common::DoubleTime>::trace()
+                            << common::TraceElement<common::DoubleTime>(get_name(), t,
+                                    common::FormalismType::PDEVS,
+                                    common::FunctionType::LAMBDA,
+                                    common::LevelType::USER)
+                            << "messages = " << bag.to_string();
+                    common::Trace<common::DoubleTime>::trace().flush();
+#endif
+
+                    return bag;
+                }
+
+            private:
+                enum Phase {
+                    WAIT, SEND
+                };
+
+                Phase _phase;
+                double _value;
+            };
+
+        }
+    }
+} // namespace artis tests dsde
+
+#endif

+ 119 - 0
src/tests/dsde/tests.cpp

@@ -0,0 +1,119 @@
+/**
+ * @file tests/dsde/tests.cpp
+ * @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/>.
+ */
+
+#include <tests/dsde/graph_manager.hpp>
+#include <tests/dsde/models.hpp>
+
+#include <artis-star/common/RootCoordinator.hpp>
+
+#define CATCH_CONFIG_MAIN
+
+#include <tests/catch.hpp>
+
+using namespace artis::tests::dsde;
+using namespace artis::common;
+
+TEST_CASE("dsde/flat", "run")
+{
+    artis::common::context::Context<artis::common::DoubleTime> context(0, 10);
+    artis::common::RootCoordinator<
+            DoubleTime, artis::dsde::Coordinator<
+                    DoubleTime,
+                    artis::tests::dsde::FlatGraphManager,
+                    artis::tests::dsde::Executive>
+    > rc(context, "root");
+
+    artis::common::Trace<DoubleTime>::trace().clear();
+    rc.run(context);
+
+//    std::cout << artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+//            "root").to_string() << std::endl;
+//    std::cout << artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+//            "beep2").to_string() << std::endl;
+//    std::cout << artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+//            "beep3").to_string() << std::endl;
+//    std::cout << artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+//            "executive").to_string() << std::endl;
+
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep").filter_function_type(artis::common::FunctionType::START).filter_time(0).size()
+            == 1);
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep2").filter_function_type(artis::common::FunctionType::START).filter_time(1).size()
+            == 1);
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep3").filter_function_type(artis::common::FunctionType::START).filter_time(3).size()
+            == 1);
+
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep2").filter_function_type(artis::common::FunctionType::DELTA_INT).filter_time(
+            1).size()
+            == 1);
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep2").filter_function_type(artis::common::FunctionType::TA).filter_time(1).size()
+            == 1);
+
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep2").filter_function_type(artis::common::FunctionType::DELTA_INT).filter_time(
+            2.5).size()
+            == 1);
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep2").filter_function_type(artis::common::FunctionType::DELTA_EXT).filter_time(
+            2.5).size()
+            == 1);
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep2").filter_function_type(artis::common::FunctionType::TA).filter_time(2.5).size()
+            == 2);
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep2").filter_function_type(artis::common::FunctionType::LAMBDA).filter_time(2.5).size()
+            == 1);
+
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep2").filter_function_type(artis::common::FunctionType::DELTA_INT).filter_time(
+            6).size()
+            == 1);
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep2").filter_function_type(artis::common::FunctionType::TA).filter_time(6).size()
+            == 1);
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep2").filter_function_type(artis::common::FunctionType::LAMBDA).filter_time(6).size()
+            == 1);
+
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep3").filter_function_type(artis::common::FunctionType::DELTA_INT).filter_time(
+            3).size()
+            == 1);
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep3").filter_function_type(artis::common::FunctionType::TA).filter_time(3).size()
+            == 1);
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep3").filter_function_type(artis::common::FunctionType::LAMBDA).filter_time(3).size()
+            == 1);
+
+    REQUIRE(artis::common::Trace<DoubleTime>::trace().elements().filter_model_name(
+            "beep3").filter_function_type(artis::common::FunctionType::FINISH).filter_time(5).size()
+            == 1);
+}

+ 1 - 1
src/tests/dtss/graph_manager.hpp

@@ -25,7 +25,7 @@
  */
 
 #ifndef TESTS_DTSS_GRAPH_MANAGER_HPP
-#define TESTS_DTSS_GRAPH_MANAGER_HPP 1
+#define TESTS_DTSS_GRAPH_MANAGER_HPP
 
 #include <tests/dtss/models.hpp>
 

+ 1 - 1
src/tests/dtss/models.hpp

@@ -25,7 +25,7 @@
  */
 
 #ifndef TESTS_DTSS_MODELS_HPP
-#define TESTS_DTSS_MODELS_HPP 1
+#define TESTS_DTSS_MODELS_HPP
 
 #include <artis-star/common/time/DoubleTime.hpp>
 #include <artis-star/common/utils/Trace.hpp>

+ 1 - 1
src/tests/mixed/graph_manager.hpp

@@ -25,7 +25,7 @@
  */
 
 #ifndef TESTS_MIXED_GRAPH_MANAGER_HPP
-#define TESTS_MIXED_GRAPH_MANAGER_HPP 1
+#define TESTS_MIXED_GRAPH_MANAGER_HPP
 
 #include <tests/mixed/models.hpp>
 

+ 1 - 1
src/tests/mixed/models.hpp

@@ -25,7 +25,7 @@
  */
 
 #ifndef TESTS_MIXED_MODELS_HPP
-#define TESTS_MIXED_MODELS_HPP 1
+#define TESTS_MIXED_MODELS_HPP
 
 #include <artis-star/common/time/DoubleTime.hpp>
 #include <artis-star/common/utils/Trace.hpp>

+ 3 - 3
src/tests/mpi/graph_manager.hpp

@@ -25,7 +25,7 @@
  */
 
 #ifndef TESTS_PDEVS_MPI_GRAPH_MANAGER_HPP
-#define TESTS_PDEVS_MPI_GRAPH_MANAGER_HPP 1
+#define TESTS_PDEVS_MPI_GRAPH_MANAGER_HPP
 
 #include <tests/multithreading/lifegame/models.hpp>
 #include <tests/pdevs/graph_manager.hpp>
@@ -353,9 +353,9 @@ namespace artis {
 
                 void init()
                 {
-                    first.set_sender(dynamic_cast < ParentCoordinator* >(get_coordinator())->get_sender());
+                    first.set_sender(dynamic_cast < ParentCoordinator* >(coordinator())->get_sender());
                     for (Nexts::const_iterator it = nexts.begin(); it != nexts.end(); ++it) {
-                        (*it)->set_sender(dynamic_cast < ParentCoordinator*  >(get_coordinator())->get_sender());
+                        (*it)->set_sender(dynamic_cast < ParentCoordinator*  >(coordinator())->get_sender());
                     }
                 }
 

+ 1 - 1
src/tests/multithreading/lifegame/graph_manager.hpp

@@ -25,7 +25,7 @@
  */
 
 #ifndef TESTS_MULTITHREADING_LIFEGAME_GRAPH_MANAGER_HPP
-#define TESTS_MULTITHREADING_LIFEGAME_GRAPH_MANAGER_HPP 1
+#define TESTS_MULTITHREADING_LIFEGAME_GRAPH_MANAGER_HPP
 
 #include <tests/multithreading/lifegame/models.hpp>
 

+ 1 - 1
src/tests/multithreading/lifegame/models.hpp

@@ -25,7 +25,7 @@
  */
 
 #ifndef TESTS_MULTITHREADING_LIFEGAME_MODELS_HPP
-#define TESTS_MULTITHREADING_LIFEGAME_MODELS_HPP 1
+#define TESTS_MULTITHREADING_LIFEGAME_MODELS_HPP
 
 #include <artis-star/common/time/DoubleTime.hpp>
 

+ 1 - 1
src/tests/pdevs/graph_manager.hpp

@@ -25,7 +25,7 @@
  */
 
 #ifndef TESTS_PDEVS_GRAPH_MANAGER_HPP
-#define TESTS_PDEVS_GRAPH_MANAGER_HPP 1
+#define TESTS_PDEVS_GRAPH_MANAGER_HPP
 
 #include <tests/pdevs/models.hpp>
 

+ 1 - 1
src/tests/pdevs/models.hpp

@@ -25,7 +25,7 @@
  */
 
 #ifndef TESTS_PDEVS_MODELS_HPP
-#define TESTS_PDEVS_MODELS_HPP 1
+#define TESTS_PDEVS_MODELS_HPP
 
 #include <artis-star/common/time/DoubleTime.hpp>
 #include <artis-star/common/utils/Trace.hpp>

+ 1 - 1
src/tests/qss/graph_manager.hpp

@@ -25,7 +25,7 @@
  */
 
 #ifndef TESTS_QSS_GRAPH_MANAGER_HPP
-#define TESTS_QSS_GRAPH_MANAGER_HPP 1
+#define TESTS_QSS_GRAPH_MANAGER_HPP
 
 #include <tests/qss/models.hpp>
 

+ 1 - 1
src/tests/qss/models.hpp

@@ -25,7 +25,7 @@
  */
 
 #ifndef TESTS_QSS_MODELS_HPP
-#define TESTS_QSS_MODELS_HPP 1
+#define TESTS_QSS_MODELS_HPP
 
 #include <artis-star/common/time/DoubleTime.hpp>
 #include <artis-star/common/utils/Trace.hpp>