Salome HOME
Add a global result for a study.
[tools/ydefx.git] / src / cpp / TMonoPyJob.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, 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 #ifndef YDEFX_TMONOPYJOB_HXX
20 #define YDEFX_TMONOPYJOB_HXX
21 #include "JobParametersProxy.hxx"
22 #include "MonoPyJob.hxx"
23 #include "SamplePyConversions.hxx"
24 #include "PyStudyFunction.hxx"
25
26 namespace ydefx
27 {
28 template <class ...Ts>
29 class TMonoPyJob : public MonoPyJob
30 {
31 public:
32   //! Create a new job.
33   TMonoPyJob(const PyStudyFunction& fnScript,
34              Sample<Ts...>& sample,
35              const JobParametersProxy& params)
36   : MonoPyJob()
37   , _sample(sample)
38   {
39     if(_lastError.empty()) // no errors during parent construction
40     {
41       try
42       {
43         py2cpp::PyPtr pySample = createPySample(sample);
44         py2cpp::PyFunction pyFn;
45         pyFn.loadExp(_pyStudy, "createNewJob");
46         pyFn(fnScript, pySample, params);
47       }
48       catch(std::exception& e)
49       {
50         _lastError = "An error occured while creating the job.\n";
51         _lastError += e.what();
52       }
53     }
54   }
55
56   //! Connect to an existing job.
57   TMonoPyJob(const std::string& jobDump, Sample<Ts...>& sample)
58   : MonoPyJob()
59   , _sample(sample)
60   {
61     if(_lastError.empty()) // no errors during parent construction
62     {
63       try
64       {
65         py2cpp::PyFunction pyFn;
66         pyFn.loadExp(_pyStudy, "loadFromString");
67         pyFn(jobDump);
68       }
69       catch(std::exception& e)
70       {
71         _lastError = "An error occured while creating the job.\n";
72         _lastError += e.what();
73       }
74     }
75   }
76   
77   virtual ~TMonoPyJob(){}
78   virtual bool fetch()
79   {
80     _lastError = "";
81     try
82     {
83       py2cpp::PyFunction pyFn;
84       pyFn.loadExp(_pyStudy, "getResult");
85       pyFn(); // python call: _pyStudy.getResult()
86       fetchResults(_pyStudy.getAttr("sample"), _sample);
87     }
88     catch(std::exception& e)
89     {
90       _lastError = "An error occured while fetching the results.\n";
91       _lastError += e.what();
92     }
93     return _lastError.empty();
94   }
95
96   const Sample<Ts...>& getSample()const{return _sample;}
97
98 private:
99   Sample<Ts...>& _sample;
100 };
101
102 }
103
104 #endif //YDEFX_TMONOPYJOB_HXX