1 # -*- coding: iso-8859-1 -*-
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
21 # python3 /home/H87074/salome/990_CEA/SALOME-master-native-DB11-SRC/SOURCES/yacs/src/yacsloader/driver.py -v -kt micro_schema.xml
22 # python3 /home/H87074/salome/990_CEA/SALOME-master-native-DB11-SRC/SOURCES/yacs/src/yacsloader/driver.py -d 3 --stop-on-error --dump-on-error="popo.xml" -v -kt -ip "PyScript0.p1=7" --dump-final="tutu.xml" micro_schema2.xml
36 {"display", 'd', "level", 0, "Display dot files: 0=never to 3=very often (default 0)"},
37 {"dump", 'g', "nbsec", OPTION_ARG_OPTIONAL, "dump state"},
38 {"save-xml-schema", 'x', "file", OPTION_ARG_OPTIONAL, "dump xml schema"},
41 my_runtime_yacs = None
43 def initializeSALOME():
45 global my_runtime_yacs
49 flags = SALOMERuntime.RuntimeSALOME.UsePython + SALOMERuntime.RuntimeSALOME.UseCorba + SALOMERuntime.RuntimeSALOME.UseXml + SALOMERuntime.RuntimeSALOME.UseCpp + SALOMERuntime.RuntimeSALOME.UseSalome
50 SALOMERuntime.RuntimeSALOME.setRuntime( flags )
51 my_runtime_yacs = SALOMERuntime.getSALOMERuntime()
52 anIOR = salome.orb.object_to_string ( salome.modulcat )
53 aCatalog = my_runtime_yacs.loadCatalog( "session", anIOR )
54 my_runtime_yacs.addCatalog( aCatalog )
56 def SALOMEInitializationNeeded(func):
57 def decaratedFunc(*args,**kwargs):
59 return func(*args,**kwargs)
62 @SALOMEInitializationNeeded
63 def loadGraph( xmlFileName ):
67 xmlFileName : XML file containing YACS schema
72 SALOMERuntime.SalomeProc : YACS graph instance
75 p=l.load( xmlFileName )
78 def patchGraph( proc, squeezeMemory, initPorts, loadStateXmlFile, reset, display):
83 proc ( SALOMERuntime.SalomeProc ) : YACS Proc instance to be evaluated
84 squeezeMemory ( bool ) : squeezememory to be activated
85 initPorts (list<string>) : list of bloc.node.port=value.
86 loadStateXmlFile (string) : file if any of state to be loaded inside proc
90 def parse_init_port(input):
96 node_port, value = input.split("=")
97 nodePortSpl = node_port.split(".")
98 port = nodePortSpl[-1]
99 node = ".".join( nodePortSpl[:-1] )
100 return node,port,value
103 logging.info("SqueezeMemory requested -> update proc")
104 allNodes = proc.getAllRecursiveNodes()
105 for node in allNodes:
106 if isinstance(proc,SALOMERuntime.PythonNode):
107 node.setSqueezeStatus( True )
109 for initPort in initPorts:
110 node,port,value = parse_init_port(initPort)
111 init_state = proc.setInPortValue(node, port, value)
112 if init_state != value:
113 raise RuntimeError(f"Error on initialization of {initPort}")
116 loader.loadState( proc, loadStateXmlFile )
118 proc.resetState(reset)
122 proc.writeDotInFile("toto")
124 @SALOMEInitializationNeeded
125 def prepareExecution(proc, isStop, dumpErrorFile):
130 pilot.ExecutorSwig : Instance of executor
133 ex=pilot.ExecutorSwig()
135 logging.info(f"Stop has been activated with {dumpErrorFile}")
136 ex.setStopOnError( dumpErrorFile!="", dumpErrorFile )
139 @SALOMEInitializationNeeded
140 def executeGraph( executor, proc, finalDump, display ):
145 executor (pilot.ExecutorSwig) : Executor in charge of evaluation.
146 proc ( SALOMERuntime.SalomeProc ) : YACS Proc instance to be evaluated
147 finalDump ( string ) : filename containing final result of graph, if any.
151 executor.RunW(proc,display)
152 if proc.getEffectiveState() != pilot.DONE:
153 raise RuntimeError( proc.getErrorReport() )
156 proc.writeDotInFile("titi")
159 logging.info(f"Final dump requested : {finalDump}")
160 SALOMERuntime.schemaSaveStateUnsafe( proc, finalDump )
162 @SALOMEInitializationNeeded
163 def destroyElementsGeneratedByExecutionOfGraph( proc, shutdown ):
168 shutdown (int) : shutdown level
172 proc.shutdown(shutdown)
173 salome.dsm.shutdownScopes()
174 my_runtime_yacs.fini( False )
176 if __name__ == "__main__":
177 from salome_utils import positionVerbosityOfLoggerRegardingState,setVerboseLevel,setVerbose
179 parser = argparse.ArgumentParser()
180 parser.add_argument('xmlfilename',help = "XML file containing YACS schema to be executed")
181 parser.add_argument("-d", "--display", dest = "display", type=int, default=[0], nargs=1, help="Display dot files: 0=never to 3=very often")
182 parser.add_argument("-v", "--verbose", dest = "verbose",help="Produce verbose output", action='store_true')
183 parser.add_argument("--stop-on-error",dest="stop",help="Stop on first error", action='store_true')
184 parser.add_argument("-e","--dump-on-error",dest="dumpErrorFile", type=str, const='dumpErrorState.xml', default="", nargs='?', help="Stop on first error and dump state")
185 parser.add_argument("-kt", "--kerneltrace", dest = "kerneltrace",help="Produce verbose of SALOME/KERNEL", action='store_true')
186 parser.add_argument("-f","--dump-final", dest ="finalDump", type=str, const='finalDumpState.xml', default="", nargs='?', help="dump final state")
187 parser.add_argument("--load-state", dest="loadState", type=str, default="", help="Load State from a previous partial execution")
188 parser.add_argument("--shutdown", dest = 'shutdown', type=int , default=1, help="Shutdown the schema: 0=no shutdown to 3=full shutdown")
189 parser.add_argument("--reset", dest = "reset", type=int , default = 0, help="Reset the schema before execution: 0=nothing, 1=reset error nodes to ready state")
190 parser.add_argument("-z","--donotsqueeze", dest = "donotsqueeze", help = "Desactivate squeeze memory optimization.", action='store_true')
191 parser.add_argument("-ip","--init-port", dest = 'init_port', type=str, help="Initialisation value of a port, specified as bloc.node.port=value.")
192 args = parser.parse_args()
193 args.display = args.display[0]
196 setVerbose( args.kerneltrace )
197 setVerboseLevel(logging.INFO)
198 positionVerbosityOfLoggerRegardingState()
200 proc = loadGraph( args.xmlfilename )
201 patchGraph( proc, not args.donotsqueeze, args.init_port.split(","), args.loadState, args.reset, args.display)
202 executor = prepareExecution( proc, args.stop, args.dumpErrorFile)
203 executeGraph( executor, proc, args.finalDump, args.display)
204 destroyElementsGeneratedByExecutionOfGraph( proc, args.shutdown )