Salome HOME
Merge branch 'V7_dev'
[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 InputPyPort;
41     class SequenceAny;
42   }
43 }
44
45 class YACSEvalYFX;
46 class YACSEvalSession;
47 class YACSEvalObserver;
48 class YACSEvalListOfResources;
49 class ResourcesManager_cpp;
50 class YACSEvalYFXRunOnlyPatternInternalObserver;
51
52 class YACSEvalYFXPattern
53 {
54 public:
55   virtual ~YACSEvalYFXPattern();
56   std::vector< YACSEvalInputPort *> getFreeInputPorts() const;
57   std::vector< YACSEvalOutputPort *> getFreeOutputPorts() const;
58   static YACSEvalYFXPattern *FindPatternFrom(YACSEvalYFX *boss, YACS::ENGINE::Proc *scheme, bool ownScheme);
59   void setParallelizeStatus(bool newVal) { _parallelizeStatus=newVal; }
60   bool getParallelizeStatus() const { return _parallelizeStatus; }
61   bool isAlreadyComputedResources() const;
62   void checkNonAlreadyComputedResources() const;
63   void checkAlreadyComputedResources() const;
64   void checkLocked() const;
65   void checkNonLocked() const;
66   static void CheckNodeIsOK(YACS::ENGINE::ComposedNode *node);
67   void registerObserver(YACSEvalObserver *observer);
68   YACSEvalObserver *getObserver() const { return _observer; }
69   YACSEvalYFX *getBoss() const { return _boss; }
70   YACSEvalListOfResources *getResourcesInternal() const { return _res; }
71   virtual void setOutPortsOfInterestForEvaluation(const std::vector<YACSEvalOutputPort *>& outputs) = 0;
72   virtual void resetOutputsOfInterest() = 0;
73   virtual void generateGraph() = 0;
74   virtual void resetGeneratedGraph() = 0;
75   virtual void assignRandomVarsInputs() = 0;
76   virtual int assignNbOfBranches() = 0;
77   virtual bool isLocked() const = 0;
78   virtual YACSEvalListOfResources *giveResources() = 0;
79   virtual YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const = 0;
80   virtual std::string getErrorDetailsInCaseOfFailure() const = 0;
81   virtual std::string getStatusOfRunStr() const = 0;
82   virtual std::vector<YACSEvalSeqAny *> getResults() const = 0;
83   virtual std::vector<YACSEvalSeqAny *> getResultsInCaseOfFailure(std::vector<unsigned int>& passedIds) const = 0;
84   virtual bool go(bool stopASAP, YACSEvalSession *session) const = 0;
85 public://for _gen
86   const std::vector< YACSEvalInputPort >& getInputs() const { return _inputs; }
87   static YACS::ENGINE::TypeCode *CreateSeqTypeCodeFrom(YACS::ENGINE::Proc *scheme, const std::string& zeType);
88   static YACSEvalSeqAny *BuildValueInPort(YACS::ENGINE::InputPyPort *port);
89 protected:
90   YACSEvalYFXPattern(YACSEvalYFX *boss, YACS::ENGINE::Proc *scheme, bool ownScheme);
91   void setResources(YACSEvalListOfResources *res);
92   void resetResources();
93   ResourcesManager_cpp *getCatalogInAppli() const { return _rm; }
94   static YACSEvalSeqAny *BuildValueFromEngineFrmt(YACS::ENGINE::SequenceAny *data);
95 private:
96   void cleanScheme();
97 private:
98   YACSEvalYFX *_boss;
99   bool _ownScheme;
100   bool _parallelizeStatus;
101   YACS::ENGINE::Proc *_scheme;
102   ResourcesManager_cpp *_rm;
103   YACSEvalListOfResources *_res;
104   mutable YACSEvalObserver *_observer;
105 protected:
106   std::vector< YACSEvalInputPort > _inputs;
107   std::vector< YACSEvalOutputPort > _outputs;
108 public:
109   static const char ST_OK[]; // execution goes to the end without any trouble -> results can be exploited without any problem with getResults and getResultsInCaseOfFailure.
110   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
111   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.
112   static const std::size_t MAX_LGTH_OF_INP_DUMP;
113 };
114
115 class YACSEvalYFXGraphGen;
116
117 class YACSEvalYFXRunOnlyPattern : public YACSEvalYFXPattern
118 {
119 public:
120   YACSEvalYFXRunOnlyPattern(YACSEvalYFX *boss, YACS::ENGINE::Proc *scheme, bool ownScheme, YACS::ENGINE::ComposedNode *runNode);
121   ~YACSEvalYFXRunOnlyPattern();
122   void setOutPortsOfInterestForEvaluation(const std::vector<YACSEvalOutputPort *>& outputsOfInterest);
123   void resetOutputsOfInterest();
124   void generateGraph();
125   void resetGeneratedGraph();
126   int assignNbOfBranches();
127   void assignRandomVarsInputs();
128   bool isLocked() const;
129   YACSEvalListOfResources *giveResources();
130   YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const;
131   std::string getErrorDetailsInCaseOfFailure() const;
132   std::string getStatusOfRunStr() const;
133   std::vector<YACSEvalSeqAny *> getResults() const;
134   std::vector<YACSEvalSeqAny *> getResultsInCaseOfFailure(std::vector<unsigned int>& passedIds) const;
135   bool go(bool stopASAP, YACSEvalSession *session) const;
136   //
137   YACS::ENGINE::ForEachLoop *getUndergroundForEach() const;
138   static bool IsMatching(YACS::ENGINE::Proc *scheme, YACS::ENGINE::ComposedNode *& runNode);
139 private:
140   void emitStart() const;
141   void buildInputPorts();
142   void buildOutputPorts();
143   YACSEvalYFXGraphGen *getGenerator() const;
144 public://for _gen
145   const std::vector<YACSEvalOutputPort *>& getOutputsOfInterest() const { return _outputsOfInterest; }
146   YACS::ENGINE::ComposedNode *getRunNode() const { return _runNode; }
147 private:
148   bool _lockedStatus;
149   YACS::ENGINE::ComposedNode *_runNode;
150   std::vector<YACSEvalOutputPort *> _outputsOfInterest;
151   YACSEvalYFXGraphGen *_gen;
152   YACSEvalYFXRunOnlyPatternInternalObserver *_obs;
153 };
154
155 class YACSEvalYFXGraphGen
156 {
157 protected:
158   YACSEvalYFXGraphGen(YACSEvalYFXRunOnlyPattern *boss);
159   YACSEvalYFXRunOnlyPattern *getBoss() const { return _boss; }
160 public:
161   virtual ~YACSEvalYFXGraphGen();
162   virtual void generateGraph() = 0;
163   virtual bool go(bool stopASAP, YACSEvalSession *session) const = 0;
164   virtual std::vector<YACSEvalSeqAny *> getResults() const = 0;
165   bool isLocked() const;
166   int assignNbOfBranches();
167   void resetGeneratedGraph();
168   YACS::ENGINE::Proc *getUndergroundGeneratedGraph() const { return _generatedGraph; }
169   YACS::ENGINE::ForEachLoop *getUndergroundForEach() const { return _FEInGeneratedGraph; }
170 private:
171   YACSEvalYFXRunOnlyPattern *_boss;
172 protected:
173   YACS::ENGINE::Proc *_generatedGraph;
174   YACS::ENGINE::ForEachLoop *_FEInGeneratedGraph;
175 public:
176   static const char DFT_PROC_NAME[];
177   static const char FIRST_FE_SUBNODE_NAME[];
178   static const char GATHER_NODE_NAME[];
179 };
180
181 class YACSEvalYFXGraphGenInteractive : public YACSEvalYFXGraphGen
182 {
183 public:
184   YACSEvalYFXGraphGenInteractive(YACSEvalYFXRunOnlyPattern *boss):YACSEvalYFXGraphGen(boss) { }
185   void generateGraph();
186   bool go(bool stopASAP, YACSEvalSession *session) const;
187   std::vector<YACSEvalSeqAny *> getResults() const;
188 };
189
190 class YACSEvalYFXGraphGenCluster : public YACSEvalYFXGraphGen
191 {
192 public:
193   YACSEvalYFXGraphGenCluster(YACSEvalYFXRunOnlyPattern *boss):YACSEvalYFXGraphGen(boss) { }
194   void generateGraph();
195   bool go(bool stopASAP, YACSEvalSession *session) const;
196   std::vector<YACSEvalSeqAny *> getResults() const;
197 private:
198   std::string _locSchemaFile;
199   std::string _jobName;
200   mutable int _jobid;
201   mutable std::string _errors;
202   mutable std::vector< std::vector<double> > _res;
203 };
204
205 #endif