presage  0.9.2~beta
predictorRegistry.cpp
Go to the documentation of this file.
1 
2 /******************************************************
3  * Presage, an extensible predictive text entry system
4  * ---------------------------------------------------
5  *
6  * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
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 2 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 along
19  with this program; if not, write to the Free Software Foundation, Inc.,
20  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  **********(*)*/
23 
24 
25 #include "predictorRegistry.h"
26 
27 #ifdef USE_SQLITE
29 #endif
36 
37 #include <set>
38 #include <algorithm>
39 
40 const char* PredictorRegistry::LOGGER = "Presage.PredictorRegistry.LOGGER";
41 const char* PredictorRegistry::PREDICTORS = "Presage.PredictorRegistry.PREDICTORS";
42 
44  : config(configuration),
45  contextTracker(0),
46  logger("PredictorRegistry", std::cerr),
47  dispatcher(this)
48 {
49  // build notification dispatch map
52 }
53 
54 
56 {
58 }
59 
60 void PredictorRegistry::setLogger (const std::string& value)
61 {
62  logger << setlevel (value);
63  logger << INFO << "LOGGER: " << value << endl;
64 }
65 
66 
68  if (contextTracker != ct)
69  {
70  contextTracker = ct;
72  }
73 }
74 
75 void PredictorRegistry::setPredictors(const std::string& predictorList)
76 {
77  predictors_list = predictorList;
78  logger << INFO << "PREDICTORS: " << predictors_list << endl;
79 
80  if (contextTracker) {
81  // predictors need tracker, only initialize them if available
82 
83  // build set of names of active predictors
84  std::set<std::string> active_predictors;
85  for (std::vector<Predictor*>::const_iterator it = predictors.begin();
86  it != predictors.end();
87  it++)
88  {
89  active_predictors.insert((*it)->getName());
90  }
91 
92  // build set of names of desired predictors
93  std::set<std::string> desired_predictors;
94  std::stringstream ss(predictors_list);
95  std::string predictor;
96  while (ss >> predictor)
97  {
98  desired_predictors.insert(predictor);
99  }
100 
101  // build set of names of predictors to remove
102  std::set<std::string> predictors_to_remove;
103  std::set_difference(active_predictors.begin(), active_predictors.end(),
104  desired_predictors.begin(), desired_predictors.end(),
105  std::inserter(predictors_to_remove, predictors_to_remove.begin()));
106 
107  // build set of names of predictors to add
108  std::set<std::string> predictors_to_add;
109  std::set_difference(desired_predictors.begin(), desired_predictors.end(),
110  active_predictors.begin(), active_predictors.end(),
111  std::inserter(predictors_to_add, predictors_to_add.begin()));
112 
113  // remove predictors
114  for (std::set<std::string>::const_iterator it = predictors_to_remove.begin();
115  it != predictors_to_remove.end();
116  it++)
117  {
118  removePredictor(*it);
119  }
120 
121  // add predictors
122  for (std::set<std::string>::const_iterator it = predictors_to_add.begin();
123  it != predictors_to_add.end();
124  it++)
125  {
126  addPredictor(*it);
127  }
128  }
129 }
130 
131 void PredictorRegistry::addPredictor(const std::string& predictorName)
132 {
133  Predictor* predictor = 0;
134  const char* name = predictorName.c_str();
135  std::string predictor_class_variable_key = "Presage.Predictors." + predictorName + ".PREDICTOR";
136  Variable* predictor_class_variable = 0;
137 
138  // TODO: this will have to do for now, until a proper predictor
139  // framework (i.e. plump) is integrated into presage. Until then,
140  // all known predictors have to be listed here and explicitly
141  // created based on their name.
142  //
143 
144  try
145  {
146  predictor_class_variable = config->find (predictor_class_variable_key);
147 
148  std::string predictor_class = predictor_class_variable->get_value();
149 
150  if (predictor_class == "AbbreviationExpansionPredictor")
151  {
152  predictor = new AbbreviationExpansionPredictor(config, contextTracker, name);
153  }
154  else if (predictor_class == "DummyPredictor")
155  {
156  predictor = new DummyPredictor(config, contextTracker, name);
157  }
158  else if (predictor_class == "DictionaryPredictor" )
159  {
160  predictor = new DictionaryPredictor(config, contextTracker, name);
161 #ifdef USE_SQLITE
162  }
163  else if (predictor_class == "SmoothedNgramPredictor")
164  {
165  predictor = new SmoothedNgramPredictor(config, contextTracker, name);
166 #endif
167  }
168  else if (predictor_class == "RecencyPredictor")
169  {
170  predictor = new RecencyPredictor(config, contextTracker, name);
171  }
172  else if (predictor_class == "DejavuPredictor")
173  {
174  predictor = new DejavuPredictor(config, contextTracker, name);
175  }
176  else if (predictor_class == "ARPAPredictor")
177  {
178  predictor = new ARPAPredictor(config, contextTracker, name);
179  }
180  else
181  {
182  logger << ERROR << predictor_class_variable_key << " class \""
183  << predictor_class << "\" is unknown." << endl;
184  }
185  }
186  catch (PresageException ex)
187  {
188  logger << ERROR << ex.what() << endl
189  << ERROR << "Predictor " + predictorName + " failed to initialize." << endl;
190  }
191 
192  if (predictor != 0)
193  {
194  predictors.push_back (predictor);
195  logger << INFO << "Activated predictor: " << predictorName << endl;
196  }
197  else
198  {
199  logger << FATAL << "Unable to initialize predictor: " << predictorName << endl;
200  throw PredictorRegistryException(PRESAGE_INIT_PREDICTOR_ERROR, "Unable to initialize predictor: " + predictorName);
201  }
202 }
203 
204 void PredictorRegistry::removePredictor(const std::string& predictor_name)
205 {
206  logger << DEBUG << "Removing predictor: " << predictor_name << endl;
207  std::vector<Predictor*>::iterator it = predictors.begin();
208  while (it != predictors.end())
209  {
210  if ((*it)->getName() == predictor_name)
211  {
212  delete *it;
213  it = predictors.erase(it);
214  logger << DEBUG << "Removed predictor: " << predictor_name << endl;
215  }
216  else
217  {
218  ++it;
219  }
220  }
221 }
222 
224 {
225  for (size_t i = 0; i < predictors.size(); i++) {
226  logger << DEBUG << "Removing predictor: " << predictors[i]->getName() << endl;
227  delete predictors[i];
228  }
229  predictors.clear();
230 }
231 
233 {
234  return Iterator(predictors);
235 }
236 
237 
239 // Iterator
240 PredictorRegistry::Iterator::Iterator(std::vector<Predictor*>& cont)
241  : iter_end(cont.end()),
242  iter_curr(cont.begin())
243 {}
244 
246 {}
247 
249 {
250  bool result = (iter_end != iter_curr);
251 
252  return result;
253 }
254 
256 {
257  Predictor* result = *iter_curr;
258  ++iter_curr;
259  return result;
260 }
261 
262 void PredictorRegistry::update (const Observable* variable)
263 {
264  logger << DEBUG << "About to invoke dispatcher: " << variable->get_name () << " - " << variable->get_value() << endl;
265 
266  dispatcher.dispatch (variable);
267 }
static const char * PREDICTORS
void dispatch(const Observable *var)
Definition: dispatcher.h:73
void removePredictor(const std::string &predictor_name)
Configuration * config
Iterator(std::vector< Predictor *> &)
virtual void update(const Observable *variable)
Logger< char > logger
PredictorRegistry(Configuration *config)
virtual const char * what() const
std::vector< Predictor * > predictors
_SetLevel setlevel(std::string __l)
Manipulator for level.
Definition: logger.h:46
Dispatcher< PredictorRegistry > dispatcher
void addPredictor(const std::string &predictor_name)
Variable * find(const std::string &variable) const
static const char * LOGGER
std::string config
Definition: presageDemo.cpp:70
std::string predictors_list
std::string get_value() const
Definition: variable.cpp:62
void setContextTracker(ContextTracker *ct)
virtual std::string get_name() const =0
void setLogger(const std::string &level)
void map(Observable *var, const mbr_func_ptr_t &ptr)
Definition: dispatcher.h:62
Tracks user interaction and context.
virtual std::string get_value() const =0
void setPredictors(const std::string &predictor_list)
ContextTracker * contextTracker
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:278