presage  0.9~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 const char* PredictorRegistry::LOGGER = "Presage.PredictorRegistry.LOGGER";
38 const char* PredictorRegistry::PREDICTORS = "Presage.PredictorRegistry.PREDICTORS";
39 
41  : config(configuration),
42  contextTracker(0),
43  logger("PredictorRegistry", std::cerr),
44  dispatcher(this)
45 {
46  // build notification dispatch map
49 }
50 
51 
53 {
55 }
56 
57 void PredictorRegistry::setLogger (const std::string& value)
58 {
59  logger << setlevel (value);
60  logger << INFO << "LOGGER: " << value << endl;
61 }
62 
63 
65  if (contextTracker != ct) {
66  contextTracker = ct;
69  }
70 }
71 
72 void PredictorRegistry::setPredictors(const std::string& predictorList)
73 {
74  predictors_list = predictorList;
75  logger << INFO << "PREDICTORS: " << predictors_list << endl;
76 
77  if (contextTracker) {
78  // predictors need tracker, only initialize them if available
79 
81 
82  std::stringstream ss(predictors_list);
83  std::string predictor;
84  while (ss >> predictor) {
85  logger << INFO << "Initializing predictor: " << predictor << endl;
86  addPredictor(predictor);
87  }
88  }
89 }
90 
91 void PredictorRegistry::addPredictor(const std::string& predictorName)
92 {
93  Predictor* predictor = 0;
94  const char* name = predictorName.c_str();
95  std::string predictor_class_variable_key = "Presage.Predictors." + predictorName + ".PREDICTOR";
96  Variable* predictor_class_variable = 0;
97 
98  // TODO: this will have to do for now, until a proper predictor
99  // framework (i.e. plump) is integrated into presage. Until then,
100  // all known predictors have to be listed here and explicitly
101  // created based on their name.
102  //
103 
104  try
105  {
106  predictor_class_variable = config->find (predictor_class_variable_key);
107 
108  std::string predictor_class = predictor_class_variable->get_value();
109 
110  if (predictor_class == "AbbreviationExpansionPredictor")
111  {
112  predictor = new AbbreviationExpansionPredictor(config, contextTracker, name);
113  }
114  else if (predictor_class == "DummyPredictor")
115  {
116  predictor = new DummyPredictor(config, contextTracker, name);
117  }
118  else if (predictor_class == "DictionaryPredictor" )
119  {
120  predictor = new DictionaryPredictor(config, contextTracker, name);
121 #ifdef USE_SQLITE
122  }
123  else if (predictor_class == "SmoothedNgramPredictor")
124  {
125  predictor = new SmoothedNgramPredictor(config, contextTracker, name);
126 #endif
127  }
128  else if (predictor_class == "RecencyPredictor")
129  {
130  predictor = new RecencyPredictor(config, contextTracker, name);
131  }
132  else if (predictor_class == "DejavuPredictor")
133  {
134  predictor = new DejavuPredictor(config, contextTracker, name);
135  }
136  else if (predictor_class == "ARPAPredictor")
137  {
138  predictor = new ARPAPredictor(config, contextTracker, name);
139  }
140  else
141  {
142  logger << ERROR << predictor_class_variable_key << " class \""
143  << predictor_class << "\" is unknown." << endl;
144  }
145  }
146  catch (PresageException ex)
147  {
148  logger << ERROR << ex.what() << endl
149  << ERROR << "Predictor " + predictorName + " failed to initialize." << endl;
150  }
151 
152  if (predictor != 0)
153  {
154  predictors.push_back (predictor);
155  logger << INFO << "Activated predictive predictor: " << predictorName << endl;
156  }
157  else
158  {
159  logger << FATAL << "Unable to initialize predictor: " << predictorName << endl;
160  throw PredictorRegistryException(PRESAGE_INIT_PREDICTOR_ERROR, "Unable to initialize predictor: " + predictorName);
161  }
162 }
163 
165 {
166  for (size_t i = 0; i < predictors.size(); i++) {
167  logger << DEBUG << "Removing predictor: " << predictors[i]->getName() << endl;
168  delete predictors[i];
169  }
170  predictors.clear();
171 }
172 
174 {
175  return Iterator(predictors);
176 }
177 
178 
180 // Iterator
181 PredictorRegistry::Iterator::Iterator(std::vector<Predictor*>& cont)
182  : iter_end(cont.end()),
183  iter_curr(cont.begin())
184 {}
185 
187 {}
188 
190 {
191  bool result = (iter_end != iter_curr);
192 
193  return result;
194 }
195 
197 {
198  Predictor* result = *iter_curr;
199  ++iter_curr;
200  return result;
201 }
202 
203 void PredictorRegistry::update (const Observable* variable)
204 {
205  logger << DEBUG << "About to invoke dispatcher: " << variable->get_name () << " - " << variable->get_value() << endl;
206 
207  dispatcher.dispatch (variable);
208 }
Iterator(std::vector< Predictor * > &)
static const char * PREDICTORS
void dispatch(const Observable *var)
Definition: dispatcher.h:73
Configuration * config
virtual void update(const Observable *variable)
Variable * find(const std::string &variable) const
Logger< char > logger
PredictorRegistry(Configuration *config)
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)
static const char * LOGGER
std::string config
Definition: presageDemo.cpp:70
std::string predictors_list
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
virtual const char * what() const
void setPredictors(const std::string &predictor_list)
ContextTracker * contextTracker
std::string get_value() const
Definition: variable.cpp:62
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:278