presage  0.8.8
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 is unknown." << endl;
143  }
144  }
145  catch (PresageException ex)
146  {
147  logger << ERROR << ex.what() << endl
148  << ERROR << "Predictor " + predictorName + " failed to initialize." << endl;
149  }
150 
151  if (predictor != 0)
152  {
153  predictors.push_back (predictor);
154  logger << INFO << "Activated predictive predictor: " << predictorName << endl;
155  }
156  else
157  {
158  logger << FATAL << "Unable to initialize predictor: " << predictorName << endl;
159  throw PredictorRegistryException(PRESAGE_INIT_PREDICTOR_ERROR, "Unable to initialize predictor: " + predictorName);
160  }
161 }
162 
164 {
165  for (size_t i = 0; i < predictors.size(); i++) {
166  logger << DEBUG << "Removing predictor: " << predictors[i]->getName() << endl;
167  delete predictors[i];
168  }
169  predictors.clear();
170 }
171 
173 {
174  return Iterator(predictors);
175 }
176 
177 
179 // Iterator
180 PredictorRegistry::Iterator::Iterator(std::vector<Predictor*>& cont)
181  : iter_end(cont.end()),
182  iter_curr(cont.begin())
183 {}
184 
186 {}
187 
189 {
190  bool result = (iter_end != iter_curr);
191 
192  return result;
193 }
194 
196 {
197  Predictor* result = *iter_curr;
198  iter_curr++;
199  return result;
200 }
201 
202 void PredictorRegistry::update (const Observable* variable)
203 {
204  logger << DEBUG << "About to invoke dispatcher: " << variable->get_name () << " - " << variable->get_value() << endl;
205 
206  dispatcher.dispatch (variable);
207 }