1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2024 CEA, EDF
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 my_runtime_yacs = None
26 def initializeSALOME():
28 global my_runtime_yacs
32 flags = SALOMERuntime.RuntimeSALOME.UsePython + SALOMERuntime.RuntimeSALOME.UseCorba + SALOMERuntime.RuntimeSALOME.UseXml + SALOMERuntime.RuntimeSALOME.UseCpp + SALOMERuntime.RuntimeSALOME.UseSalome
33 SALOMERuntime.RuntimeSALOME.setRuntime( flags )
34 my_runtime_yacs = SALOMERuntime.getSALOMERuntime()
35 anIOR = salome.orb.object_to_string ( salome.modulcat )
36 aCatalog = my_runtime_yacs.loadCatalog( "session", anIOR )
37 my_runtime_yacs.addCatalog( aCatalog )
39 def SALOMEInitializationNeeded(func):
40 def decaratedFunc(*args,**kwargs):
42 return func(*args,**kwargs)
45 @SALOMEInitializationNeeded
46 def loadGraph( xmlFileName ):
50 xmlFileName : XML file containing YACS schema
55 SALOMERuntime.SalomeProc : YACS graph instance
59 p=l.load( xmlFileName )
62 def patchGraph( proc, squeezeMemory, initPorts, xmlSchema, loadStateXmlFile, reset, display):
67 proc ( SALOMERuntime.SalomeProc ) : YACS Proc instance to be evaluated
68 squeezeMemory ( bool ) : squeezememory to be activated
69 initPorts (list<string>) : list of bloc.node.port=value.
71 loadStateXmlFile (string) : file if any of state to be loaded inside proc
77 def parse_init_port(input):
83 node_port, value = input.split("=")
84 nodePortSpl = node_port.split(".")
85 port = nodePortSpl[-1]
86 node = ".".join( nodePortSpl[:-1] )
87 return node,port,value
90 logging.info("SqueezeMemory requested -> update proc")
91 allNodes = proc.getAllRecursiveNodes()
93 if isinstance(proc,SALOMERuntime.PythonNode):
94 node.setSqueezeStatus( True )
96 for initPort in initPorts:
97 node,port,value = parse_init_port(initPort)
98 init_state = proc.setInPortValue(node, port, value)
99 if init_state != value:
100 raise RuntimeError(f"Error on initialization of {initPort}")
103 SALOMERuntime.VisitorSaveSalomeSchemaUnsafe(proc,xmlSchema)
107 loader.loadState( proc, loadStateXmlFile )
109 proc.resetState(reset)
113 proc.writeDotInFile("toto")
115 @SALOMEInitializationNeeded
116 def prepareExecution(proc, isStop, dumpErrorFile):
121 pilot.ExecutorSwig : Instance of executor
124 ex=pilot.ExecutorSwig()
126 logging.info(f"Stop has been activated with {dumpErrorFile}")
127 ex.setStopOnError( dumpErrorFile!="", dumpErrorFile )
130 @SALOMEInitializationNeeded
131 def executeGraph( executor, xmlfilename, proc, dump, finalDump, display, shutdown ):
136 executor (pilot.ExecutorSwig) : Executor in charge of evaluation.
137 proc ( SALOMERuntime.SalomeProc ) : YACS Proc instance to be evaluated
139 dump (int) : time interval between 2 dump state
140 finalDump ( string ) : filename containing final result of graph, if any.
142 shutdown (int) : shutdown level
146 def handler(signum, frame):
147 print('Signal handler called with signal', signum)
150 SALOMERuntime.schemaSaveStateUnsafe( proc, finalDump )
152 print(100*"shutdown")
153 proc.shutdown(shutdown)
155 signal.signal(signal.SIGINT, handler)
156 signal.signal(signal.SIGTERM, handler)
162 dumpFile = "dumpState_{}".format( os.path.basename(xmlfilename) )
163 lockFile = "{}.lock".format( os.path.splitext( os.path.basename(xmlfilename) )[0] )
164 dump_thread = SALOMERuntime.ThreadDumpState(proc,dump,dumpFile,lockFile)
167 executor.RunPy(proc,display,isPyThread=True,fromscratch=True) # same as RunW but releasing GIL
168 if proc.getEffectiveState() != pilot.DONE:
169 raise RuntimeError( proc.getErrorReport() )
172 proc.writeDotInFile("titi")
178 logging.info(f"Final dump requested : {finalDump}")
179 SALOMERuntime.schemaSaveStateUnsafe( proc, finalDump )
181 @SALOMEInitializationNeeded
182 def destroyElementsGeneratedByExecutionOfGraph( proc, shutdown ):
187 shutdown (int) : shutdown level
191 proc.shutdown(shutdown)
192 salome.dsm.shutdownScopes()
193 my_runtime_yacs.fini( False )
195 if __name__ == "__main__":
196 from salome_utils import positionVerbosityOfLoggerRegardingState,setVerboseLevel,setVerbose
198 parser = argparse.ArgumentParser()
199 parser.add_argument('xmlfilename',help = "XML file containing YACS schema to be executed")
200 parser.add_argument("-d", "--display", dest = "display", type=int, default=[0], nargs=1, help="Display dot files: 0=never to 3=very often")
201 parser.add_argument("-v", "--verbose", dest = "verbose",help="Produce verbose output", action='store_true')
202 parser.add_argument("-s","--stop-on-error",dest="stop",help="Stop on first error", action='store_true')
203 parser.add_argument("-e","--dump-on-error",dest="dumpErrorFile", type=str, const='dumpErrorState.xml', default="", nargs='?', help="Stop on first error and dump state")
204 parser.add_argument("-g","--dump",dest="dump", type=int, const=60, default=0, nargs='?', help="dump state")
205 parser.add_argument("-kt", "--kerneltrace", dest = "kerneltrace",help="Produce verbose of SALOME/KERNEL", action='store_true')
206 parser.add_argument("-f","--dump-final", dest ="finalDump", type=str, const='finalDumpState.xml', default="", nargs='?', help="dump final state")
207 parser.add_argument("-l","--load-state", dest="loadState", type=str, default="", help="Load State from a previous partial execution")
208 parser.add_argument("-x","--save-xml-schema", dest="saveXMLSchema", type=str, const="saveSchema.xml", nargs='?', default="", help = "dump xml schema")
209 parser.add_argument("-t","--shutdown", dest = 'shutdown', type=int , default=1, help="Shutdown the schema: 0=no shutdown to 3=full shutdown")
210 parser.add_argument("-r","--reset", dest = "reset", type=int , default = 0, help="Reset the schema before execution: 0=nothing, 1=reset error nodes to ready state")
211 parser.add_argument("-i","--init-port", dest = 'init_port', type=str, default ="", help="Initialisation value of a port, specified as bloc.node.port=value.")
212 parser.add_argument("-z","--donotsqueeze", dest = "donotsqueeze", help = "Desactivate squeeze memory optimization.", action='store_true')
213 args = parser.parse_args()
214 args.display = args.display[0]
217 setVerbose( args.kerneltrace )
218 setVerboseLevel(logging.INFO)
219 positionVerbosityOfLoggerRegardingState()
221 proc = loadGraph( args.xmlfilename )
222 patchGraph( proc, not args.donotsqueeze, [elt for elt in args.init_port.split(",") if elt !=""], args.saveXMLSchema, args.loadState, args.reset, args.display)
223 executor = prepareExecution( proc, args.stop, args.dumpErrorFile)
225 executeGraph( executor, args.xmlfilename, proc, args.dump, args.finalDump, args.display, args.shutdown)
227 destroyElementsGeneratedByExecutionOfGraph( proc, args.shutdown )