Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / src / yacsorb / YACS.py
1 #  Copyright (C) 2005  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.
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 import sys
20 import YACS_ORB__POA
21 import YACS_ORB
22 import SALOME_ComponentPy
23 import SALOME_DriverPy
24
25 import threading
26 import tempfile
27
28 import SALOMERuntime
29 import loader
30 import salomeloader
31 import pilot
32 import traceback
33
34 class proc_i(YACS_ORB__POA.ProcExec):
35     def __init__(self, xmlFile):
36         self.l = loader.YACSLoader()
37         self.e = pilot.ExecutorSwig()
38         self.p = self.l.load(xmlFile)
39         self.e.setExecMode(1) # YACS::STEPBYSTEP
40         self.run1 = None
41         pass
42
43     def getNodeState(self,numid):
44         return self.p.getNodeState(numid)
45
46     def getXMLState(self, numid):
47         return self.p.getXMLState(numid)
48
49     def getInPortValue(self, nodeNumid, portName):
50         return self.p.getInPortValue(nodeNumid, portName)
51
52     def getOutPortValue(self, nodeNumid, portName):
53         return self.p.getOutPortValue(nodeNumid, portName)
54
55     def getErrorDetails(self, nodeNumid):
56         return self.p.getNodeErrorDetails(nodeNumid)
57
58     def getErrorReport(self, nodeNumid):
59         return self.p.getNodeErrorReport(nodeNumid)
60
61     def getContainerLog(self, nodeNumid):
62         return self.p.getNodeContainerLog(nodeNumid)
63
64     def getExecutorState(self):
65         return self.e.getExecutorState()
66
67     def getIds(self):
68         numids = self.p.getNumIds()
69         ids = self.p.getIds()
70         return (numids,ids)
71
72     def getNumIds(self):
73         return self.p.getNumIds()
74
75     def getNames(self):
76         return self.p.getIds()
77
78     def Run(self):
79         execState = self.e.getExecutorState()
80         if execState >= 305:
81             # --- not clean, value from define.hxx (YACS::FINISHED)
82             self.run1.join()
83             self.run1 = None
84             pass
85         if self.run1 is None:
86             self.run1 = threading.Thread(None, self.e.RunPy, "CORBAExec", (self.p,0))
87             self.run1.start()
88             pass
89         pass
90
91     def RunFromState(self, xmlFile):
92         execState = self.e.getExecutorState()
93         if execState >= 305:
94             # --- not clean, value from define.hxx (YACS::FINISHED)
95             self.run1.join()
96             self.run1 = None
97             pass
98         try:
99             self.p.init()
100             self.p.exUpdateState();
101             sp = loader.stateParser()
102             sl = loader.stateLoader(sp,self.p)
103             sl.parse(xmlFile)
104         except IOError, ex:
105             print "IO Error: ", ex
106             return None
107         except ValueError,ex:
108             print "Caught ValueError Exception:",ex
109             return None
110         except pilot.Exception,ex:
111             print ex.what()
112             return None
113         except:
114             print "Unknown exception!"
115             return None
116         if self.run1 is None:
117             self.run1 = threading.Thread(None, self.e.RunPy, "CORBAExec", (self.p,0))
118             self.run1.start()
119             pass
120         pass
121     
122     def addObserver(self, obs, numid, event):
123         disp = SALOMERuntime.SALOMEDispatcher_getSALOMEDispatcher()
124         disp.addObserver(obs, numid, event)
125         pass
126
127     def setExecMode(self, mode):
128         if mode == YACS_ORB.CONTINUE:
129             self.e.setExecMode(0)
130             pass
131         if mode == YACS_ORB.STEPBYSTEP:
132             self.e.setExecMode(1)
133             pass
134         if mode == YACS_ORB.STOPBEFORENODES:
135             self.e.setExecMode(2)
136             pass
137         pass
138
139     def setListOfBreakPoints(self, listOfBreakPoints):
140         self.e.setListOfBreakPoints(listOfBreakPoints)
141         pass
142
143     def getTasksToLoad(self):
144         return self.e.getTasksToLoad()
145
146     def setStepsToExecute(self, listToExecute):
147         return self.e.setStepsToExecute(listToExecute)
148
149     def resumeCurrentBreakPoint(self):
150         return self.e.resumeCurrentBreakPoint()
151
152     def isNotFinished(self):
153         return self.e.isNotFinished()
154
155     def stopExecution(self):
156         self.e.stopExecution()
157         pass
158
159     def saveState(self, xmlFile):
160         return self.e.saveState(xmlFile)
161
162     def setStopOnError(self, dumpRequested, xmlFile):
163         self.e.setStopOnError(dumpRequested, xmlFile)
164         pass
165
166     def unsetStopOnError(self):
167         self.e.unsetStopOnError()
168         pass
169
170     pass
171
172
173 class YACS(YACS_ORB__POA.YACS_Gen,
174               SALOME_ComponentPy.SALOME_ComponentPy_i,
175               SALOME_DriverPy.SALOME_DriverPy_i):
176     """
177     To be a SALOME component, this Python class must have the component name
178     (YACS) and inherit the YACS_Gen class build from idl compilation
179     with omniidl and also the class SALOME_ComponentPy_i which defines general
180     SALOME component behaviour.
181     """
182     def __init__ ( self, orb, poa, contID, containerName, instanceName, 
183                    interfaceName ):
184         print "YACS.__init__: ", containerName, ';', instanceName
185         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa, contID,
186                                                          containerName, instanceName,
187                                                          interfaceName, 0)
188         SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
189
190         # --- store a naming service interface instance in _naming_service atribute
191         self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
192
193         
194         SALOMERuntime.RuntimeSALOME_setRuntime(1)
195         SALOMERuntime.SALOMEDispatcher_setSALOMEDispatcher()
196         pass
197
198     def LoadProc(self,xmlFile):
199         """
200         load an XML graph in a YACS::ENGINE::proc, create a CORBA servant
201         associated to the proc, and return a ref on the servant.
202         """
203         try:
204             procExec_i = proc_i(xmlFile)
205         except IOError, ex:
206             print >> sys.stderr ,"IO Error: ", ex
207             return None
208         except ValueError,ex:
209             print >> sys.stderr ,"Caught ValueError Exception:",ex
210             return None
211         except pilot.Exception,ex:
212             print >> sys.stderr ,ex.what()
213             return None
214         except:
215             print >> sys.stderr ,"Unknown exception!"
216             return None
217         procExec_o = procExec_i._this()
218         return procExec_o
219         
220     def convertSupervFile(self,xmlFile):
221         """
222         load a SUPERV xml graph, convert it and return the new filename.
223         """
224         try:
225             r = pilot.getRuntime()
226             lo = salomeloader.SalomeLoader()
227             e = pilot.ExecutorSwig()
228             p = lo.load(xmlFile)
229             s = pilot.SchemaSave(p)
230             hnd, convertedFile = tempfile.mkstemp(".xml","yacs_","/tmp")
231             s.save(convertedFile)
232             return convertedFile
233         except (IndexError):
234             return ""
235
236     pass
237