//----------------------------------------------------------------------------//
//                                                                            //
//    module ThrUseModel.cpp                                                  //
//                                                                            //
//    Thread function: Use given model (TCluster) with given parameters       //
//    (TParameter) to predict the unknown ouput values using only the inputs  //
//    for a given TData object. Employs the class TPrediction.                //
//                                                                            //
//    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 "ThrUseModel.h"
#pragma package(smart_init)


//----------------------------------------------------------------------------------------------------------------------
// constructor
__fastcall ThrUseModel::ThrUseModel(bool CreateSuspended, TFMain*const& _main, TData*const& _data,
         const bool& _f_IsWithOutput, TLossFunction*const& _loss, TCluster*const& _model,
         TParameter*const& _para, ofstream*const& _file) : TThread(CreateSuspended)
{
   main  = _main;                    // a) copy
   data  = _data;
   loss  = _loss;
   model = _model;
   para  = _para;
   file  = _file;

   f_IsWithOutput = _f_IsWithOutput;


   prd  = NULL;                     // b) ini

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


//----------------------------------------------------------------------------------------------------------------------
// call TPrediction::Predict()
void __fastcall ThrUseModel::Execute()
{
   try
   {
      prd = new TPrediction(model, para);         // create prediction object
      prd->Predict(data, loss, file, f_IsWithOutput, &main->GetStopFlag());     // predict
   }
   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
   }

   delete file;     // release file and prediction object when ready
   delete prd;
}

//----------------------------------------------------------------------------------------------------------------------
// call when finished
void __fastcall ThrUseModel::OnFinish(TObject*)
{
   try
   {
      main->OnDeployFinish();
   }
   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 ThrUseModel::MessageBox()
{
   char szCaption[256];
   Application->MessageBox(szText, "Error Learning Model", MB_OK | MB_ICONERROR);
}


//----------------------------------------------------------------------------------------------------------------------
// display error message
const int ThrUseModel::Progress() const
{
   if(prd)
      return prd->Progress();
   else
      return 0;
}