Salome HOME
commentaire
[tools/eficas.git] / Traducteur / regles.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2012    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 Définition des règles
22 """
23
24 debug = 0
25
26 #--------------------
27 class ensembleRegles :
28 #--------------------
29     """
30     Ensemble des règles
31     """
32     def __init__(self, liste_regles):
33         self.liste = []
34         for item in liste_regles :
35             args, clefRegle = item
36             r = regle(clefRegle, args)
37             self.liste.append(r)
38
39     def verif(self, commande) :
40         """
41         Vérification
42         """
43         bool = 1
44         for regle in self.liste :
45             result = regle.verif(commande)
46             bool = bool*result
47         return bool
48
49 #--------------------------------
50 class pasDeRegle(ensembleRegles):
51 #--------------------------------
52     """
53     Pas de règle
54     """
55     def __init__(self) :
56         pass
57
58     def verif (self, commande) :
59         """
60         Vérification
61         """
62         return 1
63  
64
65 #------------
66 class regle :
67 #------------
68     """
69     Règle
70     """
71     def __init__(self, clef_regle, args):
72         self.fonction = dictionnaire_regle[clef_regle]
73         self.list_args = args
74         self.bool = 0
75
76     def verif(self, commande):
77         """
78         Vérification
79         """
80         f = self.fonction(self.list_args)
81         return f.verif(commande)
82         
83 #---------------------
84 class existeMCFParmi :
85 #---------------------
86     """
87     Existance du mot-clé facteur parmi la liste
88     """
89     def __init__(self, list_arg):
90         self.listeMCF = list_arg
91
92     def verif(self, commande) :
93         """
94         Vérification
95         """
96         bool = 0
97         for c in commande.childNodes :
98             if c.name in self.listeMCF : 
99                 bool = 1
100                 break
101         return bool
102
103 #---------------------
104 class nexistepasMCFParmi(existeMCFParmi) :
105 #---------------------
106     """
107     Existance du mot-clé facteur parmi la liste
108     """
109     def __init__(self, list_arg):
110         self.listeMCF = list_arg
111
112     def verif(self, commande) :
113         """
114         Vérification
115         """
116         bool = existeMCFParmi.verif(self, commande)
117         if bool : return 0
118         return 1
119         
120 #----------------------
121 class existeMCsousMCF :
122 #----------------------
123     """
124     Existance du mot-clé simple sous le mot-clé facteur
125     """
126     def __init__(self, list_arg):
127         self.liste = list_arg
128         self.MCF = self.liste[0]
129         self.MC = self.liste[1]
130
131     def verif(self, commande):
132         """
133         Vérification
134         """
135         bool = 0
136         for mcf in commande.childNodes :
137             if mcf.name != self.MCF : continue 
138             l = mcf.childNodes[:]
139             l.reverse()
140             for ll in l:
141                 for mc in ll.childNodes:
142                     if mc.name != self.MC : continue
143                     bool = 1
144         return bool
145
146 #----------------------
147 class existeMCsousMCFcourant :
148 #----------------------
149     """
150     Existance du mot-clé simple sous le mot-clé facteur courant
151     """
152     def __init__(self, list_arg):
153         self.liste = list_arg
154         self.MC = self.liste[0]
155
156     def verif(self, mcf):
157         """
158         Vérification
159         """
160         bool = 0
161         l = mcf.childNodes[:]
162         l.reverse()
163         for mc in l:
164             if mc.name != self.MC : continue
165             bool = 1
166         return bool
167
168 #-----------------------------------------
169 class nexistepasMCsousMCF(existeMCsousMCF):
170 #-----------------------------------------
171     """
172     Absence du mot-clé simple sous le mot-clé facteur
173     """
174     def __init__(self, list_arg):
175         existeMCsousMCF.__init__(self, list_arg)
176         
177
178     def verif(self, commande):
179         """
180         Vérification
181         """
182         bool = existeMCsousMCF.verif(self, commande)
183         if bool : return 0
184         return 1
185
186 #-----------------------------------------
187 class nexistepasMCsousMCFcourant(existeMCsousMCFcourant):
188 #-----------------------------------------
189     """
190     Absence du mot-clé simple sous le mot-clé facteur courant
191     """
192     def __init__(self, list_arg):
193         existeMCsousMCFcourant.__init__(self, list_arg)
194         
195
196     def verif(self, commande):
197         """
198         Vérification
199         """
200         bool = existeMCsousMCFcourant.verif(self, commande)
201         if bool : return 0
202         return 1
203
204 #-------------
205 class existe :
206 #--------------
207     """
208     Existance du mot-clé simple
209     """
210     def __init__(self, list_arg):
211         self.genea = list_arg
212
213     def cherche_mot(self, niveau, commande):
214         """
215         Recherche du mot
216         """
217         if commande == None : return 0
218         if niveau == len(self.genea) : return 1
219         texte = self.genea[niveau]
220         for c in commande.childNodes :
221             if c.name == texte : 
222                 niveau = niveau+1
223                 return self.cherche_mot(niveau, c)
224         return None
225
226     def verif(self, commande):
227         """
228         Vérification
229         """
230         bool = self.cherche_mot(0, commande)
231         if bool == None : bool = 0
232         return bool
233
234 #-------------
235 class nexistepas :
236 #--------------
237     """
238     Absence du mot-clé simple
239     """
240     def __init__(self, list_arg):
241         self.genea = list_arg
242
243     def cherche_mot(self, niveau, commande):
244         """
245         Recherche du mot
246         """
247         if commande == None : return 0
248         if niveau    == len(self.genea) : return 1
249         texte = self.genea[niveau]
250         for c in commande.childNodes :
251             if c.name == texte : 
252                 niveau = niveau+1
253                 return self.cherche_mot(niveau, c)
254         return None
255
256     def verif(self, commande):
257         """
258         Vérification
259         """
260         bool = self.cherche_mot(0, commande)
261         if bool : return 0
262         return 1
263
264 #-------------------------------
265 class MCsousMCFaPourValeur :
266 #------------------------------
267     """
268     Égalité du mot-clé simple à une valeur sous le mot-clé facteur
269     """
270     def __init__(self, list_arg):
271         assert (len(list_arg)==4)
272         self.genea = list_arg[0:-2]
273         self.MCF = list_arg[0]
274         self.MC = list_arg[1]
275         self.Val = list_arg[2]
276         self.Jdc = list_arg[3]
277
278     def verif(self, commande):
279         """
280         Vérification
281         """
282         bool = 0
283         for mcf in commande.childNodes :
284             if mcf.name != self.MCF : continue 
285             l = mcf.childNodes[:]
286             l.reverse()
287             for ll in l:
288                 for mc in ll.childNodes:
289                     if mc.name != self.MC : continue
290                     TexteMC = mc.getText(self.Jdc)
291                     if (TexteMC.find(self.Val) < 0 ): continue
292                     bool = 1
293         return bool
294
295 #-------------------------------
296 class MCsousMCFcourantaPourValeur :
297 #------------------------------
298     """
299     Égalité du mot-clé simple à une valeur sous le mot-clé facteur courant
300     """
301     def __init__(self, list_arg):
302         assert (len(list_arg)==3)
303         self.genea = list_arg[0:-1]
304         self.MC = list_arg[0]
305         self.Val = list_arg[1]
306         self.Jdc = list_arg[2]
307
308     def verif(self, mcf):
309         """
310         Vérification
311         """
312         bool = 0
313         l = mcf.childNodes[:]
314         l.reverse()
315         for mc in l:
316             if mc.name != self.MC : continue
317             TexteMC = mc.getText(self.Jdc)
318             if (TexteMC.find(self.Val) < 0 ): continue
319             bool = 1
320         return bool
321
322
323 #-----------------------------
324 class MCsousMCFaPourValeurDansListe :
325 #----------------------------
326     """
327     Égalité du mot-clé simple à une valeur dans une liste 
328     sous le mot-clé facteur
329     """
330     def __init__(self, list_arg):
331         assert (len(list_arg)==4)
332         self.genea = list_arg[0:-2]
333         self.MCF = list_arg[0]
334         self.MC = list_arg[1]
335         self.LVal = list_arg[2]
336         self.Jdc = list_arg[3]
337
338     def verif(self, commande):
339         """
340         Vérification
341         """
342         bool = 0
343         for mcf in commande.childNodes :
344             if mcf.name != self.MCF : continue 
345             l = mcf.childNodes[:]
346             l.reverse()
347             for ll in l:
348                 for mc in ll.childNodes:
349                     if mc.name != self.MC : continue
350                     TexteMC = mc.getText(self.Jdc)
351                     for Val in self.LVal:
352                         if (TexteMC.find(Val) < 0 ): continue
353                         bool = 1
354         return bool        
355
356 #-----------------------------
357 class MCsousMCFcourantaPourValeurDansListe :
358 #----------------------------
359     """
360     Égalité du mot-clé simple à une valeur dans une liste 
361     sous le mot-clé facteur
362     """
363     def __init__(self, list_arg):
364         assert (len(list_arg)==3)
365         self.genea = list_arg[0:-1]
366         self.MC = list_arg[0]
367         self.LVal = list_arg[1]
368         self.Jdc = list_arg[2]
369
370     def verif(self, mcf):
371         """
372         Vérification
373         """
374         bool = 0        
375         l = mcf.childNodes[:]
376         l.reverse()
377         for mc in l:
378             if mc.name != self.MC : continue
379             TexteMC = mc.getText(self.Jdc)
380             for Val in self.LVal:
381                 if (TexteMC.find(Val) < 0 ): continue
382                 bool = 1
383         return bool    
384
385 #-----------------------------------------
386 class MCsousMCFcourantnaPasPourValeurDansListe(MCsousMCFcourantaPourValeurDansListe) :
387 #-----------------------------------------
388     """
389     Non égalité du mot-clé simple à une valeur dans une liste 
390     sous le mot-clé facteur
391     """
392     def __init__(self, list_arg):
393         MCsousMCFcourantaPourValeurDansListe.__init__(self, list_arg)
394         
395
396     def verif(self, commande):
397         bool = MCsousMCFcourantaPourValeurDansListe.verif(self, commande)
398         if bool : return 0
399         return 1
400   
401 #-----------------------------------------
402 class MCsousMCFnaPasPourValeurDansListe(MCsousMCFaPourValeurDansListe) :
403 #-----------------------------------------
404     """
405     Non égalité du mot-clé simple à une valeur dans une liste 
406     sous le mot-clé facteur
407     """
408     def __init__(self, list_arg):
409         MCsousMCFaPourValeurDansListe.__init__(self, list_arg)
410         
411
412     def verif(self, commande):
413         bool = MCsousMCFaPourValeurDansListe.verif(self, commande)
414         if bool : return 0
415         return 1
416   
417 #------------------------------
418 class MCaPourValeur :
419 #------------------------------
420     """
421     Égalité du mot-clé à une valeur
422     """
423     def __init__(self, list_arg):
424         assert (len(list_arg)==3)
425         self.MC = list_arg[0]
426         self.Val = list_arg[1]
427         self.Jdc = list_arg[2]
428
429     def verif(self, commande):
430         """
431         Vérification
432         """
433         bool = 0
434         for mc in commande.childNodes :
435             if mc.name != self.MC : continue 
436             TexteMC = mc.getText(self.Jdc)
437             if (TexteMC.find(self.Val) < 0 ): continue
438             bool = 1
439         return bool
440
441 #-----------------------------------------
442 class MCnaPasPourValeur(MCaPourValeur) :
443 #-----------------------------------------
444     """
445         Non égalité du mot-clé à une valeur 
446     """
447     def __init__(self, list_arg):
448         MCaPourValeur.__init__(self, list_arg)
449
450     def verif(self, commande):
451         """
452         Vérification
453         """
454         bool = MCaPourValeur.verif(self, commande)
455         if bool : return 0
456         return 1
457
458 #------------------------------
459 class MCaPourValeurDansListe :
460 #------------------------------
461     """
462     Égalité du mot-clé à une valeur dans une liste
463     """
464     def __init__(self, list_arg):
465         assert (len(list_arg)==3)
466         self.MC = list_arg[0]
467         self.LVal = list_arg[1]
468         self.Jdc = list_arg[2]
469
470     def verif(self, commande):
471         """
472         Vérification
473         """
474         bool = 0
475         for mc in commande.childNodes :
476             if mc.name != self.MC : continue 
477             TexteMC = mc.getText(self.Jdc)
478             #print "TexteMC=",type(TexteMC),TexteMC
479             #print "LVal=",type(self.LVal),self.LVal
480             for Val in self.LVal:
481                 #print "Val=",type(Val),Val
482                 #print "Find",TexteMC.find(Val)
483                 if (TexteMC.find(Val) < 0 ): continue
484                 bool = 1
485         return bool
486
487 #-----------------------------------------
488 class MCnaPasPourValeurDansListe(MCaPourValeurDansListe) :
489 #-----------------------------------------
490     """
491         Non égalité du mot-clé à une valeur dans une liste
492     """
493     def __init__(self, list_arg):
494         MCaPourValeurDansListe.__init__(self, list_arg)
495
496     def verif(self, commande):
497         """
498         Vérification
499         """
500         bool = MCaPourValeurDansListe.verif(self, commande)
501         if bool : return 0
502         return 1
503
504 dictionnaire_regle = {"existe":existe,
505                       "nexistepas":nexistepas,
506                       "existeMCFParmi":existeMCFParmi,
507                       "nexistepasMCFParmi":nexistepasMCFParmi,
508                       "existeMCsousMCF":existeMCsousMCF,
509                       "nexistepasMCsousMCF":nexistepasMCsousMCF,
510                       "MCsousMCFaPourValeur":MCsousMCFaPourValeur,
511                       "MCsousMCFaPourValeurDansListe":MCsousMCFaPourValeurDansListe,
512                       "MCaPourValeur":MCaPourValeur,
513                       "MCnaPasPourValeur":MCnaPasPourValeur,
514                       "existeMCsousMCFcourant":existeMCsousMCFcourant,
515                       "nexistepasMCsousMCFcourant":nexistepasMCsousMCFcourant,
516                       "MCsousMCFcourantaPourValeur":MCsousMCFcourantaPourValeur,
517                       "MCsousMCFcourantaPourValeurDansListe":MCsousMCFcourantaPourValeurDansListe,
518                       "MCsousMCFcourantnaPasPourValeurDansListe":MCsousMCFcourantnaPasPourValeurDansListe,
519                       "MCsousMCFnaPasPourValeurDansListe":MCsousMCFnaPasPourValeurDansListe,
520                       "MCaPourValeurDansListe":MCaPourValeurDansListe,
521                       "MCnaPasPourValeurDansListe":MCnaPasPourValeurDansListe}
522
523
524 SansRegle = pasDeRegle()