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