presage 0.9.1
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
28const char* PredictorActivator::LOGGER = "Presage.PredictorActivator.LOGGER";
29const char* PredictorActivator::PREDICT_TIME = "Presage.PredictorActivator.PREDICT_TIME";
30const char* PredictorActivator::MAX_PARTIAL_PREDICTION_SIZE = "Presage.PredictorActivator.MAX_PARTIAL_PREDICTION_SIZE";
31const char* PredictorActivator::COMBINATION_POLICY = "Presage.PredictorActivator.COMBINATION_POLICY";
32
34 PredictorRegistry* registry,
36 : config(configuration),
37 predictorRegistry(registry),
39 logger("PredictorActivator", std::cerr),
40 dispatcher(this)
41{
42 combiner = 0;
43
44 // build notification dispatch map
49}
50
51
56
57Prediction 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...
88
89 // ...and return final prediction
90 return result;
91}
92
93
94void PredictorActivator::setLogger (const std::string& value)
95{
96 logger << setlevel (value);
97 logger << INFO << "LOGGER: " << value << endl;
98}
99
100
101void 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
124{
125 logger << INFO << "Setting COMBINATION_POLICY to " << cp << endl;
126 delete combiner;
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}
Tracks user interaction and context.
virtual std::string get_name() const =0
virtual std::string get_value() const =0
void addSuggestion(Suggestion)
void setMaxPartialPredictionSize(const std::string &size)
void parse_internal_commands(Prediction &pred)
void setPredictTime(const std::string &predictTime)
std::string getCombinationPolicy() const
PredictorActivator(Configuration *config, PredictorRegistry *registry, ContextTracker *contextTracker)
ContextTracker * contextTracker
static const char * MAX_PARTIAL_PREDICTION_SIZE
void setCombinationPolicy(const std::string &policy)
Dispatcher< PredictorActivator > dispatcher
static const char * LOGGER
virtual void update(const Observable *variable)
static const char * PREDICT_TIME
PredictorRegistry * predictorRegistry
void setLogger(const std::string &level)
std::vector< Prediction > predictions
Prediction predict(unsigned int multiplier, const char **filter)
static const char * COMBINATION_POLICY
const std::string getName() const
Definition predictor.cpp:66
virtual Prediction predict(const size_t size, const char **filter) const =0
Generate prediction.
static char * strtolower(char *)
Definition utility.cpp:42
static int toInt(const std::string)
Definition utility.cpp:266
_SetLevel setlevel(std::string __l)
Manipulator for level.
Definition logger.h:46
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition logger.h:278
#define PACKAGE_STRING