Salome HOME
updated copyright message
[modules/yacs.git] / src / engine / OptimizerAlg.hxx
1 // Copyright (C) 2006-2023  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       int _nbOfBranches;
48
49     protected:
50       OptimizerAlgBase(Pool *pool);
51       virtual ~OptimizerAlgBase();
52
53       // Methods to implement in real algorithm classes. These methods shall be called
54       // only through proxy methods.
55       //! returns typecode of type expected as Input. OwnerShip of returned pointer is held by this.
56       virtual TypeCode *getTCForIn() const = 0;
57       //! returns typecode of type expected as Output. OwnerShip of returned pointer is held by this.
58       virtual TypeCode *getTCForOut() const = 0;
59       //! returns typecode of type expected for algo initialization. OwnerShip of returned pointer is held by this.
60       virtual TypeCode *getTCForAlgoInit() const;
61       //! returns typecode of type expected as algo result. OwnerShip of returned pointer is held by this.
62       virtual TypeCode *getTCForAlgoResult() const;
63       virtual void initialize(const Any *input) ;
64       virtual void start() = 0;//! Update _pool attribute before performing anything.
65       virtual void takeDecision() = 0;//! _pool->getCurrentId gives the \b id at the origin of this call.
66                                       //! Perform the job of analysing to know what new jobs to do (_pool->pushInSample)
67                                       //! or in case of convergence _pool->destroyAll
68       virtual void finish();//! Called when optimization has succeed.
69       virtual Any * getAlgoResult();
70
71     public:
72       // Proxy methods to encapsulate the call to real methods
73       virtual TypeCode *getTCForInProxy() const;
74       virtual TypeCode *getTCForOutProxy() const;
75       virtual TypeCode *getTCForAlgoInitProxy() const;
76       virtual TypeCode *getTCForAlgoResultProxy() const;
77       virtual void initializeProxy(const Any *input) ;
78       virtual void startProxy();
79       virtual void takeDecisionProxy();
80       virtual void finishProxy();
81       virtual Any * getAlgoResultProxy();
82
83       // Utility methods
84       virtual void setPool(Pool *pool);
85       virtual void setProc(Proc * proc);
86       virtual Proc * getProc();
87       virtual bool hasError() const;
88       virtual const std::string & getError() const;
89       virtual void setError(const std::string & message);
90       void setNbOfBranches(int nbOfBranches);
91       int getNbOfBranches() const;
92     };
93
94     typedef OptimizerAlgBase OptimizerAlgSync;
95
96     /*!
97      *  \brief Base class to implement in external dynamic lib in case of optimizer non event oriented.
98      */
99     class YACSLIBENGINE_EXPORT OptimizerAlgASync : public OptimizerAlgBase,
100                                                    public YACS::BASES::AlternateThread
101     {
102     protected:
103       OptimizerAlgASync(Pool *pool);
104     public:
105       virtual ~OptimizerAlgASync();
106       virtual void finishProxy();//! Called when optimization has succeed.
107
108     protected:
109       virtual void start();//! Update _pool attribute before performing anything.
110       virtual void takeDecision();//! _pool->getCurrentId gives the \b id at the origin of this call.
111                                   //! Perform the job of analysing to know what new jobs to do (_pool->pushInSample)
112                                   //! or in case of convergence _pool->destroyAll
113       virtual void run();
114
115       //! _pool->getCurrentId gives the \b id at the origin of this call.
116       //! Perform the job between 2 'condition->wait()' of analysing to know what new jobs to do
117       //! (_pool->pushInSample) or in case of convergence _pool->destroyAll
118       //! WARNING ! 'condition->wait()' must be called before any analyse of Pool.
119       virtual void startToTakeDecision()=0;
120     };
121
122     typedef OptimizerAlgBase *(*OptimizerAlgBaseFactory)(Pool *pool);
123   }
124 }
125
126 #endif