1 // Copyright (C) 2006-2013 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.
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 #ifndef __OPTIMIZERLOOP_HXX__
21 #define __OPTIMIZERLOOP_HXX__
23 #include "YACSlibEngineExport.hxx"
25 #include "DynParaLoop.hxx"
26 #include "DynLibLoader.hxx"
27 #include "OptimizerAlg.hxx"
28 #include "ElementaryNode.hxx"
36 class FakeNodeForOptimizerLoop : public ElementaryNode
38 friend class OptimizerLoop;
44 FakeNodeForOptimizerLoop(OptimizerLoop *loop, bool normal, std::string message);
45 FakeNodeForOptimizerLoop(const FakeNodeForOptimizerLoop& other);
46 Node *simpleClone(ComposedNode *father, bool editionOnly) const;
47 void exForwardFailed();
48 void exForwardFinished();
53 static const char NAME[];
56 class YACSLIBENGINE_EXPORT OptimizerLoop : public DynParaLoop
58 friend class FakeNodeForOptimizerLoop;
65 AnyInputPort _algoInitPort;
66 ::YACS::BASES::DynLibLoader * _loader;
67 OptimizerAlgBase *_alg;
68 AnyInputPort _retPortForOutPool;
69 std::vector<bool> _initNodeUpdated;
70 bool _convergenceReachedWithOtherCalc;
71 FakeNodeForOptimizerLoop *_nodeForSpecialCases;
72 std::vector<AnyInputPort *> _interceptorsForOutPool;
73 //! outputports interceptors leaving current scope.
74 std::map<InputPort *, std::vector<InputPort *> > _interceptors;
75 AnyOutputPort _algoResultPort;
77 OptimizerLoop(const std::string& name, const std::string& algLibWthOutExt,
78 const std::string& symbolNameToOptimizerAlgBaseInstanceFactory,
79 bool algInitOnFile,bool initAlgo=true, Proc * procForTypes = NULL);
80 OptimizerLoop(const OptimizerLoop& other, ComposedNode *father, bool editionOnly);
82 void init(bool start=true);
84 int getNumberOfInputPorts() const;
85 InputPort *edGetPortForOutPool() { return &_retPortForOutPool; }
86 InputPort *edGetAlgoInitPort() { return &_algoInitPort; }
87 OutputPort *edGetAlgoResultPort() { return &_algoResultPort; }
88 InputPort *getInputPort(const std::string& name) const throw(Exception);
89 std::list<InputPort *> getSetOfInputPort() const;
90 std::list<InputPort *> getLocalInputPorts() const;
91 void selectRunnableTasks(std::vector<Task *>& tasks);
92 void getReadyTasks(std::vector<Task *>& tasks);
93 YACS::Event updateStateOnFinishedEventFrom(Node *node);
94 void checkNoCyclePassingThrough(Node *node) throw(Exception);
95 virtual void accept(Visitor *visitor);
96 virtual std::string getSymbol() const { return _symbol; }
97 virtual std::string getAlgLib() const ;
98 virtual void setAlgorithm(const std::string& alglib,const std::string& symbol,
99 bool checkLinks=true, Proc * procForTypes = NULL);
100 virtual void checkBasicConsistency() const throw(Exception);
101 virtual std::string typeName() {return "YACS__ENGINE__OptimizerLoop";}
102 int getNumberOfOutputPorts() const;
103 std::list<OutputPort *> getSetOfOutputPort() const;
104 std::list<OutputPort *> getLocalOutputPorts() const;
105 OutPort *getOutPort(const std::string& name) const throw(Exception);
106 OutputPort *getOutputPort(const std::string& name) const throw(Exception);
107 YACS::Event finalize();
110 virtual YACS::Event updateStateOnFailedEventFrom(Node *node);
111 void buildDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView);
112 void buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView);
113 void checkControlDependancy(OutPort *start, InPort *end, bool cross,
114 std::map < ComposedNode *, std::list < OutPort * >, SortHierarc >& fw,
115 std::vector<OutPort *>& fwCross,
116 std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
117 LinkInfo& info) const;
118 void checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const;
120 void cleanInterceptors();
121 void launchMaxOfSamples(bool first);
122 bool isFullyLazy() const;
123 bool isFullyBusy(unsigned& branchId) const;
124 void initInterceptors(unsigned nbOfBr);
125 void pushValueOutOfScopeForCase(unsigned branchId);
126 Node *simpleClone(ComposedNode *father, bool editionOnly=true) const;
127 virtual void loadAlgorithm();
130 TypeCode * checkTypeCode(TypeCode * tc, const char * portName);
133 static const int NOT_RUNNING_BRANCH_ID;
134 static const int NOT_INITIALIZED_BRANCH_ID;
135 static const char NAME_OF_ALGO_INIT_PORT[];
136 static const char NAME_OF_OUT_POOL_INPUT[];
137 static const char NAME_OF_ALGO_RESULT_PORT[];