[dasher: 13/16] Refactoring: simplify state & lock management functions; reorganise training



commit c710591a5e0161c585beec59c8b174a8d4ab0b7f
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Tue Oct 20 12:58:42 2009 +0200

    Refactoring: simplify state & lock management functions; reorganise training
    
    DasherInterfaceBase::{Enter,Leave}State removed - largely unnecessary
    {Add,Release}Lock removed in favour of using CLockEvent mechanism
    NodeCreationManager selects training text files and manages locking

 Src/DasherCore/DasherInterfaceBase.cpp |  120 +++-----------------------------
 Src/DasherCore/DasherInterfaceBase.h   |   30 --------
 Src/DasherCore/NodeCreationManager.cpp |   16 +++-
 Src/DasherCore/Trainer.cpp             |   47 +------------
 Src/DasherCore/Trainer.h               |    7 +-
 Src/DasherCore/TrainingHelper.h        |    6 +-
 6 files changed, 32 insertions(+), 194 deletions(-)
---
diff --git a/Src/DasherCore/DasherInterfaceBase.cpp b/Src/DasherCore/DasherInterfaceBase.cpp
index 1c02b7a..a81ce92 100644
--- a/Src/DasherCore/DasherInterfaceBase.cpp
+++ b/Src/DasherCore/DasherInterfaceBase.cpp
@@ -360,21 +360,6 @@ void CDasherInterfaceBase::InterfaceEventHandler(Dasher::CEvent *pEvent) {
       m_pDasherModel->TriggerSlowdown();
     }
   }
