Parcourir la source

Add examples with save/restore

Eric Ramat il y a 4 ans
Parent
commit
d98ab9e920
4 fichiers modifiés avec 57 ajouts et 38 suppressions
  1. 3 3
      src/tests/pdevs/models.hpp
  2. 1 3
      src/tests/qss/CMakeLists.txt
  3. 47 25
      src/tests/qss/main.cpp
  4. 6 7
      src/tests/qss/models.hpp

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

@@ -436,11 +436,11 @@ namespace artis {
                         :
                         artis::pdevs::Dynamics<common::DoubleTime, ThreeStateModel>(name, context)
                 {
-                    States(std::vector<double>,
+                    DECLARE_STATES(std::vector<double>,
                             ((HEIGHTS, &ThreeStateModel::heights), (SPEEDS, &ThreeStateModel::speeds), (SCALES, &ThreeStateModel::scales)));
-                    States(unsigned int,
+                    DECLARE_STATES(unsigned int,
                             ((N, &ThreeStateModel::n), (INDEX, &ThreeStateModel::index)));
-                    States(typename common::DoubleTime::type,
+                    DECLARE_STATES(typename common::DoubleTime::type,
                             ((SIGMA, &ThreeStateModel::sigma), (LAST_TIME, &ThreeStateModel::_last_time)));
 
                     output_ports({{OUT, "out"}});

+ 1 - 3
src/tests/qss/CMakeLists.txt

@@ -7,6 +7,4 @@ LINK_DIRECTORIES()
 
 ADD_EXECUTABLE(qss-main graph_manager.hpp models.hpp main.cpp)
 
-TARGET_LINK_LIBRARIES(qss-main
-        ${Boost_SYSTEM_LIBRARY}
-        ${Boost_TIMER_LIBRARY})
+TARGET_LINK_LIBRARIES(qss-main  ${Boost_LIBRARIES})

+ 47 - 25
src/tests/qss/main.cpp

@@ -29,7 +29,9 @@
 #include <artis-star/common/RootCoordinator.hpp>
 #include <artis-star/common/observer/Iterator.hpp>
 
+#include <fstream>
 #include <iostream>
+#include <boost/archive/binary_oarchive.hpp>
 
 using namespace artis::tests::qss;
 
@@ -70,29 +72,22 @@ void test_parabola()
     }
 }
 
-void test_predator_prey()
-{
-    PreyPredatorGraphManagerParameters parameters = {{{45.},
-                                                             {true, true, 0.1, 3},
-                                                             {0.5, 0.01, 0.01, 0.2}},
-                                                     {{5000.},
-                                                             {true, true, 1, 3},
-                                                             {0.5, 0.01, 0.01, 0.2}}};
-
-    class View : public artis::observer::View<artis::common::DoubleTime> {
-    public:
-        View()
-        {
-            selector("PredatorView",
-                    {PreyPredatorGraphManager::PREDATOR, PredatorGraphManager::S_Integrator,
-                     artis::pdevs::qss::Integrator<artis::common::DoubleTime>::VALUE});
-            selector("PreyView",
-                    {PreyPredatorGraphManager::PREY, PreyGraphManager::S_Integrator,
-                     artis::pdevs::qss::Integrator<artis::common::DoubleTime>::VALUE});
-        }
-    };
+class PredatorPreyView : public artis::observer::View<artis::common::DoubleTime> {
+public:
+    PredatorPreyView()
+    {
+        selector("PredatorView",
+                {PreyPredatorGraphManager::PREDATOR, PredatorGraphManager::S_Integrator,
+                 artis::pdevs::qss::Integrator<artis::common::DoubleTime>::VALUE});
+        selector("PreyView",
+                {PreyPredatorGraphManager::PREY, PreyGraphManager::S_Integrator,
+                 artis::pdevs::qss::Integrator<artis::common::DoubleTime>::VALUE});
+    }
+};
 
-    artis::common::context::Context<artis::common::DoubleTime> context(0, 100);
+void run_predator_prey(artis::common::context::Context<artis::common::DoubleTime>& context,
+        const PreyPredatorGraphManagerParameters& parameters)
+{
     artis::common::RootCoordinator<
             artis::common::DoubleTime, artis::pdevs::Coordinator<
                     artis::common::DoubleTime,
@@ -100,13 +95,20 @@ void test_predator_prey()
                     PreyPredatorGraphManagerParameters>
     > rc(context, "root", parameters, artis::common::NoParameters());
 
-    rc.attachView("Value", new View());
+    rc.attachView("Value", new PredatorPreyView());
 
     rc.run(context);
 
+    rc.save(context);
+
+    std::ofstream os("state");
+    boost::archive::binary_oarchive oa(os);
+
+    oa << context;
+
     {
         artis::observer::DiscreteTimeIterator<artis::common::DoubleTime> it(
-                rc.observer().view("Value").get("PredatorView"), 0, 0.1);
+                rc.observer().view("Value").get("PredatorView"), context.begin(), 0.1);
 
         while (it.has_next()) {
             double v;
@@ -118,7 +120,7 @@ void test_predator_prey()
     }
     {
         artis::observer::DiscreteTimeIterator<artis::common::DoubleTime> it(
-                rc.observer().view("Value").get("PreyView"), 0, 0.1);
+                rc.observer().view("Value").get("PreyView"), context.begin(), 0.1);
 
         while (it.has_next()) {
             double v;
@@ -128,6 +130,26 @@ void test_predator_prey()
             ++it;
         }
     }
+
+}
+
+void test_predator_prey()
+{
+    PreyPredatorGraphManagerParameters parameters = {{{45.},
+                                                             {true, true, 0.1, 3},
+                                                             {0.5, 0.01, 0.01, 0.2}},
+                                                     {{5000.},
+                                                             {true, true, 1,   3},
+                                                             {0.5, 0.01, 0.01, 0.2}}};
+
+    artis::common::context::Context<artis::common::DoubleTime> context(0, 100);
+
+    run_predator_prey(context, parameters);
+
+    artis::common::context::Context<artis::common::DoubleTime> new_context(context);
+
+    new_context.end(200);
+    run_predator_prey(new_context, parameters);
 }
 
 int main()

+ 6 - 7
src/tests/qss/models.hpp

@@ -57,7 +57,7 @@ namespace artis {
                     : public artis::pdevs::qss::Derivative<common::DoubleTime, Parabola, ParabolaParameters> {
             public:
                 enum states {
-                    X = IN
+                    X = LAST_OUTPUT + 1
                 };
 
                 Parabola(const std::string& name,
@@ -67,7 +67,7 @@ namespace artis {
                                 name,
                                 context), _alpha(context.parameters().alpha)
                 {
-                    States(double, ((X, &Parabola::_x)));
+                    DECLARE_STATES(double, ((X, &Parabola::_x)));
                 }
 
                 ~Parabola() override = default;
@@ -95,7 +95,7 @@ namespace artis {
                 };
 
                 enum states {
-                    Y = IN, X
+                    Y = LAST_OUTPUT + 1, X
                 };
 
                 Predator(const std::string& name,
@@ -106,7 +106,7 @@ namespace artis {
                                 context), _b(context.parameters().b), _d(context.parameters().d),
                         _e(context.parameters().e)
                 {
-                    States(double, ((X, &Predator::_x), (Y, &Predator::_y)));
+                    DECLARE_STATES(double, ((X, &Predator::_x), (Y, &Predator::_y)));
 
                     input_port({IN_X, "in_x"});
                 }
@@ -134,7 +134,7 @@ namespace artis {
                 };
 
                 enum states {
-                    X = IN, Y
+                    X = LAST_OUTPUT + 1, Y
                 };
 
                 Prey(const std::string& name,
@@ -144,8 +144,7 @@ namespace artis {
                                 name,
                                 context), _a(context.parameters().a), _b(context.parameters().b)
                 {
-                    States(double,
-                            ((X, &Prey::_x), (Y, &Prey::_y)));
+                    DECLARE_STATES(double, ((X, &Prey::_x), (Y, &Prey::_y)));
 
                     input_port({IN_Y, "in_y"});
                 }