Salome HOME
db7134fae5f807cf15daf7dc17316fcddb6d5d76
[modules/yacs.git] / src / yacsloader / Test / OptimizerAlgSyncExample.cxx
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 #include <cmath>
21 #include <iostream>
22
23 #include "OptimizerAlg.hxx"
24
25 using namespace YACS::ENGINE;
26
27 extern "C"
28 {
29   OptimizerAlgBase * createOptimizerAlgSyncExample(Pool * pool);
30 }
31
32 class OptimizerAlgSyncExample : public OptimizerAlgSync
33   {
34   private:
35     int _idTest;
36     TypeCode *_tcIn;
37     TypeCode *_tcOut;
38   public:
39     OptimizerAlgSyncExample(Pool *pool);
40     virtual ~OptimizerAlgSyncExample();
41     TypeCode *getTCForIn() const;
42     TypeCode *getTCForOut() const;
43     void start();
44     void takeDecision();
45     void initialize(const Any *input) throw(YACS::Exception);
46     void finish();
47   };
48
49 OptimizerAlgSyncExample::OptimizerAlgSyncExample(Pool *pool) : OptimizerAlgSync(pool),
50                                                                _tcIn(0), _tcOut(0),
51                                                                _idTest(0)
52 {
53   _tcIn=new TypeCode(Double);
54   _tcOut=new TypeCode(Int);
55 }
56
57 OptimizerAlgSyncExample::~OptimizerAlgSyncExample()
58 {
59   std::cout << "Destroying OptimizerAlgSyncExample" << std::endl;
60   _tcIn->decrRef();
61   _tcOut->decrRef();
62   std::cout << "Destroyed OptimizerAlgSyncExample" << std::endl;
63 }
64
65 //! Return the typecode of the expected input type
66 TypeCode * OptimizerAlgSyncExample::getTCForIn() const
67 {
68   return _tcIn;
69 }
70
71 //! Return the typecode of the expected output type
72 TypeCode * OptimizerAlgSyncExample::getTCForOut() const
73 {
74   return _tcOut;
75 }
76
77 //! Start to fill the pool with samples to evaluate
78 void OptimizerAlgSyncExample::start()
79 {
80   _idTest=0;
81   Any *val=AtomAny::New(1.2);
82   _pool->pushInSample(4,val);
83   val=AtomAny::New(3.4);
84   _pool->pushInSample(9,val);
85 }
86
87 //! This method is called each time a sample has been evaluated.
88 /*!
89  *  It can either add new samples to evaluate in the pool, do nothing (wait for more
90  *  samples), or empty the pool to finish the evaluation.
91  */
92 void OptimizerAlgSyncExample::takeDecision()
93 {
94   if(_idTest==1)
95     {
96       Any *val=AtomAny::New(5.6);
97       _pool->pushInSample(16,val);
98       val=AtomAny::New(7.8);
99       _pool->pushInSample(25,val);
100       val=AtomAny::New(9. );
101       _pool->pushInSample(36,val);
102       val=AtomAny::New(12.3);
103       _pool->pushInSample(49,val);
104     }
105   else if(_idTest==4)
106     {
107       Any *val=AtomAny::New(45.6);
108       _pool->pushInSample(64,val);
109       val=AtomAny::New(78.9);
110       _pool->pushInSample(81,val);
111     }
112   else
113     {
114       Any *tmp= _pool->getCurrentInSample();
115       if(fabs(tmp->getDoubleValue()-45.6)<1.e-12)
116         _pool->destroyAll();
117     }
118   _idTest++;
119 }
120
121 //! Optional method to initialize the algorithm.
122 /*!
123  *  For now, the parameter input is always NULL. It might be used in the future to
124  *  initialize an algorithm with custom data.
125  */
126 void OptimizerAlgSyncExample::initialize(const Any *input) throw (YACS::Exception)
127 {
128 }
129
130 /*!
131  *  Optional method called when the algorithm has finished, successfully or not, to
132  *  perform any necessary clean up.
133  */
134 void OptimizerAlgSyncExample::finish()
135 {
136 }
137
138 //! Factory method to create the algorithm.
139 OptimizerAlgBase * createOptimizerAlgSyncExample(Pool *pool)
140 {
141   return new OptimizerAlgSyncExample(pool);
142 }