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