test.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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,
  32. ModelParameters>;
  33. using Factory = ModelFactory<Model, int, Creator, artis::utils::DoubleTime,
  34. ModelParameters>;
  35. DECLARE_MODEL(AModel, ::Model, ::Factory, artis::utils::DoubleTime,
  36. ModelParameters);
  37. DECLARE_MODEL(BModel, ::Model, ::Factory, artis::utils::DoubleTime,
  38. ModelParameters);
  39. DECLARE_MODEL(RootModel, ::Model, ::Factory, artis::utils::DoubleTime,
  40. ModelParameters);
  41. typedef artis::kernel::Simulator<RootModel,
  42. artis::utils::DoubleTime,
  43. ModelParameters,
  44. GlobalParameters> ASimulator;
  45. typedef artis::builder::Builder<::Factory,
  46. RootModel,
  47. artis::utils::DoubleTime,
  48. ModelParameters,
  49. GlobalParameters> ABuilder;
  50. typedef artis::context::Context<artis::utils::DoubleTime> AContext;
  51. class AView : public artis::observer::View<artis::utils::DoubleTime,
  52. ModelParameters> {
  53. public:
  54. AView()
  55. {
  56. selector("A::I", INT, {RootModel::A, AModel::IX});
  57. selector("A::B", BOOL, {RootModel::A, AModel::BX});
  58. selector("A::D", DOUBLE, {RootModel::A, AModel::DX});
  59. selector("B::I", INT, {RootModel::B, BModel::IY});
  60. selector("B::B", BOOL, {RootModel::B, BModel::BY});
  61. selector("B::D", DOUBLE, {RootModel::B, BModel::DY});
  62. }
  63. virtual ~AView() { }
  64. };
  65. typedef artis::observer::Output<artis::utils::DoubleTime,
  66. ModelParameters> AnOutput;
  67. //TEST_CASE("Simulator_tests", "simple")
  68. int main()
  69. {
  70. GlobalParameters globalParameters;
  71. ModelParameters modelParameters;
  72. /**********************/
  73. // AModel a;
  74. // artis::kernel::Any v(&AModel::_ix);
  75. // a.init(0, modelParameters);
  76. // artis::kernel::to_value < AModel >(v, &a);
  77. /**********************/
  78. ABuilder builder("{ \"type\": \"RootModel\", " \
  79. "\"name\": \"root\", " \
  80. "\"states\": [ " \
  81. "{ \"name\":\"_i\", \"type\":\"int\"} " \
  82. "]," \
  83. "\"submodels\": [ " \
  84. "{ \"type\": \"AModel\", " \
  85. "\"name\": \"_a\", " \
  86. "\"internals\": [" \
  87. "{ \"name\":\"_ix\", \"type\":\"int\"}, " \
  88. "{ \"name\":\"_bx\", \"type\":\"bool\"}, " \
  89. "{ \"name\":\"_dx\", \"type\":\"double\"} " \
  90. "] }, " \
  91. "{ \"type\": \"BModel\", " \
  92. "\"name\": \"_b\", " \
  93. "\"internals\": [" \
  94. "{ \"name\":\"_iy\", \"type\":\"int\"}, " \
  95. "{ \"name\":\"_by\", \"type\":\"bool\"}, " \
  96. "{ \"name\":\"_dy\", \"type\":\"double\"} " \
  97. "], " \
  98. "\"externals\": [" \
  99. "{ \"name\":\"_ix\", \"type\":\"int\"}, " \
  100. "{ \"name\":\"_bx\", \"type\":\"bool\"}, " \
  101. "{ \"name\":\"_dx\", \"type\":\"double\"} " \
  102. "], " \
  103. "\"states\": [ " \
  104. "{ \"name\":\"_n\", \"type\":\"int\"} " \
  105. "] }" \
  106. "] }");
  107. AContext context(artis::utils::DateTime::toJulianDayNumber("2016-1-1"),
  108. artis::utils::DateTime::toJulianDayNumber("2016-1-10"));
  109. ASimulator simulator(builder.build(), globalParameters);
  110. simulator.attachView("Root", new AView);
  111. ::Trace::trace().clear();
  112. simulator.init(0, modelParameters);
  113. simulator.run(context);
  114. std::cout << ::Trace::trace().elements().to_string() << std::endl;
  115. AnOutput output(simulator.observer());
  116. output();
  117. return 0;
  118. }