Salome HOME
CCAR: rabattre la version V1_15a4 dans la branche principale
[tools/eficas.git] / generator / OpenturnsXML.py
1 #@ AJOUT OpenturnsSolver Macro
2 # -*- coding: iso-8859-1 -*-
3 # RESPONSABLE
4
5 """
6 Ce module contient le generateur XML pour Openturns
7 """
8 import sys
9 print sys.path
10 import openturns
11
12 # Dictionnaires de conversion des valeurs lues dans EFICAS
13 # en valeurs reconnues par Open TURNS
14 # Les clefs 'None' sont les valeurs par defaut
15
16 VariableTypeByName = {
17   "in"  : openturns.WrapperDataVariableType.IN,
18   "out" : openturns.WrapperDataVariableType.OUT,
19   None  :  openturns.WrapperDataVariableType.IN,
20   }
21
22 FileTypeByName = {
23   "in"  : openturns.WrapperDataFileType.IN,
24   "out" : openturns.WrapperDataFileType.OUT,
25   None  : openturns.WrapperDataFileType.IN,
26   }
27
28 SymbolProvidedByName = {
29   "no"  : openturns.WrapperSymbolProvided.NO,
30   "yes" : openturns.WrapperSymbolProvided.YES,
31   None  : openturns.WrapperSymbolProvided.NO,
32   }
33
34 WrapperStateByName = {
35   "shared"   : openturns.WrapperState.SHARED,
36   "specific" : openturns.WrapperState.SPECIFIC,
37   None       : openturns.WrapperState.SPECIFIC,
38   }
39
40 WrapperModeByName = {
41   "static-link"  : openturns.WrapperMode.STATICLINK,
42   "dynamic-link" : openturns.WrapperMode.DYNAMICLINK,
43   "fork"         : openturns.WrapperMode.FORK,
44   None           : openturns.WrapperMode.FORK,
45   }
46
47 WrapperDataTransferByName = {
48   "files"     : openturns.WrapperDataTransfer.FILES,
49   "pipe"      : openturns.WrapperDataTransfer.PIPE,
50   "arguments" : openturns.WrapperDataTransfer.ARGUMENTS,
51   "socket"    : openturns.WrapperDataTransfer.SOCKET,
52   "CORBA"     : openturns.WrapperDataTransfer.CORBA,
53   None        : openturns.WrapperDataTransfer.FILES,
54   }
55
56 #==========================
57 # La classe de creation XML 
58 #==========================
59
60 class XMLGenerateur :
61
62   '''
63   Generation du fichier XML
64   '''
65   def __init__ (self, appli, DictMCVal, DictVariables ) :
66     self.DictMCVal = DictMCVal
67     self.DictVariables = DictVariables
68     self.appli = appli
69
70   def CreeXML (self) :
71     '''
72     Pilotage general de la creation du fichier XML
73     '''
74     data = openturns.WrapperData()
75     data.setLibraryPath( self.GetMCVal('WrapperPath','') )
76     data.setVariableList( self.VariableList() )
77     data.setFunctionDescription( self.FunctionDefinition() )
78     data.setGradientDescription( self.GradientDefinition() )
79     data.setHessianDescription(  self.HessianDefinition()  )
80     data.setFileList( self.FileList() )
81     data.setParameters( self.Parameters() )
82     
83     wrapper=openturns.WrapperFile()
84     wrapper.setWrapperData( data )
85     
86     return wrapper
87
88
89   def VariableList (self) :
90     '''
91     Ecrit la liste des variables
92     '''
93     varList = openturns.WrapperDataVariableList()
94     for var in self.DictVariables.keys() :
95       varList.add( self.Variable( var, self.DictVariables[var] ) )
96     return varList
97
98   def Variable (self, var, dictVar) :
99     '''
100     Ecrit le parametrage d une variable
101     '''
102     variable = openturns.WrapperDataVariable()
103     variable.id_ = var
104     if dictVar[ 'Type' ] in VariableTypeByName.keys() :
105       variable.type_ = VariableTypeByName[ dictVar[ 'Type' ] ]
106     if dictVar.has_key('Comment')   : variable.comment_ = dictVar[ 'Comment' ]
107     if dictVar.has_key('Unit')      : variable.unit_    = dictVar[ 'Unit'    ]
108     if dictVar.has_key('Regexp')    : variable.regexp_  = dictVar[ 'Regexp'  ]
109     if dictVar.has_key('Format')    : variable.format_  = dictVar[ 'Format'  ]
110     return variable
111
112   def FunctionDefinition (self) :
113     '''
114     Ecrit la description de la Fonction
115     '''
116     func = openturns.WrapperFunctionDescription()
117     func.name_ = self.GetMCVal( 'FunctionName', '' )
118     if (len(func.name_) != 0) : func.provided_ = SymbolProvidedByName[ 'yes' ]
119     return func
120   
121   def GradientDefinition (self) :
122     '''
123     Ecrit la description du Gradient
124     '''
125     grad = openturns.WrapperFunctionDescription()
126     grad.name_ = self.GetMCVal( 'GradientName', '' )
127     if (len(grad.name_) != 0) : grad.provided_ = SymbolProvidedByName[ 'yes' ]
128     return grad
129   
130   def HessianDefinition (self) :
131     '''
132     Ecrit la description de la Hessienne
133     '''
134     hess = openturns.WrapperFunctionDescription()
135     hess.name_ = self.GetMCVal( 'HessianName', '' )
136     if (len(hess.name_) != 0) : hess.provided_ = SymbolProvidedByName[ 'yes' ]
137     return hess
138   
139
140
141   def FileList (self) :
142     '''
143     Ecrit la liste des fichiers
144     '''
145     fileList = openturns.WrapperDataFileList()
146     for dictFile in self.GetMCVal('Files', []) :
147       fileList.add( self.File( dictFile ) )
148     return fileList
149
150   def File (self, dictFile ) :
151     '''
152     Ecrit le parametrage d un fichier
153     '''
154     fich = openturns.WrapperDataFile()
155     fich.id_ = dictFile[ 'Id' ]
156     if dictFile[ 'Type' ] in FileTypeByName.keys() :
157       fich.type_ = FileTypeByName[ dictFile[ 'Type' ] ]
158     if dictFile.has_key('Name')   : fich.name_  = dictFile[ 'Name' ]
159     if dictFile.has_key('Path')   : fich.path_  = dictFile[ 'Path' ]
160     if dictFile.has_key('Subst')  :
161       import string
162       fich.subst_ = string.join( dictFile[ 'Subst' ], ',' )
163     return fich
164
165   def Parameters (self) :
166     '''
167     Ecrit les parametres de couplage au code externe
168     '''
169     parameters = openturns.WrapperParameter()
170     parameters.mode_  = WrapperModeByName[ self.GetMCVal('WrapCouplingMode') ]
171     parameters.state_ = WrapperStateByName[ self.GetMCVal('State') ]
172     parameters.in_    = WrapperDataTransferByName[ self.GetMCVal('InDataTransfer') ]
173     parameters.out_   = WrapperDataTransferByName[ self.GetMCVal('OutDataTransfer') ]
174     return parameters
175   
176
177
178
179   # ---------------------------------------------------------------------------------
180
181
182   def GetTag (self, tag) :
183     '''
184     Recupere la chaine associee au tag dans la table dictTagsXML.
185     Leve une exception si le tag n est pas trouve
186     '''
187     if ( dictTagsXML.has_key(tag) ) :
188       return dictTagsXML[tag]
189     else :
190       raise KeyError, "Tag '%s' is undefined. This is an internal bug. Report bug to developers" % tag 
191     pass
192   
193   def GetMCVal (self, MC, default = None, mandatory = False) :
194     '''
195     Recupere la chaine associee au MC dans la table DictMCVal.
196     Leve une exception si le MC n est pas trouve et mandatory vaut True
197     '''
198     if ( self.DictMCVal.has_key(MC) and self.DictMCVal[MC] != None ) :
199       return self.DictMCVal[MC]
200     else :
201       if ( mandatory ) :
202         raise KeyError, "Keyword '%s' is mandatory" % MC
203       else :
204         return default
205     pass