jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
anomaly_factory.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2012 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #include "anomaly_factory.hpp"
18 
19 #include <string>
20 
21 #include "jubatus/util/lang/shared_ptr.h"
22 #include "jubatus/util/text/json.h"
23 
24 #include "../common/exception.hpp"
25 #include "../common/jsonconfig.hpp"
26 #include "../nearest_neighbor/nearest_neighbor_factory.hpp"
27 #include "../unlearner/unlearner_factory.hpp"
28 #include "../storage/column_table.hpp"
29 #include "../recommender/recommender_factory.hpp"
30 #include "anomaly.hpp"
31 
34 using jubatus::util::lang::shared_ptr;
35 using jubatus::util::text::json::json;
36 using std::string;
37 
38 namespace jubatus {
39 namespace core {
40 namespace anomaly {
41 
42 namespace {
43 struct lof_config : public lof_storage::config {
44  std::string method;
46 
47  template <typename Ar>
48  void serialize(Ar& ar) {
50  ar
51  & JUBA_MEMBER(method)
52  & JUBA_MEMBER(parameter);
53  }
54 };
55 
56 struct light_lof_config : public light_lof::config {
57  std::string method;
59  jubatus::util::data::optional<std::string> unlearner;
60  jubatus::util::data::optional<jubatus::core::common::jsonconfig::config>
62 
63  template <typename Ar>
64  void serialize(Ar& ar) {
66  ar
67  & JUBA_MEMBER(method)
68  & JUBA_MEMBER(parameter)
69  & JUBA_MEMBER(unlearner)
70  & JUBA_MEMBER(unlearner_parameter);
71  }
72 };
73 } // namespace
74 
75 shared_ptr<anomaly_base> anomaly_factory::create_anomaly(
76  const string& name,
77  const config& param,
78  const string& id) {
79  if (name == "lof") {
80  lof_config conf = config_cast_check<lof_config>(param);
81 
82  return shared_ptr<anomaly_base>(new lof(
83  conf,
85  conf.method,
86  conf.parameter, id)));
87  } else if (name == "light_lof") {
88  light_lof_config conf = config_cast_check<light_lof_config>(param);
89 
90  jubatus::util::lang::shared_ptr<storage::column_table>
91  nearest_neighbor_table(new storage::column_table);
92  jubatus::util::lang::shared_ptr<nearest_neighbor::nearest_neighbor_base>
93  nearest_neighbor_engine(nearest_neighbor::create_nearest_neighbor(
94  conf.method, conf.parameter, nearest_neighbor_table, id));
95 
96  if (conf.unlearner) {
97  if (!conf.unlearner_parameter) {
98  throw JUBATUS_EXCEPTION(
101  "unlearner is set but unlearner_parameter is not found"));
102  }
103  jubatus::util::lang::shared_ptr<unlearner::unlearner_base> unlearner(
105  *conf.unlearner,
106  *conf.unlearner_parameter));
107  return shared_ptr<anomaly_base>(
108  new light_lof(conf, id, nearest_neighbor_engine, unlearner));
109  }
110 
111  return jubatus::util::lang::shared_ptr<anomaly_base>(
112  new light_lof(conf, id, nearest_neighbor_engine));
113  } else {
115  }
116 };
117 
118 } // namespace anomaly
119 } // namespace core
120 } // namespace jubatus
T config_cast_check(const config &c)
Definition: cast.hpp:311
void serialize(member_collector &mem, jubatus::util::data::serialization::named_value< T > &v)
Definition: cast.hpp:54
static jubatus::util::lang::shared_ptr< anomaly_base > create_anomaly(const std::string &name, const common::jsonconfig::config &param, const std::string &id)
static jubatus::util::lang::shared_ptr< recommender_base > create_recommender(const std::string &name, const common::jsonconfig::config &param, const std::string &id)
shared_ptr< unlearner_base > create_unlearner(const std::string &name, const common::jsonconfig::config &config)
jubatus::util::data::optional< jubatus::core::common::jsonconfig::config > unlearner_parameter
jubatus::core::common::jsonconfig::config parameter
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
jubatus::util::data::optional< std::string > unlearner
std::string method
shared_ptr< nearest_neighbor_base > create_nearest_neighbor(const std::string &name, const common::jsonconfig::config &config, shared_ptr< storage::column_table > table, const std::string &id)