Builder.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @file artis/builder/Builder.hpp
  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. #ifndef ARTIS_BUILDER_BUILDER_HPP
  22. #define ARTIS_BUILDER_BUILDER_HPP
  23. #include <boost/property_tree/ptree.hpp>
  24. #include <boost/property_tree/json_parser.hpp>
  25. #include <artis/kernel/AbstractCoupledModel.hpp>
  26. #include <artis/builder/ModelFactory.hpp>
  27. #include <sstream>
  28. namespace artis {
  29. namespace builder {
  30. template<typename F, typename T, typename U, typename V, typename W>
  31. class Builder {
  32. public:
  33. Builder(const std::string& json)
  34. {
  35. std::stringstream ss;
  36. ss << json;
  37. boost::property_tree::read_json(ss, tree);
  38. }
  39. virtual ~Builder() { }
  40. T* build() { return dynamic_cast < T* >(build_model(tree)); }
  41. private:
  42. kernel::AbstractModel<U, V>* build_model(
  43. boost::property_tree::ptree const& pt)
  44. {
  45. using boost::property_tree::ptree;
  46. kernel::AbstractModel<U, V>* model = nullptr;
  47. kernel::AbstractModels<U, V> submodels;
  48. std::string name;
  49. ptree::const_iterator it_type = pt.end();
  50. ptree::const_iterator it_name = pt.end();
  51. ptree::const_iterator it_states = pt.end();
  52. ptree::const_iterator it_internals = pt.end();
  53. ptree::const_iterator it_externals = pt.end();
  54. ptree::const_iterator it_submodels = pt.end();
  55. for (ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) {
  56. if (it->first == "type") {
  57. it_type = it;
  58. } else if (it->first == "name") {
  59. it_name = it;
  60. } else if (it->first == "states") {
  61. it_states = it;
  62. } else if (it->first == "internals") {
  63. it_internals = it;
  64. } else if (it->first == "externals") {
  65. it_externals = it;
  66. } else if (it->first == "submodels") {
  67. it_submodels = it;
  68. }
  69. }
  70. // name
  71. if (it_name != pt.end()) {
  72. name = it_type->second.get_value<std::string>();
  73. }
  74. // submodels
  75. if (it_submodels != pt.end()) {
  76. for (ptree::const_iterator itm = it_submodels->second.begin();
  77. itm != it_submodels->second.end(); ++itm) {
  78. submodels.push_back(build_model(itm->second));
  79. }
  80. }
  81. // states
  82. if (it_states != pt.end()) {
  83. build_states(it_states->second, model);
  84. }
  85. // internals
  86. if (it_internals != pt.end()) {
  87. build_internals(it_internals->second, model);
  88. }
  89. // externals
  90. if (it_externals != pt.end()) {
  91. build_externals(it_externals->second, model);
  92. }
  93. // type
  94. if (it_type != pt.end()) {
  95. model = F::factory().create(
  96. it_type->second.get_value<std::string>(), submodels);
  97. }
  98. return model;
  99. }
  100. void build_internals(boost::property_tree::ptree const& pt,
  101. kernel::AbstractModel<U, V>* model)
  102. {
  103. using boost::property_tree::ptree;
  104. for (ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) {
  105. build_variable(it->second, model);
  106. }
  107. }
  108. void build_externals(boost::property_tree::ptree const& pt,
  109. kernel::AbstractModel<U, V>* model)
  110. {
  111. using boost::property_tree::ptree;
  112. for (ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) {
  113. build_variable(it->second, model);
  114. }
  115. }
  116. void build_states(boost::property_tree::ptree const& pt,
  117. kernel::AbstractModel<U, V>* model)
  118. {
  119. using boost::property_tree::ptree;
  120. for (ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) {
  121. build_variable(it->second, model);
  122. }
  123. }
  124. void build_variable(boost::property_tree::ptree const& pt,
  125. kernel::AbstractModel<U, V>* /* model */)
  126. {
  127. using boost::property_tree::ptree;
  128. std::string name;
  129. std::string type;
  130. for (ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) {
  131. if (it->first == "name") {
  132. name = it->second.get_value<std::string>();
  133. } else if (it->first == "type") {
  134. type = it->second.get_value<std::string>();
  135. }
  136. }
  137. std::cout << name << ": " << type << std::endl;
  138. }
  139. boost::property_tree::ptree tree;
  140. };
  141. }
  142. }
  143. #endif