Salome HOME
Initialization.
[tools/ydefx.git] / src / cpp / TMonoPyJob.hxx
1 #ifndef YDEFX_TMONOPYJOB_HXX
2 #define YDEFX_TMONOPYJOB_HXX
3 #include "JobParametersProxy.hxx"
4 #include "MonoPyJob.hxx"
5 #include "Sample.hxx"
6 #include "SamplePyConversions.hxx"
7 #include "PyStudyFunction.hxx"
8 #include <py2cpp/py2cpp.hxx>
9
10 namespace ydefx
11 {
12 template <class ...Ts>
13 class TMonoPyJob : public MonoPyJob
14 {
15 public:
16   //! Create a new job.
17   TMonoPyJob(const PyStudyFunction& fnScript,
18              Sample<Ts...>& sample,
19              const JobParametersProxy& params)
20   : MonoPyJob()
21   , _sample(sample)
22   {
23     if(_lastError.empty()) // no errors during parent contruction
24     {
25       try
26       {
27         py2cpp::PyPtr pySample = createPySample(sample);
28         py2cpp::PyFunction pyFn;
29         pyFn.loadExp(_pyStudy, "createNewJob");
30         pyFn(fnScript.content(), pySample, params);
31       }
32       catch(std::exception& e)
33       {
34         _lastError = "An error occured while creating the job.\n";
35         _lastError += e.what();
36       }
37     }
38   }
39
40   //! Connect to an existing job.
41   TMonoPyJob(const std::string& jobDump, Sample<Ts...>& sample)
42   : MonoPyJob()
43   , _sample(sample)
44   {
45     if(_lastError.empty()) // no errors during parent contruction
46     {
47       try
48       {
49         py2cpp::PyFunction pyFn;
50         pyFn.loadExp(_pyStudy, "loadFromString");
51         pyFn(jobDump);
52       }
53       catch(std::exception& e)
54       {
55         _lastError = "An error occured while creating the job.\n";
56         _lastError += e.what();
57       }
58     }
59   }
60   
61   virtual ~TMonoPyJob(){}
62   virtual bool fetch()
63   {
64     _lastError = "";
65     try
66     {
67       py2cpp::PyFunction pyFn;
68       pyFn.loadExp(_pyStudy, "getResult");
69       fetchResults(_pyStudy.getAttr("sample"), _sample);
70     }
71     catch(std::exception& e)
72     {
73       _lastError = "An error occured while fetching the results.\n";
74       _lastError += e.what();
75     }
76     return _lastError.empty();
77   }
78
79   const Sample<Ts...>& getSample()const{return _sample;}
80
81 private:
82   Sample<Ts...>& _sample;
83 };
84
85 }
86
87 #endif //YDEFX_TMONOPYJOB_HXX