1 // Copyright (C) 2006-2014 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
26 using namespace YACS::ENGINE;
28 Any *Pool::ExpData::NOT_USED_NOR_COMPUTED = 0;
30 Any *Pool::ExpData::USED_BUT_NOT_COMPUTED_YET = (Any *) 1;
32 const char Pool::MESSAGEFORUNXSTNGID[]="The id specified not exists. Unable to handle with. Either internal error, or invalid use of Pool from Optimizer Algorithm";
34 Pool::ExpData::ExpData(Any *inValue, unsigned char priority):_in(inValue),_out(NOT_USED_NOR_COMPUTED),_priority(priority)
40 Pool::ExpData::ExpData(const ExpData& other):_in(other._in),_out(other._out),_priority(other._priority)
46 Pool::ExpData::~ExpData()
50 if(_out!=USED_BUT_NOT_COMPUTED_YET && _out!=NOT_USED_NOR_COMPUTED)
55 * When used _out is assumed to be equal to 'USED_BUT_NOT_COMPUTED_YET' before call.
57 void Pool::ExpData::setOutValue(Any *outValue)
59 if(_out!=USED_BUT_NOT_COMPUTED_YET && _out!=NOT_USED_NOR_COMPUTED)
60 _out->decrRef();//should absolutely never happend.
66 * When called _out is assumed to be equal to 'NOT_USED_NOR_COMPUTED' before call.
68 void Pool::ExpData::markItAsInUse()
70 _out=USED_BUT_NOT_COMPUTED_YET;
73 bool Pool::ExpData::isLaunchable() const
75 return _out==NOT_USED_NOR_COMPUTED;
78 int Pool::getCurrentId() const
81 throw YACS::Exception("no current case set in pool");
83 return _currentCase->first;
85 Any *Pool::getCurrentInSample() const
88 throw YACS::Exception("no current case set in pool");
90 return (*_currentCase).second.inValue();
93 Any *Pool::getCurrentOutSample() const
96 throw YACS::Exception("no current case set in pool");
98 return (*_currentCase).second.outValue();
101 Any *Pool::getOutSample(int id)
104 throw YACS::Exception("no current case set in pool");
106 std::list< std::pair<int, ExpData> >::iterator iter;
107 for(iter=_container.begin();iter!=_container.end();iter++)
108 if((*iter).first==id)
109 return (*iter).second.outValue();
110 if(iter==_container.end())
111 throw YACS::Exception("no current case set in pool");
115 //! Push a sample. \b WARNING inSample ownership is released to current Pool instance (this) !
116 void Pool::pushInSample(int id, Any *inSample, unsigned char priority)
118 std::pair<int, ExpData> eltToAdd(id,Pool::ExpData(inSample,priority));
119 _container.push_back(eltToAdd);
123 void Pool::destroyAll()
128 void Pool::destroyCurrentCase()
130 if(!_container.empty())
131 _container.erase(_currentCase);
136 * This method is typically called by OptimizerNode to check the consistency, that is to say that optimizer algorithm has not
140 void Pool::checkConsistency() throw(YACS::Exception)
142 // First check unicity of ids.
144 std::list< std::pair<int, ExpData> >::iterator iter;
145 for(iter=_container.begin();iter!=_container.end();iter++)
147 std::pair< std::set<int>::iterator, bool > verdict=ids.insert((*iter).first);
150 std::ostringstream what;
151 what << "Id with value : " << (*iter).first << " appears several times.";
152 throw Exception(what.str());
158 * \throw See the \b throw case of pushOutSampleAt method.
160 void Pool::setCurrentId(int id) throw(YACS::Exception)
162 std::list< std::pair<int, ExpData> >::iterator iter;
163 for(iter=_container.begin();iter!=_container.end();iter++)
164 if((*iter).first==id)
169 if(iter==_container.end())
170 throw Exception(MESSAGEFORUNXSTNGID);
175 * Push a result of case discriminated by \b id. It also sets the \b _currentCase pointer on the case discriminated by \b id.
176 * So after this call, the call to setCurrentId with the same \b id is useless.
177 * \throw When case id is not found in 'this'. This is particulary true, if not an internal error, when optimizer algorithm
178 * has destroyed a case id different from its id.
181 void Pool::putOutSampleAt(int id, Any *outValue) throw(YACS::Exception)
183 std::list< std::pair<int, ExpData> >::iterator iter;
184 for(iter=_container.begin();iter!=_container.end();iter++)
185 if((*iter).first==id)
188 (*iter).second.setOutValue(outValue);
191 if(iter==_container.end())
192 throw Exception(MESSAGEFORUNXSTNGID);
197 * This method is typically called by OptimizerNode instance owner of 'this' that wants to launch an another job on one branch.
198 * \return : In case there are more jobs to do 2 parameters are returned.
199 * - \b id to locate the computation to do.
200 * - \b priority attached.
202 * In case no more jobs are required id and priority stay unchanged and the returned value is equal to 0.
205 Any *Pool::getNextSampleWithHighestPriority(int& id, unsigned char& priority) const
207 unsigned char myPriority=0;
208 std::list< std::pair<int, ExpData> >::const_iterator iter,ptToSelected;
209 ptToSelected=_container.end();
210 for(iter=_container.begin();iter!=_container.end();iter++)
212 if((*iter).second.isLaunchable())
213 if((*iter).second.getPriority()>myPriority || ptToSelected==_container.end())
216 myPriority=(*iter).second.getPriority();
219 //Search performed. No performing output writings if needed.
220 if(ptToSelected==_container.end())
223 id=(*ptToSelected).first;
224 return (*ptToSelected).second.inValue();
229 * Typically called after 'this->destroyCurrentCase' 'this->checkConsistency' and 'this->getNextSampleWithHighestPriority' have been called.
230 * At this point the case with id \b id is marked as in use in order to avoid to be used by an another branch of OptimizerNode.
233 void Pool::markIdAsInUse(int id)
235 std::list< std::pair<int, ExpData> >::iterator iter;
236 for(iter=_container.begin();iter!=_container.end();iter++)
237 if((*iter).first==id)
239 (*iter).second.markItAsInUse();
245 * Typically called after takeDecision of OptimizerAlg as been performed. If true is returned, that is to say that convergence has been reached.
247 bool Pool::empty() const
249 return _container.empty();