test.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @file test/test.cpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2019 ULCO http://www.univ-littoral.fr
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. //#define CATCH_CONFIG_MAIN
  22. //#include <test/catch.hpp>
  23. #include <test/models.hpp>
  24. #include <artis/builder/Builder.hpp>
  25. #include <artis/builder/ModelFactory.hpp>
  26. #include <artis/kernel/Simulator.hpp>
  27. #include <artis/observer/Output.hpp>
  28. #include <artis/utils/DateTime.hpp>
  29. using namespace artis::kernel;
  30. using namespace artis::builder;
  31. using Creator = ObjectCreator<Model, artis::utils::DoubleTime, ModelParameters>;
  32. using Factory = ModelFactory<Model, int, Creator, artis::utils::DoubleTime, ModelParameters>;
  33. DECLARE_MODEL(AModel, ::Model, ::Factory, artis::utils::DoubleTime, ModelParameters);
  34. DECLARE_MODEL(BModel, ::Model, ::Factory, artis::utils::DoubleTime, ModelParameters);
  35. DECLARE_MODEL(RootModel, ::Model, ::Factory, artis::utils::DoubleTime, ModelParameters);
  36. typedef artis::kernel::Simulator<RootModel, artis::utils::DoubleTime, ModelParameters, GlobalParameters> ASimulator;
  37. typedef artis::builder::Builder<::Factory, RootModel, artis::utils::DoubleTime, ModelParameters, GlobalParameters> ABuilder;
  38. typedef artis::context::Context<artis::utils::DoubleTime> AContext;
  39. class AView : public artis::observer::View<artis::utils::DoubleTime,
  40. ModelParameters> {
  41. public:
  42. AView()
  43. {
  44. selector("A::I", INT, {RootModel::A, AModel::IX});
  45. selector("A::B", BOOL, {RootModel::A, AModel::BX});
  46. selector("A::D", DOUBLE, {RootModel::A, AModel::DX});
  47. selector("B::I", INT, {RootModel::B, BModel::IY});
  48. selector("B::B", BOOL, {RootModel::B, BModel::BY});
  49. selector("B::D", DOUBLE, {RootModel::B, BModel::DY});
  50. }
  51. virtual ~AView() override { }
  52. };
  53. typedef artis::observer::Output<artis::utils::DoubleTime,
  54. ModelParameters> AnOutput;
  55. //TEST_CASE("Simulator_tests", "simple")
  56. int main()
  57. {
  58. GlobalParameters globalParameters;
  59. ModelParameters modelParameters;
  60. /**********************/
  61. // AModel a;
  62. // artis::kernel::Any v(&AModel::_ix);
  63. // a.init(0, modelParameters);
  64. // artis::kernel::to_value < AModel >(v, &a);
  65. /**********************/
  66. ABuilder builder("{ \"type\": \"RootModel\", " \
  67. "\"name\": \"root\", " \
  68. "\"states\": [ " \
  69. "{ \"name\":\"_i\", \"type\":\"int\"} " \
  70. "]," \
  71. "\"submodels\": [ " \
  72. "{ \"type\": \"AModel\", " \
  73. "\"name\": \"_a\", " \
  74. "\"internals\": [" \
  75. "{ \"name\":\"_ix\", \"type\":\"int\"}, " \
  76. "{ \"name\":\"_bx\", \"type\":\"bool\"}, " \
  77. "{ \"name\":\"_dx\", \"type\":\"double\"} " \
  78. "] }, " \
  79. "{ \"type\": \"BModel\", " \
  80. "\"name\": \"_b\", " \
  81. "\"internals\": [" \
  82. "{ \"name\":\"_iy\", \"type\":\"int\"}, " \
  83. "{ \"name\":\"_by\", \"type\":\"bool\"}, " \
  84. "{ \"name\":\"_dy\", \"type\":\"double\"} " \
  85. "], " \
  86. "\"externals\": [" \
  87. "{ \"name\":\"_ix\", \"type\":\"int\"}, " \
  88. "{ \"name\":\"_bx\", \"type\":\"bool\"}, " \
  89. "{ \"name\":\"_dx\", \"type\":\"double\"} " \
  90. "], " \
  91. "\"states\": [ " \
  92. "{ \"name\":\"_n\", \"type\":\"int\"} " \
  93. "] }" \
  94. "] }");
  95. AContext context(artis::utils::DateTime::toJulianDayNumber("2016-1-1"),
  96. artis::utils::DateTime::toJulianDayNumber("2016-1-10"));
  97. ASimulator simulator(builder.build(), globalParameters);
  98. simulator.attachView("Root", new AView);
  99. ::Trace::trace().clear();
  100. simulator.init(0, modelParameters);
  101. simulator.run(context);
  102. std::cout << ::Trace::trace().elements().to_string() << std::endl;
  103. AnOutput output(simulator.observer());
  104. output();
  105. return 0;
  106. }