1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-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
21 Ce module contient le generateur XML pour Openturns
25 from Extensions.i18n import tr
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
32 VariableTypeByName = {
33 "in" : openturns.WrapperDataVariableType.IN,
34 "out" : openturns.WrapperDataVariableType.OUT,
35 None : openturns.WrapperDataVariableType.IN,
39 "in" : openturns.WrapperDataFileType.IN,
40 "out" : openturns.WrapperDataFileType.OUT,
41 None : openturns.WrapperDataFileType.IN,
44 SymbolProvidedByName = {
45 "no" : openturns.WrapperSymbolProvided.NO,
46 "yes" : openturns.WrapperSymbolProvided.YES,
47 None : openturns.WrapperSymbolProvided.NO,
50 WrapperStateByName = {
51 "shared" : openturns.WrapperState.SHARED,
52 "specific" : openturns.WrapperState.SPECIFIC,
53 None : openturns.WrapperState.SPECIFIC,
57 "static-link" : openturns.WrapperMode.STATICLINK,
58 "dynamic-link" : openturns.WrapperMode.DYNAMICLINK,
59 "fork" : openturns.WrapperMode.FORK,
60 None : openturns.WrapperMode.FORK,
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,
72 #==========================
73 # La classe de creation XML
74 #==========================
79 Generation du fichier XML
81 def __init__ (self, appli, DictMCVal, DictVariables ) :
82 self.DictMCVal = DictMCVal
83 self.DictVariables = DictVariables
88 Pilotage general de la creation du fichier XML
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() )
100 wrapper=openturns.WrapperFile()
101 wrapper.setWrapperData( data )
106 class __variable_ordering:
107 def __init__ (self, dictVar) :
108 self.dictVar = dictVar
110 def __call__(self, a, b):
111 return self.dictVar[a]['numOrdre'] - self.dictVar[b]['numOrdre']
113 def VariableList (self) :
115 Ecrit la liste des variables
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] ) )
122 def Variable (self, var, dictVar) :
124 Ecrit le parametrage d une variable
126 variable = openturns.WrapperDataVariable()
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' ]
136 def FunctionDefinition (self) :
138 Ecrit la description de la Fonction
140 func = openturns.WrapperFunctionDescription()
141 func.name_ = self.GetMCVal( 'FunctionName', '' )
142 if (len(func.name_) != 0) : func.provided_ = SymbolProvidedByName[ 'yes' ]
145 def GradientDefinition (self) :
147 Ecrit la description du Gradient
149 grad = openturns.WrapperFunctionDescription()
150 grad.name_ = self.GetMCVal( 'GradientName', '' )
151 if (len(grad.name_) != 0) : grad.provided_ = SymbolProvidedByName[ 'yes' ]
154 def HessianDefinition (self) :
156 Ecrit la description de la Hessienne
158 hess = openturns.WrapperFunctionDescription()
159 hess.name_ = self.GetMCVal( 'HessianName', '' )
160 if (len(hess.name_) != 0) : hess.provided_ = SymbolProvidedByName[ 'yes' ]
165 def FileList (self) :
167 Ecrit la liste des fichiers
169 fileList = openturns.WrapperDataFileCollection()
170 for dictFile in self.GetMCVal('Files', []) :
171 fileList.add( self.File( dictFile ) )
174 def File (self, dictFile ) :
176 Ecrit le parametrage d un fichier
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') :
186 fich.subst_ = string.join( dictFile[ 'Subst' ], ',' )
189 def Parameters (self) :
191 Ecrit les parametres de couplage au code externe
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') ]
204 def FrameworkData (self) :
206 Ecrit les donnees liees a l utilisation d un framework englobant
208 framework = openturns.WrapperFrameworkData()
209 # framework.studycase_ = "12:23:34"
210 # framework.componentname_ = self.GetMCVal('SolverComponentName', 'UNDEFINED')
211 CN = self.GetMCVal('SolverComponentName', 'UNDEFINED')
213 framework.componentname_ = CN
217 # ---------------------------------------------------------------------------------
220 def GetTag (self, tag) :
222 Recupere la chaine associee au tag dans la table dictTagsXML.
223 Leve une exception si le tag n est pas trouve
225 if ( dictTagsXML.has_key(tag) ) :
226 return dictTagsXML[tag]
228 raise KeyError, tr("Tag %s non-defini. Ceci est un bogue interne. en informer les developpeurs.", tag)
231 def GetMCVal (self, MC, default = None, mandatory = False) :
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
236 if ( self.DictMCVal.has_key(MC) and self.DictMCVal[MC] != None ) :
237 return self.DictMCVal[MC]
240 raise KeyError, tr(" Le mot-cle %s est obligatoire.", MC)