Salome HOME
1c83f75820675c939acc526cbfccb75a54758305
[modules/yacs.git] / src / runtime / PythonNode.hxx
1 // Copyright (C) 2006-2022  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
20 #ifndef _PYTHONNODE_HXX_
21 #define _PYTHONNODE_HXX_
22
23 #include "YACSRuntimeSALOMEExport.hxx"
24 #include "InlineNode.hxx"
25 #include <SALOMEconfig.h>
26 #include CORBA_CLIENT_HEADER(SALOME_PyNode)
27 #include CORBA_CLIENT_HEADER(SALOME_Component)
28
29 #include <Python.h>
30
31 namespace YACS
32 {
33   namespace ENGINE
34   {
35     class YACSRUNTIMESALOME_EXPORT PythonEntry
36     {
37     public:
38       /*! return true only if ob is a proxy and destroy flag set to true*/
39       static bool GetDestroyStatus( PyObject *ob );
40       static void DoNotTouchFileIfProxy( PyObject *ob );
41       static void UnlinkOnDestructorIfProxy( PyObject *ob );
42     protected:
43       PythonEntry();
44       ~PythonEntry();
45       virtual void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp) = 0;
46       //! returns (if any) an object, you have to deal with (UnRegister)
47       virtual Engines::PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const = 0;
48       //! returns an object, you have to deal with (UnRegister)
49       virtual void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer) = 0;
50       virtual Engines::PyNodeBase_var getRemoteInterpreterHandle() = 0;
51       virtual const char *getSerializationScript() const = 0;
52       //
53       void commonRemoteLoad(InlineNode *reqNode);
54       void loadRemoteContainer(InlineNode *reqNode);
55       Engines::Container_var loadPythonAdapter(InlineNode *reqNode, bool& isInitializeRequested);
56       void loadRemoteContext(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested);
57       static std::string GetContainerLog(const std::string& mode, Container *container, const Task *askingTask);
58       virtual bool hasImposedResource()const;
59       static void IfProxyDoSomething( PyObject *ob, const char *meth );
60     protected:
61       PyObject *_context;
62       PyObject *_pyfuncSer;
63       PyObject *_pyfuncUnser;
64       PyObject *_pyfuncSimpleSer;
65       static PyObject *_pyClsBigObject;
66       std::string _imposedResource;
67       std::string _imposedContainer;
68     public:
69       static const char SCRIPT_FOR_SIMPLE_SERIALIZATION[];
70     };
71
72     class YACSRUNTIMESALOME_EXPORT PythonNode : public InlineNode, public PythonEntry
73     {
74     protected:
75       Node *simpleClone(ComposedNode *father, bool editionOnly) const;
76       // overload part of PythonEntry
77       void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer);
78       Engines::PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const;
79       void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp);
80       Engines::PyNodeBase_var getRemoteInterpreterHandle();
81       const char *getSerializationScript() const  { return SCRIPT_FOR_SERIALIZATION; }
82       // A kernel container may manage several python contexts identified by
83       // their name (PyNode and PyScript node). This function returns  the name
84       // of the context used by this object. See SALOME_Component.idl in KERNEL.
85       std::string pythonEntryName()const;
86     public:
87       PythonNode(const PythonNode& other, ComposedNode *father);
88       PythonNode(const std::string& name);
89       virtual ~PythonNode();
90       virtual void checkBasicConsistency() const ;
91       virtual void execute();
92       virtual void load();
93       virtual void loadRemote();
94       virtual void loadLocal();
95       virtual void executeRemote();
96       virtual void executeLocal();
97       virtual void shutdown(int level);
98       void imposeResource(const std::string& resource_name,
99                           const std::string& container_name) override;
100       bool canAcceptImposedResource()override;
101       bool hasImposedResource()const override;
102       bool isUsingPythonCache()const;
103       std::string getContainerLog();
104       PythonNode* cloneNode(const std::string& name);
105       virtual std::string typeName() { return "YACS__ENGINE__PythonNode"; }
106       void applyDPLScope(ComposedNode *gfn);
107       void setSqueezeStatus(bool sqStatus) { _autoSqueeze=sqStatus; }
108       bool getSqueezeStatus() const { return _autoSqueeze; }
109       void squeezeMemorySafe();
110     protected:
111       void squeezeMemory();
112       void squeezeMemoryRemote();
113       void freeKernelPynode();
114     public:
115       static const char KIND[];
116       static const char IMPL_NAME[];
117       static const char SCRIPT_FOR_SERIALIZATION[];
118       static const char REMOTE_NAME[];
119       static const char DPL_INFO_NAME[];
120     protected:
121       bool _autoSqueeze = false;
122       Engines::PyScriptNode_var _pynode;
123     };
124
125     class YACSRUNTIMESALOME_EXPORT PyFuncNode : public InlineFuncNode, public PythonEntry
126     {
127     protected:
128       Node *simpleClone(ComposedNode *father, bool editionOnly) const;
129       // overload part of PythonEntry
130       void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer);Engines::
131       PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const;
132       void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp);
133       Engines::PyNodeBase_var getRemoteInterpreterHandle();
134       const char *getSerializationScript() const { return SCRIPT_FOR_SERIALIZATION; }
135     public:
136       PyFuncNode(const PyFuncNode& other, ComposedNode *father);
137       PyFuncNode(const std::string& name);
138       virtual ~PyFuncNode();
139       virtual void init(bool start=true);
140       virtual void checkBasicConsistency() const ;
141       virtual void execute();
142       virtual void load();
143       virtual void loadRemote();
144       virtual void loadLocal();
145       virtual void executeRemote();
146       virtual void executeLocal();
147       virtual void shutdown(int level);
148       void imposeResource(const std::string& resource_name,
149                           const std::string& container_name) override;
150       bool canAcceptImposedResource()override;
151       bool hasImposedResource()const override;
152       std::string getContainerLog();
153       PyFuncNode* cloneNode(const std::string& name);
154       virtual std::string typeName() { return "YACS__ENGINE__PyFuncNode"; }
155     public:
156       static const char SCRIPT_FOR_SERIALIZATION[];
157     protected:
158       PyObject* _pyfunc;
159       Engines::PyNode_var _pynode;
160     };
161   }
162 }
163
164 #endif