1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010-2013 EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 # Author: André Ribes, andre.ribes@edf.fr, EDF R&D
22 from generator.generator_python import PythonGenerator
28 Retourne les informations necessaires pour le chargeur de plugins
30 Ces informations sont retournees dans un dictionnaire
35 # La factory pour creer une instance du plugin
36 'factory' : AdaoGenerator,
39 class AdaoGenerator(PythonGenerator):
41 def __init__(self,cr=None):
42 PythonGenerator.__init__(self, cr)
46 self.text_da_status = False
47 self.logger = logging.getLogger('ADAO EFICAS GENERATOR')
48 self.logger.setLevel(logging.INFO)
49 ch = logging.StreamHandler()
50 ch.setLevel(logging.INFO)
51 formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
52 ch.setFormatter(formatter)
53 self.logger.addHandler(ch)
55 def gener(self,obj,format='brut',config=None):
56 self.logger.debug("method gener called")
57 self.text_comm = PythonGenerator.gener(self, obj, format, config)
58 for key, value in self.dictMCVal.iteritems():
59 self.logger.debug("dictMCVAl %s %s" % (key,value))
62 self.text_da_status = False
64 self.text_da_status = True
66 self.logger.info("Case is not correct, python command file for YACS schema generation cannot be created")
67 self.logger.debug(self.text_da)
72 def writeDefault(self, fn):
73 if self.text_da_status:
74 self.logger.debug("write adao python command file")
75 filename = fn[:fn.rfind(".")] + '.py'
76 f = open( str(filename), 'wb')
77 f.write( self.text_da )
80 def generMCSIMP(self,obj) :
82 Convertit un objet MCSIMP en texte python
85 for i in obj.get_genealogie() :
87 self.dictMCVal[clef]=obj.valeur
89 s=PythonGenerator.generMCSIMP(self,obj)
92 def generate_da(self):
94 if "__CHECKING_STUDY__Study_name" in self.dictMCVal.keys():
95 self.type_of_study = "CHECKING_STUDY"
97 self.type_of_study = "ASSIMILATION_STUDY"
99 self.text_da += "#-*-coding:iso-8859-1-*- \n"
100 self.text_da += "study_config = {} \n"
102 # Extraction de Study_type
103 self.text_da += "study_config['StudyType'] = '" + self.type_of_study + "'\n"
104 # Extraction de Study_name
105 self.text_da += "study_config['Name'] = '" + self.dictMCVal["__"+self.type_of_study+"__Study_name"] + "'\n"
106 # Extraction de Debug
107 self.text_da += "study_config['Debug'] = '" + str(self.dictMCVal["__"+self.type_of_study+"__Debug"]) + "'\n"
108 # Extraction de Algorithm
109 self.text_da += "study_config['Algorithm'] = '" + self.dictMCVal["__"+self.type_of_study+"__Algorithm"] + "'\n"
111 if "__"+self.type_of_study+"__Background__INPUT_TYPE" in self.dictMCVal.keys():
112 self.add_data("Background")
113 if "__"+self.type_of_study+"__BackgroundError__INPUT_TYPE" in self.dictMCVal.keys():
114 self.add_data("BackgroundError")
115 if "__"+self.type_of_study+"__Observation__INPUT_TYPE" in self.dictMCVal.keys():
116 self.add_data("Observation")
117 if "__"+self.type_of_study+"__ObservationError__INPUT_TYPE" in self.dictMCVal.keys():
118 self.add_data("ObservationError")
119 if "__"+self.type_of_study+"__CheckingPoint__INPUT_TYPE" in self.dictMCVal.keys():
120 self.add_data("CheckingPoint")
121 if "__"+self.type_of_study+"__ObservationOperator__INPUT_TYPE" in self.dictMCVal.keys():
122 self.add_data("ObservationOperator")
123 if "__"+self.type_of_study+"__EvolutionModel__INPUT_TYPE" in self.dictMCVal.keys():
124 self.add_data("EvolutionModel")
125 if "__"+self.type_of_study+"__EvolutionError__INPUT_TYPE" in self.dictMCVal.keys():
126 self.add_data("EvolutionError")
127 if "__"+self.type_of_study+"__ControlInput__INPUT_TYPE" in self.dictMCVal.keys():
128 self.add_data("ControlInput")
131 # Parametres optionnels
133 # Extraction du Study_repertory
134 if "__"+self.type_of_study+"__Study_repertory" in self.dictMCVal.keys():
135 self.text_da += "study_config['Repertory'] = '" + self.dictMCVal["__"+self.type_of_study+"__Study_repertory"] + "'\n"
136 # Extraction de AlgorithmParameters
137 if "__"+self.type_of_study+"__AlgorithmParameters__INPUT_TYPE" in self.dictMCVal.keys():
138 self.add_algorithm_parameters()
139 # Extraction de UserPostAnalysis
140 if "__"+self.type_of_study+"__UserPostAnalysis__FROM" in self.dictMCVal.keys():
141 self.add_UserPostAnalysis()
142 if "__"+self.type_of_study+"__UserDataInit__INIT_FILE" in self.dictMCVal.keys():
144 if "__"+self.type_of_study+"__Observers__SELECTION" in self.dictMCVal.keys():
147 def add_data(self, data_name):
149 # Extraction des données
150 search_text = "__"+self.type_of_study+"__" + data_name + "__"
151 data_type = self.dictMCVal[search_text + "INPUT_TYPE"]
152 search_type = search_text + data_type + "__data__"
153 from_type = self.dictMCVal[search_type + "FROM"]
155 if from_type == "String":
156 data = self.dictMCVal[search_type + "STRING_DATA__STRING"]
157 elif from_type == "Script":
158 data = self.dictMCVal[search_type + "SCRIPT_DATA__SCRIPT_FILE"]
159 elif from_type == "ScriptWithSwitch":
160 data = self.dictMCVal[search_type + "SCRIPTWITHSWITCH_DATA__SCRIPTWITHSWITCH_FILE"]
161 elif from_type == "ScriptWithFunctions":
162 data = self.dictMCVal[search_type + "SCRIPTWITHFUNCTIONS_DATA__SCRIPTWITHFUNCTIONS_FILE"]
163 elif from_type == "ScriptWithOneFunction":
164 data = self.dictMCVal[search_type + "SCRIPTWITHONEFUNCTION_DATA__SCRIPTWITHONEFUNCTION_FILE"]
165 elif from_type == "FunctionDict":
166 data = self.dictMCVal[search_type + "FUNCTIONDICT_DATA__FUNCTIONDICT_FILE"]
168 raise Exception('From Type unknown', from_type)
170 if from_type == "String" or from_type == "Script":
171 self.text_da += data_name + "_config = {}\n"
172 self.text_da += data_name + "_config['Type'] = '" + data_type + "'\n"
173 self.text_da += data_name + "_config['From'] = '" + from_type + "'\n"
174 self.text_da += data_name + "_config['Data'] = '" + data + "'\n"
175 if search_text+"Stored" in self.dictMCVal.keys():
176 self.text_da += data_name + "_config['Stored'] = '" + str(self.dictMCVal[search_text+"Stored"]) + "'\n"
177 self.text_da += "study_config['" + data_name + "'] = " + data_name + "_config\n"
179 if from_type == "ScriptWithSwitch":
180 self.text_da += data_name + "_ScriptWithSwitch = {}\n"
181 self.text_da += data_name + "_ScriptWithSwitch['Function'] = ['Direct', 'Tangent', 'Adjoint']\n"
182 self.text_da += data_name + "_ScriptWithSwitch['Script'] = {}\n"
183 self.text_da += data_name + "_ScriptWithSwitch['Script']['Direct'] = '" + data + "'\n"
184 self.text_da += data_name + "_ScriptWithSwitch['Script']['Tangent'] = '" + data + "'\n"
185 self.text_da += data_name + "_ScriptWithSwitch['Script']['Adjoint'] = '" + data + "'\n"
186 self.text_da += data_name + "_config = {}\n"
187 self.text_da += data_name + "_config['Type'] = 'Function'\n"
188 self.text_da += data_name + "_config['From'] = 'ScriptWithSwitch'\n"
189 self.text_da += data_name + "_config['Data'] = " + data_name + "_ScriptWithSwitch\n"
190 self.text_da += "study_config['" + data_name + "'] = " + data_name + "_config\n"
192 if from_type == "ScriptWithFunctions":
193 self.text_da += data_name + "_ScriptWithFunctions = {}\n"
194 self.text_da += data_name + "_ScriptWithFunctions['Function'] = ['Direct', 'Tangent', 'Adjoint']\n"
195 self.text_da += data_name + "_ScriptWithFunctions['Script'] = {}\n"
196 self.text_da += data_name + "_ScriptWithFunctions['Script']['Direct'] = '" + data + "'\n"
197 self.text_da += data_name + "_ScriptWithFunctions['Script']['Tangent'] = '" + data + "'\n"
198 self.text_da += data_name + "_ScriptWithFunctions['Script']['Adjoint'] = '" + data + "'\n"
199 self.text_da += data_name + "_config = {}\n"
200 self.text_da += data_name + "_config['Type'] = 'Function'\n"
201 self.text_da += data_name + "_config['From'] = 'ScriptWithFunctions'\n"
202 self.text_da += data_name + "_config['Data'] = " + data_name + "_ScriptWithFunctions\n"
203 self.text_da += "study_config['" + data_name + "'] = " + data_name + "_config\n"
205 if from_type == "ScriptWithOneFunction":
206 self.text_da += data_name + "_ScriptWithOneFunction = {}\n"
207 self.text_da += data_name + "_ScriptWithOneFunction['Function'] = ['Direct', 'Tangent', 'Adjoint']\n"
208 self.text_da += data_name + "_ScriptWithOneFunction['Script'] = {}\n"
209 self.text_da += data_name + "_ScriptWithOneFunction['Script']['Direct'] = '" + data + "'\n"
210 self.text_da += data_name + "_ScriptWithOneFunction['Script']['Tangent'] = '" + data + "'\n"
211 self.text_da += data_name + "_ScriptWithOneFunction['Script']['Adjoint'] = '" + data + "'\n"
212 self.text_da += data_name + "_ScriptWithOneFunction['DifferentialIncrement'] = " + str(float(self.dictMCVal[search_type + "SCRIPTWITHONEFUNCTION_DATA__DifferentialIncrement"])) + "\n"
213 self.text_da += data_name + "_ScriptWithOneFunction['CenteredFiniteDifference'] = " + str(self.dictMCVal[search_type + "SCRIPTWITHONEFUNCTION_DATA__CenteredFiniteDifference"]) + "\n"
214 self.text_da += data_name + "_config = {}\n"
215 self.text_da += data_name + "_config['Type'] = 'Function'\n"
216 self.text_da += data_name + "_config['From'] = 'ScriptWithOneFunction'\n"
217 self.text_da += data_name + "_config['Data'] = " + data_name + "_ScriptWithOneFunction\n"
218 self.text_da += "study_config['" + data_name + "'] = " + data_name + "_config\n"
220 if from_type == "FunctionDict":
221 self.text_da += data_name + "_FunctionDict = {}\n"
222 self.text_da += data_name + "_FunctionDict['Function'] = ['Direct', 'Tangent', 'Adjoint']\n"
223 self.text_da += data_name + "_FunctionDict['Script'] = {}\n"
224 self.text_da += data_name + "_FunctionDict['Script']['Direct'] = '" + data + "'\n"
225 self.text_da += data_name + "_FunctionDict['Script']['Tangent'] = '" + data + "'\n"
226 self.text_da += data_name + "_FunctionDict['Script']['Adjoint'] = '" + data + "'\n"
227 self.text_da += data_name + "_config = {}\n"
228 self.text_da += data_name + "_config['Type'] = 'Function'\n"
229 self.text_da += data_name + "_config['From'] = 'FunctionDict'\n"
230 self.text_da += data_name + "_config['Data'] = " + data_name + "_FunctionDict\n"
231 self.text_da += "study_config['" + data_name + "'] = " + data_name + "_config\n"
233 def add_algorithm_parameters(self):
235 data_name = "AlgorithmParameters"
238 data = self.dictMCVal["__"+self.type_of_study+"__AlgorithmParameters__Dict__data__SCRIPT_DATA__SCRIPT_FILE"]
240 self.text_da += data_name + "_config = {} \n"
241 self.text_da += data_name + "_config['Type'] = '" + data_type + "'\n"
242 self.text_da += data_name + "_config['From'] = '" + from_type + "'\n"
243 self.text_da += data_name + "_config['Data'] = '" + data + "'\n"
244 self.text_da += "study_config['" + data_name + "'] = " + data_name + "_config\n"
248 init_file_data = self.dictMCVal["__"+self.type_of_study+"__UserDataInit__INIT_FILE"]
249 init_target_list = self.dictMCVal["__"+self.type_of_study+"__UserDataInit__TARGET_LIST"]
251 self.text_da += "Init_config = {}\n"
252 self.text_da += "Init_config['Type'] = 'Dict'\n"
253 self.text_da += "Init_config['From'] = 'Script'\n"
254 self.text_da += "Init_config['Data'] = '" + init_file_data + "'\n"
255 self.text_da += "Init_config['Target'] = ["
256 if type(init_target_list) is type("str"):
257 self.text_da += "'" + init_target_list + "',"
259 for target in init_target_list:
260 self.text_da += "'" + target + "',"
261 self.text_da += "]\n"
262 self.text_da += "study_config['UserDataInit'] = Init_config\n"
264 def add_UserPostAnalysis(self):
266 from_type = self.dictMCVal["__"+self.type_of_study+"__UserPostAnalysis__FROM"]
268 if from_type == "String":
269 data = self.dictMCVal["__"+self.type_of_study+"__UserPostAnalysis__STRING_DATA__STRING"]
270 self.text_da += "Analysis_config = {}\n"
271 self.text_da += "Analysis_config['From'] = 'String'\n"
272 self.text_da += "Analysis_config['Data'] = \"\"\"" + data + "\"\"\"\n"
273 self.text_da += "study_config['UserPostAnalysis'] = Analysis_config\n"
274 elif from_type == "Script":
275 data = self.dictMCVal["__"+self.type_of_study+"__UserPostAnalysis__SCRIPT_DATA__SCRIPT_FILE"]
276 self.text_da += "Analysis_config = {}\n"
277 self.text_da += "Analysis_config['From'] = 'Script'\n"
278 self.text_da += "Analysis_config['Data'] = '" + data + "'\n"
279 self.text_da += "study_config['UserPostAnalysis'] = Analysis_config\n"
281 raise Exception('From Type unknown', from_type)
283 def add_variables(self):
286 if "__"+self.type_of_study+"__InputVariables__NAMES" in self.dictMCVal.keys():
289 if isinstance(self.dictMCVal["__"+self.type_of_study+"__InputVariables__NAMES"], type("")):
290 names.append(self.dictMCVal["__"+self.type_of_study+"__InputVariables__NAMES"])
292 names = self.dictMCVal["__"+self.type_of_study+"__InputVariables__NAMES"]
293 if isinstance(self.dictMCVal["__"+self.type_of_study+"__InputVariables__SIZES"], type(1)):
294 sizes.append(self.dictMCVal["__"+self.type_of_study+"__InputVariables__SIZES"])
296 sizes = self.dictMCVal["__"+self.type_of_study+"__InputVariables__SIZES"]
298 self.text_da += "inputvariables_config = {}\n"
299 self.text_da += "inputvariables_config['Order'] = %s\n" % list(names)
300 for name, size in zip(names, sizes):
301 self.text_da += "inputvariables_config['%s'] = %s\n" % (name,size)
302 self.text_da += "study_config['InputVariables'] = inputvariables_config\n"
304 self.text_da += "inputvariables_config = {}\n"
305 self.text_da += "inputvariables_config['Order'] =['adao_default']\n"
306 self.text_da += "inputvariables_config['adao_default'] = -1\n"
307 self.text_da += "study_config['InputVariables'] = inputvariables_config\n"
310 if "__"+self.type_of_study+"__OutputVariables__NAMES" in self.dictMCVal.keys():
313 if isinstance(self.dictMCVal["__"+self.type_of_study+"__OutputVariables__NAMES"], type("")):
314 names.append(self.dictMCVal["__"+self.type_of_study+"__OutputVariables__NAMES"])
316 names = self.dictMCVal["__"+self.type_of_study+"__OutputVariables__NAMES"]
317 if isinstance(self.dictMCVal["__"+self.type_of_study+"__OutputVariables__SIZES"], type(1)):
318 sizes.append(self.dictMCVal["__"+self.type_of_study+"__OutputVariables__SIZES"])
320 sizes = self.dictMCVal["__"+self.type_of_study+"__OutputVariables__SIZES"]
322 self.text_da += "outputvariables_config = {}\n"
323 self.text_da += "outputvariables_config['Order'] = %s\n" % list(names)
324 for name, size in zip(names, sizes):
325 self.text_da += "outputvariables_config['%s'] = %s\n" % (name,size)
326 self.text_da += "study_config['OutputVariables'] = outputvariables_config\n"
328 self.text_da += "outputvariables_config = {}\n"
329 self.text_da += "outputvariables_config['Order'] = ['adao_default']\n"
330 self.text_da += "outputvariables_config['adao_default'] = -1\n"
331 self.text_da += "study_config['OutputVariables'] = outputvariables_config\n"
333 def add_observers(self):
335 observer = self.dictMCVal["__"+self.type_of_study+"__Observers__SELECTION"]
336 if isinstance(observer, type("")):
337 self.add_observer_in_dict(observer, observers)
339 for observer in self.dictMCVal["__"+self.type_of_study+"__Observers__SELECTION"]:
340 self.add_observer_in_dict(observer, observers)
342 # Write observers in the python command file
344 self.text_da += "observers = {}\n"
345 for observer in observers.keys():
347 self.text_da += "observers[\"" + observer + "\"] = {}\n"
348 self.text_da += "observers[\"" + observer + "\"][\"number\"] = " + str(number) + "\n"
349 self.text_da += "observers[\"" + observer + "\"][\"nodetype\"] = \"" + observers[observer]["nodetype"] + "\"\n"
350 if observers[observer]["nodetype"] == "String":
351 self.text_da += "observers[\"" + observer + "\"][\"String\"] = \"\"\"" + observers[observer]["script"] + "\"\"\"\n"
353 self.text_da += "observers[\"" + observer + "\"][\"Script\"] = \"" + observers[observer]["file"] + "\"\n"
354 if "scheduler" in observers[observer].keys():
355 self.text_da += "observers[\"" + observer + "\"][\"scheduler\"] = \"\"\"" + observers[observer]["scheduler"] + "\"\"\"\n"
356 if "info" in observers[observer].keys():
357 self.text_da += "observers[\"" + observer + "\"][\"info\"] = \"\"\"" + observers[observer]["info"] + "\"\"\"\n"
358 self.text_da += "study_config['Observers'] = observers\n"
360 def add_observer_in_dict(self, observer, observers):
362 Add observer in the observers dict.
364 observers[observer] = {}
365 observers[observer]["name"] = observer
366 observer_eficas_name = "__"+self.type_of_study+"__Observers__" + observer + "__" + observer + "_data__"
368 node_type_key_name = observer_eficas_name + "NodeType"
369 observers[observer]["nodetype"] = self.dictMCVal[node_type_key_name]
371 # NodeType script/file
372 if observers[observer]["nodetype"] == "String":
373 observers[observer]["script"] = self.dictMCVal[observer_eficas_name + "PythonScript__Value"]
375 observers[observer]["file"] = self.dictMCVal[observer_eficas_name + "UserFile__Value"]
378 scheduler_key_name = observer_eficas_name + "Scheduler"
379 if scheduler_key_name in self.dictMCVal.keys():
380 observers[observer]["scheduler"] = self.dictMCVal[scheduler_key_name]
383 info_key_name = observer_eficas_name + "Info"
384 if info_key_name in self.dictMCVal.keys():
385 observers[observer]["info"] = self.dictMCVal[info_key_name]