]> SALOME platform Git repositories - modules/adao.git/commitdiff
Salome HOME
Observers in progress
authorAndré Ribes <andre.ribes@edf.fr>
Mon, 5 Mar 2012 08:59:35 +0000 (09:59 +0100)
committerAndré Ribes <andre.ribes@edf.fr>
Mon, 5 Mar 2012 08:59:35 +0000 (09:59 +0100)
src/daEficas/generator_adao.py
src/daSalome/daYacsSchemaCreator/help_methods.py
src/daSalome/daYacsSchemaCreator/methods.py

index 7c289da4a409cd1a01a64bcde2e3630d5e7f909b..89ebb7e1e481303536ba8a78dc9b9edd8af8967e 100644 (file)
@@ -273,11 +273,12 @@ class AdaoGenerator(PythonGenerator):
       self.text_da += "observers[\"" + observer + "\"] = {}\n"
       self.text_da += "observers[\"" + observer + "\"][\"nodetype\"] = \"" + observers[observer]["nodetype"] + "\"\n"
       if observers[observer]["nodetype"] == "pyscript":
-        self.text_da += "observers[\"" + observer + "\"][\"script\"] = \"\"\"" + observers[observer]["script"] + "\"\"\"\n"
+        self.text_da += "observers[\"" + observer + "\"][\"pyscript\"] = \"\"\"" + observers[observer]["script"] + "\"\"\"\n"
       else:
-        self.text_da += "observers[\"" + observer + "\"][\"file\"] = \"" + observers[observer]["file"] + "\"\n"
+        self.text_da += "observers[\"" + observer + "\"][\"userfile\"] = \"" + observers[observer]["file"] + "\"\n"
       if "scheduler" in observers[observer].keys():
         self.text_da += "observers[\"" + observer + "\"][\"scheduler\"] = \"\"\"" + observers[observer]["scheduler"] + "\"\"\"\n"
+    self.text_da += "study_config['Observers'] = observers\n"
 
   def add_observer_in_dict(self, observer, observers):
     """
index f6faa645a0583c4f8582e5bd841c83babce2b1c0..9879ca6490fe0ed0a67b75887f71cdecd975e6ef 100644 (file)
@@ -119,6 +119,51 @@ def check_study(study_config):
         logging.fatal("File is %s" % check_file_name)
         sys.exit(1)
 
+  # Check observers
+  if "Observers" in study_config.keys():
+    for obs_var in study_config["Observers"]:
+      # Check du type
+      if not isinstance(study_config["Observers"][obs_var], type({})):
+        logging.fatal("An observer description has to be a Python dictionary")
+        logging.fatal("Observer is %s" % obs_var)
+        sys.exit(1)
+      if "nodetype" not in study_config["Observers"][obs_var].keys():
+        logging.fatal("An observer description must provide a nodetype")
+        logging.fatal("Observer is %s" % obs_var)
+        sys.exit(1)
+      nodetype = study_config["Observers"][obs_var]["nodetype"]
+      if not isinstance(study_config["Observers"][obs_var]["nodetype"], type("")):
+        logging.fatal("An observer nodetype description must be a string")
+        logging.fatal("Observer is %s" % obs_var)
+        sys.exit(1)
+      if nodetype != "pyscript" and nodetype != "userfile":
+        logging.fatal("An observer nodetype must be equal to 'pyscript' or 'userfile'")
+        logging.fatal("Observer is %s" % obs_var)
+        sys.exit(1)
+      if nodetype == "pyscript":
+        if "pyscript" not in study_config["Observers"][obs_var].keys():
+          logging.fatal("An observer with nodetype pyscript must provide a pyscript")
+          logging.fatal("Observer is %s" % obs_var)
+          sys.exit(1)
+        if not isinstance(study_config["Observers"][obs_var]["pyscript"], type("")):
+          logging.fatal("An observer pyscript description must be a string")
+          logging.fatal("Observer is %s" % obs_var)
+          sys.exit(1)
+      if nodetype == "userfile":
+        if "userfile" not in study_config["Observers"][obs_var].keys():
+          logging.fatal("An observer with nodetype userfileuserfilemust provide a pyscript")
+          logging.fatal("Observer is %s" % obs_var)
+          sys.exit(1)
+        if not isinstance(study_config["Observers"][obs_var]["userfile"], type("")):
+          logging.fatal("An observer userfile description must be a string")
+          logging.fatal("Observer is %s" % obs_var)
+          sys.exit(1)
+      if "scheduler" in study_config["Observers"][obs_var].keys():
+        if not isinstance(study_config["Observers"][obs_var]["scheduler"], type("")):
+          logging.fatal("An observer scheduler description must be a string")
+          logging.fatal("Observer is %s" % obs_var)
+          sys.exit(1)
+
 def check_variables(name, study_config):
 
   if name not in study_config.keys():
index 3c845248524a5ff8e30fdf0816495c8ab8ddb0ac..585daae764533ed9109b044df289ffb1ea9e9304 100644 (file)
@@ -238,80 +238,83 @@ def create_yacs_proc(study_config):
              CAS_node.getInputPort(port_name).edInitPy(FunctionDict["Script"][FunctionName])
 
   # Step 3: create compute bloc
-  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())
+  if "Observers" in study_config.keys():
+    # Todo
+  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())
 
-      # 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"))
-    else:
-      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"))
+      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)
+        # 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())
+        # 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())
 
   # Step 4: create post-processing from user configuration
   if "UserPostAnalysis" in study_config.keys():