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