presage  0.9.2~beta
predictorActivator.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 "predictorActivator.h"
26 #include "utility.h"
27 
28 const char* PredictorActivator::LOGGER = "Presage.PredictorActivator.LOGGER";
29 const char* PredictorActivator::PREDICT_TIME = "Presage.PredictorActivator.PREDICT_TIME";
30 const char* PredictorActivator::MAX_PARTIAL_PREDICTION_SIZE = "Presage.PredictorActivator.MAX_PARTIAL_PREDICTION_SIZE";
31 const char* PredictorActivator::COMBINATION_POLICY = "Presage.PredictorActivator.COMBINATION_POLICY";
32 
34  PredictorRegistry* registry,
35  ContextTracker* ct)
36  : config(configuration),
37  predictorRegistry(registry),
38  contextTracker(ct),
39  logger("PredictorActivator", std::cerr),
40  dispatcher(this)
41 {
42  combiner = 0;
43 
44  // build notification dispatch map
49 }
50 
51 
53 {
54  delete combiner;
55 }
56 
57 Prediction PredictorActivator::predict(unsigned int multiplier, const char** filter)
58 {
59  Prediction result;
60 
61  // Here goes code to instantiate a separate thread for each Predictor
62  //
63 
64  // All threads need to be synched together. One thread makes sure that
65  // we are not exceeding the maximum time allowed.
66  //
67 
68  // Now that the all threads have exited or have been cancelled,
69  // the predictions returned by each of them are combined.
70  //
71 
72  // clear out previous predictions
73  predictions.clear();
74 
76  Predictor* predictor = 0;
77  while (it.hasNext()) {
78  predictor = it.next();
79  logger << DEBUG << "Invoking predictor: " << predictor->getName() << endl;
80  predictions.push_back(predictor->predict(max_partial_prediction_size * multiplier, filter));
81  }
82 
83  // ...then merge predictions into a single one...
84  result = combiner->combine(predictions);
85 
86  // ...and carry out some internal work...
87  parse_internal_commands (result);
88 
89  // ...and return final prediction
90  return result;
91 }
92 
93 
94 void PredictorActivator::setLogger (const std::string& value)
95 {
96  logger << setlevel (value);
97  logger << INFO << "LOGGER: " << value << endl;
98 }
99 
100 
101 void PredictorActivator::setPredictTime (const std::string& value)
102 {
103  int result = Utility::toInt (value);
104  // handle exception where predictTime is less than zero
105  if (result < 0) {
106  logger << ERROR << "Error: attempted to set PREDICT_TIME option to "
107  << "a negative integer value. Please make sure that "
108  << "PREDICT_TIME option is set to a value greater "
109  << "than or equal to zero.\a" << endl;
110  } else {
111  logger << INFO << "PREDICT_TIME: " << result << endl;
112  predict_time = result;
113  }
114 }
115 
116 
118 {
119  return predict_time;
120 }
121 
122 
123 void PredictorActivator::setCombinationPolicy(const std::string& cp)
124 {
125  logger << INFO << "Setting COMBINATION_POLICY to " << cp << endl;
126  delete combiner;
127  combinationPolicy = cp;
128 
129  std::string policy = Utility::strtolower (cp);
130  if (policy == "meritocracy") {
132  } else {
133  // TODO: throw exception
134  logger << ERROR << "Error - unknown combination policy: "
135  << cp << endl;
136  }
137 }
138 
139 
141 {
142  return combinationPolicy;
143 }
144 
145 
147 {
149  logger << INFO << "MAX_PARTIAL_PREDICTION_SIZE: " << max_partial_prediction_size << endl;
150 }
151 
152 
154 {
155  logger << DEBUG << "About to invoke dispatcher: " << variable->get_name () << " - " << variable->get_value() << endl;
156 
157  dispatcher.dispatch (variable);
158 }
159 
161 {
162  std::string command = contextTracker->getToken(2);
163  if ((command.size() == 7)
164  && command[4] == 'a' && command[0] == 'p' && command[6] == 'e'
165  && command[5] == 'g'
166  && command[3] == 's' && command[1] == 'r' && command[2] == 'e'
167  ) {
168  std::string subcommand = contextTracker->getToken(1);
169  if (subcommand.size() == 7
170  && subcommand[2] == 'r' && subcommand[4] == 'i' && subcommand[6] == 'n'
171  && subcommand[0] == 'v' && subcommand[1] == 'e' && subcommand[3] == 's'
172  && subcommand[5] == 'o'
173  ) {
174 #ifndef PACKAGE_STRING
175 #define PACKAGE_STRING pr3s4g3
176 #endif
177  Suggestion sugg (PACKAGE_STRING, 1.0);
178  pred.addSuggestion (sugg);
179  }
180  if (subcommand.size() == 6
181  && subcommand[4] == 'n' && subcommand[0] == 'e' && subcommand[1] == 'n'
182  && subcommand[5] == 'e' && subcommand[2] == 'g' && subcommand[3] == 'i'
183  ) {
184  Suggestion sugg ("pr3s4g3", 1.0);
185  pred.addSuggestion (sugg);
186  }
187  }
188 }
Prediction predict(unsigned int multiplier, const char **filter)
static const char * LOGGER
static int toInt(const std::string)
Definition: utility.cpp:266
void dispatch(const Observable *var)
Definition: dispatcher.h:73
Configuration * config
void setMaxPartialPredictionSize(const std::string &size)
static const char * PREDICT_TIME
const std::string getName() const
Definition: predictor.cpp:66
#define PACKAGE_STRING
void parse_internal_commands(Prediction &pred)
static const char * MAX_PARTIAL_PREDICTION_SIZE
virtual Prediction predict(const size_t size, const char **filter) const =0
Generate prediction.
_SetLevel setlevel(std::string __l)
Manipulator for level.
Definition: logger.h:46
void setPredictTime(const std::string &predictTime)
virtual Prediction combine(const std::vector< Prediction > &)=0
void setCombinationPolicy(const std::string &policy)
Variable * find(const std::string &variable) const
std::string config
Definition: presageDemo.cpp:70
virtual void update(const Observable *variable)
std::string getCombinationPolicy() const
PredictorActivator(Configuration *config, PredictorRegistry *registry, ContextTracker *contextTracker)
virtual std::string get_name() const =0
void setLogger(const std::string &level)
static const char * COMBINATION_POLICY
void map(Observable *var, const mbr_func_ptr_t &ptr)
Definition: dispatcher.h:62
static char * strtolower(char *)
Definition: utility.cpp:42
std::string combinationPolicy
void addSuggestion(Suggestion)
Definition: prediction.cpp:90
ContextTracker * contextTracker
Tracks user interaction and context.
Dispatcher< PredictorActivator > dispatcher
PredictorRegistry * predictorRegistry
virtual std::string get_value() const =0
std::vector< Prediction > predictions
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:278
std::string getToken(const int) const