Salome HOME
Merge branch V7_3_1_BR
[modules/yacs.git] / src / yacsloader / Test / OptimizerAlgASyncExample.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 "OptimizerAlg.hxx"
21
22 using namespace YACS::ENGINE;
23
24 extern "C"
25 {
26   OptimizerAlgBase * createOptimizerAlgASyncExample(Pool * pool);
27 }
28
29 class OptimizerAlgASyncExample : public OptimizerAlgASync
30   {
31   private:
32     TypeCode * _tcIn;
33     TypeCode * _tcOut;
34   public:
35     OptimizerAlgASyncExample(Pool * pool);
36     virtual ~OptimizerAlgASyncExample();
37     TypeCode * getTCForIn() const;
38     TypeCode * getTCForOut() const;
39     void startToTakeDecision();
40   };
41
42 OptimizerAlgASyncExample::OptimizerAlgASyncExample(Pool * pool) : OptimizerAlgASync(pool),
43                                                                   _tcIn(0), _tcOut(0)
44 {
45   _tcIn = new TypeCode(Double);
46   _tcOut = new TypeCode(Int);
47 }
48
49 OptimizerAlgASyncExample::~OptimizerAlgASyncExample()
50 {
51   _tcIn->decrRef();
52   _tcOut->decrRef();
53 }
54
55 //! Return the typecode of the expected input type
56 TypeCode *OptimizerAlgASyncExample::getTCForIn() const
57 {
58   return _tcIn;
59 }
60
61 //! Return the typecode of the expected output type
62 TypeCode *OptimizerAlgASyncExample::getTCForOut() const
63 {
64   return _tcOut;
65 }
66
67 //! This method is called only once to launch the algorithm.
68 /*!
69  *  It must first fill the pool with samples to evaluate and call signalMasterAndWait()
70  *  to block until a sample has been evaluated. When returning from this method, it MUST
71  *  check for an eventual termination request (with the method isTerminationRequested()).
72  *  If the termination is requested, the method must perform any necessary cleanup and
73  *  return as soon as possible. Otherwise it can either add new samples to evaluate in
74  *  the pool, do nothing (wait for more samples), or empty the pool and return to finish
75  *  the evaluation.
76  */
77 void OptimizerAlgASyncExample::startToTakeDecision()
78 {
79   double val = 1.2;
80   for (int i=0 ; i<5 ; i++) {
81     // push a sample in the input of the slave node
82     _pool->pushInSample(i, AtomAny::New(val));
83     // wait until next sample is ready
84     signalMasterAndWait();
85     // check error notification
86     if (isTerminationRequested()) {
87       _pool->destroyAll();
88       return;
89     }
90
91     // get a sample from the output of the slave node
92     Any * v = _pool->getCurrentOutSample();
93     val += v->getIntValue();
94   }
95
96   // in the end destroy the pool content
97   _pool->destroyAll();
98 }
99
100 //! Factory method to create the algorithm.
101 OptimizerAlgBase * createOptimizerAlgASyncExample(Pool * pool)
102 {
103   return new OptimizerAlgASyncExample(pool);
104 }