Salome HOME
7452d1fa9c80aa45a17c7bd1ccd93b3befa62b33
[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
9 #  Les variables
10 #---------------------
11
12 # OrdreVariable contient l'ordre des MC pour definir une variable en XML
13 # dictMCXML a pour cle le nom du MC dans la commande,
14 # Il contient aussi une liste donnant les informations suivantes
15 #       0 : debut de ligne
16 #       1 : fin de ligne
17 #       2 : code : 0 : facultatif, 1 : obligatoire 
18 # Type est sur la meme ligne que Name
19
20 OrdreVariable = ( 'Name', 'Type', 'Comment', 'Unit', 'Regexp', 'Format' )
21 dictMCXML = { "Name"    : ( '        <variable id="',  '" ',           1 ), 
22               "Type"    : ( 'type ="'               ,  '">\n',         1 ),
23               "Comment" : ( '          <comment>'   ,  '</comment>\n', 0 ),
24               "Unit"    : ( '          <unit>'      ,  '</unit>\n',    0 ),
25               "Regexp"  : ( '          <regexp>'    ,  '</regexp>\n',  0 ),
26               "Format"  : ( '          <format>'    ,  '</format>\n',  0 ),
27               }
28
29
30 #  Les fonctions du wrapper
31 #--------------------------------
32 # OrdreLibrary contient l'ordre des MC pour definir la partie library en XML
33 # dictLibXML a pour cle le nom du MC dans la commande,
34 # Il contient aussi une liste donnant les informations suivantes :
35 #       0 : debut de ligne
36 #       1 : milieu de ligne si le MC est present
37 #       2 : milieu de ligne si le MC n est pas present
38 #       3 : fin de ligne
39 #   4 : code :  0 : facultatif, 1 : obligatoire sans defaut
40
41 OrdreLibrary = ( 'FunctionName', 'GradientName', 'HessianName' )
42 dictLibXML = { "FunctionName" : ( '\n\n      <!-- The function that we try to execute through the wrapper -->',
43                                   '\n      <function provided="yes">',
44                                   '\n      <function provided="no">',
45                                     '</function>',
46                                   1,
47                                   ),
48                "GradientName" : ( '\n\n      <!-- The gradient of the function -->',
49                                   '\n      <gradient provided="yes">',
50                                   '\n      <gradient provided="no">',
51                                   '</gradient>',
52                                   0,
53                                   ),
54                "HessianName"  : ( '\n\n      <!-- The hessian of the function wrapper -->',
55                                   '\n      <hessian provided="yes">',
56                                   '\n      <hessian provided="no">' ,
57                                   '</hessian>\n\n',
58                                   0,
59                                   ),
60                }
61
62 #  Les communications du wrapper
63 #--------------------------------
64 # OrdreWrapMode contient l'ordre des MC pour definir la partie WrapMode en XML
65 # dictWrMXML a pour cle le nom du MC dans la commande,
66 # Il contient aussi une liste donnant les informations suivantes
67 #       0 : debut de ligne
68 #       1 : fin de ligne
69 #       2 : code : 0 : facultatif, 1 : obligatoire sans defaut, 2 : obligatoire avec defaut
70 #       3 : valeur par defaut eventuelle
71 OrdreWrapMode = ( 'WrapCouplingMode', 'State', 'InDataTransfer', 'OutDataTransfer' )
72 dictWrMXML = { "WrapCouplingMode" : ( '\n    <wrap-mode type="'        , '"'     ,  1 ),
73                "State"            : ( ' state="'                       , '">\n'  ,  2,  'shared">\n' ),
74                "InDataTransfer"   : ( '      <in-data-transfer mode="' , '" />\n',  0 ),
75                "OutDataTransfer"  : ( '      <out-data-transfer mode="', '" />\n',  0 ),
76                }
77
78 # Les fichiers d'echange du wrapper
79 #-----------------------------------------
80 # OrdreExchangeFile contient l'ordre des MC pour definir la partie OrdreExchangeFile en XML
81 # dictFilXML a pour cle le nom du MC dans la commande,
82 # Il contient aussi une liste donnant les informations suivantes
83 #       0 : debut de ligne
84 #       1 : fin de ligne
85 #       2 : code : 0 : facultatif, 1 : obligatoire sans defaut
86 OrdreExchangeFile = ( 'Id', 'Type', 'Name', 'Path', 'Subst' )
87 dictFilXML = { "Id"    : ( '\n      <file id="', '">'       ),
88                "Type"  : ( ' type="'           , '">'       ),
89                "Name"  : ( '\n        <name>'  , '</name>'  ),
90                "Path"  : ( '\n        <path>'  , '</path>'  ),
91                "Subst" : ( '\n        <subst>' , '</subst>' ),
92                }
93
94
95 #==========================
96 # La classe de creation XML 
97 #==========================
98
99 class XMLGenerateur :
100
101   '''
102   Generation du fichier XML
103   '''
104   def __init__ (self, DictMCVal, ListeVariables, DictLois ) :
105   #---------------------------------------------------------#
106     self.ListeFiles = []
107     self.DictMCVal = DictMCVal
108     self.ListeVariables = ListeVariables
109     self.DictLois = DictLois 
110
111   def CreeXML (self) :
112   #------------------#
113     '''
114     Pilotage general de la creation du fichier XML
115     '''
116     self.texte  = self.CreeEntete()
117     self.texte += self.CreeWrapperAndPath()
118     self.texte += self.CreeVariables()
119     self.texte += self.CreeLibrary()
120     self.texte += self.CreeFile()
121     self.texte += self.CreeWrapMode()
122     self.texte += self.CreeCommande()
123     return self.texte
124
125
126   def CreeEntete (self) :
127   #---------------------#
128     '''
129     La variable DTDDirectory doit etre dans DictMCVal
130     '''
131     #PN a faire : recuperer DTDDirectory dans editeur.ini
132     texte = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
133     if self.DictMCVal.has_key("DTDDirectory")  :
134       aux = os.path.join(self.DictMCVal["DTDDirectory"], "wrapper.dtd")
135       texte += "<!DOCTYPE wrapper SYSTEM \"" + aux + "\">\n"
136     texte += '\n<wrapper>\n'
137     texte += '\n  <library>\n\n'
138     return texte
139
140
141   def CreeWrapperAndPath (self) :
142   #-----------------------------#
143     texte  = '    <!-- The path of the shared object -->\n'
144     texte += '    <path>'
145     if self.DictMCVal.has_key("WrapperPath") :
146       texte += self.DictMCVal["WrapperPath"]
147     else :
148       print "*********************************************"
149       print "*          ERREUR GENERATION XML            *"
150       print "*       champ WrapperPath non rempli        *"
151       print "*********************************************"
152     texte += '</path>\n\n\n'
153
154     return texte
155
156
157   def CreeVariables (self) :
158   #------------------------#
159     texte  ='    <!-- This section describes all exchanges data between the wrapper and the platform -->\n'
160     texte +='    <description>\n\n'
161     texte +='      <!-- Those variables are substituted in the files above -->\n'
162     texte +='      <!-- The order of variables is the order of the arguments of the function -->\n\n'
163     texte += '      <variable-list>'
164
165     numvar = 0
166     for DictVariable in self.ListeVariables :
167       texte += "\n        <!-- The definition of variable # "+  str(numvar) + " -->\n"
168       for MC in OrdreVariable :
169         if DictVariable.has_key(MC) :
170           texte += dictMCXML[MC][0] + DictVariable[MC] + dictMCXML[MC][1]
171         else :
172           if dictMCXML[MC][2] :
173             print "**************************************************"
174             print "*            ERREUR GENERATION XML               *"
175             print "*  champ obligatoire non rempli pour variable    *"
176             print "**************************************************"
177       texte += '        </variable>\n'
178       numvar += 1
179     texte += '\n      </variable-list>\n'
180     texte += '      <!-- End of variable description -->\n'
181     return texte
182
183   def CreeLibrary (self) :
184   #----------------------#
185     '''
186     Librairies
187     '''
188     texte = ""
189     for MC in OrdreLibrary :
190       texte += dictLibXML[MC][0]
191       if self.DictMCVal.has_key(MC) :
192         texte += dictLibXML[MC][1] + self.DictMCVal[MC] + dictLibXML[MC][3]
193       else :
194         texte += dictLibXML[MC][2] + dictLibXML[MC][3]
195         if dictLibXML[MC][4] :
196           print "**************************************************"
197           print "*            ERREUR GENERATION XML               *"
198           print "*  champ obligatoire non rempli pour wrapper     *"
199           print "**************************************************"
200     texte += '    </description>\n\n'
201     texte += '  </library>\n'
202     return texte
203
204   def CreeFile (self) :
205   #-------------------#
206     '''
207     Fichiers
208     '''
209     texte  = '\n  <external-code>\n'
210     texte += '\n    <!-- Those data are external to the platform (input files, etc.)-->\n'
211     texte += '    <data>\n'
212
213     if self.DictMCVal.has_key("exchange_file") :
214        for dico in self.DictMCVal["exchange_file"] :
215           texte += "\n      <!-- The definition of file  -->"
216           for MC in OrdreExchangeFile :
217               if dico.has_key(MC) :
218                  texte += dictFilXML[MC][0] + dico[MC] + dictFilXML[MC][1]
219           texte += "\n      </file>\n"
220     texte += '\n    </data>\n'
221     return texte
222
223   def CreeWrapMode (self) :
224   #-----------------------#
225     '''
226     WrapMode
227     '''
228     texte = '\n    <!-- Transfert data mode through Wrapper -->'
229
230     for MC in OrdreWrapMode :
231       if self.DictMCVal.has_key(MC) :
232         texte += dictWrMXML[MC][0] + self.DictMCVal[MC] + dictWrMXML[MC][1]
233       else :
234         if dictWrMXML[MC][2] == 2 :
235           texte += dictWrMXML[MC][0] + dictWrMXML[MC][3]
236         elif dictWrMXML[MC][2] == 1 :
237           print "**************************************************"
238           print "*            ERREUR GENERATION XML               *"
239           print "*  champ obligatoire non rempli pour external    *"
240           print "**************************************************"
241     texte += '    </wrap-mode>\n\n'
242     return texte
243
244   def CreeCommande (self) :
245   #-----------------------#
246     '''
247     La commande
248     On lui passera en argument les options supplementaires eventuelles
249     '''
250     texte  = '    <!-- Command -->\n'
251     texte += '    <command>'
252     if self.DictMCVal.has_key("Command") :
253       texte += self.DictMCVal["Command"]
254       if self.DictMCVal.has_key("ArguCommande") :
255          for argument in self.DictMCVal[ArguCommande] :
256              texte += " " + argument
257          texte += "\n"
258     else :
259       texte += '# no command'
260     texte +='</command>\n'
261     texte +='\n  </external-code>\n'
262     texte +='\n</wrapper>\n'
263     return texte
264