]> SALOME platform Git repositories - modules/adao.git/commitdiff
Salome HOME
Generation of a schema with observers in progress
authorAndré Ribes <andre.ribes@edf.fr>
Tue, 6 Mar 2012 10:15:58 +0000 (11:15 +0100)
committerAndré Ribes <andre.ribes@edf.fr>
Tue, 6 Mar 2012 10:15:58 +0000 (11:15 +0100)
resources/ADAOSchemaCatalog.xml
src/daEficas/generator_adao.py
src/daSalome/daYacsSchemaCreator/methods.py

index 6ce262294c910e5842629de1ae1a65887ab7ca22..7d645188bb9502fd5419010409c9d8bd4c9882c4 100644 (file)
@@ -325,4 +325,74 @@ user_script_module = sys.modules[module_name]
     <outport name="init_data" type="pyobj"/>
   </inline>
 
+  <inline name="ReadForSwitchNode">
+    <script><code><![CDATA[
+print "Entering in ReadForSwitch"
+switch_value = -1
+for param in data["specificParameters"]:
+  if param["name"] == "switch_value":
+    switch_value = int(param["value"])
+]]></code></script>
+    <inport name="data" type="SALOME_TYPES/ParametricInput"/>
+    <outport name="data" type="SALOME_TYPES/ParametricInput"/>
+    <outport name="switch_value" type="int"/>
+  </inline>
+
+  <inline name="ExtractDataNode">
+    <script><code><![CDATA[
+import pickle
+print "Entering in ExtractData"
+var = None
+info = None
+for param in data["specificParameters"]:
+  if param["name"] == "var":
+    var = pickle.loads(param["value"])
+  if param["name"] == "info":
+    info = pickle.loads(param["value"])
+]]></code></script>
+    <inport name="data" type="SALOME_TYPES/ParametricInput"/>
+    <outport name="var" type="pyobj"/>
+    <outport name="info" type="pyobj"/>
+  </inline>
+
+  <inline name="ObservationNodeString">
+    <script><code><![CDATA[
+print "Entering in Observation"
+
+]]></code></script>
+    <inport name="var" type="pyobj"/>
+    <inport name="info" type="pyobj"/>
+  </inline>
+
+  <inline name="ObservationNodeFile">
+    <script><code><![CDATA[
+print "Entering in Observation"
+# Get file path and filename
+import sys
+import os
+filepath = os.path.dirname(script)
+filename = os.path.basename(script)
+module_name = os.path.splitext(filename)[0]
+sys.path.insert(0,filepath)
+# Import script
+__import__(module_name)
+
+]]></code></script>
+    <inport name="var"    type="pyobj"/>
+    <inport name="info"   type="pyobj"/>
+    <inport name="script" type="string"/>
+  </inline>
+
+  <inline name="EndObservationNode">
+    <script><code><![CDATA[
+# Create a fake output object.
+# An observer is always successful.
+output = {}
+output["outputValues"]        = [[[[]]]]
+output["specificOutputInfos"] = []
+output["returnCode"]          = 0
+output["errorMessage"]        = ""
+]]></code></script>
+    <outport name="output" type="SALOME_TYPES/ParametricOutput"/>
+  </inline>
 </proc>
index 89ebb7e1e481303536ba8a78dc9b9edd8af8967e..14df65125e497955d7a000f788463032bbcdcfbf 100644 (file)
@@ -268,9 +268,12 @@ class AdaoGenerator(PythonGenerator):
         self.add_observer_in_dict(observer, observers)
 
     # Write observers in the python command file
+    number = 1
     self.text_da += "observers = {}\n"
     for observer in observers.keys():
+      number += 1
       self.text_da += "observers[\"" + observer + "\"] = {}\n"
+      self.text_da += "observers[\"" + observer + "\"][\"number\"] = " + str(number) + "\n"
       self.text_da += "observers[\"" + observer + "\"][\"nodetype\"] = \"" + observers[observer]["nodetype"] + "\"\n"
       if observers[observer]["nodetype"] == "pyscript":
         self.text_da += "observers[\"" + observer + "\"][\"pyscript\"] = \"\"\"" + observers[observer]["script"] + "\"\"\"\n"
index 585daae764533ed9109b044df289ffb1ea9e9304..4c672c75967b98810c88a8c41eb23a22395d49df 100644 (file)
@@ -238,83 +238,131 @@ def create_yacs_proc(study_config):
              CAS_node.getInputPort(port_name).edInitPy(FunctionDict["Script"][FunctionName])
 
   # Step 3: create compute bloc
