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