From 141d16d7d25d2b0833e05691bfe830ca7bb22fbb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20Ribes?= Date: Tue, 6 Mar 2012 11:15:58 +0100 Subject: [PATCH] Generation of a schema with observers in progress --- resources/ADAOSchemaCatalog.xml | 70 +++++++ src/daEficas/generator_adao.py | 3 + src/daSalome/daYacsSchemaCreator/methods.py | 196 ++++++++++++-------- 3 files changed, 195 insertions(+), 74 deletions(-) diff --git a/resources/ADAOSchemaCatalog.xml b/resources/ADAOSchemaCatalog.xml index 6ce2622..7d64518 100644 --- a/resources/ADAOSchemaCatalog.xml +++ b/resources/ADAOSchemaCatalog.xml @@ -325,4 +325,74 @@ user_script_module = sys.modules[module_name] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/daEficas/generator_adao.py b/src/daEficas/generator_adao.py index 89ebb7e..14df651 100644 --- a/src/daEficas/generator_adao.py +++ b/src/daEficas/generator_adao.py @@ -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" diff --git a/src/daSalome/daYacsSchemaCreator/methods.py b/src/daSalome/daYacsSchemaCreator/methods.py index 585daae..4c672c7 100644 --- a/src/daSalome/daYacsSchemaCreator/methods.py +++ b/src/daSalome/daYacsSchemaCreator/methods.py @@ -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(): -- 2.39.2