Builder.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(boost::property_tree::ptree const& pt)
  43. {
  44. using boost::property_tree::ptree;
  45. kernel::AbstractModel<U, V>* model = nullptr;
  46. kernel::AbstractModels<U, V> submodels;
  47. std::string name;
  48. ptree::const_iterator it_type = pt.end();
  49. ptree::const_iterator it_name = pt.end();
  50. ptree::const_iterator it_states = pt.end();
  51. ptree::const_iterator it_internals = pt.end();
  52. ptree::const_iterator it_externals = pt.end();
  53. ptree::const_iterator it_submodels = pt.end();
  54. for (ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) {
  55. if (it->first == "type") {
  56. it_type = it;
  57. } else if (it->first == "name") {
  58. it_name = it;
  59. } else if (it->first == "states") {
  60. it_states = it;
  61. } else if (it->first == "internals") {
  62. it_internals = it;
  63. } else if (it->first == "externals") {
  64. it_externals = it;
  65. } else if (it->first == "submodels") {
  66. it_submodels = it;
  67. }
  68. }
  69. // name
  70. if (it_name != pt.end()) {
  71. name = it_type->second.get_value<std::string>();
  72. }
  73. // submodels
  74. if (it_submodels != pt.end()) {
  75. for (ptree::const_iterator itm = it_submodels->second.begin();
  76. itm != it_submodels->second.end(); ++itm) {
  77. submodels.push_back(build_model(itm->second));
  78. }
  79. }
  80. // states
  81. if (it_states != pt.end()) {
  82. build_states(it_states->second, model);
  83. }
  84. // internals
  85. if (it_internals != pt.end()) {
  86. build_internals(it_internals->second, model);
  87. }
  88. // externals
  89. if (it_externals != pt.end()) {
  90. build_externals(it_externals->second, model);
  91. }
  92. // type
  93. if (it_type != pt.end()) {
  94. model = F::factory().create(
  95. it_type->second.get_value<std::string>(), submodels);
  96. }
  97. return model;
  98. }
  99. void build_internals(boost::property_tree::ptree const& pt, kernel::AbstractModel<U, V>* model)
  100. {
  101. using boost::property_tree::ptree;
  102. for (ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) {
  103. build_variable(it->second, model);
  104. }
  105. }
  106. void build_externals(boost::property_tree::ptree const& pt, kernel::AbstractModel<U, V>* model)
  107. {
  108. using boost::property_tree::ptree;
  109. for (ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) {
  110. build_variable(it->second, model);
  111. }
  112. }
  113. void build_states(boost::property_tree::ptree const& pt, kernel::AbstractModel<U, V>* model)
  114. {
  115. using boost::property_tree::ptree;
  116. for (ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) {
  117. build_variable(it->second, model);
  118. }
  119. }
  120. void build_variable(boost::property_tree::ptree const& pt, kernel::AbstractModel<U, V>* /* model */)
  121. {
  122. using boost::property_tree::ptree;
  123. std::string name;
  124. std::string type;
  125. for (ptree::const_iterator it = pt.begin(); it != pt.end(); ++it) {
  126. if (it->first == "name") {
  127. name = it->second.get_value<std::string>();
  128. } else if (it->first == "type") {
  129. type = it->second.get_value<std::string>();
  130. }
  131. }
  132. std::cout << name << ": " << type << std::endl;
  133. }
  134. boost::property_tree::ptree tree;
  135. };
  136. }
  137. }
  138. #endif