Salome HOME
Remove useless dependency to py2cpp.
[tools/adao_interface.git] / AdaoModelKeyVal.hxx
1 // Copyright (C) 2019 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.
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, anthony.geay@edf.fr, EDF R&D
20
21 #pragma once
22
23 #include "PyObjectRAII.hxx"
24
25 #include <string>
26 #include <memory>
27 #include <vector>
28
29 namespace AdaoModel
30 {
31   enum class Type
32   {
33       Double,
34       UnsignedInt,
35       Bool,
36       String,
37       PyObj,
38       EnumAlgo,
39       ListStrings,
40       None,
41       Child
42   };
43
44   enum class EnumAlgo
45   {
46       ThreeDVar,
47       Blue,
48       NonLinearLeastSquares,
49       LinearLeastSquares
50   };
51
52   class GenericKeyVal;
53   class MainModel;
54   
55   class TopEntry
56   {
57   public:
58     std::string getParamForSet(const GenericKeyVal& entry) const;
59   };
60   
61   class PythonLeafVisitor;
62   class RecursiveVisitor;
63
64   class GenericKeyVal
65   {
66   public:
67     virtual bool isNeeded() const = 0;
68     virtual Type getType() const = 0;
69     virtual std::string pyStr() const = 0;
70     virtual void visitPython(MainModel *godFather, PythonLeafVisitor *visitor) { }
71     virtual void visitAll(MainModel *godFather, RecursiveVisitor *visitor);
72     virtual ~GenericKeyVal() { }
73     std::string pyStrKeyVal() const;
74     std::string getKey() const { return _key; }
75   protected:
76     GenericKeyVal(const std::string& key):_key(key) { }
77   private:
78     std::string _key;
79   };
80
81   class NeededGenericKeyVal : public GenericKeyVal
82   {
83   protected:
84     NeededGenericKeyVal(const std::string& key):GenericKeyVal(key) { }
85   public:
86     bool isNeeded() const override { return true; }
87   };
88
89   class NotNeededGenericKeyVal : public GenericKeyVal
90   {
91   public:
92     bool isNeeded() const override { return false; }
93   };
94
95   class DoubleKeyVal : public NeededGenericKeyVal
96   {
97   public:
98     DoubleKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
99     Type getType() const override { return Type::Double; }
100     void setVal(double val) { _val=val; }
101     double getVal() const { return _val; }
102     std::string pyStr() const override;
103   private:
104     double _val = 0.;
105   };
106
107   class BoolKeyVal : public NeededGenericKeyVal
108   {
109   public:
110     BoolKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
111     Type getType() const override { return Type::Bool; }
112     virtual void setVal(bool val) { _val=val; }
113     bool getVal() const { return _val; }
114     std::string pyStr() const override;
115   private:
116     bool _val = false;
117   };
118
119   class CostDecrementTolerance : public DoubleKeyVal
120   {
121   public:
122     CostDecrementTolerance():DoubleKeyVal(KEY) { setVal(1e-7); }
123   public:
124     static const char KEY[];
125   };
126   
127   class StringKeyVal : public NeededGenericKeyVal
128   {
129   public:
130     StringKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
131     Type getType() const override { return Type::String; }
132     std::string pyStr() const override;
133     void setVal(const std::string& val) { _val = val; }
134     std::string getVal() const { return _val; }
135   private:
136     std::string _val;
137   };
138
139   class NoneKeyVal : public NeededGenericKeyVal
140   {
141   public:
142     NoneKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
143     Type getType() const override { return Type::None; }
144     std::string pyStr() const override;
145   };
146
147   class StringObserver : public NoneKeyVal
148   {
149   public:
150     StringObserver():NoneKeyVal(KEY) { }
151   public:
152     static const char KEY[];
153   };
154
155   class InfoObserver : public NoneKeyVal
156   {
157   public:
158     InfoObserver():NoneKeyVal(KEY) { }
159   public:
160     static const char KEY[];
161   };
162
163   class ListStringsKeyVal : public NeededGenericKeyVal
164   {
165   protected:
166     ListStringsKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
167   public:
168     Type getType() const override { return Type::ListStrings; }
169     std::string pyStr() const override;
170   protected:
171     std::vector< std::string > _val;
172   };
173
174   class StoreSupplKeyVal : public ListStringsKeyVal
175   {
176   public:
177     StoreSupplKeyVal();
178   public:
179     static const char *DFTL[];
180     static const char KEY[];
181   };
182
183   class DictKeyVal;
184   
185   class EnumAlgoKeyVal : public NeededGenericKeyVal
186   {
187   public:
188     EnumAlgoKeyVal():NeededGenericKeyVal(KEY),_enum(EnumAlgo::ThreeDVar) { }
189     Type getType() const override { return Type::EnumAlgo; }
190     std::shared_ptr<DictKeyVal> generateDftParameters() const;
191     std::string getRepresentation() const;
192     std::string pyStr() const override;
193     void setVal(EnumAlgo newAlgo) { _enum = newAlgo; }
194     EnumAlgo getVal() const { return _enum; }
195   private:
196     std::shared_ptr<DictKeyVal> templateForBlue() const;
197     std::shared_ptr<DictKeyVal> templateForOthers() const;
198   private:
199     static const char KEY[];
200     EnumAlgo _enum;
201   };
202   
203   class DictKeyVal : public NeededGenericKeyVal
204   {
205   public:
206     DictKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
207     void pushBack(std::shared_ptr<GenericKeyVal> elt) { _pairs.push_back(elt); }
208     Type getType() const override { return Type::Child; }
209     std::string pyStr() const override;
210     void visitPython(MainModel *godFather, PythonLeafVisitor *visitor) override;
211     void visitAll(MainModel *godFather, RecursiveVisitor *visitor) override;
212   protected:
213     std::vector< std::shared_ptr<GenericKeyVal> > _pairs;
214   };
215
216   class ParametersOfAlgorithmParameters : public DictKeyVal
217   {
218   public:
219     ParametersOfAlgorithmParameters():DictKeyVal(KEY) { }
220   public:
221     static const char KEY[];
222   };
223
224   class PyObjKeyVal : public NeededGenericKeyVal
225   {
226   public:
227     PyObjKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
228     Type getType() const override { return Type::PyObj; }
229     void setVal(PyObject *obj) { _val = PyObjectRAII::FromBorrowed(obj); }
230     // borrowed ref
231     PyObject *getVal() const { return _val; }
232     std::string pyStr() const;
233     void setVarName(const std::string& vn) { _var_name = vn; }
234     void visitPython(MainModel *godFather, PythonLeafVisitor *visitor) override;
235   private:
236     PyObjectRAII _val;
237     std::string _var_name;
238   };
239
240   class Bounds : public PyObjKeyVal
241   {
242   public:
243     Bounds():PyObjKeyVal(KEY) { }
244   public:
245     static const char KEY[];
246   };
247
248   class UnsignedIntKeyVal : public NeededGenericKeyVal
249   {
250   public:
251     UnsignedIntKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
252     void setVal(unsigned int val) { _val=val; }
253     unsigned int getVal() const { return _val; }
254     Type getType() const override { return Type::UnsignedInt; }
255     std::string pyStr() const;
256   private:
257     unsigned int _val = -1;
258   };
259   
260   class MaximumNumberOfSteps : public UnsignedIntKeyVal
261   {
262   public:
263     MaximumNumberOfSteps():UnsignedIntKeyVal(KEY) { setVal(100); }
264   public:
265     static const char KEY[];
266   };
267
268   class VectorBackground : public PyObjKeyVal
269   {
270   public:
271     VectorBackground():PyObjKeyVal(KEY) { }
272   public:
273     static const char KEY[];
274   };
275
276   class StoreBackground : public BoolKeyVal
277   {
278   public:
279     StoreBackground():BoolKeyVal(KEY) { }
280   public:
281     static const char KEY[];
282   };
283
284   class MatrixBackgroundError : public PyObjKeyVal
285   {
286   public:
287     MatrixBackgroundError():PyObjKeyVal(KEY) { }
288   public:
289     static const char KEY[];
290   };
291
292   class ScalarSparseMatrixError : public DoubleKeyVal
293   {
294   public:
295     ScalarSparseMatrixError(double val):DoubleKeyVal(KEY) { setVal(val); }
296   public:
297     static const char KEY[];
298   };
299
300   class DiagonalSparseMatrixError : public PyObjKeyVal
301   {
302   public:
303     DiagonalSparseMatrixError():PyObjKeyVal(KEY) { }
304   public:
305     static const char KEY[];
306   };
307
308   class OneFunction : public PyObjKeyVal
309   {
310   public:
311     OneFunction():PyObjKeyVal(KEY) { }
312   public:
313     static const char KEY[];
314   };
315
316   class DifferentialIncrement : public DoubleKeyVal
317   {
318   public:
319     DifferentialIncrement():DoubleKeyVal(KEY) { setVal(0.0001); }
320   public:
321     static const char KEY[];
322   };
323
324   class ObservationOperatorParameters : public DictKeyVal
325   {
326   public:
327     ObservationOperatorParameters();
328   public:
329     static const char KEY[];
330   };
331
332   class CenteredFiniteDifference : public BoolKeyVal
333   {
334   public:
335     CenteredFiniteDifference():BoolKeyVal(KEY) { setVal(false); }
336   public:
337     static const char KEY[];
338   };
339
340   class InputFunctionAsMulti : public BoolKeyVal
341   {
342   public:
343     InputFunctionAsMulti():BoolKeyVal(KEY) { BoolKeyVal::setVal(true); }
344     void setVal(bool val) override;
345   public:
346     static const char KEY[];
347   };
348
349   class VariableKV : public StringKeyVal
350   {
351   public:
352     VariableKV():StringKeyVal(KEY) { setVal("CurrentState"); }
353   public:
354     static const char KEY[];
355   };
356
357   class TemplateKV : public StringKeyVal
358   {
359   public:
360     TemplateKV():StringKeyVal(KEY) { setVal("CurrentState"); }
361   public:
362     static const char KEY[];
363   };
364
365   //////////////
366
367   class AlgorithmParameters : public DictKeyVal, public TopEntry
368   {
369   public:
370     AlgorithmParameters();
371   public:
372     static const char KEY[];
373   };
374
375   class Background : public DictKeyVal, public TopEntry
376   {
377   public:
378     Background();
379   public:
380     static const char KEY[];
381   };
382
383   class GenericError : public DictKeyVal, public TopEntry
384   {
385   protected:
386     GenericError(const std::string& key, double dftValForScalarSparseMatrix);
387   };
388
389   class BackgroundError : public GenericError
390   {
391   public:
392     BackgroundError():GenericError(KEY,BACKGROUND_SCALAR_SPARSE_DFT) { }
393   public:
394     static const char KEY[];
395     static const double BACKGROUND_SCALAR_SPARSE_DFT;
396   };
397
398   class ObservationError : public GenericError
399   {
400   public:
401     ObservationError():GenericError(KEY,BACKGROUND_SCALAR_SPARSE_DFT) { }
402   public:
403     static const char KEY[];
404     static const double BACKGROUND_SCALAR_SPARSE_DFT;
405   };
406
407   class ObservationOperator : public DictKeyVal, public TopEntry
408   {
409   public:
410     ObservationOperator();
411   public:
412     static const char KEY[];
413   };
414
415   class Observation : public DictKeyVal, public TopEntry
416   {
417   public:
418     Observation();
419   public:
420     static const char KEY[];
421   };
422
423   class ObserverEntry : public DictKeyVal, public TopEntry
424   {
425   public:
426     ObserverEntry();
427   public:
428     static const char KEY[];
429   };
430
431   ///////////////
432
433   class PythonLeafVisitor
434   {
435   public:
436     virtual void visit(MainModel *godFather, PyObjKeyVal *obj) = 0;
437     virtual ~PythonLeafVisitor() { }
438   };
439
440   class RecursiveVisitor
441   {
442   public:
443     virtual ~RecursiveVisitor() { }
444     virtual void visit(GenericKeyVal *obj) = 0;
445     virtual void enterSubDir(DictKeyVal *subdir) = 0;
446     virtual void exitSubDir(DictKeyVal *subdir) = 0;
447   };
448
449   class MainModel
450   {
451   public:
452     MainModel();
453     std::string findPathOf(GenericKeyVal *elt);
454     std::string pyStr() const;
455     std::vector< std::shared_ptr<GenericKeyVal> > toVect() const;
456     void visitPythonLeaves(PythonLeafVisitor *visitor);
457     void visitAll(RecursiveVisitor *visitor);
458   private:
459     std::shared_ptr<AlgorithmParameters> _algo;
460     std::shared_ptr<Background> _bg;
461     std::shared_ptr<BackgroundError> _bg_err;
462     std::shared_ptr<Observation> _obs;
463     std::shared_ptr<ObservationError> _obs_err;
464     std::shared_ptr<ObservationOperator> _observ_op;
465     std::shared_ptr<ObserverEntry> _observ_entry;
466   };
467 }