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