Kernel Quantum Probability Library
The KQP library aims at providing tools for working with quantums probabilities
space_factory.hpp
1 /*
2  This file is part of the Kernel Quantum Probability library (KQP).
3 
4  KQP is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  KQP is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with KQP. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 #ifndef __KQP_SPACE_FACTORY_H__
20 #define __KQP_SPACE_FACTORY_H__
21 
22 #include <fstream>
23 #include <sstream>
24 
25 #include <kqp/picojson.hpp>
26 #include <kqp/feature_matrix.hpp>
27 #include <kqp/cleanup.hpp>
28 
29 namespace kqp {
30  template<typename Scalar, typename SpaceInstance> static AbstractSpace* CONSTRUCTOR() {
31  return new SpaceInstance();
32  };
33 
35  class SpaceFactory {
36  public:
38  typedef AbstractSpace * (*BaseConstructor)();
39 
40  static inline void registerSpace(const std::string &name, BaseConstructor constructor) {
41  constructors()[name] = constructor;
42  }
43 
45  static inline boost::shared_ptr<AbstractSpace> load(const picojson::object &value) {
46  std::string scalar_name = get<std::string>("", value, "scalar", "double");
47  std::string name = get<std::string>("", value, "name") + "[" + scalar_name + "]";
48  BaseConstructor constructor = constructors()[name];
49  if (!constructor)
50  KQP_THROW_EXCEPTION_F(exception, "Cannot retrieve space with name %s and scalar %s", %name %scalar_name);
51  boost::shared_ptr<AbstractSpace> fSpace (constructor());
52  fSpace->load(value);
53  return fSpace;
54  }
55 
57  static inline boost::shared_ptr<AbstractSpace> loadFromFile(const std::string &filename) {
58  picojson::value v = readJsonFromFile(filename);
59  return load(v.get<picojson::object>());
60  }
61 
63  static inline boost::shared_ptr<AbstractSpace> loadFromString(const std::string &jsonstring) {
64  picojson::value v = readJsonFromString(jsonstring);
65  return load(v.get<picojson::object>());
66  }
67 
68 
70  static inline void saveToFile(const std::string &filename, AbstractSpace &space) {
71  picojson::object json = space.save();
72  std::ofstream out(filename.c_str());
73  picojson::value(json).serialize(std::ostream_iterator<char>(out));
74  }
75 
77  static inline std::string getJSONString(const AbstractSpace &space) {
78  picojson::object json = space.save();
79  std::ostringstream out;
80  picojson::value(json).serialize(std::ostream_iterator<char>(out));
81  return out.str();
82  }
83 
84 #ifndef SWIG
85 
86  template<typename Scalar, class SpaceInstance> struct Register {
87  Register() {
88  std::string name = SpaceInstance::NAME() + "[" + ScalarInfo<Scalar>::name() + "]";
89  SpaceFactory::registerSpace(name, CONSTRUCTOR<Scalar, SpaceInstance>);
90  }
91  };
92  static std::vector<std::string> registered() {
93  std::vector<std::string> list;
94  for(auto e: constructors())
95  if (e.second)
96  list.push_back(e.first);
97  else
98  list.push_back("[UNDEFINED] " + e.first);
99  return list;
100  }
101 
103  static std::map<std::string, BaseConstructor> &constructors() {
104  static std::map<std::string, BaseConstructor> _CONSTRUCTORS_;
105  return _CONSTRUCTORS_;
106  }
107 #endif
108 
109  };
110 }
111 #endif