-  if "Observers" in study_config.keys():
-    # Todo
+  compute_bloc = runtime.createBloc("compute_bloc")
+  proc.edAddChild(compute_bloc)
+  proc.edAddCFLink(CAS_node, compute_bloc)
+  # We use an optimizer loop
+  name = "Execute" + study_config["Algorithm"]
+  algLib = "daYacsIntegration.py"
+  factoryName = "AssimilationAlgorithm_asynch"
+  optimizer_node = runtime.createOptimizerLoop(name, algLib, factoryName, "")
+  compute_bloc.edAddChild(optimizer_node)
+  proc.edAddDFLink(CAS_node.getOutputPort("Study"), optimizer_node.edGetAlgoInitPort())
+  # Check if we have a python script for OptimizerLoopNode
+  data_config = study_config["ObservationOperator"]
+  opt_script_node = None
+  if data_config["Type"] == "Function" and data_config["From"] == "FunctionDict":
+    # Get script
+    FunctionDict = data_config["Data"]
+    script_filename = ""
+    for FunctionName in FunctionDict["Function"]:
+      # We currently support only one file
+      script_filename = FunctionDict["Script"][FunctionName]
+      break
+
+    # We create a new pyscript node
+    opt_script_node = runtime.createScriptNode("", "FunctionNode")
+    if repertory:
+      script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
+    try:
+      script_str= open(script_filename, 'r')
+    except:
+      logging.fatal("Exception in opening function script file : " + script_filename)
+      traceback.print_exc()
+      sys.exit(1)
+    node_script  = "#-*-coding:iso-8859-1-*-\n"
+    node_script += "import sys, os \n"
+    if base_repertory != "":
+      node_script += "filepath = \"" + base_repertory + "\"\n"
+    else:
+      node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
+    node_script += "sys.path.insert(0,os.path.dirname(filepath))\n"
+    node_script += script_str.read()
+    opt_script_node.setScript(node_script)
+    opt_script_node.edAddInputPort("computation", t_param_input)
+    opt_script_node.edAddOutputPort("result", t_param_output)
   else:
