1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "OptimizerAlg.hxx"
23 //using namespace YACS::ENGINE;
27 YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgASyncExample(YACS::ENGINE::Pool * pool);
30 class OptimizerAlgASyncExample : public YACS::ENGINE::OptimizerAlgASync
33 YACS::ENGINE::TypeCode *_tcInt;
34 YACS::ENGINE::TypeCode *_tcDouble;
36 OptimizerAlgASyncExample(YACS::ENGINE::Pool *pool);
37 virtual ~OptimizerAlgASyncExample();
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();
53 OptimizerAlgASyncExample::OptimizerAlgASyncExample(YACS::ENGINE::Pool *pool)
54 : YACS::ENGINE::OptimizerAlgASync(pool), _tcInt(0), _tcDouble(0)
56 _tcDouble = new YACS::ENGINE::TypeCode(YACS::ENGINE::Double);
57 _tcInt = new YACS::ENGINE::TypeCode(YACS::ENGINE::Int);
60 OptimizerAlgASyncExample::~OptimizerAlgASyncExample()
66 //! Return the typecode of the expected input of the internal node
67 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForIn() const
72 //! Return the typecode of the expected output of the internal node
73 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForOut() const
78 //! Return the typecode of the expected input of the algorithm (algoInit port)
79 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForAlgoInit() const
84 //! Return the typecode of the expected output of the algorithm (algoResult port)
85 YACS::ENGINE::TypeCode * OptimizerAlgASyncExample::getTCForAlgoResult() const
90 //! Optional method to initialize the algorithm.
92 * For now, the parameter input is always NULL. It might be used in the
93 * future to initialize an algorithm with custom data.
95 void OptimizerAlgASyncExample::initialize(const YACS::ENGINE::Any *input)
96 throw (YACS::Exception)
98 std::cout << "Algo initialize, input = " << input->getIntValue() << std::endl;
101 //! This method is called only once to launch the algorithm.
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.
112 void OptimizerAlgASyncExample::startToTakeDecision()
114 std::cout << "startToTakeDecision" << std::endl;
116 YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(0.5);
117 _pool->pushInSample(iter, val);
119 signalMasterAndWait();
120 while(!isTerminationRequested())
122 int currentId = _pool->getCurrentId();
123 double valIn = _pool->getCurrentInSample()->getDoubleValue();
124 int valOut = _pool->getCurrentOutSample()->getIntValue();
126 std::cout << "Compute currentId=" << currentId;
127 std::cout << ", valIn=" << valIn;
128 std::cout << ", valOut=" << valOut << std::endl;
133 YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(valIn + 1);
134 _pool->pushInSample(iter, val);
136 signalMasterAndWait();
141 * Optional method called when the algorithm has finished, successfully or
142 * not, to perform any necessary clean up.
144 void OptimizerAlgASyncExample::finish()
146 std::cout << "Algo finish" << std::endl;
151 * Return the value of the algoResult port.
153 YACS::ENGINE::Any * OptimizerAlgASyncExample::getAlgoResult()
155 YACS::ENGINE::Any *val=YACS::ENGINE::AtomAny::New(42);
159 //! Factory method to create the algorithm.
160 YACS::ENGINE::OptimizerAlgBase * createOptimizerAlgASyncExample(YACS::ENGINE::Pool *pool)
162 return new OptimizerAlgASyncExample(pool);