]> SALOME platform Git repositories - modules/parametric.git/commitdiff
Salome HOME
Execution of schemas with user Python script
authorRenaud Barate <renaud.barate@edf.fr>
Mon, 3 Dec 2012 09:59:27 +0000 (10:59 +0100)
committerRenaud Barate <renaud.barate@edf.fr>
Mon, 3 Dec 2012 09:59:27 +0000 (10:59 +0100)
src/PARAMETRIC/PARAMETRIC.py
src/salome/parametric/gui/execparams.ui

index 7ad2c812c1ed3ba7f41de49659d3dcc5ec899f3c..52d3ba6769ff385e1f1435673dec21d9a78c9891 100644 (file)
@@ -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)
index 4a47bcc77843fa3b2d9943acf0c94ff65177fc44..18361c1c147a193897ea6a4af1e6292d72067099 100644 (file)
            </widget>
           </item>
           <item>
-           <widget class="QTextEdit" name="pythonScriptTE"/>
+           <widget class="QTextEdit" name="pythonScriptTE">
+            <property name="font">
+             <font>
+              <family>DejaVu Sans Mono</family>
+             </font>
+            </property>
+           </widget>
           </item>
          </layout>
         </item>