//----------------------------------------------------------------------------//
//    module ThrLearnModule.cpp                                               //
//                                                                            //
//    Thread function: Learns a model using TPnc and transforms the learned   //
//    model afterwards to TCluster by calling TPnc::ToTCluster().             //
//                                                                            //
//    copyright (c) 2003 by Lars Haendel                                      //
//    home: www.newty.de                                                      //
//                                                                            //
//    This program is free software and can be used under the terms of the    //
//    GNU licence. See header file for further information and disclaimer.    //
//                                                                            //
//----------------------------------------------------------------------------//



#include <vcl.h>
#pragma hdrstop

#include "ThrLearnModel.h"
#pragma package(smart_init)


//---------------------------------------------------------------------------------------------------------------------------------------
// constructor
__fastcall ThrLearnModel::ThrLearnModel(bool CreateSuspended, TFMain*const& _main, TPnc*const& _pnc, const bool& _f_Prune, const int& _K)
            : TThread(CreateSuspended)
{
   main    = _main;                  // a) copy
   pnc     = _pnc;
   f_Prune = _f_Prune;                 // flag: prune cuboids
   K       = _K;                       // cardinality

   model = NULL;


   Priority        = tpNormal;         // b) set thread's properties
   OnTerminate     = &OnFinish;
   FreeOnTerminate = true;
}


//---------------------------------------------------------------------------------------------------------------------------------------
// thread function: call TTune
void __fastcall ThrLearnModel::Execute()
{
   try
   {
      while(!main->GetStopFlag() && pnc->Iterate());                          // learn model

      if(pnc->IsFinished())
      {

         // convert to TCluster: then the TPnc object is not needed anymore
         model = pnc->ToTCluster(f_Prune, &main->GetStopFlag(), K);

         if(!main->GetStopFlag())
            model->Prepare();                     // prepare model if stop flag is not set
      }
   }
   catch(TExceptionU excp)                         // exception handling 'type U' exception
   {
      strcpy(szText, excp.GetErrorText());            // copy error message ...
      Synchronize(MessageBox);                        // ... and display it in a message box
   }
   catch(TExceptionAB excp)                        // exception handling 'type AB' exception
   {
      strcpy(szText, excp.GetErrorText());            // copy error message ...
      Synchronize(MessageBox);                        // ... and display it in a message box
   }
   catch(...)                                      // all other exceptions
   {
      strcpy(szText, "C++ Exception ;-)");            // copy error message ...
      Synchronize(MessageBox);                        // ... and display it in a message box
   }
}


//----------------------------------------------------------------------------------------------------------------------
// call when learning is finished
void __fastcall ThrLearnModel::OnFinish(TObject*)
{
   try
   {
      main->OnLearnModelFinished(model);
   }
   catch(TExceptionU excp)                         // exception handling 'type U' exception
   {
      strcpy(szText, excp.GetErrorText());            // copy error message ...
      Synchronize(MessageBox);                        // ... and display it in a message box
   }
   catch(TExceptionAB excp)                        // exception handling 'type AB' exception
   {
      strcpy(szText, excp.GetErrorText());            // copy error message ...
      Synchronize(MessageBox);                        // ... and display it in a message box
   }
   catch(...)                                      // all other exceptions
   {
      strcpy(szText, "C++ Exception ;-)");            // copy error message ...
      Synchronize(MessageBox);                        // ... and display it in a message box
   }
}



//----------------------------------------------------------------------------------------------------------------------
// display error message
void __fastcall ThrLearnModel::MessageBox()
{
   Application->MessageBox(szText, "Error Learning Model", MB_OK | MB_ICONERROR);
}