Salome HOME
travail sur monPlusieurs
[tools/eficas.git] / Noyau / N_ENTITE.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013   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     Ce module contient la classe ENTITE qui est la classe de base
22     de toutes les classes de definition d'EFICAS.
23 """
24
25 import re
26 import types
27 import N_CR
28 import N_OPS
29 import N_VALIDATOR
30 from strfunc import ufmt
31
32 class ENTITE:
33    """
34       Classe de base pour tous les objets de definition : mots cles et commandes
35       Cette classe ne contient que des methodes utilitaires
36       Elle ne peut être instanciee et doit d abord être specialisee
37    """
38    CR=N_CR.CR
39    factories={'validator':N_VALIDATOR.validatorFactory}
40
41    def __init__(self,validators=None):
42       """
43          Initialise les deux attributs regles et entites d'une classe dérivée
44          à : pas de règles et pas de sous-entités.
45
46          L'attribut regles doit contenir la liste des regles qui s'appliquent
47          sur ses sous-entités
48
49          L'attribut entités doit contenir le dictionnaires des sous-entités
50          (clé = nom, valeur=objet)
51       """
52       self.regles=()
53       self.entites={}
54       if validators:
55          self.validators=self.factories['validator'](validators)
56       else:
57          self.validators=validators
58
59    def affecter_parente(self):
60       """
61           Cette methode a pour fonction de donner un nom et un pere aux
62           sous entités qui n'ont aucun moyen pour atteindre leur parent
63           directement
64           Il s'agit principalement des mots cles
65       """
66       for k,v in self.entites.items():
67         v.pere = self
68         v.nom = k
69
70    def verif_cata(self):
71       """
72           Cette methode sert à valider les attributs de l'objet de définition
73       """
74       raise NotImplementedError("La méthode verif_cata de la classe %s doit être implémentée"
75                                 % self.__class__.__name__)
76
77    def __call__(self):
78       """
79           Cette methode doit retourner un objet dérivé de la classe OBJECT
80       """
81       raise NotImplementedError("La méthode __call__ de la classe %s doit être implémentée"
82                                 % self.__class__.__name__)
83
84    def report(self):
85       """
86          Cette méthode construit pour tous les objets dérivés de ENTITE un
87          rapport de validation de la définition portée par cet objet
88       """
89       self.cr = self.CR()
90       self.verif_cata()
91       for k,v in self.entites.items() :
92          try :
93             cr = v.report()
94             cr.debut = u"Début "+v.__class__.__name__+ ' : ' + k
95             cr.fin = u"Fin "+v.__class__.__name__+ ' : ' + k
96             self.cr.add(cr)
97          except:
98             self.cr.fatal(_(u"Impossible d'obtenir le rapport de %s %s"), k,`v`)
99             print "Impossible d'obtenir le rapport de %s %s" %(k,`v`)
100             print "père =",self
101       return self.cr
102
103    def verif_cata_regles(self):
104       """
105          Cette méthode vérifie pour tous les objets dérivés de ENTITE que
106          les objets REGLES associés ne portent que sur des sous-entités
107          existantes
108       """
109       for regle in self.regles :
110         l=[]
111         for mc in regle.mcs :
112           if not self.entites.has_key(mc) :
113             l.append(mc)
114         if l != [] :
115           txt = str(regle)
116           self.cr.fatal(_(u"Argument(s) non permis : %r pour la règle : %s"), l, txt)
117
118    def check_definition(self, parent):
119       """Verifie la definition d'un objet composite (commande, fact, bloc)."""
120       args = self.entites.copy()
121       mcs = set()
122       for nom, val in args.items():
123          if val.label == 'SIMP':
124             mcs.add(nom)
125             #XXX
126             #if val.max != 1 and val.type == 'TXM':
127                 #print "#CMD", parent, nom
128          elif val.label == 'FACT':
129             val.check_definition(parent)
130             # CALC_SPEC !
131             #assert self.label != 'FACT', \
132                #'Commande %s : Mot-clef facteur present sous un mot-clef facteur : interdit !' \
133                #% parent
134          else:
135             continue
136          del args[nom]
137       # seuls les blocs peuvent entrer en conflit avec les mcs du plus haut niveau
138       for nom, val in args.items():
139          if val.label == 'BLOC':
140             mcbloc = val.check_definition(parent)
141             #XXX
142             #print "#BLOC", parent, re.sub('\s+', ' ', val.condition)
143             #assert mcs.isdisjoint(mcbloc), "Commande %s : Mot(s)-clef(s) vu(s) plusieurs fois : %s" \
144             #   % (parent, tuple(mcs.intersection(mcbloc)))
145       return mcs
146
147    def check_op(self, valmin=-9999, valmax=9999):
148       """Vérifie l'attribut op."""
149       if self.op is not None and \
150          (type(self.op) is not int or self.op < valmin or self.op > valmax):
151          self.cr.fatal(_(u"L'attribut 'op' doit être un entier "
152                          u"compris entre %d et %d : %r"), valmin, valmax, self.op)
153
154    def check_proc(self):
155       """Vérifie l'attribut proc."""
156       if self.proc is not None and not isinstance(self.proc, N_OPS.OPS):
157          self.cr.fatal(_(u"L'attribut op doit être une instance d'OPS : %r"), self.proc)
158
159    def check_regles(self):
160       """Vérifie l'attribut regles."""
161       if type(self.regles) is not tuple:
162          self.cr.fatal(_(u"L'attribut 'regles' doit être un tuple : %r"),
163             self.regles)
164
165    def check_fr(self):
166       """Vérifie l'attribut fr."""
167       if type(self.fr) not in (str, unicode):
168          self.cr.fatal(_(u"L'attribut 'fr' doit être une chaine de caractères : %r"),
169             self.fr)
170
171    def check_docu(self):
172       """Vérifie l'attribut docu."""
173       if type(self.docu) not in (str, unicode):
174          self.cr.fatal(_(u"L'attribut 'docu' doit être une chaine de caractères : %r"),
175             self.docu)
176
177    def check_nom(self):
178       """Vérifie l'attribut proc."""
179       if type(self.nom) != types.StringType :
180          self.cr.fatal(_(u"L'attribut 'nom' doit être une chaine de caractères : %r"),
181             self.nom)
182
183    def check_reentrant(self):
184       """Vérifie l'attribut reentrant."""
185       if self.reentrant not in ('o', 'n', 'f'):
186          self.cr.fatal(_(u"L'attribut 'reentrant' doit valoir 'o','n' ou 'f' : %r"),
187             self.reentrant)
188
189    def check_statut(self, into=('o', 'f', 'c', 'd')):
190       """Vérifie l'attribut statut."""
191       if self.statut not in into:
192          self.cr.fatal(_(u"L'attribut 'statut' doit être parmi %s : %r"),
193             into, self.statut)
194
195    def check_condition(self):
196       """Vérifie l'attribut condition."""
197       if self.condition != None :
198          if type(self.condition) != types.StringType :
199             self.cr.fatal(_(u"L'attribut 'condition' doit être une chaine de caractères : %r"),
200                 self.condition)
201       else:
202          self.cr.fatal(_(u"La condition ne doit pas valoir None !"))
203
204    def check_min_max(self):
205       """Vérifie les attributs min/max."""
206       if type(self.min) != types.IntType :
207          if self.min != '**':
208             self.cr.fatal(_(u"L'attribut 'min' doit être un entier : %r"), self.min)
209       if type(self.max) != types.IntType :
210          if self.max != '**':
211             self.cr.fatal(_(u"L'attribut 'max' doit être un entier : %r"), self.max)
212       if self.min > self.max :
213          self.cr.fatal(_(u"Nombres d'occurrence min et max invalides : %r %r"),
214             self.min, self.max)
215
216    def check_validators(self):
217       """Vérifie les validateurs supplémentaires"""
218       if self.validators and not self.validators.verif_cata():
219          self.cr.fatal(_(u"Un des validateurs est incorrect. Raison : %s"),
220             self.validators.cata_info)
221
222    def check_homo(self):
223       """Vérifie l'attribut homo."""
224       if self.homo != 0 and self.homo != 1 :
225           self.cr.fatal(_(u"L'attribut 'homo' doit valoir 0 ou 1 : %r"), self.homo)
226
227    def check_into(self):
228       """Vérifie l'attribut into."""
229       if self.into != None :
230          if type(self.into) != types.TupleType :
231             self.cr.fatal(_(u"L'attribut 'into' doit être un tuple : %r"), self.into)
232
233    def check_position(self):
234       """Vérifie l'attribut position."""
235       if self.position not in ('local', 'global', 'global_jdc'):
236          self.cr.fatal(_(u"L'attribut 'position' doit valoir 'local', 'global' "
237                              u"ou 'global_jdc' : %r"), self.position)