Builder.hpp 5.1 KB

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