Salome HOME
Merge branch V7_3_1_BR
[modules/yacs.git] / src / engine / OptimizerAlg.hxx
1 // Copyright (C) 2006-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef __OPTIMIZERALG_HXX__
21 #define __OPTIMIZERALG_HXX__
22
23 #include <string>
24
25 #include "YACSlibEngineExport.hxx"
26 #include "RefCounter.hxx"
27 #include "Exception.hxx"
28 #include "AlternateThread.hxx"
29 #include "Pool.hxx"
30 #include "TypeCode.hxx"
31
32 namespace YACS
33 {
34   namespace ENGINE
35   {
36     class Proc;
37
38     /*!
39      *  \brief Base class factorizing common methods for all algorithms interfaces.
40      */
41     class YACSLIBENGINE_EXPORT OptimizerAlgBase : public RefCounter
42     {
43     protected:
44       Pool *_pool;
45       Proc * _proc;
46       std::string _errorMessage;
47
48     protected:
49       OptimizerAlgBase(Pool *pool);
50       virtual ~OptimizerAlgBase();
51
52       // Methods to implement in real algorithm classes. These methods shall be called
53       // only through proxy methods.
54       //! returns typecode of type expected as Input. OwnerShip of returned pointer is held by this.
55       virtual TypeCode *getTCForIn() const = 0;
56       //! returns typecode of type expected as Output. OwnerShip of returned pointer is held by this.
57       virtual TypeCode *getTCForOut() const = 0;
58       //! returns typecode of type expected for algo initialization. OwnerShip of returned pointer is held by this.
59       virtual TypeCode *getTCForAlgoInit() const;
60       //! returns typecode of type expected as algo result. OwnerShip of returned pointer is held by this.
61       virtual TypeCode *getTCForAlgoResult() const;
62       virtual void initialize(const Any *input) throw (Exception);
63       virtual void start() = 0;//! Update _pool attribute before performing anything.
64       virtual void takeDecision() = 0;//! _pool->getCurrentId gives the \b id at the origin of this call.
65                                       //! Perform the job of analysing to know what new jobs to do (_pool->pushInSample)
66                                       //! or in case of convergence _pool->destroyAll
67       virtual void finish();//! Called when optimization has succeed.
68       virtual Any * getAlgoResult();
69
70     public:
71       // Proxy methods to encapsulate the call to real methods
72       virtual TypeCode *getTCForInProxy() const;
73       virtual TypeCode *getTCForOutProxy() const;
74       virtual TypeCode *getTCForAlgoInitProxy() const;
75       virtual TypeCode *getTCForAlgoResultProxy() const;
76       virtual void initializeProxy(const Any *input) throw (Exception);
77       virtual void startProxy();
78       virtual void takeDecisionProxy();
79       virtual void finishProxy();
80       virtual Any * getAlgoResultProxy();
81
82       // Utility methods
83       virtual void setPool(Pool *pool);
84       virtual void setProc(Proc * proc);
85       virtual Proc * getProc();
86       virtual bool hasError() const;
87       virtual const std::string & getError() const;
88       virtual void setError(const std::string & message);
89
90     };
91
92     typedef OptimizerAlgBase OptimizerAlgSync;
93
94     /*!
95      *  \brief Base class to implement in external dynamic lib in case of optimizer non event oriented.
96      */
97     class YACSLIBENGINE_EXPORT OptimizerAlgASync : public OptimizerAlgBase,
98                                                    public YACS::BASES::AlternateThread
99     {
100     protected:
101       OptimizerAlgASync(Pool *pool);
102     public:
103       virtual ~OptimizerAlgASync();
104       virtual void finishProxy();//! Called when optimization has succeed.
105
106     protected:
107       virtual void start();//! Update _pool attribute before performing anything.
108       virtual void takeDecision();//! _pool->getCurrentId gives the \b id at the origin of this call.
109                                   //! Perform the job of analysing to know what new jobs to do (_pool->pushInSample)
110                                   //! or in case of convergence _pool->destroyAll
111       virtual void run();
112
113       //! _pool->getCurrentId gives the \b id at the origin of this call.
114       //! Perform the job between 2 'condition->wait()' of analysing to know what new jobs to do
115       //! (_pool->pushInSample) or in case of convergence _pool->destroyAll
116       //! WARNING ! 'condition->wait()' must be called before any analyse of Pool.
117       virtual void startToTakeDecision()=0;
118     };
119
120     typedef OptimizerAlgBase *(*OptimizerAlgBaseFactory)(Pool *pool);
121   }
122 }
123
124 #endif