]> SALOME platform Git repositories - modules/adao.git/blob - src/daSalome/daYacsSchemaCreator/methods.py
Salome HOME
Simplifying python files installation scheme
[modules/adao.git] / src / daSalome / daYacsSchemaCreator / methods.py
1 #-*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2018 EDF R&D
4 #
5 # This file is part of SALOME ADAO module
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23 # Author: Andre Ribes, andre.ribes@edf.fr, EDF R&D
24
25 import sys
26 import traceback
27 import logging
28 import pilot
29 import loader
30 import SALOMERuntime
31 import os
32
33 from daYacsSchemaCreator.infos_daComposant import *
34
35 def _Internal_Add_dir_script_ports(node, sf, ed, br, t_type):
36   # On conserve le pointeur de "node" et "t_type"
37   __scriptfile = str( sf )
38   __exist_dir  = bool( ed )
39   __base_dir   = str( br )
40   __full_name  = os.path.join(__base_dir, os.path.basename(__scriptfile))
41   if os.path.exists(__full_name):
42     node.getInputPort("script").edInitPy(__full_name)
43   else:
44     node.getInputPort("script").edInitPy(__scriptfile)
45   if __exist_dir:
46     node.edAddInputPort("studydir", t_type)
47     node.getInputPort("studydir").edInitPy(__base_dir)
48
49 def create_yacs_proc(study_config):
50
51   logging.debug("[create_yacs_proc]")
52
53   # Init part
54   SALOMERuntime.RuntimeSALOME_setRuntime()
55   l = loader.YACSLoader()
56   l.registerProcCataLoader()
57   runtime = pilot.getRuntime()
58   try:
59     catalogAd = runtime.loadCatalog("proc", os.environ["ADAO_ROOT_DIR"] + "/share/salome/resources/adao/ADAOSchemaCatalog.xml")
60     runtime.addCatalog(catalogAd)
61   except:
62     raise ValueError("Exception in loading ADAO YACS Schema catalog")
63
64   # Starting creating proc
65   proc = runtime.createProc("proc")
66   proc.setTypeCode("pyobj", runtime.getTypeCode("pyobj"))
67   proc.setTypeCode("SALOME_TYPES/ParametricInput", catalogAd._typeMap["SALOME_TYPES/ParametricInput"])
68   proc.setTypeCode("SALOME_TYPES/ParametricOutput", catalogAd._typeMap["SALOME_TYPES/ParametricOutput"])
69   t_pyobj  = proc.getTypeCode("pyobj")
70   t_string = proc.getTypeCode("string")
71   t_bool = proc.getTypeCode("bool")
72   t_param_input  = proc.getTypeCode("SALOME_TYPES/ParametricInput")
73   t_param_output = proc.getTypeCode("SALOME_TYPES/ParametricOutput")
74   if "Repertory" in list(study_config.keys()):
75     base_repertory = study_config["Repertory"]
76     repertory      = True
77   else:
78     base_repertory = ""
79     repertory = False
80   if UseYACSContainer:
81     mycontainer = proc.createContainer("AdaoContainer")
82
83   # Create ADAO case bloc
84   ADAO_Case = runtime.createBloc("ADAO_Case_Bloc")
85   proc.edAddChild(ADAO_Case)
86
87   # Step 0: create AssimilationStudyObject
88   factory_CAS_node = catalogAd.getNodeFromNodeMap("CreateAssimilationStudy")
89   CAS_node = factory_CAS_node.cloneNode("CreateAssimilationStudy")
90   CAS_node.getInputPort("Name").edInitPy(study_config["Name"])
91   CAS_node.getInputPort("Algorithm").edInitPy(study_config["Algorithm"])
92   if "Debug" in study_config and study_config["Debug"] == "1":
93     CAS_node.getInputPort("Debug").edInitPy(True)
94   else:
95     CAS_node.getInputPort("Debug").edInitPy(False)
96
97   # Ajout des Variables
98   InputVariablesNames = []
99   InputVariablesSizes = []
100   for var in study_config["InputVariables"]["Order"]:
101     InputVariablesNames.append(var)
102     InputVariablesSizes.append(int(study_config["InputVariables"][var]))
103   CAS_node.getInputPort("InputVariablesNames").edInitPy(InputVariablesNames)
104   CAS_node.getInputPort("InputVariablesSizes").edInitPy(InputVariablesSizes)
105   OutputVariablesNames = []
106   OutputVariablesSizes = []
107   for var in study_config["OutputVariables"]["Order"]:
108     OutputVariablesNames.append(var)
109     OutputVariablesSizes.append(int(study_config["OutputVariables"][var]))
110   CAS_node.getInputPort("OutputVariablesNames").edInitPy(OutputVariablesNames)
111   CAS_node.getInputPort("OutputVariablesSizes").edInitPy(OutputVariablesSizes)
112
113   ADAO_Case.edAddChild(CAS_node)
114
115   # Adding an observer init node if an user defines some
116   factory_init_observers_node = catalogAd.getNodeFromNodeMap("SetObserversNode")
117   init_observers_node = factory_init_observers_node.cloneNode("SetObservers")
118   if "Observers" in list(study_config.keys()):
119     node_script = init_observers_node.getScript()
120     node_script += "has_observers = True\n"
121     node_script += "observers = " + str(study_config["Observers"]) + "\n"
122     init_observers_node.setScript(node_script)
123     ADAO_Case.edAddChild(init_observers_node)
124     ADAO_Case.edAddDFLink(init_observers_node.getOutputPort("has_observers"), CAS_node.getInputPort("has_observers"))
125     ADAO_Case.edAddDFLink(init_observers_node.getOutputPort("observers"), CAS_node.getInputPort("observers"))
126   else:
127     node_script = init_observers_node.getScript()
128     node_script += "has_observers = False\n"
129     node_script += "observers = \"\"\n"
130     init_observers_node.setScript(node_script)
131     ADAO_Case.edAddChild(init_observers_node)
132     ADAO_Case.edAddDFLink(init_observers_node.getOutputPort("has_observers"), CAS_node.getInputPort("has_observers"))
133     ADAO_Case.edAddDFLink(init_observers_node.getOutputPort("observers"), CAS_node.getInputPort("observers"))
134
135   # Step 0.5: Find if there is a user init node
136   init_config = {}
137   init_config["Target"] = []
138   if "UserDataInit" in list(study_config.keys()):
139     init_config = study_config["UserDataInit"]
140     factory_init_node = catalogAd.getNodeFromNodeMap("UserDataInitFromScript")
141     init_node = factory_init_node.cloneNode("UserDataInit")
142     _Internal_Add_dir_script_ports( init_node, init_config["Data"], repertory, base_repertory, t_string)
143     init_node_script = init_node.getScript()
144     init_node_script += "# Import script and get data\n__import__(module_name)\nuser_script_module = sys.modules[module_name]\n\n"
145     init_node_script += "init_data = user_script_module.init_data\n"
146     init_node.setScript(init_node_script)
147     ADAO_Case.edAddChild(init_node)
148
149   # Step 1: get input data from user configuration
150
151   st_keys = sorted(list(study_config.keys()))
152   for key in st_keys:
153     ad_keys = sorted(AssimData)
154     if key in ad_keys:
155       data_config = study_config[key]
156
157       key_type = key + "Type"
158       key_stored = key + "Stored"
159
160       if data_config["Type"] == "Dict" and data_config["From"] == "Script":
161         # Create node
162         factory_back_node = catalogAd.getNodeFromNodeMap("CreateDictFromScript")
163         back_node = factory_back_node.cloneNode("Get" + key)
164         _Internal_Add_dir_script_ports( back_node, data_config["Data"], repertory, base_repertory, t_string)
165         back_node.edAddOutputPort(key, t_pyobj)
166         ADAO_Case.edAddChild(back_node)
167         # Set content of the node
168         back_node_script = back_node.getScript()
169         if key in init_config["Target"]:
170           # Connect node with InitUserData
171           back_node_script += "__builtins__[\"init_data\"] = init_data\n"
172           back_node.edAddInputPort("init_data", t_pyobj)
173           ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
174         back_node_script += "# Import script and get data\n__import__(module_name)\nuser_script_module = sys.modules[module_name]\n\n"
175         if key == "AlgorithmParameters":
176             back_node_script += "if hasattr(user_script_module,'" + key + "'):\n  "
177             back_node_script += key + " = user_script_module." + key + "\n"
178             skey = "Parameters"
179             back_node_script += "elif hasattr(user_script_module,'" + skey + "'):\n  "
180             back_node_script += key + " = user_script_module." + skey + "\n"
181         else:
182             back_node_script += key + " = user_script_module." + key + "\n"
183         back_node.setScript(back_node_script)
184         # Connect node with CreateAssimilationStudy
185         CAS_node.edAddInputPort(key, t_pyobj)
186         ADAO_Case.edAddDFLink(back_node.getOutputPort(key), CAS_node.getInputPort(key))
187
188       if data_config["Type"] == "Dict" and data_config["From"] == "String":
189         # Create node
190         factory_back_node = catalogAd.getNodeFromNodeMap("CreateDictFromString")
191         back_node = factory_back_node.cloneNode("Get" + key)
192         back_node.getInputPort("dict_in_string").edInitPy(data_config["Data"])
193         back_node.edAddOutputPort(key, t_pyobj)
194         ADAO_Case.edAddChild(back_node)
195         # Set content of the node
196         back_node_script = back_node.getScript()
197         if key in init_config["Target"]:
198           # Connect node with InitUserData
199           back_node_script += "__builtins__[\"init_data\"] = init_data\n"
200           back_node.edAddInputPort("init_data", t_pyobj)
201           ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
202         back_node_script += key + " = dict(dico)\n"
203         back_node_script += "logging.debug(\"Dict is %ss\"%s%s)"%("%","%",key)
204         back_node.setScript(back_node_script)
205         # Connect node with CreateAssimilationStudy
206         CAS_node.edAddInputPort(key, t_pyobj)
207         ADAO_Case.edAddDFLink(back_node.getOutputPort(key), CAS_node.getInputPort(key))
208
209       if data_config["Type"] == "Vector" and data_config["From"] == "String":
210         # Create node
211         factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpyVectorFromString")
212         back_node = factory_back_node.cloneNode("Get" + key)
213         back_node.getInputPort("vector_in_string").edInitPy(data_config["Data"])
214         ADAO_Case.edAddChild(back_node)
215         # Set content of the node
216         back_node_script = back_node.getScript()
217         if "Stored" in data_config:
218             back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
219         else:
220             back_node_script += "stored = 0\n"
221         if key in init_config["Target"]:
222           # Connect node with InitUserData
223           back_node_script += "__builtins__[\"init_data\"] = init_data\n"
224           back_node.edAddInputPort("init_data", t_pyobj)
225           ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
226         back_node.setScript(back_node_script)
227         # Connect node with CreateAssimilationStudy
228         CAS_node.edAddInputPort(key, t_pyobj)
229         CAS_node.edAddInputPort(key_type, t_string)
230         CAS_node.edAddInputPort(key_stored, t_bool)
231         ADAO_Case.edAddDFLink(back_node.getOutputPort("vector"), CAS_node.getInputPort(key))
232         ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
233         ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
234
235       if data_config["Type"] == "Vector" and data_config["From"] == "Script":
236         # Create node
237         factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpyVectorFromScript")
238         back_node = factory_back_node.cloneNode("Get" + key)
239         _Internal_Add_dir_script_ports( back_node, data_config["Data"], repertory, base_repertory, t_string)
240         back_node.edAddOutputPort(key, t_pyobj)
241         ADAO_Case.edAddChild(back_node)
242         # Set content of the node
243         back_node_script = back_node.getScript()
244         if key in init_config["Target"]:
245           # Connect node with InitUserData
246           back_node_script += "__builtins__[\"init_data\"] = init_data\n"
247           back_node.edAddInputPort("init_data", t_pyobj)
248           ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
249         back_node_script += "# Import script and get data\n__import__(module_name)\nuser_script_module = sys.modules[module_name]\n\n"
250         back_node_script += key + " = user_script_module." + key + "\n"
251         if "Stored" in data_config:
252             back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
253         else:
254             back_node_script += "stored = 0\n"
255         back_node.setScript(back_node_script)
256         # Connect node with CreateAssimilationStudy
257         CAS_node.edAddInputPort(key, t_pyobj)
258         CAS_node.edAddInputPort(key_type, t_string)
259         CAS_node.edAddInputPort(key_stored, t_bool)
260         ADAO_Case.edAddDFLink(back_node.getOutputPort(key), CAS_node.getInputPort(key))
261         ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
262         ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
263
264       if data_config["Type"] == "VectorSerie" and data_config["From"] == "String":
265         # Create node
266         factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpyVectorSerieFromString")
267         back_node = factory_back_node.cloneNode("Get" + key)
268         back_node.getInputPort("vector_in_string").edInitPy(data_config["Data"])
269         ADAO_Case.edAddChild(back_node)
270         # Set content of the node
271         back_node_script = back_node.getScript()
272         if "Stored" in data_config:
273             back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
274         else:
275             back_node_script += "stored = 0\n"
276         if key in init_config["Target"]:
277           # Connect node with InitUserData
278           back_node_script += "__builtins__[\"init_data\"] = init_data\n" + back_node_script
279           back_node.edAddInputPort("init_data", t_pyobj)
280           ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
281         back_node.setScript(back_node_script)
282         # Connect node with CreateAssimilationStudy
283         CAS_node.edAddInputPort(key, t_pyobj)
284         CAS_node.edAddInputPort(key_type, t_string)
285         CAS_node.edAddInputPort(key_stored, t_bool)
286         ADAO_Case.edAddDFLink(back_node.getOutputPort("vector"), CAS_node.getInputPort(key))
287         ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
288         ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
289
290       if data_config["Type"] == "VectorSerie" and data_config["From"] == "Script":
291         # Create node
292         factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpyVectorSerieFromScript")
293         back_node = factory_back_node.cloneNode("Get" + key)
294         _Internal_Add_dir_script_ports( back_node, data_config["Data"], repertory, base_repertory, t_string)
295         back_node.edAddOutputPort(key, t_pyobj)
296         ADAO_Case.edAddChild(back_node)
297         # Set content of the node
298         back_node_script = back_node.getScript()
299         if key in init_config["Target"]:
300           # Connect node with InitUserData
301           back_node_script += "__builtins__[\"init_data\"] = init_data\n"
302           back_node.edAddInputPort("init_data", t_pyobj)
303           ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
304         back_node_script += "# Import script and get data\n__import__(module_name)\nuser_script_module = sys.modules[module_name]\n\n"
305         back_node_script += key + " = user_script_module." + key + "\n"
306         if "Stored" in data_config:
307             back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
308         else:
309             back_node_script += "stored = 0\n"
310         back_node.setScript(back_node_script)
311         # Connect node with CreateAssimilationStudy
312         CAS_node.edAddInputPort(key, t_pyobj)
313         CAS_node.edAddInputPort(key_type, t_string)
314         CAS_node.edAddInputPort(key_stored, t_bool)
315         ADAO_Case.edAddDFLink(back_node.getOutputPort(key), CAS_node.getInputPort(key))
316         ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
317         ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
318
319       if data_config["Type"] in ("Matrix", "ScalarSparseMatrix", "DiagonalSparseMatrix") and data_config["From"] == "String":
320         # Create node
321         factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpy%sFromString"%(data_config["Type"],))
322         back_node = factory_back_node.cloneNode("Get" + key)
323         back_node.getInputPort("matrix_in_string").edInitPy(data_config["Data"])
324         ADAO_Case.edAddChild(back_node)
325         # Set content of the node
326         back_node_script = back_node.getScript()
327         if "Stored" in data_config:
328             back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
329         else:
330             back_node_script += "stored = 0\n"
331         if key in init_config["Target"]:
332           # Connect node with InitUserData
333           back_node_script += "__builtins__[\"init_data\"] = init_data\n"
334           back_node.edAddInputPort("init_data", t_pyobj)
335           ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
336         back_node.setScript(back_node_script)
337         # Connect node with CreateAssimilationStudy
338         CAS_node.edAddInputPort(key, t_pyobj)
339         CAS_node.edAddInputPort(key_type, t_string)
340         CAS_node.edAddInputPort(key_stored, t_bool)
341         ADAO_Case.edAddDFLink(back_node.getOutputPort("matrix"), CAS_node.getInputPort(key))
342         ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
343         ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
344
345       if data_config["Type"] in ("Matrix", "ScalarSparseMatrix", "DiagonalSparseMatrix") and data_config["From"] == "Script":
346         # Create node
347         factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpy%sFromScript"%(data_config["Type"],))
348         back_node = factory_back_node.cloneNode("Get" + key)
349         _Internal_Add_dir_script_ports( back_node, data_config["Data"], repertory, base_repertory, t_string)
350         back_node.edAddOutputPort(key, t_pyobj)
351         ADAO_Case.edAddChild(back_node)
352         # Set content of the node
353         back_node_script = back_node.getScript()
354         if "Stored" in data_config:
355             back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
356         else:
357             back_node_script += "stored = 0\n"
358         if key in init_config["Target"]:
359           # Connect node with InitUserData
360           back_node_script += "__builtins__[\"init_data\"] = init_data\n"
361           back_node.edAddInputPort("init_data", t_pyobj)
362           ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
363         back_node_script += "# Import script and get data\n__import__(module_name)\nuser_script_module = sys.modules[module_name]\n\n"
364         back_node_script += key + " = user_script_module." + key + "\n"
365         back_node.setScript(back_node_script)
366         # Connect node with CreateAssimilationStudy
367         CAS_node.edAddInputPort(key, t_pyobj)
368         CAS_node.edAddInputPort(key_type, t_string)
369         CAS_node.edAddInputPort(key_stored, t_bool)
370         ADAO_Case.edAddDFLink(back_node.getOutputPort(key), CAS_node.getInputPort(key))
371         ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
372         ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
373
374       if data_config["Type"] == "Function" and (key == "ObservationOperator" or key == "EvolutionModel"):
375          TheData = data_config["Data"]
376          for FunctionName in TheData["Function"]:
377            port_name = key + FunctionName
378            CAS_node.edAddInputPort(port_name, t_string)
379            if os.path.exists(os.path.join(base_repertory, os.path.basename(TheData["Script"][FunctionName]))):
380              CAS_node.getInputPort(port_name).edInitPy(os.path.join(base_repertory, os.path.basename(TheData["Script"][FunctionName])))
381            else:
382              CAS_node.getInputPort(port_name).edInitPy(TheData["Script"][FunctionName])
383            try:
384              CAS_node.edAddInputPort("studydir", t_string)
385              CAS_node.getInputPort("studydir").edInitPy(base_repertory)
386            except: pass
387
388   # Step 3: create compute bloc
389   compute_bloc = runtime.createBloc("compute_bloc")
390   ADAO_Case.edAddChild(compute_bloc)
391   ADAO_Case.edAddCFLink(CAS_node, compute_bloc)
392   # We use an optimizer loop
393   name = "Execute" + study_config["Algorithm"]
394   algLib = "daYacsIntegration.py"
395   factoryName = "AssimilationAlgorithm_asynch"
396   optimizer_node = runtime.createOptimizerLoop(name, algLib, factoryName, "")
397   compute_bloc.edAddChild(optimizer_node)
398   ADAO_Case.edAddDFLink(CAS_node.getOutputPort("Study"), optimizer_node.edGetAlgoInitPort())
399
400   # Check if we have a python script for OptimizerLoopNode
401   data_config = study_config["ObservationOperator"]
402   opt_script_nodeOO = None
403   if data_config["Type"] == "Function" and (data_config["From"] == "ScriptWithSwitch" or data_config["From"] == "FunctionDict"):
404     # Get script
405     TheData = data_config["Data"]
406     script_filename = ""
407     for FunctionName in TheData["Function"]:
408       # We currently support only one file
409       script_filename = TheData["Script"][FunctionName]
410       break
411     # We create a new pyscript node
412     opt_script_nodeOO = runtime.createScriptNode("", "FunctionNodeOO")
413     if UseYACSContainer:
414       opt_script_nodeOO.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
415       opt_script_nodeOO.setContainer(mycontainer)
416     if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))):
417       script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
418     try:
419       script_str= open(script_filename, 'r')
420     except:
421       raise ValueError("Exception in opening function script file: " + script_filename)
422     node_script  = "#-*- coding: utf-8 -*-\n"
423     node_script += "import sys, os \n"
424     node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
425     node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n"
426     node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
427     node_script += "  sys.path.insert(0,filepath)\n"
428     node_script += script_str.read()
429     opt_script_nodeOO.setScript(node_script)
430     opt_script_nodeOO.edAddInputPort("computation", t_param_input)
431     opt_script_nodeOO.edAddOutputPort("result", t_param_output)
432
433   elif data_config["Type"] == "Function" and data_config["From"] == "ScriptWithFunctions":
434     # Get script
435     ScriptWithFunctions = data_config["Data"]
436     script_filename = ""
437     for FunctionName in ScriptWithFunctions["Function"]:
438       # We currently support only one file
439       script_filename = ScriptWithFunctions["Script"][FunctionName]
440       break
441
442     # We create a new pyscript node
443     opt_script_nodeOO = runtime.createScriptNode("", "FunctionNodeOO")
444     if UseYACSContainer:
445       opt_script_nodeOO.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
446       opt_script_nodeOO.setContainer(mycontainer)
447     if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))):
448       script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
449     try:
450       script_str= open(script_filename, 'r')
451     except:
452       raise ValueError("Exception in opening function script file: " + script_filename)
453     node_script  = "#-*- coding: utf-8 -*-\n"
454     node_script += "import sys, os, numpy, logging\n"
455     node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
456     node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n"
457     node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
458     node_script += "  sys.path.insert(0,filepath)\n"
459     node_script += """# ==============================================\n"""
460     node_script += script_str.read()
461     node_script += """# ==============================================\n"""
462     node_script += """__method = None\n"""
463     node_script += """for param in computation["specificParameters"]:\n"""
464     node_script += """  if param["name"] == "method": __method = param["value"]\n"""
465     node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n"""
466     node_script += """  raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n"""
467     node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n"""
468     node_script += """#\n"""
469     node_script += """#\n"""
470     node_script += """__data = []\n"""
471     node_script += """if __method == "Direct":\n"""
472     node_script += """  try:\n"""
473     node_script += """      DirectOperator\n"""
474     node_script += """  except NameError:\n"""
475     node_script += """      raise ValueError("ComputationFunctionNode: DirectOperator not found in the imported user script file")\n"""
476     node_script += """  logging.debug("ComputationFunctionNode: Direct computation")\n"""
477     node_script += """  __Xcurrent = computation["inputValues"][0][0][0]\n"""
478     node_script += """  __data = DirectOperator(numpy.matrix( __Xcurrent ).T)\n"""
479     node_script += """#\n"""
480     node_script += """if __method == "Tangent":\n"""
481     node_script += """  try:\n"""
482     node_script += """    TangentOperator\n"""
483     node_script += """  except NameError:\n"""
484     node_script += """    raise ValueError("ComputationFunctionNode:  TangentOperator not found in the imported user script file")\n"""
485     node_script += """  logging.debug("ComputationFunctionNode: Tangent computation")\n"""
486     node_script += """  __Xcurrent  = computation["inputValues"][0][0][0]\n"""
487     node_script += """  __dXcurrent = computation["inputValues"][0][0][1]\n"""
488     node_script += """  __data = TangentOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __dXcurrent ).T))\n"""
489     node_script += """#\n"""
490     node_script += """if __method == "Adjoint":\n"""
491     node_script += """  try:\n"""
492     node_script += """    AdjointOperator\n"""
493     node_script += """  except NameError:\n"""
494     node_script += """    raise ValueError("ComputationFunctionNode: AdjointOperator not found in the imported user script file")\n"""
495     node_script += """  logging.debug("ComputationFunctionNode: Adjoint computation")\n"""
496     node_script += """  __Xcurrent = computation["inputValues"][0][0][0]\n"""
497     node_script += """  __Ycurrent = computation["inputValues"][0][0][1]\n"""
498     node_script += """  __data = AdjointOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __Ycurrent ).T))\n"""
499     node_script += """#\n"""
500     node_script += """logging.debug("ComputationFunctionNode: Formatting the output")\n"""
501     node_script += """__it = 1.*numpy.ravel(__data)\n"""
502     node_script += """outputValues = [[[[]]]]\n"""
503     node_script += """outputValues[0][0][0] = list(__it)\n"""
504     node_script += """#\n"""
505     node_script += """result = {}\n"""
506     node_script += """result["outputValues"]        = outputValues\n"""
507     node_script += """result["specificOutputInfos"] = []\n"""
508     node_script += """result["returnCode"]          = 0\n"""
509     node_script += """result["errorMessage"]        = ""\n"""
510     node_script += """# ==============================================\n"""
511     #
512     opt_script_nodeOO.setScript(node_script)
513     opt_script_nodeOO.edAddInputPort("computation", t_param_input)
514     opt_script_nodeOO.edAddOutputPort("result", t_param_output)
515
516   elif data_config["Type"] == "Function" and data_config["From"] == "ScriptWithOneFunction":
517     # Get script
518     ScriptWithOneFunction = data_config["Data"]
519     script_filename = ""
520     for FunctionName in ScriptWithOneFunction["Function"]:
521       # We currently support only one file
522       script_filename = ScriptWithOneFunction["Script"][FunctionName]
523       break
524
525     # We create a new pyscript node
526     opt_script_nodeOO = runtime.createScriptNode("", "FunctionNodeOO")
527     if UseYACSContainer:
528       opt_script_nodeOO.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
529       opt_script_nodeOO.setContainer(mycontainer)
530     if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))):
531       script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
532     try:
533       script_str= open(script_filename, 'r')
534     except:
535       raise ValueError("Exception in opening function script file: " + script_filename)
536     node_script  = "#-*- coding: utf-8 -*-\n"
537     node_script += "import sys, os, numpy, logging\n"
538     node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
539     node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n"
540     node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
541     node_script += "  sys.path.insert(0,filepath)\n"
542     node_script += """# ==============================================\n"""
543     node_script += script_str.read()
544     node_script += """# ==============================================\n"""
545     node_script += """__method = None\n"""
546     node_script += """for param in computation["specificParameters"]:\n"""
547     node_script += """  if param["name"] == "method": __method = param["value"]\n"""
548     node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n"""
549     node_script += """  raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n"""
550     node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n"""
551     node_script += """#\n"""
552     node_script += """try:\n"""
553     node_script += """    DirectOperator\n"""
554     node_script += """except NameError:\n"""
555     node_script += """    raise ValueError("ComputationFunctionNode: DirectOperator not found in the imported user script file")\n"""
556     node_script += """from daNumerics import ApproximatedDerivatives\n"""
557     node_script += """FDA = ApproximatedDerivatives.FDApproximation(\n"""
558     node_script += """    Function   = DirectOperator,\n"""
559     node_script += """    increment  = %s,\n"""%str(ScriptWithOneFunction['DifferentialIncrement'])
560     node_script += """    centeredDF = %s,\n"""%str(ScriptWithOneFunction['CenteredFiniteDifference'])
561     if 'EnableMultiProcessing' in list(ScriptWithOneFunction.keys()):
562         node_script += """    mpEnabled  = %s,\n"""%str(ScriptWithOneFunction['EnableMultiProcessing'])
563     if 'NumberOfProcesses' in list(ScriptWithOneFunction.keys()):
564         node_script += """    mpWorkers  = %s,\n"""%str(ScriptWithOneFunction['NumberOfProcesses'])
565     node_script += """    )\n"""
566     node_script += """#\n"""
567     node_script += """__data = []\n"""
568     node_script += """if __method == "Direct":\n"""
569     node_script += """  logging.debug("ComputationFunctionNode: Direct computation")\n"""
570     node_script += """  __Xcurrent = computation["inputValues"][0][0][0]\n"""
571     node_script += """  __data = FDA.DirectOperator(numpy.matrix( __Xcurrent ).T)\n"""
572     node_script += """#\n"""
573     node_script += """if __method == "Tangent":\n"""
574     node_script += """  logging.debug("ComputationFunctionNode: Tangent computation")\n"""
575     node_script += """  __Xcurrent  = computation["inputValues"][0][0][0]\n"""
576     node_script += """  __dXcurrent = computation["inputValues"][0][0][1]\n"""
577     node_script += """  __data = FDA.TangentOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __dXcurrent ).T))\n"""
578     node_script += """#\n"""
579     node_script += """if __method == "Adjoint":\n"""
580     node_script += """  logging.debug("ComputationFunctionNode: Adjoint computation")\n"""
581     node_script += """  __Xcurrent = computation["inputValues"][0][0][0]\n"""
582     node_script += """  __Ycurrent = computation["inputValues"][0][0][1]\n"""
583     node_script += """  __data = FDA.AdjointOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __Ycurrent ).T))\n"""
584     node_script += """#\n"""
585     node_script += """logging.debug("ComputationFunctionNode: Formatting the output")\n"""
586     node_script += """__it = 1.*numpy.ravel(__data)\n"""
587     node_script += """outputValues = [[[[]]]]\n"""
588     node_script += """outputValues[0][0][0] = list(__it)\n"""
589     node_script += """#\n"""
590     node_script += """result = {}\n"""
591     node_script += """result["outputValues"]        = outputValues\n"""
592     node_script += """result["specificOutputInfos"] = []\n"""
593     node_script += """result["returnCode"]          = 0\n"""
594     node_script += """result["errorMessage"]        = ""\n"""
595     node_script += """# ==============================================\n"""
596     #
597     opt_script_nodeOO.setScript(node_script)
598     opt_script_nodeOO.edAddInputPort("computation", t_param_input)
599     opt_script_nodeOO.edAddOutputPort("result", t_param_output)
600
601   else:
602     factory_opt_script_node = catalogAd.getNodeFromNodeMap("FakeOptimizerLoopNode")
603     opt_script_nodeOO = factory_opt_script_node.cloneNode("FakeFunctionNode")
604
605   # Check if we have a python script for OptimizerLoopNode
606   if "EvolutionModel" in list(study_config.keys()):
607     data_config = study_config["EvolutionModel"]
608     opt_script_nodeEM = None
609     if data_config["Type"] == "Function" and (data_config["From"] == "ScriptWithSwitch" or data_config["From"] == "FunctionDict"):
610       # Get script
611       TheData = data_config["Data"]
612       script_filename = ""
613       for FunctionName in TheData["Function"]:
614         # We currently support only one file
615         script_filename = TheData["Script"][FunctionName]
616         break
617       # We create a new pyscript node
618       opt_script_nodeEM = runtime.createScriptNode("", "FunctionNodeEM")
619       if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))):
620         script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
621       try:
622         script_str= open(script_filename, 'r')
623       except:
624         raise ValueError("Exception in opening function script file: " + script_filename)
625       node_script  = "#-*- coding: utf-8 -*-\n"
626       node_script += "import sys, os \n"
627       node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
628       node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n"
629       node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
630       node_script += "  sys.path.insert(0,filepath)\n"
631       node_script += script_str.read()
632       opt_script_nodeEM.setScript(node_script)
633       opt_script_nodeEM.edAddInputPort("computation", t_param_input)
634       opt_script_nodeEM.edAddOutputPort("result", t_param_output)
635
636     elif data_config["Type"] == "Function" and data_config["From"] == "ScriptWithFunctions":
637       # Get script
638       ScriptWithFunctions = data_config["Data"]
639       script_filename = ""
640       for FunctionName in ScriptWithFunctions["Function"]:
641         # We currently support only one file
642         script_filename = ScriptWithFunctions["Script"][FunctionName]
643         break
644       # We create a new pyscript node
645       opt_script_nodeEM = runtime.createScriptNode("", "FunctionNodeEM")
646       if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))):
647         script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
648       try:
649         script_str= open(script_filename, 'r')
650       except:
651         raise ValueError("Exception in opening function script file: " + script_filename)
652       node_script  = "#-*- coding: utf-8 -*-\n"
653       node_script += "import sys, os, numpy, logging\n"
654       node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
655       node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n"
656       node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
657       node_script += "  sys.path.insert(0,filepath)\n"
658       node_script += script_str.read()
659       node_script += """# ==============================================\n"""
660       node_script += """__method = None\n"""
661       node_script += """for param in computation["specificParameters"]:\n"""
662       node_script += """  if param["name"] == "method": __method = param["value"]\n"""
663       node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n"""
664       node_script += """  raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n"""
665       node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n"""
666       node_script += """#\n"""
667       node_script += """#\n"""
668       node_script += """__data = []\n"""
669       node_script += """if __method == "Direct":\n"""
670       node_script += """  try:\n"""
671       node_script += """    DirectOperator\n"""
672       node_script += """  except NameError:\n"""
673       node_script += """    raise ValueError("ComputationFunctionNode: mandatory DirectOperator not found in the imported user script file")\n"""
674       node_script += """  logging.debug("ComputationFunctionNode: Direct computation")\n"""
675       node_script += """  __Xcurrent = computation["inputValues"][0][0][0]\n"""
676       node_script += """  if len(computation["inputValues"][0][0]) == 2:\n"""
677       node_script += """    __Ucurrent = computation["inputValues"][0][0][1]\n"""
678       node_script += """    __data = DirectOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __Ucurrent ).T))\n"""
679       node_script += """  else:\n"""
680       node_script += """    __data = DirectOperator(numpy.matrix( __Xcurrent ).T)\n"""
681       node_script += """#\n"""
682       node_script += """if __method == "Tangent":\n"""
683       node_script += """  try:\n"""
684       node_script += """    TangentOperator\n"""
685       node_script += """  except NameError:\n"""
686       node_script += """    raise ValueError("ComputationFunctionNode: mandatory TangentOperator not found in the imported user script file")\n"""
687       node_script += """  logging.debug("ComputationFunctionNode: Tangent computation")\n"""
688       node_script += """  __Xcurrent  = computation["inputValues"][0][0][0]\n"""
689       node_script += """  __dXcurrent = computation["inputValues"][0][0][1]\n"""
690       node_script += """  __data = TangentOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __dXcurrent ).T))\n"""
691       node_script += """#\n"""
692       node_script += """if __method == "Adjoint":\n"""
693       node_script += """  try:\n"""
694       node_script += """    AdjointOperator\n"""
695       node_script += """  except NameError:\n"""
696       node_script += """    raise ValueError("ComputationFunctionNode: mandatory AdjointOperator not found in the imported user script file")\n"""
697       node_script += """  logging.debug("ComputationFunctionNode: Adjoint computation")\n"""
698       node_script += """  __Xcurrent = computation["inputValues"][0][0][0]\n"""
699       node_script += """  __Ycurrent = computation["inputValues"][0][0][1]\n"""
700       node_script += """  __data = AdjointOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __Ycurrent ).T))\n"""
701       node_script += """#\n"""
702       node_script += """logging.debug("ComputationFunctionNode: Formatting the output")\n"""
703       node_script += """__it = 1.*numpy.ravel(__data)\n"""
704       node_script += """outputValues = [[[[]]]]\n"""
705       node_script += """outputValues[0][0][0] = list(__it)\n"""
706       node_script += """#\n"""
707       node_script += """result = {}\n"""
708       node_script += """result["outputValues"]        = outputValues\n"""
709       node_script += """result["specificOutputInfos"] = []\n"""
710       node_script += """result["returnCode"]          = 0\n"""
711       node_script += """result["errorMessage"]        = ""\n"""
712       node_script += """# ==============================================\n"""
713       #
714       opt_script_nodeEM.setScript(node_script)
715       opt_script_nodeEM.edAddInputPort("computation", t_param_input)
716       opt_script_nodeEM.edAddOutputPort("result", t_param_output)
717
718     elif data_config["Type"] == "Function" and data_config["From"] == "ScriptWithOneFunction":
719       # Get script
720       ScriptWithOneFunction = data_config["Data"]
721       script_filename = ""
722       for FunctionName in ScriptWithOneFunction["Function"]:
723         # We currently support only one file
724         script_filename = ScriptWithOneFunction["Script"][FunctionName]
725         break
726       # We create a new pyscript node
727       opt_script_nodeEM = runtime.createScriptNode("", "FunctionNodeEM")
728       if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))):
729         script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
730       try:
731         script_str= open(script_filename, 'r')
732       except:
733         raise ValueError("Exception in opening function script file: " + script_filename)
734       node_script  = "#-*- coding: utf-8 -*-\n"
735       node_script += "import sys, os, numpy, logging\n"
736       node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
737       node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n"
738       node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
739       node_script += "  sys.path.insert(0,filepath)\n"
740       node_script += script_str.read()
741       node_script += """# ==============================================\n"""
742       node_script += """__method = None\n"""
743       node_script += """for param in computation["specificParameters"]:\n"""
744       node_script += """  if param["name"] == "method": __method = param["value"]\n"""
745       node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n"""
746       node_script += """  raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n"""
747       node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n"""
748       node_script += """#\n"""
749       node_script += """try:\n"""
750       node_script += """    DirectOperator\n"""
751       node_script += """except NameError:\n"""
752       node_script += """    raise ValueError("ComputationFunctionNode: DirectOperator not found in the imported user script file")\n"""
753       node_script += """from daNumerics import ApproximatedDerivatives\n"""
754       node_script += """FDA = ApproximatedDerivatives.FDApproximation(\n"""
755       node_script += """    Function   = DirectOperator,\n"""
756       node_script += """    increment  = %s,\n"""%str(ScriptWithOneFunction['DifferentialIncrement'])
757       node_script += """    centeredDF = %s,\n"""%str(ScriptWithOneFunction['CenteredFiniteDifference'])
758       if 'EnableMultiProcessing' in list(ScriptWithOneFunction.keys()):
759           node_script += """    mpEnabled  = %s,\n"""%str(ScriptWithOneFunction['EnableMultiProcessing'])
760       if 'NumberOfProcesses' in list(ScriptWithOneFunction.keys()):
761           node_script += """    mpWorkers  = %s,\n"""%str(ScriptWithOneFunction['NumberOfProcesses'])
762       node_script += """    )\n"""
763       node_script += """#\n"""
764       node_script += """__data = []\n"""
765       node_script += """if __method == "Direct":\n"""
766       node_script += """  logging.debug("ComputationFunctionNode: Direct computation")\n"""
767       node_script += """  if len(computation["inputValues"][0][0]) == 2:\n"""
768       node_script += """    raise ValueError("ComputationFunctionNode: you have to build explicitly the controled evolution model and its tangent and adjoint operators, instead of using approximate derivative.")"""
769       node_script += """  __Xcurrent = computation["inputValues"][0][0][0]\n"""
770       node_script += """  __data = FDA.DirectOperator(numpy.matrix( __Xcurrent ).T)\n"""
771       node_script += """#\n"""
772       node_script += """if __method == "Tangent":\n"""
773       node_script += """  logging.debug("ComputationFunctionNode: Tangent computation")\n"""
774       node_script += """  __Xcurrent  = computation["inputValues"][0][0][0]\n"""
775       node_script += """  __dXcurrent = computation["inputValues"][0][0][1]\n"""
776       node_script += """  __data = FDA.TangentOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __dXcurrent ).T))\n"""
777       node_script += """#\n"""
778       node_script += """if __method == "Adjoint":\n"""
779       node_script += """  logging.debug("ComputationFunctionNode: Adjoint computation")\n"""
780       node_script += """  __Xcurrent = computation["inputValues"][0][0][0]\n"""
781       node_script += """  __Ycurrent = computation["inputValues"][0][0][1]\n"""
782       node_script += """  __data = FDA.AdjointOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __Ycurrent ).T))\n"""
783       node_script += """#\n"""
784       node_script += """logging.debug("ComputationFunctionNode: Formatting the output")\n"""
785       node_script += """__it = 1.*numpy.ravel(__data)\n"""
786       node_script += """outputValues = [[[[]]]]\n"""
787       node_script += """outputValues[0][0][0] = list(__it)\n"""
788       node_script += """#\n"""
789       node_script += """result = {}\n"""
790       node_script += """result["outputValues"]        = outputValues\n"""
791       node_script += """result["specificOutputInfos"] = []\n"""
792       node_script += """result["returnCode"]          = 0\n"""
793       node_script += """result["errorMessage"]        = ""\n"""
794       node_script += """# ==============================================\n"""
795       #
796       opt_script_nodeEM.setScript(node_script)
797       opt_script_nodeEM.edAddInputPort("computation", t_param_input)
798       opt_script_nodeEM.edAddOutputPort("result", t_param_output)
799
800     else:
801       factory_opt_script_node = catalogAd.getNodeFromNodeMap("FakeOptimizerLoopNode")
802       opt_script_nodeEM = factory_opt_script_node.cloneNode("FakeFunctionNode")
803
804   # Add computation bloc
805   if "Observers" in list(study_config.keys()):
806     execution_bloc = runtime.createBloc("Execution Bloc")
807     optimizer_node.edSetNode(execution_bloc)
808
809     # Add a node that permits to configure the switch
810     factory_read_for_switch_node = catalogAd.getNodeFromNodeMap("ReadForSwitchNode")
811     read_for_switch_node = factory_read_for_switch_node.cloneNode("ReadForSwitch")
812     execution_bloc.edAddChild(read_for_switch_node)
813     ADAO_Case.edAddDFLink(optimizer_node.edGetSamplePort(), read_for_switch_node.getInputPort("data"))
814
815     # Add a switch
816     switch_node = runtime.createSwitch("Execution Switch")
817     execution_bloc.edAddChild(switch_node)
818     # Connect switch
819     ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("switch_value"), switch_node.edGetConditionPort())
820
821     # First case: computation bloc
822     computation_blocOO = runtime.createBloc("computation_blocOO")
823     computation_blocOO.edAddChild(opt_script_nodeOO)
824     switch_node.edSetNode(1, computation_blocOO)
825
826     # We connect with the script
827     ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("data"), opt_script_nodeOO.getInputPort("computation"))
828     ADAO_Case.edAddDFLink(opt_script_nodeOO.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
829
830     # Second case: evolution bloc
831     if "EvolutionModel" in list(study_config.keys()):
832       computation_blocEM = runtime.createBloc("computation_blocEM")
833       computation_blocEM.edAddChild(opt_script_nodeEM)
834       switch_node.edSetNode(2, computation_blocEM)
835
836       # We connect with the script
837       ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("data"), opt_script_nodeEM.getInputPort("computation"))
838       ADAO_Case.edAddDFLink(opt_script_nodeEM.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
839
840     # For each observer add a new bloc in the switch
841     observer_config = study_config["Observers"]
842     for observer_name in observer_config:
843       observer_cfg = observer_config[observer_name]
844       observer_bloc = runtime.createBloc("Observer %s" % observer_name)
845       switch_node.edSetNode(observer_cfg["number"], observer_bloc)
846
847       factory_extract_data_node = catalogAd.getNodeFromNodeMap("ExtractDataNode")
848       extract_data_node = factory_extract_data_node.cloneNode("ExtractData")
849       observer_bloc.edAddChild(extract_data_node)
850       ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("data"), extract_data_node.getInputPort("data"))
851
852       observation_node = None
853       if observer_cfg["nodetype"] == "String":
854         factory_observation_node = catalogAd.getNodeFromNodeMap("ObservationNodeString")
855         observation_node = factory_observation_node.cloneNode("Observation")
856         node_script = observation_node.getScript()
857         node_script += observer_cfg["String"]
858         observation_node.setScript(node_script)
859       else:
860         factory_observation_node = catalogAd.getNodeFromNodeMap("ObservationNodeFile")
861         observation_node = factory_observation_node.cloneNode("Observation")
862         _Internal_Add_dir_script_ports( observation_node, observer_cfg["Script"], repertory, base_repertory, t_string)
863       observer_bloc.edAddChild(observation_node)
864       ADAO_Case.edAddDFLink(extract_data_node.getOutputPort("var"), observation_node.getInputPort("var"))
865       ADAO_Case.edAddDFLink(extract_data_node.getOutputPort("info"), observation_node.getInputPort("info"))
866
867       factory_end_observation_node = catalogAd.getNodeFromNodeMap("EndObservationNode")
868       end_observation_node = factory_end_observation_node.cloneNode("EndObservation")
869       observer_bloc.edAddChild(end_observation_node)
870       ADAO_Case.edAddCFLink(observation_node, end_observation_node)
871       ADAO_Case.edAddDFLink(end_observation_node.getOutputPort("output"), optimizer_node.edGetPortForOutPool())
872
873   elif "EvolutionModel" in list(study_config.keys()):
874     execution_bloc = runtime.createBloc("Execution Bloc")
875     optimizer_node.edSetNode(execution_bloc)
876
877     # Add a node that permits to configure the switch
878     factory_read_for_switch_node = catalogAd.getNodeFromNodeMap("ReadForSwitchNode")
879     read_for_switch_node = factory_read_for_switch_node.cloneNode("ReadForSwitch")
880     execution_bloc.edAddChild(read_for_switch_node)
881     ADAO_Case.edAddDFLink(optimizer_node.edGetSamplePort(), read_for_switch_node.getInputPort("data"))
882
883     # Add a switch
884     switch_node = runtime.createSwitch("Execution Switch")
885     execution_bloc.edAddChild(switch_node)
886     # Connect switch
887     ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("switch_value"), switch_node.edGetConditionPort())
888
889     # First case: computation bloc
890     computation_blocOO = runtime.createBloc("computation_blocOO")
891     computation_blocOO.edAddChild(opt_script_nodeOO)
892     switch_node.edSetNode(1, computation_blocOO)
893
894     # We connect with the script
895     ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("data"), opt_script_nodeOO.getInputPort("computation"))
896     ADAO_Case.edAddDFLink(opt_script_nodeOO.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
897
898     # Second case: evolution bloc
899     computation_blocEM = runtime.createBloc("computation_blocEM")
900     computation_blocEM.edAddChild(opt_script_nodeEM)
901     switch_node.edSetNode(2, computation_blocEM)
902
903     # We connect with the script
904     ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("data"), opt_script_nodeEM.getInputPort("computation"))
905     ADAO_Case.edAddDFLink(opt_script_nodeEM.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
906
907   else:
908     computation_blocOO = runtime.createBloc("computation_blocOO")
909     optimizer_node.edSetNode(computation_blocOO)
910     computation_blocOO.edAddChild(opt_script_nodeOO)
911
912     # We connect Optimizer with the script
913     ADAO_Case.edAddDFLink(optimizer_node.edGetSamplePort(), opt_script_nodeOO.getInputPort("computation"))
914     ADAO_Case.edAddDFLink(opt_script_nodeOO.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
915
916   # Connect node with InitUserData
917   if "ObservationOperator" in init_config["Target"]:
918     opt_node_script = opt_script_nodeOO.getScript()
919     opt_node_script = "__builtins__[\"init_data\"] = init_data\n" + opt_node_script
920     opt_script_nodeOO.setScript(opt_node_script)
921     opt_script_nodeOO.edAddInputPort("init_data", t_pyobj)
922     ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), opt_script_nodeOO.getInputPort("init_data"))
923
924   # Step 4: create post-processing from user configuration
925   if "UserPostAnalysis" in list(study_config.keys()):
926     analysis_config = study_config["UserPostAnalysis"]
927     if analysis_config["From"] == "String":
928       factory_analysis_node = catalogAd.getNodeFromNodeMap("SimpleUserAnalysis")
929       analysis_node = factory_analysis_node.cloneNode("UsePostAnalysis")
930       default_script = analysis_node.getScript()
931       final_script = default_script + analysis_config["Data"]
932       analysis_node.setScript(final_script)
933       ADAO_Case.edAddChild(analysis_node)
934       ADAO_Case.edAddCFLink(compute_bloc, analysis_node)
935       if AlgoType[study_config["Algorithm"]] == "Optim":
936         ADAO_Case.edAddDFLink(optimizer_node.edGetAlgoResultPort(), analysis_node.getInputPort("Study"))
937       else:
938         ADAO_Case.edAddDFLink(execute_node.getOutputPort("Study"), analysis_node.getInputPort("Study"))
939
940       # Connect node with InitUserData
941       if "UserPostAnalysis" in init_config["Target"]:
942         node_script = analysis_node.getScript()
943         node_script = "__builtins__[\"init_data\"] = init_data\n" + node_script
944         analysis_node.setScript(node_script)
945         analysis_node.edAddInputPort("init_data", t_pyobj)
946         ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), analysis_node.getInputPort("init_data"))
947
948     elif analysis_config["From"] == "Script":
949       factory_analysis_node = catalogAd.getNodeFromNodeMap("SimpleUserAnalysis")
950       analysis_node = factory_analysis_node.cloneNode("UserPostAnalysis")
951       default_script = analysis_node.getScript()
952       analysis_file_name = analysis_config["Data"]
953       if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(analysis_file_name))):
954         analysis_file_name = os.path.join(base_repertory, os.path.basename(analysis_file_name))
955       try:
956         analysis_file = open(analysis_file_name, 'r')
957       except:
958         raise ValueError("Exception in opening analysis file: " + str(analysis_config["Data"]))
959       node_script  = "#-*- coding: utf-8 -*-\n"
960       node_script += "import sys, os \n"
961       node_script += "filepath = \"" + os.path.dirname(analysis_file_name) + "\"\n"
962       node_script += "filename = \"" + os.path.basename(analysis_file_name) + "\"\n"
963       node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
964       node_script += "  sys.path.insert(0,filepath)\n"
965       node_script += default_script
966       node_script += analysis_file.read()
967       analysis_node.setScript(node_script)
968       ADAO_Case.edAddChild(analysis_node)
969       ADAO_Case.edAddCFLink(compute_bloc, analysis_node)
970       if AlgoType[study_config["Algorithm"]] == "Optim":
971         ADAO_Case.edAddDFLink(optimizer_node.edGetAlgoResultPort(), analysis_node.getInputPort("Study"))
972       else:
973         ADAO_Case.edAddDFLink(execute_node.getOutputPort("Study"), analysis_node.getInputPort("Study"))
974       # Connect node with InitUserData
975       if "UserPostAnalysis" in init_config["Target"]:
976         node_script = analysis_node.getScript()
977         node_script = "__builtins__[\"init_data\"] = init_data\n" + node_script
978         analysis_node.setScript(node_script)
979         analysis_node.edAddInputPort("init_data", t_pyobj)
980         ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), analysis_node.getInputPort("init_data"))
981
982       pass
983
984   return proc
985
986 def write_yacs_proc(proc, yacs_schema_filename):
987
988   proc.saveSchema(yacs_schema_filename)
989