-  else if(pEvent->m_iEventType == EV_LOCK) {
-//    CLockEvent *pLockEvent(static_cast<CLockEvent *>(pEvent));
-
-//     // TODO: Sort this out - at the moment these don't occur in pairs, so the old boolean variable is still needed
-//     if(pLockEvent->m_bLock) {
-//       if(m_bGlobalLock)
-// 	AddLock(0);
-//     }
-//     else {
-//       if(!m_bGlobalLock)
-// 	ReleaseLock(0);
-//     }
-
-//    m_bGlobalLock = pLockEvent->m_bLock;
-  }
 }
 
 void CDasherInterfaceBase::WriteTrainFileFull() {
@@ -417,16 +402,6 @@ void CDasherInterfaceBase::CreateNCManager() {
   if( lmID == -1 )
     return;
 
- // Train the new language model
-  //    CLockEvent *pEvent;
-
-    //    pEvent = new CLockEvent("Training Dasher", true, 0);
-    //  m_pEventHandler->InsertEvent(pEvent);
-    //  delete pEvent;
-
-    // TODO: Move training into nc manager
-    int iTrainingLock = AddLock("Training Dasher");
-
     int iOffset;
 
     if(m_pDasherModel)
@@ -449,15 +424,6 @@ void CDasherInterfaceBase::CreateNCManager() {
 
     m_Alphabet = m_pNCManager->GetAlphabet();
 
-    ReleaseLock(iTrainingLock);
-
-
-//     pEvent = new CLockEvent("Training Dasher", false, 0);
-//     m_pEventHandler->InsertEvent(pEvent);
-//     delete pEvent;
-
-//     ReleaseLock(0);
-
     // TODO: Eventually we'll not have to pass the NC manager to the model...
     CreateModel(iOffset);
 }
@@ -620,11 +586,6 @@ void CDasherInterfaceBase::Redraw(bool bRedrawNodes, NodeQueue &nodeQueue) {
   bActionButtonsChanged = DrawActionButtons();
 #endif
 
-  SLockData *pCurrentLock = GetCurrentLock();
-  if(pCurrentLock) {
-    std::cout << "Rendering lock" << std::endl;
-  }
-
   // Only blit the image to the display if something has actually changed
   if(bRedrawNodes || bDecorationsChanged || bActionButtonsChanged)
     m_pDasherView->Display();
@@ -1106,86 +1067,27 @@ void CDasherInterfaceBase::OnUIRealised() {
 
 void CDasherInterfaceBase::ChangeState(ETransition iTransition) {
   static EState iTransitionTable[ST_NUM][TR_NUM] = {
-    {ST_MODEL, ST_UI, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN},
-    {ST_FORBIDDEN, ST_NORMAL, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN},
-    {ST_NORMAL, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN},
-    {ST_FORBIDDEN, ST_FORBIDDEN, ST_LOCKED, ST_FORBIDDEN, ST_SHUTDOWN},
-    {ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN, ST_NORMAL, ST_FORBIDDEN},
-    {ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN}
+    {ST_MODEL, ST_UI, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN},//ST_START
+    {ST_FORBIDDEN, ST_NORMAL, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN},//ST_MODEL
+    {ST_NORMAL, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN},//ST_UI
+    {ST_FORBIDDEN, ST_FORBIDDEN, ST_LOCKED, ST_FORBIDDEN, ST_SHUTDOWN},//ST_NORMAL
+    {ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN, ST_NORMAL, ST_FORBIDDEN},//ST_LOCKED
+    {ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN}//ST_SHUTDOWN
+  //TR_MODEL_INIT, TR_UI_INIT,   TR_LOCK,      TR_UNLOCK,    TR_SHUTDOWN
   };
 
   EState iNewState(iTransitionTable[m_iCurrentState][iTransition]);
 
   if(iNewState != ST_FORBIDDEN) {
-    LeaveState(m_iCurrentState);
-    EnterState(iNewState);
+    if (iNewState == ST_SHUTDOWN) {
+      ShutdownTimer();  
+      WriteTrainFileFull();      
+    }
 
     m_iCurrentState = iNewState;
   }
 }
 
-void CDasherInterfaceBase::LeaveState(EState iState) {
-
-}
-
-void CDasherInterfaceBase::EnterState(EState iState) {
-  switch(iState) {
-  case ST_SHUTDOWN:
-    ShutdownTimer();
-    WriteTrainFileFull();
-    break;
-  default:
-    // Not handled
-    break;
-  }
-}
-
-int CDasherInterfaceBase::AddLock(const std::string &strDisplay) {
-#ifdef DEBUG
-  std::cout << "Adding lock " << strDisplay << std::endl;
-#endif
-  SLockData sNewLock;
-
-  sNewLock.strDisplay = strDisplay;
-  sNewLock.iPercent = 0;
-
-  if(m_mapCurrentLocks.size() == 0)
-    ChangeState(TR_LOCK);
-
-  m_mapCurrentLocks[m_iNextLockID] = sNewLock;
-  ++m_iNextLockID;
-
-  NoNodeQueue nnq; //don't allow change to expansion state.
-  Redraw(false, nnq);
-
-  return (m_iNextLockID - 1);
-}
-
-void CDasherInterfaceBase::ReleaseLock(int iLockID) {
-#ifdef DEBUG
-  std::cout << "Releasing Lock" << std::endl;
-#endif
-  std::map<int, SLockData>::iterator it = m_mapCurrentLocks.find(iLockID);
-
-  if(it != m_mapCurrentLocks.end()) {
-    m_mapCurrentLocks.erase(it);
-  }
-
-  NoNodeQueue nnq; //don't allow changes to expansion state.
-  Redraw(false, nnq);
-
-  if(m_mapCurrentLocks.size() == 0)
-    ChangeState(TR_UNLOCK);
-}
-
-SLockData *CDasherInterfaceBase::GetCurrentLock() {
-
-  if(m_mapCurrentLocks.size() > 0)
-    return &(m_mapCurrentLocks.begin()->second);
-  else
-    return NULL;
-}
-
 void CDasherInterfaceBase::SetBuffer(int iOffset) {
    CreateModel(iOffset);
 }
diff --git a/Src/DasherCore/DasherInterfaceBase.h b/Src/DasherCore/DasherInterfaceBase.h
index f587a73..6c4fbf3 100644
--- a/Src/DasherCore/DasherInterfaceBase.h
+++ b/Src/DasherCore/DasherInterfaceBase.h
@@ -530,38 +530,8 @@ protected:
   void PositionActionButtons();
   bool DrawActionButtons();
 
-  void LeaveState(EState iState);
-  void EnterState(EState iState);
-
   void WriteTrainFileFull();
   void WriteTrainFilePartial();
-
-  
-  /// @name Lock Management
-  /// Functions for locking/unlocking the core. Note that the lock
-  /// flags parameter is currently ignored, but will be used to give
-  /// finer granularity
-  /// @{
-
-  ///
-  /// Add a lock
-  ///
-
-  int AddLock(const std::string &strDisplay);
-
-  ///
-  /// Release an existing lock. Note that these functions have minimal
-  /// error checking at the moment, so be careful.
-  ///
-
-  void ReleaseLock(int iLockID);
-
-  ///
-  /// Return the lock to display, or NULL for no locks present
-  ///
-
-  SLockData *GetCurrentLock(); 
-  /// @}
   
   std::deque<std::string> m_deGameModeStrings;
 
diff --git a/Src/DasherCore/NodeCreationManager.cpp b/Src/DasherCore/NodeCreationManager.cpp
index 844c5ee..f293cc7 100644
--- a/Src/DasherCore/NodeCreationManager.cpp
+++ b/Src/DasherCore/NodeCreationManager.cpp
@@ -7,6 +7,7 @@
 #include "LanguageModelling/PPMPYLanguageModel.h"
 #include "NodeCreationManager.h"
 #include "ControlManager.h"
+#include "EventHandler.h"
 
 using namespace Dasher;
 
@@ -24,12 +25,19 @@ CNodeCreationManager::CNodeCreationManager(Dasher::CDasherInterfaceBase *pInterf
   m_pLanguageModel = m_pAlphabetManagerFactory->GetLanguageModel();
   m_pAlphabet = m_pAlphabetManagerFactory->GetAlphabet();
 
-  // Train the language model
   CTrainer *pTrainer =  m_pAlphabetManagerFactory->GetTrainer();
 
-  pTrainer->Train(GetStringParameter(SP_USER_LOC), GetStringParameter(SP_SYSTEM_LOC));
+  //1. Look for system training text...
+  CLockEvent oEvent("Training on System Text", true, 0);
+  pEventHandler->InsertEvent(&oEvent);
+  pTrainer->LoadFile(GetStringParameter(SP_SYSTEM_LOC) + m_pAlphabet->GetTrainingFile());
+  //Now add in any user-provided individual training text...
+  oEvent.m_strMessage = "Training on User Text"; oEvent.m_bLock=true; oEvent.m_iPercent = 0;
+  pEventHandler->InsertEvent(&oEvent);
+  pTrainer->LoadFile(GetStringParameter(SP_USER_LOC) + m_pAlphabet->GetTrainingFile());
+  oEvent.m_bLock = false;
+  pEventHandler->InsertEvent(&oEvent);
 
-  delete pTrainer;
 #ifdef DEBUG_LM_READWRITE
   {
     //test...
@@ -188,7 +196,7 @@ CNodeCreationManager::ImportTrainingText(const std::string &strPath) {
     pTrainer = m_pAlphabetManagerFactory->GetTrainer();
 
   if(m_pAlphabet && pTrainer)
-	pTrainer->Train(strPath);
+	pTrainer->LoadFile(strPath);
 
   delete pTrainer;
 }
diff --git a/Src/DasherCore/Trainer.cpp b/Src/DasherCore/Trainer.cpp
index 2839958..1ffa3fe 100644
--- a/Src/DasherCore/Trainer.cpp
+++ b/Src/DasherCore/Trainer.cpp
@@ -26,34 +26,6 @@ CTrainer::CTrainer(CLanguageModel *pLanguageModel, CAlphabet *pAlphabet)
   : CTrainingHelper(pAlphabet), m_pLanguageModel(pLanguageModel) {
 }
 
-void CTrainer::Train(const std::string &strUserLoc,
-				 const std::string &strSystemLoc) {
-
-	std::string strTrainingFile = m_pAlphabet->GetTrainingFile();
-
-	if (strTrainingFile.empty()) {
-#ifdef DEBUG
-		std::cerr << "Trying to load empty training file (location)" << std::endl;
-#endif
-	} else {
-		LoadFile(strUserLoc   + strTrainingFile);
-		LoadFile(strSystemLoc + strTrainingFile);
-	}
-}
-
-
-void 
-CTrainer::Train(const std::string &strPath) {
-	
-	if (strPath.empty()) {
-#ifdef DEBUG
-		std::cerr << "Trying to load empty training file (path)" << std::endl;
-#endif
-	} else {
-		LoadFile(strPath);
-	}
-}
-
 void CTrainer::Train(const std::vector<symbol> &vSymbols) {
   CLanguageModel::Context sContext = m_pLanguageModel->CreateEmptyContext();
 
@@ -72,26 +44,13 @@ CMandarinTrainer::CMandarinTrainer(CLanguageModel *pLanguageModel, CAlphabet *pA
 //The training of Mandarin Dasher may evolve in to possible paths: 1.Include punctuation (more work); 2.User defined training files (not sure how); 3.Learning as one types (more work)
 //As Manager is produced, training happens in AlphabetManagerFactory
 
-void CMandarinTrainer::Train(const std::string &strUserLoc, const std::string &strSystemLoc){
-
+void CMandarinTrainer::LoadFile(const std::string &strPath) {
   //TrainMandarin takes in the Super Pin Yin Alphabet, and uses the Mandarin Character alphabet stored in private AlphabetManagerFactory
-
-  std::string strTrainingFile = m_pAlphabet->GetTrainingFile();
-
-  std::string strUserPath = strUserLoc + strTrainingFile;
-  std::string strSystemPath = strSystemLoc + strTrainingFile;
-
-  FILE * fpUser = fopen (strUserPath.c_str(), "rb");
-  FILE * fpSystem = fopen(strSystemPath.c_str(), "rb");
-  FILE * fpTrain = fpSystem;
+  FILE * fpTrain = fopen(strPath.c_str(), "rb");
   
   if(!fpTrain) {
-
-    fpTrain = fpUser;
-    if(!fpTrain){
-      printf("Mandarin Training File: cannot open file or incorrect directory\n");
+    std::cout << "Mandarin Training File: cannot open file or incorrect directory" << std::endl;
     return;
-    }
   }
   unsigned numberofchar = 0;
 
diff --git a/Src/DasherCore/Trainer.h b/Src/DasherCore/Trainer.h
index fbefe6b..d8622c9 100644
--- a/Src/DasherCore/Trainer.h
+++ b/Src/DasherCore/Trainer.h
@@ -12,8 +12,6 @@ namespace Dasher {
   class CTrainer : public CTrainingHelper {
   public:
     CTrainer(CLanguageModel *pLanguageModel, CAlphabet *pAlphabet);
-    void Train(const std::string &strUserLoc, const std::string &strSystemLoc);
-	void Train(const std::string &strPath);
 
   protected:
 	virtual void Train(const std::vector<symbol> &vSymbols);
@@ -23,8 +21,9 @@ namespace Dasher {
   class CMandarinTrainer : public CTrainer {
   public:
     CMandarinTrainer(CLanguageModel *pLanguageModel, CAlphabet *pAlphabet, CAlphabet *pCHAlphabet);
-    
-    virtual void Train(const std::string &strUserLoc, const std::string &strSystemLoc);
+
+    //override...
+    virtual void LoadFile(const std::string &strPath);
     
   private:
     CAlphabet * m_pCHAlphabet;
diff --git a/Src/DasherCore/TrainingHelper.h b/Src/DasherCore/TrainingHelper.h
index 7ec7594..750cb6f 100644
--- a/Src/DasherCore/TrainingHelper.h
+++ b/Src/DasherCore/TrainingHelper.h
@@ -39,10 +39,10 @@ namespace Dasher {
     void HandleCData(const XML_Char *szS, 
 		     int iLen);
 
-  protected:
-	void LoadFile(const std::string &strFileName);
+    void LoadFile(const std::string &strFileName);
 
-	const Dasher::CAlphabet *m_pAlphabet;
+  protected:
+    const Dasher::CAlphabet *m_pAlphabet;
 
 	virtual void Train(const std::vector<symbol> &symbols)=0;
 	  



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]