-    compute_bloc = runtime.createBloc("compute_bloc")
-    proc.edAddChild(compute_bloc)
-    proc.edAddCFLink(CAS_node, compute_bloc)
-
-    if AlgoType[study_config["Algorithm"]] == "Optim":
-      # We use an optimizer loop
-      name = "Execute" + study_config["Algorithm"]
-      algLib = "daYacsIntegration.py"
-      factoryName = "AssimilationAlgorithm_asynch"
-      optimizer_node = runtime.createOptimizerLoop(name, algLib, factoryName, "")
-      compute_bloc.edAddChild(optimizer_node)
-      proc.edAddDFLink(CAS_node.getOutputPort("Study"), optimizer_node.edGetAlgoInitPort())
-
-      # Check if we have a python script for OptimizerLoopNode
-      data_config = study_config["ObservationOperator"]
-      if data_config["Type"] == "Function" and data_config["From"] == "FunctionDict":
-        # Get script
-        FunctionDict = data_config["Data"]
-        script_filename = ""
-        for FunctionName in FunctionDict["Function"]:
-          # We currently support only one file
-          script_filename = FunctionDict["Script"][FunctionName]
-          break
-
-        # We create a new pyscript node
-        opt_script_node = runtime.createScriptNode("", "FunctionNode")
-        if repertory:
-          script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
-        try:
-          script_str= open(script_filename, 'r')
-        except:
-          logging.fatal("Exception in opening function script file : " + script_filename)
-          traceback.print_exc()
-          sys.exit(1)
-        node_script  = "#-*-coding:iso-8859-1-*-\n"
-        node_script += "import sys, os \n"
-        if base_repertory != "":
-          node_script += "filepath = \"" + base_repertory + "\"\n"
-        else:
-          node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
-        node_script += "sys.path.insert(0,os.path.dirname(filepath))\n"
-        node_script += script_str.read()
-        opt_script_node.setScript(node_script)
-        opt_script_node.edAddInputPort("computation", t_param_input)
-        opt_script_node.edAddOutputPort("result", t_param_output)
-
-        # Add it
-        computation_bloc = runtime.createBloc("computation_bloc")
-        optimizer_node.edSetNode(computation_bloc)
-        computation_bloc.edAddChild(opt_script_node)
-
-        # We connect Optimizer with the script
-        proc.edAddDFLink(optimizer_node.edGetSamplePort(), opt_script_node.getInputPort("computation"))
-        proc.edAddDFLink(opt_script_node.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
+    factory_opt_script_node = catalogAd.getNodeFromNodeMap("FakeOptimizerLoopNode")
+    opt_script_node = factory_opt_script_node.cloneNode("FakeFunctionNode")
 
-        # Connect node with InitUserData
-        if "ObservationOperator" in init_config["Target"]:
-          opt_node_script = opt_script_node.getScript()
-          opt_node_script = "__builtins__[\"init_data\"] = init_data\n" + opt_node_script
-          opt_script_node.setScript(opt_node_script)
-          opt_script_node.edAddInputPort("init_data", t_pyobj)
-          proc.edAddDFLink(init_node.getOutputPort("init_data"), opt_script_node.getInputPort("init_data"))
+  # Add computation bloc
+  if "Observers" in study_config.keys():
+    execution_bloc = runtime.createBloc("Execution Bloc")
+    optimizer_node.edSetNode(execution_bloc)
+
+    # Add a node that permits to configure the switch
+    factory_read_for_switch_node = catalogAd.getNodeFromNodeMap("ReadForSwitchNode")
+    read_for_switch_node = factory_read_for_switch_node.cloneNode("ReadForSwitch")
+    execution_bloc.edAddChild(read_for_switch_node)
+    proc.edAddDFLink(optimizer_node.edGetSamplePort(), read_for_switch_node.getInputPort("data"))
+
+    # Add a switch
+    switch_node = runtime.createSwitch("Execution Switch")
+    execution_bloc.edAddChild(switch_node)
+    # Connect switch
+    proc.edAddDFLink(read_for_switch_node.getOutputPort("switch_value"), switch_node.edGetConditionPort())
+
+    # First case: always computation bloc
+    computation_bloc = runtime.createBloc("computation_bloc")
+    computation_bloc.edAddChild(opt_script_node)
+    switch_node.edSetNode(1, computation_bloc)
+
+    # We connect Optimizer with the script
+    proc.edAddDFLink(read_for_switch_node.getOutputPort("data"), opt_script_node.getInputPort("computation"))
+    proc.edAddDFLink(opt_script_node.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
+
+
+    # For each observer add a new bloc in the switch
+    observer_config = study_config["Observers"]
+    for observer_name in observer_config:
+      observer_cfg = observer_config[observer_name]
+      observer_bloc = runtime.createBloc("Observer %s" % observer_name)
+      switch_node.edSetNode(observer_cfg["number"], observer_bloc)
+
+      factory_extract_data_node = catalogAd.getNodeFromNodeMap("ExtractDataNode")
+      extract_data_node = factory_extract_data_node.cloneNode("ExtractData")
+      observer_bloc.edAddChild(extract_data_node)
+      proc.edAddDFLink(read_for_switch_node.getOutputPort("data"), extract_data_node.getInputPort("data"))
+
+      observation_node = None
+      if observer_cfg["nodetype"] == "pyscript":
+        factory_observation_node = catalogAd.getNodeFromNodeMap("ObservationNodeString")
+        observation_node = factory_observation_node.cloneNode("Observation")
+        node_script = observation_node.getScript()
+        node_script += observer_cfg["pyscript"]
+        observation_node.setScript(node_script)
       else:
-        factory_opt_script_node = catalogAd.getNodeFromNodeMap("FakeOptimizerLoopNode")
-        opt_script_node = factory_opt_script_node.cloneNode("FakeFunctionNode")
-
-        # Add it
-        computation_bloc = runtime.createBloc("computation_bloc")
-        optimizer_node.edSetNode(computation_bloc)
-        computation_bloc.edAddChild(opt_script_node)
-
-        # We connect Optimizer with the script
-        proc.edAddDFLink(optimizer_node.edGetSamplePort(), opt_script_node.getInputPort("computation"))
-        proc.edAddDFLink(opt_script_node.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
+        factory_observation_node = catalogAd.getNodeFromNodeMap("ObservationNodeFile")
+        observation_node = factory_observation_node.cloneNode("Observation")
+        if repertory:
+          observation_node.getInputPort("script").edInitPy(os.path.join(base_repertory, os.path.basename(observer_cfg["userfile"])))
+        else:
+          observation_node.getInputPort("script").edInitPy(observer_cfg["userfile"])
+      observer_bloc.edAddChild(observation_node)
+      proc.edAddDFLink(extract_data_node.getOutputPort("var"), observation_node.getInputPort("var"))
+      proc.edAddDFLink(extract_data_node.getOutputPort("info"), observation_node.getInputPort("info"))
+
+      factory_end_observation_node = catalogAd.getNodeFromNodeMap("EndObservationNode")
+      end_observation_node = factory_end_observation_node.cloneNode("EndObservation")
+      observer_bloc.edAddChild(end_observation_node)
+      proc.edAddCFLink(observation_node, end_observation_node)
+      proc.edAddDFLink(end_observation_node.getOutputPort("output"), optimizer_node.edGetPortForOutPool())
+  else:
+    computation_bloc = runtime.createBloc("computation_bloc")
+    optimizer_node.edSetNode(computation_bloc)
+    computation_bloc.edAddChild(opt_script_node)
+
+    # We connect Optimizer with the script
+    proc.edAddDFLink(optimizer_node.edGetSamplePort(), opt_script_node.getInputPort("computation"))
+    proc.edAddDFLink(opt_script_node.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
+
+  # Connect node with InitUserData
+  if "ObservationOperator" in init_config["Target"]:
+    opt_node_script = opt_script_node.getScript()
+    opt_node_script = "__builtins__[\"init_data\"] = init_data\n" + opt_node_script
+    opt_script_node.setScript(opt_node_script)
+    opt_script_node.edAddInputPort("init_data", t_pyobj)
+    proc.edAddDFLink(init_node.getOutputPort("init_data"), opt_script_node.getInputPort("init_data"))
 
   # Step 4: create post-processing from user configuration
   if "UserPostAnalysis" in study_config.keys():