1 #@ AJOUT OpenturnsSolver Macro
2 # -*- coding: iso-8859-1 -*-
6 Ce module contient le generateur XML pour Openturns
10 #---------------------
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
17 # 2 : code : 0 : facultatif, 1 : obligatoire
18 # Type est sur la meme ligne que Name
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 ),
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 :
36 # 1 : milieu de ligne si le MC est present
37 # 2 : milieu de ligne si le MC n est pas present
39 # 4 : code : 0 : facultatif, 1 : obligatoire sans defaut
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">',
48 "GradientName" : ( '\n\n <!-- The gradient of the function -->',
49 '\n <gradient provided="yes">',
50 '\n <gradient provided="no">',
54 "HessianName" : ( '\n\n <!-- The hessian of the function wrapper -->',
55 '\n <hessian provided="yes">',
56 '\n <hessian provided="no">' ,
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
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 ),
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
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>' ),
95 #==========================
96 # La classe de creation XML
97 #==========================
102 Generation du fichier XML
104 def __init__ (self, DictMCVal, ListeVariables, DictLois ) :
105 #---------------------------------------------------------#
107 self.DictMCVal = DictMCVal
108 self.ListeVariables = ListeVariables
109 self.DictLois = DictLois
114 Pilotage general de la creation du fichier XML
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()
126 def CreeEntete (self) :
127 #---------------------#
129 La variable DTDDirectory doit etre dans DictMCVal
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'
141 def CreeWrapperAndPath (self) :
142 #-----------------------------#
143 texte = ' <!-- The path of the shared object -->\n'
145 if self.DictMCVal.has_key("WrapperPath") :
146 texte += self.DictMCVal["WrapperPath"]
148 print "*********************************************"
149 print "* ERREUR GENERATION XML *"
150 print "* champ WrapperPath non rempli *"
151 print "*********************************************"
152 texte += '</path>\n\n\n'
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>'
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]
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'
179 texte += '\n </variable-list>\n'
180 texte += ' <!-- End of variable description -->\n'
183 def CreeLibrary (self) :
184 #----------------------#
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]
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'
204 def CreeFile (self) :
205 #-------------------#
209 texte = '\n <external-code>\n'
210 texte += '\n <!-- Those data are external to the platform (input files, etc.)-->\n'
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'
223 def CreeWrapMode (self) :
224 #-----------------------#
228 texte = '\n <!-- Transfert data mode through Wrapper -->'
230 for MC in OrdreWrapMode :
231 if self.DictMCVal.has_key(MC) :
232 texte += dictWrMXML[MC][0] + self.DictMCVal[MC] + dictWrMXML[MC][1]
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'
244 def CreeCommande (self) :
245 #-----------------------#
248 On lui passera en argument les options supplementaires eventuelles
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
259 texte += '# no command'
260 texte +='</command>\n'
261 texte +='\n </external-code>\n'
262 texte +='\n</wrapper>\n'