implemented infinite backoff (fixes #311767)
[dasher.git] / Src / DasherCore / DasherModel.h
blob9858884770c5e8940dc579c1da591d862765f91b
1 // DasherModel.h
2 //
3 // Copyright (c) 2001-2005 David Ward
5 #ifndef __DasherModel_h__
6 #define __DasherModel_h__
8 #include "../Common/NoClones.h"
10 #include "LanguageModelling/LanguageModel.h"
11 #include "DashEdit.h"
12 #include "DasherNode.h"
13 #include "DasherComponent.h"
14 #include "Alphabet/Alphabet.h"
15 #include "AlphabetManagerFactory.h"
16 #include "ControlManagerFactory.h"
17 #include <math.h>
18 #include "DasherTypes.h"
19 #include "FrameRate.h"
20 #include <vector>
21 #include <deque>
22 #include "DasherGameMode.h"
25 namespace Dasher {
26 class CDasherModel;
27 class CDasherInterfaceBase;
30 class Dasher::CDasherInterfaceBase;
31 ///
32 /// \brief Dasher 'world' data structures and dynamics.
33 ///
34 /// The DasherModel represents the current state of Dasher
35 /// It contains a tree of DasherNodes
36 /// knows the current viewpoint
37 /// knows how to evolve the viewpoint
39 class Dasher::CDasherModel:public Dasher::CDasherComponent, private NoClones
41 public:
43 class CTrainer {
44 public:
45 CTrainer(CDasherModel & DasherModel);
47 void Train(const std::vector < symbol > &vSymbols);
49 ~CTrainer();
51 private:
52 CLanguageModel::Context m_Context;
53 CDasherModel & m_DasherModel;
57 typedef enum {
58 idPPM = 0,
59 idBigram = 1,
60 idWord = 2,
61 idMixture = 3,
62 idJapanese = 4
63 } LanguageModelID;
65 CDasherModel(CEventHandler * pEventHandler, CSettingsStore * pSettingsStore, CDasherInterfaceBase * pDashIface);
66 ~CDasherModel();
68 bool WriteLMToFile(const std::string &strFilename) {
69 if(m_pLanguageModel)
70 return m_pLanguageModel->WriteToFile(strFilename);
71 else
72 return false;
75 bool ReadLMFromFile(const std::string &strFilename) {
76 if(m_pLanguageModel)
77 return m_pLanguageModel->ReadFromFile(strFilename);
78 else
79 return false;
82 void HandleEvent(Dasher::CEvent * pEvent);
84 CTrainer *GetTrainer();
86 // framerate functions
87 void NewFrame(unsigned long Time) {
88 m_fr.NewFrame(Time);
89 } // called everytime we render a new frame
90 double Framerate() const {
91 return m_fr.Framerate();
92 } // return the framerate
93 void Reset_framerate(unsigned long Time) {
94 m_fr.Reset(Time);
97 void Halt() {
98 m_fr.Initialise();
101 void RecursiveOutput(CDasherNode *pNode, Dasher::VECTOR_SYMBOL_PROB* pAdded);
103 // User control of speed
104 void SetBitrate(double TargetRate) {
105 m_fr.SetBitrate(TargetRate);
106 } // Use or start at this bitrate
107 //void SetMaxBitrate(double MaxRate) {m_dMaxRate=MaxRate;m_fr.SetMaxBitrate(MaxRate);} // Cap any adaption at this rate
109 std::string GroupLabel(int group) const {
110 return m_pcAlphabet->GetGroupLabel(group);
112 int GroupColour(int group) const {
113 return m_pcAlphabet->GetGroupColour(group);
115 void SetContext(std::string & sNewContext);
117 // functions returning private data (read only access)
118 myint Rootmin() const {
119 return m_Rootmin;
121 myint Rootmax() const {
122 return m_Rootmax;
124 myint DasherOX() const {
125 return m_DasherOX;
127 myint DasherOY() const {
128 return m_DasherOY;
130 CDasherNode *Root() const {
131 return m_Root;
133 myint DasherY() const {
134 return m_DasherY;
136 void OutputCharacters(CDasherNode * node);
137 bool DeleteCharacters(CDasherNode * newnode, CDasherNode * oldnode, int* pNumDeleted = NULL);
139 void Trace() const; // diagnostics
140 //void Learn_symbol(symbol Symbol) {m_languagemodel->learn_symbol(Symbol);} // feed character to language model
142 void Tap_on_display(myint, myint, unsigned long Time, Dasher::VECTOR_SYMBOL_PROB* pAdded = NULL, int* pNumDeleted = NULL); // evolves the current viewpoint
144 void GoTo(double, myint); // jumps to a new viewpoint
145 void NewGoTo(myint n1, myint n2, int style);
146 double CDasherModel::Plan_new_goto_coords(int iRxnew, myint mousey, int *iSteps, myint *o1, myint *o2 , myint *n1, myint *n2);
148 void Start(); // initializes the data structure
149 void Make_root(CDasherNode *whichchild); // find a new root node
150 void Reparent_root(int lower, int upper); // change back to the previous root
152 int GetMode() {
153 int mode;
155 if(GetBoolParameter(BP_NUMBER_DIMENSIONS))
156 mode = 1;
157 else if(GetBoolParameter(BP_EYETRACKER_MODE))
158 mode = 2;
159 else
160 mode = 0;
162 return mode;
165 void ResetNats() {
166 total_nats = 0;
169 double GetNats() {
170 return total_nats;
173 myint PlotGoTo(myint MouseX, myint MouseY);
175 void NewControlTree(ControlTree * tree) {
176 m_pControltree = tree;
178 ControlTree *GetControlTree() const {
179 return m_pControltree;
181 struct CRange {
182 CRange(myint _iMin, myint _iMax):iMin(_iMin), iMax(_iMax) {
184 CRange() {
186 myint iMin;
187 myint iMax;
190 void SetActive(const CRange & range) {
191 m_Active = range;
194 void EnterText(CLanguageModel::Context Context, std::string TheText) const;
195 void LearnText(CLanguageModel::Context Context, std::string * TheText, bool IsMore);
197 CLanguageModel::Context CreateEmptyContext()const;
199 // Alphabet pass-through functions for widely needed information
200 symbol GetSpaceSymbol() const {
201 return m_pcAlphabet->GetSpaceSymbol();
203 symbol GetControlSymbol() const {
204 return m_pcAlphabet->GetControlSymbol();
206 const std::string & GetDisplayText(int iSymbol) const {
207 return m_pcAlphabet->GetDisplayText(iSymbol);
209 const CAlphabet & GetAlphabet() const {
210 return *m_pcAlphabet;
212 CDasherNode *Get_node_under_crosshair(); // Needed for Game Mode
213 myint GetGameModePointerLoc() {
214 return m_pGameMode->GetDasherCoordOfTarget();
218 CDasherNode *GetRoot( int iType, CDasherNode *pParent, int iLower, int iUpper, void *pUserData ) {
219 if( iType == 0 )
220 return m_pAlphabetManagerFactory->GetRoot(pParent, iLower, iUpper, pUserData);
221 else
222 return m_pControlManagerFactory->GetRoot(pParent, iLower, iUpper, pUserData);
225 // FIXME - only public temporarily
226 void GetProbs(CLanguageModel::Context context, std::vector < symbol > &NewSymbols, std::vector < unsigned int >&Probs, int iNorm) const;
227 int GetColour(symbol s) const;
230 // Control mode stuff
232 void RegisterNode( int iID, const std::string &strLabel, int iColour ) {
233 m_pControlManagerFactory->RegisterNode(iID, strLabel, iColour);
236 void ConnectNode(int iChild, int iParent, int iAfter) {
237 m_pControlManagerFactory->ConnectNode(iChild, iParent, iAfter);
240 void Push_Node(CDasherNode * pNode); // give birth to children
242 bool m_bContextSensitive;
244 std::string m_strContextBuffer;
246 private:
248 CDasherInterfaceBase * m_pDasherInterface;
250 /////////////////////////////////////////////////////////////////////////////
252 // Interfaces
254 CLanguageModel *m_pLanguageModel; // pointer to the language model
256 const CAlphabet *m_pcAlphabet; // pointer to the alphabet
258 CLanguageModel::Context LearnContext; // Used to add data to model as it is entered
260 /////////////////////////////////////////////////////////////////////////////
262 CDasherGameMode *m_pGameMode;
264 CDasherNode *m_Root;
266 // Old root notes
267 std::deque < CDasherNode * >oldroots;
269 // Rootmin and Rootmax specify the position of the root node in Dasher coords
270 myint m_Rootmin, m_Rootmax;
272 myint m_Rootmin_min, m_Rootmax_max;
274 // Size of Dasher's arithmetic coding interval - it defines the Dasher coordinate system
275 myint m_DasherY;
277 // x position of crosshair in Dasher coords - distance from RHS is square Dasher
278 myint m_DasherOX;
280 // y position of crosshair in Dasher coords - distance from top in square Dasher
281 myint m_DasherOY;
283 // The active interval over which Dasher nodes are maintained - this is most likely bigger than (0,DasherY)
284 CRange m_Active;
286 CFrameRate m_fr; // keep track of framerate
288 double total_nats; // Information entered so far
290 // the probability that gets added to every symbol
291 double m_dAddProb;
293 double m_dMaxRate;
295 int m_Stepnum;
297 CDasherNode *Get_node_under_mouse(myint smousex, myint smousey);
299 double Get_new_root_coords(myint mousex, myint mousey);
301 void DoZoom(myint iTargetMin, myint iTargetMax);
303 void Get_new_goto_coords(double zoomfactor, myint mousey);
305 void Get_string_under_mouse(const myint smousex, const myint smousey, std::vector < symbol > &str);
310 void Recursive_Push_Node(CDasherNode * pNode, int depth);
313 ControlTree *m_pControltree;
315 CAlphabetManagerFactory *m_pAlphabetManagerFactory;
316 CControlManagerFactory *m_pControlManagerFactory;
318 friend class CDasherGameMode;
319 friend class CTrainer;
320 friend class CDasherNode;
324 /////////////////////////////////////////////////////////////////////////////
326 inline CLanguageModel::Context CDasherModel::CreateEmptyContext() const {
327 return m_pLanguageModel->CreateEmptyContext();
330 inline int CDasherModel::GetColour(symbol s) const {
331 return m_pcAlphabet->GetColour(s);
334 #endif /* #ifndef __DasherModel_h__ */