]> SALOME platform Git repositories - modules/adao.git/blob - src/daEficas/generator_adao.py
Salome HOME
Mise à jour des copyrights
[modules/adao.git] / src / daEficas / generator_adao.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010-2011 EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author: André Ribes, andre.ribes@edf.fr, EDF R&D
21
22 print "import generator_adao"
23
24 from generator.generator_python import PythonGenerator
25 import traceback
26 import logging
27
28 def entryPoint():
29    """
30       Retourne les informations necessaires pour le chargeur de plugins
31
32       Ces informations sont retournees dans un dictionnaire
33    """
34    return {
35         # Le nom du plugin
36         'name' : 'adao',
37         # La factory pour creer une instance du plugin
38           'factory' : AdaoGenerator,
39           }
40
41 class AdaoGenerator(PythonGenerator):
42
43   def __init__(self,cr=None):
44     PythonGenerator.__init__(self, cr)
45     self.dictMCVal={}
46     self.text_comm = ""
47     self.text_da = ""
48     self.text_da_status = False
49     self.logger = logging.getLogger('ADAO EFICAS GENERATOR')
50     self.logger.setLevel(logging.INFO)
51     ch = logging.StreamHandler()
52     ch.setLevel(logging.DEBUG)
53     formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
54     ch.setFormatter(formatter)
55     self.logger.addHandler(ch)
56
57   def gener(self,obj,format='brut',config=None):
58     self.logger.debug("method gener called")
59     self.text_comm = PythonGenerator.gener(self, obj, format, config)
60     for key, value in self.dictMCVal.iteritems():
61       self.logger.debug("dictMCVAl %s %s" % (key,value))
62
63     try :
64       self.text_da_status = False
65       self.generate_da()
66       self.text_da_status = True
67     except:
68       self.logger.info("Case is not correct, python command file for YACS schema generation cannot be created")
69       self.logger.debug(self.text_da)
70       self.dictMCVal = {}
71       traceback.print_exc()
72     return self.text_comm
73
74   def writeDefault(self, fn):
75     if self.text_da_status:
76       print "write adao python command file"
77       filename = fn[:fn.rfind(".")] + '.py'
78       f = open( str(filename), 'wb')
79       f.write( self.text_da )
80       f.close()
81
82   def generMCSIMP(self,obj) :
83     """
84     Convertit un objet MCSIMP en texte python
85     """
86     clef=""
87     for i in obj.get_genealogie() :
88       clef=clef+"__"+i
89     self.dictMCVal[clef]=obj.valeur
90
91     s=PythonGenerator.generMCSIMP(self,obj)
92     return s
93
94   def generate_da(self):
95
96     self.text_da += "#-*-coding:iso-8859-1-*- \n"
97     self.text_da += "study_config = {} \n"
98
99     # Extraction de Study_name
100     self.text_da += "study_config[\"Name\"] = \"" + self.dictMCVal["__ASSIMILATION_STUDY__Study_name"] + "\"\n"
101     # Extraction de Debug
102     self.text_da += "study_config[\"Debug\"] = \"" + str(self.dictMCVal["__ASSIMILATION_STUDY__Debug"]) + "\"\n"
103     # Extraction de Algorithm
104     self.text_da += "study_config[\"Algorithm\"] = \"" + self.dictMCVal["__ASSIMILATION_STUDY__Algorithm"] + "\"\n"
105
106     self.add_data("Background")
107     self.add_data("BackgroundError")
108     self.add_data("Observation")
109     self.add_data("ObservationError")
110     self.add_data("ObservationOperator")
111
112     self.add_variables()
113     # Parametres optionnels
114
115     # Extraction du Study_repertory
116     if "__ASSIMILATION_STUDY__Study_repertory" in self.dictMCVal.keys():
117       self.text_da += "study_config[\"Repertory\"] = \"" + self.dictMCVal["__ASSIMILATION_STUDY__Study_repertory"] + "\"\n"
118     # Extraction de AlgorithmParameters
119     if "__ASSIMILATION_STUDY__AlgorithmParameters__INPUT_TYPE" in self.dictMCVal.keys():
120       self.add_algorithm_parameters()
121     # Extraction de UserPostAnalysis
122     if "__ASSIMILATION_STUDY__UserPostAnalysis__FROM" in self.dictMCVal.keys():
123       self.add_UserPostAnalysis()
124     if "__ASSIMILATION_STUDY__UserDataInit__INIT_FILE" in self.dictMCVal.keys():
125       self.add_init()
126
127   def add_data(self, data_name):
128
129     # Extraction des données
130     search_text = "__ASSIMILATION_STUDY__" + data_name + "__"
131     data_type = self.dictMCVal[search_text + "INPUT_TYPE"]
132     search_type = search_text + data_type + "__data__"
133     from_type = self.dictMCVal[search_type + "FROM"]
134     data = ""
135     if from_type == "String":
136       data = self.dictMCVal[search_type + "STRING_DATA__STRING"]
137     elif from_type == "Script":
138       data = self.dictMCVal[search_type + "SCRIPT_DATA__SCRIPT_FILE"]
139     elif from_type == "FunctionDict":
140       data = self.dictMCVal[search_type + "FUNCTIONDICT_DATA__FUNCTIONDICT_FILE"]
141     else:
142       raise Exception('From Type unknown', from_type)
143
144     if from_type == "String" or from_type == "Script":
145       self.text_da += data_name + "_config = {} \n"
146       self.text_da += data_name + "_config[\"Type\"] = \"" + data_type + "\" \n"
147       self.text_da += data_name + "_config[\"From\"] = \"" + from_type + "\" \n"
148       self.text_da += data_name + "_config[\"Data\"] = \"" + data      + "\" \n"
149       self.text_da += "study_config[\"" + data_name + "\"] = " + data_name + "_config \n"
150
151     if from_type == "FunctionDict":
152       self.text_da += data_name + "_FunctionDict = {} \n"
153       self.text_da += data_name + "_FunctionDict[\"Function\"] = [\"Direct\", \"Tangent\", \"Adjoint\"] \n"
154       self.text_da += data_name + "_FunctionDict[\"Script\"] = {} \n"
155       self.text_da += data_name + "_FunctionDict[\"Script\"][\"Direct\"] = \""  + data + "\" \n"
156       self.text_da += data_name + "_FunctionDict[\"Script\"][\"Tangent\"] = \"" + data + "\" \n"
157       self.text_da += data_name + "_FunctionDict[\"Script\"][\"Adjoint\"] = \"" + data + "\" \n"
158       self.text_da += data_name + "_config = {} \n"
159       self.text_da += data_name + "_config[\"Type\"] = \"Function\" \n"
160       self.text_da += data_name + "_config[\"From\"] = \"FunctionDict\" \n"
161       self.text_da += data_name + "_config[\"Data\"] = " + data_name + "_FunctionDict \n"
162       self.text_da += "study_config[\"" + data_name + "\"] = " + data_name + "_config \n"
163
164   def add_algorithm_parameters(self):
165
166     data_name = "AlgorithmParameters"
167     data_type = "Dict"
168     from_type = "Script"
169     data = self.dictMCVal["__ASSIMILATION_STUDY__AlgorithmParameters__Dict__data__SCRIPT_DATA__SCRIPT_FILE"]
170
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     self.text_da += "study_config[\"" + data_name + "\"] = " + data_name + "_config \n"
176
177   def add_init(self):
178
179       init_file_data = self.dictMCVal["__ASSIMILATION_STUDY__UserDataInit__INIT_FILE"]
180       init_target_list = self.dictMCVal["__ASSIMILATION_STUDY__UserDataInit__TARGET_LIST"]
181
182       self.text_da += "Init_config = {} \n"
183       self.text_da += "Init_config[\"Type\"] = \"Dict\" \n"
184       self.text_da += "Init_config[\"From\"] = \"Script\" \n"
185       self.text_da += "Init_config[\"Data\"] = \"" + init_file_data + "\"\n"
186       self.text_da += "Init_config[\"Target\"] = ["
187       for target in init_target_list:
188         self.text_da += "\"" + target + "\","
189       self.text_da += "] \n"
190       self.text_da += "study_config[\"UserDataInit\"] = Init_config \n"
191
192   def add_UserPostAnalysis(self):
193
194     from_type = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__FROM"]
195     data = ""
196     if from_type == "String":
197       data = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__STRING_DATA__STRING"]
198       self.text_da += "Analysis_config = {} \n"
199       self.text_da += "Analysis_config[\"From\"] = \"String\" \n"
200       self.text_da += "Analysis_config[\"Data\"] = \"\"\"" + data + "\"\"\" \n"
201       self.text_da += "study_config[\"UserPostAnalysis\"] = Analysis_config \n"
202     elif from_type == "Script":
203       data = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__SCRIPT_DATA__SCRIPT_FILE"]
204       self.text_da += "Analysis_config = {} \n"
205       self.text_da += "Analysis_config[\"From\"] = \"Script\" \n"
206       self.text_da += "Analysis_config[\"Data\"] = \"" + data + "\" \n"
207       self.text_da += "study_config[\"UserPostAnalysis\"] = Analysis_config \n"
208     else:
209       raise Exception('From Type unknown', from_type)
210
211   def add_variables(self):
212
213     # Input variables
214     if "__ASSIMILATION_STUDY__InputVariables__NAMES" in self.dictMCVal.keys():
215       names = []
216       sizes = []
217       if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__NAMES"], type("")):
218         names.append(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__NAMES"])
219       else:
220         names = self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__NAMES"]
221       if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__SIZES"], type(1)):
222         sizes.append(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__SIZES"])
223       else:
224         sizes = self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__SIZES"]
225
226       self.text_da += "inputvariables_config = {} \n"
227       self.text_da += "inputvariables_config[\"Order\"] = %s \n" % list(names)
228       for name, size in zip(names, sizes):
229         self.text_da += "inputvariables_config[\"%s\"] = %s \n" % (name,size)
230       self.text_da += "study_config[\"InputVariables\"] = inputvariables_config \n"
231     else:
232       self.text_da += "inputvariables_config = {} \n"
233       self.text_da += "inputvariables_config[\"Order\"] =[\"adao_default\"] \n"
234       self.text_da += "inputvariables_config[\"adao_default\"] = -1 \n"
235       self.text_da += "study_config[\"InputVariables\"] = inputvariables_config \n"
236
237     # Output variables
238     if "__ASSIMILATION_STUDY__OutputVariables__NAMES" in self.dictMCVal.keys():
239       names = []
240       sizes = []
241       if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__NAMES"], type("")):
242         names.append(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__NAMES"])
243       else:
244         names = self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__NAMES"]
245       if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__SIZES"], type(1)):
246         sizes.append(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__SIZES"])
247       else:
248         sizes = self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__SIZES"]
249
250       self.text_da += "outputvariables_config = {} \n"
251       self.text_da += "outputvariables_config[\"Order\"] = %s \n" % list(names)
252       for name, size in zip(names, sizes):
253         self.text_da += "outputvariables_config[\"%s\"] = %s \n" % (name,size)
254       self.text_da += "study_config[\"OutputVariables\"] = outputvariables_config \n"
255     else:
256       self.text_da += "outputvariables_config = {} \n"
257       self.text_da += "outputvariables_config[\"Order\"] = [\"adao_default\"] \n"
258       self.text_da += "outputvariables_config[\"adao_default\"] = -1 \n"
259       self.text_da += "study_config[\"OutputVariables\"] = outputvariables_config \n"