Salome HOME
Telemac 1eres lectures/ecriture
[tools/eficas.git] / generator / OpenturnsXML.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013   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 """
21 Ce module contient le generateur XML pour Openturns
22 """
23 import sys
24 print sys.path
25 from Extensions.i18n import tr
26 import openturns
27
28 # Dictionnaires de conversion des valeurs lues dans EFICAS
29 # en valeurs reconnues par Open TURNS
30 # Les clefs 'None' sont les valeurs par defaut
31
32 VariableTypeByName = {
33   "in"  : openturns.WrapperDataVariableType.IN,
34   "out" : openturns.WrapperDataVariableType.OUT,
35   None  :  openturns.WrapperDataVariableType.IN,
36   }
37
38 FileTypeByName = {
39   "in"  : openturns.WrapperDataFileType.IN,
40   "out" : openturns.WrapperDataFileType.OUT,
41   None  : openturns.WrapperDataFileType.IN,
42   }
43
44 SymbolProvidedByName = {
45   "no"  : openturns.WrapperSymbolProvided.NO,
46   "yes" : openturns.WrapperSymbolProvided.YES,
47   None  : openturns.WrapperSymbolProvided.NO,
48   }
49
50 WrapperStateByName = {
51   "shared"   : openturns.WrapperState.SHARED,
52   "specific" : openturns.WrapperState.SPECIFIC,
53   None       : openturns.WrapperState.SPECIFIC,
54   }
55
56 WrapperModeByName = {
57   "static-link"  : openturns.WrapperMode.STATICLINK,
58   "dynamic-link" : openturns.WrapperMode.DYNAMICLINK,
59   "fork"         : openturns.WrapperMode.FORK,
60   None           : openturns.WrapperMode.FORK,
61   }
62
63 WrapperDataTransferByName = {
64   "files"     : openturns.WrapperDataTransfer.FILES,
65   "pipe"      : openturns.WrapperDataTransfer.PIPE,
66   "arguments" : openturns.WrapperDataTransfer.ARGUMENTS,
67   "socket"    : openturns.WrapperDataTransfer.SOCKET,
68   "corba"     : openturns.WrapperDataTransfer.CORBA,
69   None        : openturns.WrapperDataTransfer.FILES,
70   }
71
72 #==========================
73 # La classe de creation XML 
74 #==========================
75
76 class XMLGenerateur :
77
78   '''
79   Generation du fichier XML
80   '''
81   def __init__ (self, appli, DictMCVal, DictVariables ) :
82     self.DictMCVal = DictMCVal
83     self.DictVariables = DictVariables
84     self.appli = appli
85
86   def CreeXML (self) :
87     '''
88     Pilotage general de la creation du fichier XML
89     '''
90     data = openturns.WrapperData()
91     data.setLibraryPath( self.GetMCVal('WrapperPath','') )
92     data.setVariableList( self.VariableList() )
93     data.setFunctionDescription( self.FunctionDefinition() )
94     data.setGradientDescription( self.GradientDefinition() )
95     data.setHessianDescription(  self.HessianDefinition()  )
96     data.setFileList( self.FileList() )
97     data.setParameters( self.Parameters() )
98     #data.setFrameworkData( self.FrameworkData() )
99     
100     wrapper=openturns.WrapperFile()
101     wrapper.setWrapperData( data )
102     
103     return wrapper
104
105
106   class __variable_ordering:
107     def __init__ (self, dictVar) :
108       self.dictVar = dictVar
109       
110     def __call__(self, a, b):
111       return self.dictVar[a]['numOrdre'] - self.dictVar[b]['numOrdre']
112   
113   def VariableList (self) :
114     '''
115     Ecrit la liste des variables
116     '''
117     varList = openturns.WrapperDataVariableCollection()
118     for var in sorted( self.DictVariables.keys(), self.__variable_ordering( self.DictVariables ) ) :
119       varList.add( self.Variable( var, self.DictVariables[var] ) )
120     return varList
121
122   def Variable (self, var, dictVar) :
123     '''
124     Ecrit le parametrage d une variable
125     '''
126     variable = openturns.WrapperDataVariable()
127     variable.id_ = var
128     if dictVar[ 'Type' ] in VariableTypeByName.keys() :
129       variable.type_ = VariableTypeByName[ dictVar[ 'Type' ] ]
130     if dictVar.has_key('Comment')   : variable.comment_ = dictVar[ 'Comment' ]
131     if dictVar.has_key('Unit')      : variable.unit_    = dictVar[ 'Unit'    ]
132     if dictVar.has_key('Regexp')    : variable.regexp_  = dictVar[ 'Regexp'  ]
133     if dictVar.has_key('Format')    : variable.format_  = dictVar[ 'Format'  ]
134     return variable
135
136   def FunctionDefinition (self) :
137     '''
138     Ecrit la description de la Fonction
139     '''
140     func = openturns.WrapperFunctionDescription()
141     func.name_ = self.GetMCVal( 'FunctionName', '' )
142     if (len(func.name_) != 0) : func.provided_ = SymbolProvidedByName[ 'yes' ]
143     return func
144   
145   def GradientDefinition (self) :
146     '''
147     Ecrit la description du Gradient
148     '''
149     grad = openturns.WrapperFunctionDescription()
150     grad.name_ = self.GetMCVal( 'GradientName', '' )
151     if (len(grad.name_) != 0) : grad.provided_ = SymbolProvidedByName[ 'yes' ]
152     return grad
153   
154   def HessianDefinition (self) :
155     '''
156     Ecrit la description de la Hessienne
157     '''
158     hess = openturns.WrapperFunctionDescription()
159     hess.name_ = self.GetMCVal( 'HessianName', '' )
160     if (len(hess.name_) != 0) : hess.provided_ = SymbolProvidedByName[ 'yes' ]
161     return hess
162   
163
164
165   def FileList (self) :
166     '''
167     Ecrit la liste des fichiers
168     '''
169     fileList = openturns.WrapperDataFileCollection()
170     for dictFile in self.GetMCVal('Files', []) :
171       fileList.add( self.File( dictFile ) )
172     return fileList
173
174   def File (self, dictFile ) :
175     '''
176     Ecrit le parametrage d un fichier
177     '''
178     fich = openturns.WrapperDataFile()
179     fich.id_ = dictFile[ 'Id' ]
180     if dictFile[ 'Type' ] in FileTypeByName.keys() :
181       fich.type_ = FileTypeByName[ dictFile[ 'Type' ] ]
182     if dictFile.has_key('Name')   : fich.name_  = dictFile[ 'Name' ]
183     if dictFile.has_key('Path')   : fich.path_  = dictFile[ 'Path' ]
184     if dictFile.has_key('Subst')  :
185       import string
186       fich.subst_ = string.join( dictFile[ 'Subst' ], ',' )
187     return fich
188
189   def Parameters (self) :
190     '''
191     Ecrit les parametres de couplage au code externe
192     '''
193     parameters = openturns.WrapperParameter()
194     parameters.mode_  = WrapperModeByName[ self.GetMCVal('WrapCouplingMode') ]
195     if (parameters.mode_ == openturns.WrapperMode.FORK ):
196       parameters.command_ = self.GetMCVal('Command')
197       userPrefix = self.GetMCVal('UserPrefix', None)
198       if userPrefix != None : parameters.userPrefix_ = userPrefix
199     parameters.state_ = WrapperStateByName[ self.GetMCVal('State') ]
200     parameters.in_    = WrapperDataTransferByName[ self.GetMCVal('InDataTransfer') ]
201     parameters.out_   = WrapperDataTransferByName[ self.GetMCVal('OutDataTransfer') ]
202     return parameters
203   
204   def FrameworkData (self) :
205     '''
206     Ecrit les donnees liees a l utilisation d un framework englobant
207     '''
208     framework = openturns.WrapperFrameworkData()
209 #   framework.studycase_ = "12:23:34"
210 #   framework.componentname_ = self.GetMCVal('SolverComponentName', 'UNDEFINED')
211     CN = self.GetMCVal('SolverComponentName', 'UNDEFINED')
212     print 'CN = %s', CN
213     framework.componentname_ = CN
214     return framework
215
216
217   # ---------------------------------------------------------------------------------
218
219
220   def GetTag (self, tag) :
221     '''
222     Recupere la chaine associee au tag dans la table dictTagsXML.
223     Leve une exception si le tag n est pas trouve
224     '''
225     if ( dictTagsXML.has_key(tag) ) :
226       return dictTagsXML[tag]
227     else :
228       raise KeyError, tr("Tag %s non-defini. Ceci est un bogue interne. en informer les developpeurs.", tag)
229     pass
230   
231   def GetMCVal (self, MC, default = None, mandatory = False) :
232     '''
233     Recupere la chaine associee au MC dans la table DictMCVal.
234     Leve une exception si le MC n est pas trouve et mandatory vaut True
235     '''
236     if ( self.DictMCVal.has_key(MC) and self.DictMCVal[MC] != None ) :
237       return self.DictMCVal[MC]
238     else :
239       if ( mandatory ) :
240         raise KeyError, tr(" Le mot-cle %s est obligatoire.", MC)
241       else :
242         return default
243     pass