Salome HOME
Merge remote-tracking branch 'origin/master' into V9_dev
[modules/yacs.git] / src / yacsorb / YACS.py
index d7cc41ba5b15bdc5bcdef3a4989f310b0df8601a..5ce0f7ff68b1797cbe6dd146c0ba407ac5c90b2d 100644 (file)
@@ -1,21 +1,22 @@
-#  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
+# Copyright (C) 2006-2016  CEA/DEN, EDF R&D
 #
-#  This library is free software; you can redistribute it and/or
-#  modify it under the terms of the GNU Lesser General Public
-#  License as published by the Free Software Foundation; either
-#  version 2.1 of the License.
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
 #
-#  This library is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY; without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-#  Lesser General Public License for more details.
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
 #
-#  You should have received a copy of the GNU Lesser General Public
-#  License along with this library; if not, write to the Free Software
-#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 #
-#  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
+
 import sys
 import YACS_ORB__POA
 import YACS_ORB
@@ -24,6 +25,7 @@ import SALOME_DriverPy
 
 import threading
 import tempfile
+import os
 
 import SALOMERuntime
 import loader
@@ -38,19 +40,38 @@ class proc_i(YACS_ORB__POA.ProcExec):
         self.e.setExecMode(1) # YACS::STEPBYSTEP
         self.run1 = None
         self.p = self.l.load(xmlFile)
+        self.xmlFile=xmlFile
         pass
 
     def getNodeState(self,numid):
         return self.p.getNodeState(numid)
 
+    def getNodeProgress(self,numid):
+        return self.p.getNodeProgress(numid)
+
     def getXMLState(self, numid):
         return self.p.getXMLState(numid)
 
     def getInPortValue(self, nodeNumid, portName):
+      try:
         return self.p.getInPortValue(nodeNumid, portName)
+      except:
+        traceback.print_exc()
+        return ""
+
+    def setInPortValue(self, nodeName, portName, value):
+      try:
+        return self.p.setInPortValue(nodeName, portName, value)
+      except:
+        traceback.print_exc()
+        return ""
 
     def getOutPortValue(self, nodeNumid, portName):
+      try:
         return self.p.getOutPortValue(nodeNumid, portName)
+      except:
+        traceback.print_exc()
+        return ""
 
     def getErrorDetails(self, nodeNumid):
         return self.p.getNodeErrorDetails(nodeNumid)
@@ -61,6 +82,9 @@ class proc_i(YACS_ORB__POA.ProcExec):
     def getContainerLog(self, nodeNumid):
         return self.p.getNodeContainerLog(nodeNumid)
 
+    def shutdownProc(self, level):
+        return self.p.shutdown(level)
+
     def getExecutorState(self):
         return self.e.getExecutorState()
 
@@ -75,50 +99,85 @@ class proc_i(YACS_ORB__POA.ProcExec):
     def getNames(self):
         return self.p.getIds()
 
+    def runProc(self,debug, isPyThread, fromscratch):
+      print("**************************Begin schema execution %s**************************" % self.xmlFile)
+      self.e.RunPy(self.p,debug, isPyThread, fromscratch)
+      print("**************************End schema execution %s****************************" % self.xmlFile)
+
     def Run(self):
-        execState = self.e.getExecutorState()
-        if execState >= 305:
-            # --- not clean, value from define.hxx (YACS::FINISHED)
+        if self.run1 is not None:
+          execState = self.e.getExecutorState()
+          if execState >= pilot.FINISHED:
             self.run1.join()
             self.run1 = None
-            pass
+
         if self.run1 is None:
-            self.run1 = threading.Thread(None, self.e.RunPy, "CORBAExec", (self.p,0,1,1))
+            self.run1 = threading.Thread(None, self.runProc, "CORBAExec", (0,True,True))
             self.run1.start()
-            pass
-        pass
 
     def RunFromState(self, xmlFile):
-        execState = self.e.getExecutorState()
-        if execState >= 305:
-            # --- not clean, value from define.hxx (YACS::FINISHED)
+        """Start an execution from the state given by the file xmlFile
+           If xmlFile == "", start execution from the current state
+        """
+        if self.run1 is not None:
+          execState = self.e.getExecutorState()
+          if execState >= pilot.FINISHED:
             self.run1.join()
             self.run1 = None
-            pass
-        try:
+
+        if xmlFile:
+          try:
             self.p.init()
             self.p.exUpdateState();
             sp = loader.stateParser()
             sl = loader.stateLoader(sp,self.p)
             sl.parse(xmlFile)
