From 97e80057c7544f3d038196ac73cda51c18702e39 Mon Sep 17 00:00:00 2001 From: Renaud Barate Date: Mon, 3 Dec 2012 10:59:27 +0100 Subject: [PATCH] Execution of schemas with user Python script --- src/PARAMETRIC/PARAMETRIC.py | 86 ++++++++++++++++++------- src/salome/parametric/gui/execparams.ui | 8 ++- 2 files changed, 70 insertions(+), 24 deletions(-) diff --git a/src/PARAMETRIC/PARAMETRIC.py b/src/PARAMETRIC/PARAMETRIC.py index 7ad2c81..52d3ba6 100644 --- a/src/PARAMETRIC/PARAMETRIC.py +++ b/src/PARAMETRIC/PARAMETRIC.py @@ -37,7 +37,34 @@ from salome.kernel import termcolor logger = Logger("PARAMETRIC", color = termcolor.BLUE) logger.setLevel(logging.DEBUG) -from salome.parametric import ParametricStudyEditor +from salome.parametric import ParametricStudyEditor, ParametricStudy + +start_script = """ +from salome.kernel.parametric.pyscript_utils import \ + create_input_dict, create_normal_parametric_output, create_error_parametric_output + +try: + globals().update(create_input_dict({}, paramInput)) + + ### Start of user code + +""" + +end_script = """ + ### End of user code + + output_dict = {} + for output_var in paramInput["outputVarList"]: + if globals().has_key(output_var): + output_dict[output_var] = globals()[output_var] + else: + raise Exception("User Python script has not created variable %s" % output_var) + + paramOutput = create_normal_parametric_output(output_dict, paramInput) + +except Exception, exc: + paramOutput = create_error_parametric_output(str(exc)) +""" class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOME_DriverPy_i): @@ -98,36 +125,49 @@ class PARAMETRIC(PARAMETRIC_ORB__POA.PARAMETRIC_Gen, SALOME_ComponentPy_i, SALOM param_input_tc = runtime.getTypeCode("SALOME_TYPES/ParametricInput") if param_input_tc is None: raise Exception ("Internal error: No typecode found for type 'SALOME_TYPES/ParametricInput'") + param_output_tc = runtime.getTypeCode("SALOME_TYPES/ParametricOutput") + if param_output_tc is None: + raise Exception ("Internal error: No typecode found for type 'SALOME_TYPES/ParametricOutput'") foreach = pilot.ForEachLoop("ForEach", param_input_tc) foreach.edGetNbOfBranchesPort().edInit(param_study.nb_parallel_computations) proc.edAddChild(foreach) - - solver_code = param_study.salome_component_name - solver_compo_inst = proc.createComponentInstance(solver_code) - solver_compo_def = self.session_catalog._componentMap[solver_code] distrib_container = proc.createContainer("DistribContainer") distrib_container.setProperty("hostname", "localhost") - solver_compo_inst.setContainer(distrib_container) - - init_solver = solver_compo_def._serviceMap["Init"].clone(None) - init_solver.setComponent(solver_compo_inst) - init_solver.getInputPort("studyID").edInit(studyId) - entry = self._parse_entry(param_study.solver_case_entry) - init_solver.getInputPort("detCaseEntry").edInit(entry) - foreach.edSetInitNode(init_solver) - exec_solver = solver_compo_def._serviceMap["Exec"].clone(None) - exec_solver.setComponent(solver_compo_inst) - foreach.edSetNode(exec_solver) - - finalize_solver = solver_compo_def._serviceMap["Finalize"].clone(None) - finalize_solver.setComponent(solver_compo_inst) - foreach.edSetFinalizeNode(finalize_solver) + if param_study.solver_code_type == ParametricStudy.SALOME_COMPONENT: + solver_code = param_study.salome_component_name + solver_compo_inst = proc.createComponentInstance(solver_code) + solver_compo_def = self.session_catalog._componentMap[solver_code] + solver_compo_inst.setContainer(distrib_container) + + init_solver = solver_compo_def._serviceMap["Init"].clone(None) + init_solver.setComponent(solver_compo_inst) + init_solver.getInputPort("studyID").edInit(studyId) + entry = self._parse_entry(param_study.solver_case_entry) + init_solver.getInputPort("detCaseEntry").edInit(entry) + foreach.edSetInitNode(init_solver) + + exec_solver = solver_compo_def._serviceMap["Exec"].clone(None) + exec_solver.setComponent(solver_compo_inst) + foreach.edSetNode(exec_solver) + + finalize_solver = solver_compo_def._serviceMap["Finalize"].clone(None) + finalize_solver.setComponent(solver_compo_inst) + foreach.edSetFinalizeNode(finalize_solver) + else: + exec_solver = runtime.createScriptNode(SALOMERuntime.PythonNode.KIND, "Exec") + exec_solver.edAddInputPort("paramInput", param_input_tc) + exec_solver.edAddOutputPort("paramOutput", param_output_tc) + indented_user_script = "" + for line in param_study.python_script.splitlines(): + indented_user_script += " " + line + "\n" + exec_script = start_script + indented_user_script + end_script + exec_solver.setScript(exec_script) + exec_solver.setExecutionMode("remote") + exec_solver.setContainer(distrib_container) + foreach.edSetNode(exec_solver) - param_output_tc = runtime.getTypeCode("SALOME_TYPES/ParametricOutput") - if param_output_tc is None: - raise Exception ("Internal error: No typecode found for type 'SALOME_TYPES/ParametricOutput'") seq_param_output_tc = proc.createSequenceTc("", "seq_param_output", param_output_tc) aggregator = runtime.createScriptNode(SALOMERuntime.PythonNode.KIND, "Aggregator") aggregator_input_port = aggregator.edAddInputPort("results", seq_param_output_tc) diff --git a/src/salome/parametric/gui/execparams.ui b/src/salome/parametric/gui/execparams.ui index 4a47bcc..18361c1 100644 --- a/src/salome/parametric/gui/execparams.ui +++ b/src/salome/parametric/gui/execparams.ui @@ -157,7 +157,13 @@ - + + + + DejaVu Sans Mono + + + -- 2.39.2