Various small changes
[dasher.git] / Src / DasherCore / DasherModel.h
blob42affb6c51ab99b92afbd2892f9883602692af6b
3 // DasherModel.h
4 //
5 // Copyright (c) 2001-2005 David Ward
7 #ifndef __DasherModel_h__
8 #define __DasherModel_h__
10 #ifndef WIN32
11 #include "config.h"
12 #endif
14 #include "../Common/NoClones.h"
16 #include "LanguageModelling/LanguageModel.h"
17 #include "DasherNode.h"
18 #include "DasherComponent.h"
19 #include "Alphabet/Alphabet.h"
20 #include "Alphabet/AlphIO.h"
21 #include "AlphabetManagerFactory.h"
22 #include "ControlManagerFactory.h"
24 #ifdef JAPANESE
25 #include "ConversionManagerFactory.h"
26 #endif
28 #include <math.h>
29 #include "DasherTypes.h"
30 #include "FrameRate.h"
31 #include <vector>
32 #include <deque>
36 namespace Dasher {
37 class CDasherModel;
38 class CDasherInterfaceBase;
39 class CDasherView;
42 ///
43 /// \brief Dasher 'world' data structures and dynamics.
44 ///
45 /// The DasherModel represents the current state of Dasher
46 /// It contains a tree of DasherNodes
47 /// knows the current viewpoint
48 /// knows how to evolve the viewpoint
50 class Dasher::CDasherModel:public Dasher::CDasherComponent, private NoClones
52 public:
54 class CTrainer {
55 public:
56 CTrainer(CDasherModel & DasherModel);
58 void Train(const std::vector < symbol > &vSymbols);
60 ~CTrainer();
62 private:
63 CLanguageModel::Context m_Context;
64 CDasherModel & m_DasherModel;
68 typedef enum {
69 idPPM = 0,
70 idBigram = 1,
71 idWord = 2,
72 idMixture = 3,
73 idJapanese = 4
74 } LanguageModelID;
76 CDasherModel(CEventHandler * pEventHandler, CSettingsStore * pSettingsStore, CDasherInterfaceBase * pDashIface, CAlphIO *pAlphIO, bool bGameMode = false, const std::string &strGameModeText = "");
77 ~CDasherModel();
79 bool WriteLMToFile(const std::string &strFilename) {
80 if(m_pLanguageModel)
81 return m_pLanguageModel->WriteToFile(strFilename);
82 else
83 return false;
86 bool ReadLMFromFile(const std::string &strFilename) {
87 if(m_pLanguageModel)
88 return m_pLanguageModel->ReadFromFile(strFilename);
89 else
90 return false;
93 void HandleEvent(Dasher::CEvent * pEvent);
95 CTrainer *GetTrainer();
97 // framerate functions
98 void NewFrame(unsigned long Time) {
99 m_fr.NewFrame(Time);
102 // called everytime we render a new frame
103 double Framerate() const {
104 return m_fr.Framerate();
107 void Reset_framerate(unsigned long Time) {
108 m_fr.Reset(Time);
111 void Halt() {
112 m_fr.Initialise();
115 void RecursiveOutput(CDasherNode *pNode, Dasher::VECTOR_SYMBOL_PROB* pAdded);
117 // User control of speed
118 void SetBitrate(double TargetRate) {
119 m_fr.SetBitrate(TargetRate);
120 } // Use or start at this bitrate
121 //void SetMaxBitrate(double MaxRate) {m_dMaxRate=MaxRate;m_fr.SetMaxBitrate(MaxRate);} // Cap any adaption at this rate
123 /* std::string GroupLabel(int group) const { */
124 /* return m_pcAlphabet->GetGroupLabel(group); */
125 /* } */
126 /* int GroupColour(int group) const { */
127 /* return m_pcAlphabet->GetGroupColour(group); */
128 /* } */
130 void SetContext(std::string & sNewContext);
132 // functions returning private data (read only access)
133 myint Rootmin() const {
134 return m_Rootmin;
136 myint Rootmax() const {
137 return m_Rootmax;
139 CDasherNode *Root() const {
140 return m_Root;
143 void OutputCharacters(CDasherNode * node);
144 bool DeleteCharacters(CDasherNode * newnode, CDasherNode * oldnode, int* pNumDeleted = NULL);
146 void Trace() const; // diagnostics
147 //void Learn_symbol(symbol Symbol) {m_languagemodel->learn_symbol(Symbol);} // feed character to language model
149 bool Tap_on_display(myint, myint, unsigned long iTime, Dasher::VECTOR_SYMBOL_PROB* pAdded = NULL, int* pNumDeleted = NULL); // evolves the current viewpoint
151 /* void GoTo(double, myint); // jumps to a new viewpoint */
152 void NewGoTo(myint n1, myint n2, Dasher::VECTOR_SYMBOL_PROB* pAdded, int* pNumDeleted);
153 void OldPush(myint iMousex, myint iMousey);
154 double Plan_new_goto_coords(int iRxnew, myint mousey, int *iSteps, myint *o1, myint *o2 , myint *n1, myint *n2);
156 void Start(); // initializes the data structure
158 ///
159 /// Make a child of the root into a new root
162 void Make_root(CDasherNode *whichchild);
165 /// Clear the queue of old roots - used when those nodes become
166 /// invalid, eg during changes to conrol mode
169 void ClearRootQueue();
172 /// A version of Make_root which is suitable for arbitrary
173 /// descendents of the root, not just immediate children.
176 void RecursiveMakeRoot(CDasherNode *pNewRoot);
179 /// Rebuild the data structure such that a given node is guaranteed
180 /// to be in the same place on the screen. This would usually be the
181 /// node under the crosshair
184 void RebuildAroundNode(CDasherNode *pNode);
186 void Reparent_root(int lower, int upper); // change back to the previous root
189 void ResetNats() {
190 m_dTotalNats = 0.0;
193 double GetNats() {
194 return m_dTotalNats;
197 // myint PlotGoTo(myint MouseX, myint MouseY);
199 void EnterText(CLanguageModel::Context Context, std::string TheText) const;
200 void LearnText(CLanguageModel::Context Context, std::string * TheText, bool IsMore);
202 CLanguageModel::Context CreateEmptyContext()const;
204 // Alphabet pass-through functions for widely needed information
205 symbol GetSpaceSymbol() const {
206 return m_pcAlphabet->GetSpaceSymbol();
209 symbol GetControlSymbol() const {
210 return m_pcAlphabet->GetControlSymbol();
213 symbol GetStartConversionSymbol() const {
214 return m_pcAlphabet->GetStartConversionSymbol();
218 const std::string & GetDisplayText(int iSymbol) const {
219 return m_pcAlphabet->GetDisplayText(iSymbol);
222 const CAlphabet & GetAlphabet() const {
223 return *m_pcAlphabet;
226 CAlphabet *GetAlphabetNew() const {
227 return m_pcAlphabet;
230 CDasherNode *Get_node_under_crosshair(); // Needed for Game Mode
232 CDasherNode *GetRoot( int iType, CDasherNode *pParent, int iLower, int iUpper, void *pUserData ) {
233 switch(iType) {
234 case 0:
235 return m_pAlphabetManagerFactory->GetRoot(pParent, iLower, iUpper, pUserData);
236 case 1:
237 return m_pControlManagerFactory->GetRoot(pParent, iLower, iUpper, pUserData);
238 #ifdef JAPANESE
239 case 2:
240 return m_pConversionManagerFactory->GetRoot(pParent, iLower, iUpper, pUserData);
241 #endif
245 // FIXME - only public temporarily
246 void GetProbs(CLanguageModel::Context context, std::vector < symbol > &NewSymbols, std::vector < unsigned int >&Probs, int iNorm) const;
247 int GetColour(symbol s) const;
250 // Control mode stuff
252 void RegisterNode( int iID, const std::string &strLabel, int iColour ) {
253 m_pControlManagerFactory->RegisterNode(iID, strLabel, iColour);
256 void ConnectNode(int iChild, int iParent, int iAfter) {
257 m_pControlManagerFactory->ConnectNode(iChild, iParent, iAfter);
260 void DisconnectNode(int iChild, int iParent) {
261 m_pControlManagerFactory->DisconnectNode(iChild, iParent);
264 void Push_Node(CDasherNode * pNode); // give birth to children
266 bool m_bContextSensitive;
268 std::string m_strContextBuffer;
270 bool RenderToView(CDasherView *pView, bool bRedrawDisplay);
272 bool CheckForNewRoot(CDasherView *pView);
274 void ScheduleZoom(int dasherx, int dashery);
276 void HandleOutput(CDasherNode *pNewNode, CDasherNode *pOldNode, Dasher::VECTOR_SYMBOL_PROB* pAdded, int* pNumDeleted);
278 int ScheduledSteps() {
279 return m_deGotoQueue.size();
282 // Apply an offset to the 'target' coordinates - implements the jumps in
283 // two button dynamic mode.
284 void Offset(int iOffset);
286 // Reset the 'target' root coordinates to match those currently visible.
287 // Appropriate for abrubt changes in behaviour (such as backing off in
288 // button modes)
289 void MatchTarget(bool bReverse);
291 // This is pretty horrible - a rethink of the start/reset mechanism
292 // is definitely in order
293 void LimitRoot(int iMaxWidth);
296 private:
298 CDasherInterfaceBase * m_pDasherInterface;
300 /////////////////////////////////////////////////////////////////////////////
302 // Interfaces
304 CLanguageModel *m_pLanguageModel; // pointer to the language model
306 CAlphabet *m_pcAlphabet; // pointer to the alphabet
308 CLanguageModel::Context LearnContext; // Used to add data to model as it is entered
310 /////////////////////////////////////////////////////////////////////////////
312 CDasherNode *m_Root;
314 // Old root notes
315 std::deque < CDasherNode * >oldroots;
317 // Rootmin and Rootmax specify the position of the root node in Dasher coords
318 myint m_Rootmin, m_Rootmax;
320 myint m_iTargetMin;
321 myint m_iTargetMax;
323 myint m_iTargetOffset; // Displayed rootmin/max - actual rootmin/rootmax
325 myint m_Rootmin_min, m_Rootmax_max;
327 // The active interval over which Dasher nodes are maintained - this is most likely bigger than (0,DasherY)
328 // CRange m_Active;
330 CFrameRate m_fr; // keep track of framerate
332 double m_dTotalNats; // Information entered so far
334 // the probability that gets added to every symbol
335 double m_dAddProb;
337 double m_dMaxRate;
339 int m_Stepnum;
341 CDasherNode *Get_node_under_mouse(myint smousex, myint smousey);
343 void Get_new_root_coords(myint mousex, myint mousey, myint &iNewMin, myint &iNewMax, unsigned long iTime);
345 // void Get_new_goto_coords(double zoomfactor, myint mousey);
347 void Get_string_under_mouse(const myint smousex, const myint smousey, std::vector < symbol > &str);
349 double CorrectionFactor(int dasherx, int dashery);
351 void Recursive_Push_Node(CDasherNode * pNode, int depth);
353 // ControlTree *m_pControltree;
355 CAlphabetManagerFactory *m_pAlphabetManagerFactory;
356 CControlManagerFactory *m_pControlManagerFactory;
358 bool m_bGameMode;
360 unsigned long m_iStartTime;
362 #ifdef JAPANESE
363 CConversionManagerFactory *m_pConversionManagerFactory;
364 #endif
366 struct SGotoItem {
367 myint iN1;
368 myint iN2;
369 int iStyle;
372 std::deque<SGotoItem> m_deGotoQueue;
374 // Whether characters entered by alphabet manager are expected to
375 // require conversion.
376 // TODO: Need to rethink this at some point.
377 bool m_bRequireConversion;
379 friend class CTrainer;
380 friend class CDasherNode;
384 /////////////////////////////////////////////////////////////////////////////
386 inline CLanguageModel::Context CDasherModel::CreateEmptyContext() const {
387 return m_pLanguageModel->CreateEmptyContext();
390 inline int CDasherModel::GetColour(symbol s) const {
391 return m_pcAlphabet->GetColour(s);
394 #endif /* #ifndef __DasherModel_h__ */