Salome HOME
Copyright update: 2016
[modules/yacs.git] / src / yacsloader / Test / OptimizerAlgASyncExample.cxx
1 // Copyright (C) 2006-2016  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 #include <iostream>
23 //using namespace YACS::ENGINE;
24
25 extern "C"
26 {
27   YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgASyncExample(YACS::ENGINE::Pool * pool);
28 }
29
30 class OptimizerAlgASyncExample : public YACS::ENGINE::OptimizerAlgASync
31 {
32   private:
33     YACS::ENGINE::TypeCode *_tcInt;
34     YACS::ENGINE::TypeCode *_tcDouble;
35   public:
36     OptimizerAlgASyncExample(YACS::ENGINE::Pool *pool);
37     virtual ~OptimizerAlgASyncExample();
38     
39     //! returns typecode of type expected as Input. OwnerShip of returned pointer is held by this.
40     virtual YACS::ENGINE::TypeCode *getTCForIn() const;
41     //! returns typecode of type expected as Output. OwnerShip of returned pointer is held by this.
42     virtual YACS::ENGINE::TypeCode *getTCForOut() const;
43     //! returns typecode of type expected for algo initialization. OwnerShip of returned pointer is held by this.
44     virtual YACS::ENGINE::TypeCode *getTCForAlgoInit() const;
45     //! returns typecode of type expected as algo result. OwnerShip of returned pointer is held by this.
46     virtual YACS::ENGINE::TypeCode *getTCForAlgoResult() const;
47     virtual void initialize(const YACS::ENGINE::Any *input) throw (YACS::Exception);
48     virtual void startToTakeDecision();
49     virtual void finish();//! Called when optimization has succeed.
50     virtual YACS::ENGINE::Any * getAlgoResult();
51 };
52
53 OptimizerAlgASyncExample::OptimizerAlgASyncExample(YACS::ENGINE::Pool *pool)
54   : YACS::ENGINE::OptimizerAlgASync(pool), _tcInt(0), _tcDouble(0)
55 {
56   _tcDouble = new YACS::ENGINE::TypeCode(YACS::ENGINE::Double);
57   _tcInt    = new YACS::ENGINE::TypeCode(YACS::ENGINE::Int);
58 }
59
60 OptimizerAlgASyncExample::~OptimizerAlgASyncExample()
61 {
62   _tcDouble->decrRef();
63   _tcInt->decrRef();
64 }
65
66 //! Return the typecode of the expected input of the internal node
67 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForIn() const
68 {
69   return _tcDouble;
70 }
71
72 //! Return the typecode of the expected output of the internal node
73 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForOut() const
74 {
75   return _tcInt;
76 }
77
78 //! Return the typecode of the expected input of the algorithm (algoInit port)
79 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForAlgoInit() const
80 {
81   return _tcInt;
82 }
83
84 //! Return the typecode of the expected output of the algorithm (algoResult port)
85 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForAlgoResult() const
86 {
87   return _tcInt;
88 }
89
90 //! Optional method to initialize the algorithm.
91 /*!
92  *  For now, the parameter input is always NULL. It might be used in the
93  *  future to initialize an algorithm with custom data.
94  */
95 void OptimizerAlgASyncExample::initialize(const YACS::ENGINE::Any *input)
96   throw (YACS::Exception)
97 {
98   std::cout << "Algo initialize, input = " << input->getIntValue() << std::endl;
99 }
100
101 //! This method is called only once to launch the algorithm.
102 /*!
103  *  It must first fill the pool with samples to evaluate and call
104  *  signalMasterAndWait() to block until a sample has been evaluated. When
105  *  returning from this method, it MUST check for an eventual termination
106  *  request (with the method isTerminationRequested()). If the termination
107  *  is requested, the method must perform any necessary cleanup and return
108  *  as soon as possible. Otherwise it can either add new samples to evaluate
109  *  in the pool, do nothing (wait for more samples), or empty the pool and
110  *  return to finish the evaluation.
111  */
112 void OptimizerAlgASyncExample::startToTakeDecision()
113 {
114   std::cout << "startToTakeDecision" << std::endl;
115   int iter = 0;
116   YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(0.5);
117   _pool->pushInSample(iter, val);
118   
119   signalMasterAndWait();
120   while(!isTerminationRequested())
121   {
122     int currentId = _pool->getCurrentId();
123     double valIn  = _pool->getCurrentInSample()->getDoubleValue();
124     int valOut    = _pool->getCurrentOutSample()->getIntValue();
125     
126     std::cout << "Compute currentId=" << currentId;
127     std::cout << ", valIn=" << valIn;
128     std::cout << ", valOut=" << valOut << std::endl;
129     
130     iter++;
131     if(iter < 3)
132     {
133       YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(valIn + 1);
134       _pool->pushInSample(iter, val);
135     }
136     signalMasterAndWait();
137   }
138 }
139
140 /*!
141  *  Optional method called when the algorithm has finished, successfully or
142  *  not, to perform any necessary clean up.
143  */
144 void OptimizerAlgASyncExample::finish()
145 {
146   std::cout << "Algo finish" << std::endl;
147   _pool->destroyAll();
148 }
149
150 /*!
151  *  Return the value of the algoResult port.
152  */
153 YACS::ENGINE::Any * OptimizerAlgASyncExample::getAlgoResult()
154 {
155   YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(42);
156   return val;
157 }
158
159 //! Factory method to create the algorithm.
160 YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgASyncExample(YACS::ENGINE::Pool *pool)
161 {
162   return new OptimizerAlgASyncExample(pool);
163 }