Salome HOME
Version cmt-01
[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 MCaPourValeur :
403 #------------------------------
404     """
405     Égalité du mot-clé à une valeur
406     """
407     def __init__(self, list_arg):
408         assert (len(list_arg)==3)
409         self.MC = list_arg[0]
410         self.Val = list_arg[1]
411         self.Jdc = list_arg[2]
412
413     def verif(self, commande):
414         """
415         Vérification
416         """
417         bool = 0
418         for mc in commande.childNodes :
419             if mc.name != self.MC : continue 
420             TexteMC = mc.getText(self.Jdc)
421             if (TexteMC.find(self.Val) < 0 ): continue
422             bool = 1
423         return bool
424
425 #-----------------------------------------
426 class MCnaPasPourValeur(MCaPourValeur) :
427 #-----------------------------------------
428     """
429         Non égalité du mot-clé à une valeur 
430     """
431     def __init__(self, list_arg):
432         MCaPourValeur.__init__(self, list_arg)
433
434     def verif(self, commande):
435         """
436         Vérification
437         """
438         bool = MCaPourValeur.verif(self, commande)
439         if bool : return 0
440         return 1
441
442 #------------------------------
443 class MCaPourValeurDansListe :
444 #------------------------------
445     """
446     Égalité du mot-clé à une valeur dans une liste
447     """
448     def __init__(self, list_arg):
449         assert (len(list_arg)==3)
450         self.MC = list_arg[0]
451         self.LVal = list_arg[1]
452         self.Jdc = list_arg[2]
453
454     def verif(self, commande):
455         """
456         Vérification
457         """
458         bool = 0
459         for mc in commande.childNodes :
460             if mc.name != self.MC : continue 
461             TexteMC = mc.getText(self.Jdc)
462             #print "TexteMC=",type(TexteMC),TexteMC
463             #print "LVal=",type(self.LVal),self.LVal
464             for Val in self.LVal:
465                 #print "Val=",type(Val),Val
466                 #print "Find",TexteMC.find(Val)
467                 if (TexteMC.find(Val) < 0 ): continue
468                 bool = 1
469         return bool
470
471 #-----------------------------------------
472 class MCnaPasPourValeurDansListe(MCaPourValeurDansListe) :
473 #-----------------------------------------
474     """
475         Non égalité du mot-clé à une valeur dans une liste
476     """
477     def __init__(self, list_arg):
478         MCaPourValeurDansListe.__init__(self, list_arg)
479
480     def verif(self, commande):
481         """
482         Vérification
483         """
484         bool = MCaPourValeurDansListe.verif(self, commande)
485         if bool : return 0
486         return 1
487
488 dictionnaire_regle = {"existe":existe,
489                       "nexistepas":nexistepas,
490                       "existeMCFParmi":existeMCFParmi,
491                       "nexistepasMCFParmi":nexistepasMCFParmi,
492                       "existeMCsousMCF":existeMCsousMCF,
493                       "nexistepasMCsousMCF":nexistepasMCsousMCF,
494                       "MCsousMCFaPourValeur":MCsousMCFaPourValeur,
495                       "MCsousMCFaPourValeurDansListe":MCsousMCFaPourValeurDansListe,
496                       "MCaPourValeur":MCaPourValeur,
497                       "MCnaPasPourValeur":MCnaPasPourValeur,
498                       "existeMCsousMCFcourant":existeMCsousMCFcourant,
499                       "nexistepasMCsousMCFcourant":nexistepasMCsousMCFcourant,
500                       "MCsousMCFcourantaPourValeur":MCsousMCFcourantaPourValeur,
501                       "MCsousMCFcourantaPourValeurDansListe":MCsousMCFcourantaPourValeurDansListe,
502                       "MCsousMCFcourantnaPasPourValeurDansListe":MCsousMCFcourantnaPasPourValeurDansListe,
503                       "MCaPourValeurDansListe":MCaPourValeurDansListe,
504                       "MCnaPasPourValeurDansListe":MCnaPasPourValeurDansListe}
505
506
507 SansRegle = pasDeRegle()