Salome HOME
Merge branch 'master' into tlr/playground_optimization
[modules/yacs.git] / src / evalyfx / YACSEvalYFXPattern.hxx
1 // Copyright (C) 2012-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 // Author : Anthony Geay (EDF R&D)
20
21 #ifndef __YACSEVALYFXPATTERN_HXX__
22 #define __YACSEVALYFXPATTERN_HXX__
23
24 #include "YACSEvalPort.hxx"
25
26 #include <vector>
27
28 #ifdef WIN32
29 #define NOMINMAX
30 #endif
31
32 namespace YACS
33 {
34   namespace ENGINE
35   {
36     class Proc;
37     class TypeCode;
38     class ForEachLoop;
39     class ComposedNode;
40     class InlineNode;
41     class InputPyPort;
42     class SequenceAny;
43   }
44 }
45
46 class YACSEvalYFX;
47 class YACSEvalSession;
48 class YACSEvalObserver;
49 class YACSEvalExecParams;
50 class YACSEvalListOfResources;
51 class ResourcesManager_cpp;
52 class YACSEvalYFXRunOnlyPatternInternalObserver;
53
54 class YACSEvalYFXPattern
55 {
56 public:
57   virtual ~YACSEvalYFXPattern();
58   std::vector< YACSEvalInputPort *> getFreeInputPorts() const;
59   std::vector< YACSEvalOutputPort *> getFreeOutputPorts() const;
60   static YACSEvalYFXPattern *FindPatternFrom(YACSEvalYFX *boss, YACS::ENGINE::Proc *scheme, bool ownScheme);
61   void setParallelizeStatus(bool newVal) { _parallelizeStatus=newVal; }
62   bool getParallelizeStatus() const { return _parallelizeStatus; }
63   bool isAlreadyComputedResources() const;
64   void checkNonAlreadyComputedResources() const;
65   void checkAlreadyComputedResources() const;
66   void checkLocked() const;
67   void checkNonLocked() const;
68   static void CheckNodeIsOK(YACS::ENGINE::ComposedNode *node);
69   void registerObserver(YACSEvalObserver *observer);
70   YACSEvalObserver *getObserver() const { return _observer; }
71   YACSEvalYFX *getBoss() const { return _boss; }
72   YACSEvalListOfResources *getResourcesInternal() const { return _res; }
73   virtual void setOutPortsOfInterestForEvaluation(const std::vector<YACSEvalOutputPort *>& outputs) = 0;
74   virtual void resetOutputsOfInterest() = 0;
75   virtual void generateGraph() = 0;
76   virtual void resetGeneratedGraph() = 0;
77   virtual void assignRandomVarsInputs() = 0;
78   virtual int assignNbOfBranches() = 0;
79   virtual bool isLocked() const = 0;
80   virtual YACSEvalListOfResources *giveResources() = 0;
81   virtual YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const = 0;
82   virtual std::string getErrorDetailsInCaseOfFailure() const = 0;
83   virtual std::string getStatusOfRunStr() const = 0;
84   virtual std::vector<YACSEvalSeqAny *> getResults() const = 0;
85   virtual std::vector<YACSEvalSeqAny *> getResultsInCaseOfFailure(std::vector<unsigned int>& passedIds) const = 0;
86   virtual bool go(const YACSEvalExecParams& params, YACSEvalSession *session) const = 0;
87 public://for _gen
88   const std::vector< YACSEvalInputPort >& getInputs() const { return _inputs; }
89   static YACS::ENGINE::TypeCode *CreateSeqTypeCodeFrom(YACS::ENGINE::Proc *scheme, const std::string& zeType);
90   static YACSEvalSeqAny *BuildValueInPort(YACS::ENGINE::InputPyPort *port);
91 protected:
92   YACSEvalYFXPattern(YACSEvalYFX *boss, YACS::ENGINE::Proc *scheme, bool ownScheme);
93   void setResources(YACSEvalListOfResources *res);
94   void resetResources();
95   ResourcesManager_cpp *getCatalogInAppli() const { return _rm; }
96   static YACSEvalSeqAny *BuildValueFromEngineFrmt(YACS::ENGINE::SequenceAny *data);
97 private:
98   void cleanScheme();
99 private:
100   YACSEvalYFX *_boss;
101   bool _ownScheme;
102   bool _parallelizeStatus;
103   YACS::ENGINE::Proc *_scheme;
104   ResourcesManager_cpp *_rm;
105   YACSEvalListOfResources *_res;
106   mutable YACSEvalObserver *_observer;
107 protected:
108   std::vector< YACSEvalInputPort > _inputs;
109   std::vector< YACSEvalOutputPort > _outputs;
110 public:
111   static const char ST_OK[]; // execution goes to the end without any trouble -> results can be exploited without any problem with getResults and getResultsInCaseOfFailure.
112   static const char ST_FAILED[]; // execution has reached some failed evaluation (normal errors due to incapacity of one node to evaluate) -> results can be exploited without any problem with getResultsInCaseOfFailure
113   static const char ST_ERROR[]; // execution has encountered hard errors (SIGSEGV in a server, internal error in YACS) -> results can be exploited with getResultsInCaseOfFailure but you can't make hypothesis for other ids not in passedIds.
114   static const std::size_t MAX_LGTH_OF_INP_DUMP;
115 };
116
117 class YACSEvalYFXGraphGen;
118
119 class YACSEvalYFXRunOnlyPattern : public YACSEvalYFXPattern
120 {
121 public:
122   YACSEvalYFXRunOnlyPattern(YACSEvalYFX *boss, YACS::ENGINE::Proc *scheme, bool ownScheme, YACS::ENGINE::ComposedNode *runNode);
123   ~YACSEvalYFXRunOnlyPattern();
124   void setOutPortsOfInterestForEvaluation(const std::vector<YACSEvalOutputPort *>& outputsOfInterest);
125   void resetOutputsOfInterest();
126   void generateGraph();
127   void resetGeneratedGraph();
128   int assignNbOfBranches();
129   void assignRandomVarsInputs();
130   bool isLocked() const;
131   YACSEvalListOfResources *giveResources();
132   YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const;
133   std::string getErrorDetailsInCaseOfFailure() const;
134   std::string getStatusOfRunStr() const;
135   std::vector<YACSEvalSeqAny *> getResults() const;
136   std::vector<YACSEvalSeqAny *> getResultsInCaseOfFailure(std::vector<unsigned int>& passedIds) const;
137   bool go(const YACSEvalExecParams& params, YACSEvalSession *session) const;
138   //
139   YACS::ENGINE::ForEachLoop *getUndergroundForEach() const;
140   static bool IsMatching(YACS::ENGINE::Proc *scheme, YACS::ENGINE::ComposedNode *& runNode);
141 private:
142   void emitStart() const;
143   void buildInputPorts();
144   void buildOutputPorts();
145   YACSEvalYFXGraphGen *getGenerator() const;
146 public://for _gen
147   const std::vector<YACSEvalOutputPort *>& getOutputsOfInterest() const { return _outputsOfInterest; }
148   YACS::ENGINE::ComposedNode *getRunNode() const { return _runNode; }
149 private:
150   bool _lockedStatus;
151   YACS::ENGINE::ComposedNode *_runNode;
152   std::vector<YACSEvalOutputPort *> _outputsOfInterest;
153   YACSEvalYFXGraphGen *_gen;
154   YACSEvalYFXRunOnlyPatternInternalObserver *_obs;
155 };
156
157 class YACSEvalYFXGraphGen
158 {
159 public:
160   class CustomPatcher
161   {
162   public:
163     virtual ~CustomPatcher() { }
164     virtual void addOutputVar(const std::string& name) = 0;
165     virtual void assignOutput(YACS::ENGINE::InlineNode *node) = 0;
166   };
167 protected:
168   YACSEvalYFXGraphGen(YACSEvalYFXRunOnlyPattern *boss);
169   YACSEvalYFXRunOnlyPattern *getBoss() const { return _boss; }
170 public:
171   virtual ~YACSEvalYFXGraphGen();
172   virtual void generateGraph() = 0;
173   virtual bool go(const YACSEvalExecParams& params, YACSEvalSession *session) const = 0;
174   virtual std::vector<YACSEvalSeqAny *> getResults() const = 0;
175   virtual std::string getErrors()const = 0;
176   bool isLocked() const;
177   int assignNbOfBranches();
178   void resetGeneratedGraph();
179   YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const { return _generatedGraph; }
180   YACS::ENGINE::ForEachLoop *getUndergroundForEach() const { return _FEInGeneratedGraph; }
181 protected:
182   void generateGraphCommon(CustomPatcher& patcher);
183 private:
184   YACSEvalYFXRunOnlyPattern *_boss;
185 protected:
186   YACS::ENGINE::Proc *_generatedGraph;
187   YACS::ENGINE::ForEachLoop *_FEInGeneratedGraph;
188 public:
189   static const char DFT_PROC_NAME[];
190   static const char FIRST_FE_SUBNODE_NAME[];
191   static const char GATHER_NODE_NAME[];
192   static const char HIDDEN_INDEX_VAR[];
193 };
194
195 class YACSEvalYFXGraphGenInteractive : public YACSEvalYFXGraphGen
196 {
197 public:
198   YACSEvalYFXGraphGenInteractive(YACSEvalYFXRunOnlyPattern *boss):YACSEvalYFXGraphGen(boss) { }
199   void generateGraph();
200   bool go(const YACSEvalExecParams& params, YACSEvalSession *session) const;
201   std::vector<YACSEvalSeqAny *> getResults() const;
202   virtual std::string getErrors()const;
203 };
204
205 class YACSEvalYFXGraphGenCluster : public YACSEvalYFXGraphGen
206 {
207 public:
208   YACSEvalYFXGraphGenCluster(YACSEvalYFXRunOnlyPattern *boss):YACSEvalYFXGraphGen(boss),_jobid(0) { }
209   void generateGraph();
210   bool go(const YACSEvalExecParams& params, YACSEvalSession *session) const;
211   std::vector<YACSEvalSeqAny *> getResults() const;
212   virtual std::string getErrors()const;
213 private:
214   std::string _locSchemaFile;
215   std::string _jobName;
216   mutable int _jobid;
217   mutable std::string _errors;
218   mutable std::vector< std::vector<double> > _res;
219 };
220
221 #endif