-        except IOError, ex:
-            print "IO Error: ", ex
-            return None
-        except ValueError,ex:
-            print "Caught ValueError Exception:",ex
-            return None
-        except pilot.Exception,ex:
-            print ex.what()
-            return None
-        except:
-            print "Unknown exception!"
-            return None
+          except IOError as ex:
+            print("IO Error: ", ex)
+            return
+          except ValueError as ex:
+            print("Caught ValueError Exception:",ex)
+            return
+          except pilot.Exception as ex:
+            print(ex.what())
+            return
+          except:
+            print("Unknown exception!")
+            return
+
         if self.run1 is None:
-            self.run1 = threading.Thread(None, self.e.RunPy, "CORBAExec", (self.p,0,1,0))
+            self.run1 = threading.Thread(None, self.runProc, "CORBAExec", (0,True,False))
             self.run1.start()
+
+    def RestartFromState(self, xmlFile):
+        """Reset the procedure state to ready state for all nodes in error
+           if xmlFile exists first try to load the state from this file.
+           then start execution
+        """
+        if self.run1 is not None:
+          execState = self.e.getExecutorState()
+          if execState >= pilot.FINISHED:
+            self.run1.join()
+            self.run1 = None
+          else:
+            return
+
+        try:
+          if os.path.exists(xmlFile):
+            self.p.init()
+            sp = loader.stateParser()
+            sl = loader.stateLoader(sp,self.p)
+            sl.parse(xmlFile)
+
+          self.p.resetState(1)
+          self.p.exUpdateState();
+        except:
             pass
-        pass
-    
+
+        if self.run1 is None:
+            self.run1 = threading.Thread(None, self.runProc, "CORBAExec", (0,True,False))
+            self.run1.start()
+
     def addObserver(self, obs, numid, event):
         disp = SALOMERuntime.SALOMEDispatcher_getSALOMEDispatcher()
         disp.addObserver(obs, numid, event)
@@ -179,21 +238,41 @@ class YACS(YACS_ORB__POA.YACS_Gen,
     with omniidl and also the class SALOME_ComponentPy_i which defines general
     SALOME component behaviour.
     """
-    def __init__ ( self, orb, poa, contID, containerName, instanceName, 
+    def __init__ ( self, orb, poa, contID, containerName, instanceName,
                    interfaceName ):
-        print "YACS.__init__: ", containerName, ';', instanceName
+        print("YACS.__init__: ", containerName, ';', instanceName)
         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa, contID,
                                                          containerName, instanceName,
-                                                         interfaceName, 0)
+                                                         interfaceName, False)
         SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
 
         # --- store a naming service interface instance in _naming_service atribute
         self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
 
-        
         SALOMERuntime.RuntimeSALOME_setRuntime(1)
         SALOMERuntime.SALOMEDispatcher_setSALOMEDispatcher()
-        pass
+        r=pilot.getRuntime()
+
+        try:
+          #try to load SALOME module catalogs
+          modul_catalog = self._naming_service.Resolve("/Kernel/ModulCatalog")
+          ior= orb.object_to_string(modul_catalog)
+          cata=r.loadCatalog("session",ior)
+          r.addCatalog(cata)
+        except :
+          pass
+
+    """
+    Get version information.
+    """
+    def getVersion( self ):
+        try:
+            rt = SALOMERuntime.getSALOMERuntime()
+            version = rt.getVersion()
+        except:
+            version = ""
+            pass
+        return version
 
     def LoadProc(self,xmlFile):
         """
@@ -204,18 +283,18 @@ class YACS(YACS_ORB__POA.YACS_Gen,
             procExec_i = proc_i(xmlFile)
             logger=procExec_i.p.getLogger("parser")
             if not logger.isEmpty():
-              print "The imported file has errors :"
-              print logger.getStr()
+              print("The imported file has errors :")
+              print(logger.getStr())
               sys.stdout.flush()
               return None
-        except IOError, ex:
-            print >> sys.stderr ,"IO Error: ", ex
+        except IOError as ex:
+            print("IO Error: ", ex, file=sys.stderr)
             return None
-        except ValueError,ex:
-            print >> sys.stderr ,"Caught ValueError Exception:",ex
+        except ValueError as ex:
+            print("Caught ValueError Exception:",ex, file=sys.stderr)
             return None
-        except pilot.Exception,ex:
-            print >> sys.stderr ,ex.what()
+        except pilot.Exception as ex:
+            print(ex.what(), file=sys.stderr)
             return None
         except:
             traceback.print_exc()