Salome HOME
1d420229faaad2c2985c8d6e2163cba9b8337971
[modules/shaper.git] / src / Model / Model_Validator.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Validator.cpp
4 // Created:     2 Jul 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "Model_Validator.h"
8
9 #include "Model_AttributeValidator.h"
10 #include "Model_FeatureValidator.h"
11
12 #include <ModelAPI_Attribute.h>
13 #include <ModelAPI_AttributeString.h>
14 #include <ModelAPI_AttributeValidator.h>
15 #include <ModelAPI_Feature.h>
16 #include <Model_Data.h>
17
18 #include <Events_InfoMessage.h>
19
20 void Model_ValidatorsFactory::registerValidator(const std::string& theID,
21   ModelAPI_Validator* theValidator)
22 {
23   if (myIDs.find(theID) != myIDs.end()) {
24     Events_InfoMessage("Model_Validator", "Validator %1 is already registered").arg(theID).send();
25   } else {
26     myIDs[theID] = theValidator;
27   }
28 }
29
30 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
31   const std::string& theFeatureID)
32 {
33   if (myFeatures.find(theFeatureID) == myFeatures.end()) {
34     myFeatures[theFeatureID] = AttrValidators();
35   }
36   if (myFeatures[theFeatureID].find(theID) != myFeatures[theFeatureID].end()) {
37     //Events_Error::send(std::string("Validator ") + theID + 
38     //  " for feature " + theFeatureID + "is already registered");
39   } else {
40     myFeatures[theFeatureID][theID] = std::list<std::string>();
41   }
42 }
43
44 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
45   const std::string& theFeatureID,
46   const std::list<std::string>& theArguments)
47 {
48   if (myFeatures.find(theFeatureID) == myFeatures.end()) {
49     myFeatures[theFeatureID] = AttrValidators();
50   }
51
52   if (myFeatures[theFeatureID].find(theID) != myFeatures[theFeatureID].end()) {
53     //Events_Error::send(std::string("Validator ") + theID + 
54     //  " for feature " + theFeatureID + "is already registered");
55   } else {
56     myFeatures[theFeatureID][theID] = theArguments;
57   }
58 }
59
60 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
61   const std::string& theFeatureID,
62   const std::string& theAttrID,
63   const std::list<std::string>& theArguments)
64 {
65   // create feature-structures if not exist
66   std::map<std::string, std::map<std::string, AttrValidators> >::iterator aFeature = myAttrs.find(
67     theFeatureID);
68   if (aFeature == myAttrs.end()) {
69     myAttrs[theFeatureID] = std::map<std::string, AttrValidators>();
70     aFeature = myAttrs.find(theFeatureID);
71   }
72   // add attr-structure if not exist, or generate error if already exist
73   std::map<std::string, AttrValidators>::iterator anAttr = aFeature->second.find(theAttrID);
74   if (anAttr == aFeature->second.end()) {
75     aFeature->second[theAttrID] = AttrValidators();
76   }
77   aFeature->second[theAttrID][theID] = theArguments;
78 }
79
80 void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
81                                          Validators& theValidators) const
82 {
83   std::map<std::string, AttrValidators>::const_iterator aFeatureIt = 
84       myFeatures.find(theFeatureID);
85   if (aFeatureIt != myFeatures.cend()) {
86     AttrValidators::const_iterator aValidatorsIt = aFeatureIt->second.cbegin();
87     for (; aValidatorsIt != aFeatureIt->second.cend(); aValidatorsIt++) {
88       if (!validator(aValidatorsIt->first)) {
89         Events_InfoMessage("Model_Validator", "Validator %1 was not registered")
90           .arg(aValidatorsIt->first).send();
91       } else {
92         theValidators.push_back(std::make_pair(aValidatorsIt->first, aValidatorsIt->second));
93       }
94     }
95   }
96   addDefaultValidators(theValidators);
97 }
98
99 void Model_ValidatorsFactory::validators(const std::string& theFeatureID, const std::string& theAttrID,
100                                          Validators& theValidators) const
101 {
102   std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeatureIt = 
103       myAttrs.find(theFeatureID);
104   if (aFeatureIt != myAttrs.cend()) {
105     std::map<std::string, AttrValidators>::const_iterator anAttrIt = aFeatureIt->second.find(theAttrID);
106     if (anAttrIt != aFeatureIt->second.end()) {
107       AttrValidators::const_iterator aValidatorsIt = anAttrIt->second.cbegin();
108       for (; aValidatorsIt != anAttrIt->second.cend(); aValidatorsIt++) {
109         if (!validator(aValidatorsIt->first)) {
110           Events_InfoMessage("Model_Validator", "Validator %1 was not registered")
111             .arg(aValidatorsIt->first).send();
112         } else {
113           theValidators.push_back(std::make_pair(aValidatorsIt->first, aValidatorsIt->second));
114         }
115       }
116     }
117   }
118   addDefaultAttributeValidators(theValidators);
119 }
120
121 Model_ValidatorsFactory::Model_ValidatorsFactory()
122   : ModelAPI_ValidatorsFactory()
123 {
124   registerValidator("Model_FeatureValidator", new Model_FeatureValidator);
125   registerValidator("Model_AttributeValidator", new Model_AttributeValidator);
126 }
127
128 const ModelAPI_Validator* Model_ValidatorsFactory::validator(const std::string& theID) const
129 {
130   std::map<std::string, ModelAPI_Validator*>::const_iterator aIt = myIDs.find(theID);
131   if (aIt != myIDs.end()) {
132     return aIt->second;
133   }
134   return NULL;
135 }
136
137 void Model_ValidatorsFactory::addDefaultValidators(Validators& theValidators) const
138 {
139   const static std::string kDefaultId = "Model_FeatureValidator";
140   if (!validator(kDefaultId))
141     return;
142   theValidators.push_back(std::make_pair(kDefaultId, std::list<std::string>()));
143 }
144
145 void Model_ValidatorsFactory::addDefaultAttributeValidators(Validators& theValidators) const
146 {
147   const static std::string kDefaultId = "Model_AttributeValidator";
148   if (!validator(kDefaultId))
149     return;
150   theValidators.push_back(std::make_pair(kDefaultId, std::list<std::string>()));
151 }
152
153 bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
154 {
155   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
156   if (aData.get() && aData->isValid()) {
157     if (aData->execState() == ModelAPI_StateDone)
158       aData->eraseErrorString(); // no error => erase the information string
159   } else {
160     return false; // feature is broken, already not presented in the data structure
161   }
162
163   // check feature validators first
164   Validators aValidators;
165   validators(theFeature->getKind(), aValidators);
166
167   if (!aValidators.empty()) {
168     Validators::const_iterator aValidatorIt = aValidators.cbegin();
169     for(; aValidatorIt != aValidators.cend(); aValidatorIt++) {
170       const std::string& aValidatorID = aValidatorIt->first;
171       const std::list<std::string>& anArguments = aValidatorIt->second;
172       // validators() checks invalid validator names
173       //if (!aValidator) {
174       //  Events_Error::send(std::string("Validator ") + aValidatorID + " was not registered");
175       //  continue;
176       //}
177       const ModelAPI_FeatureValidator* aFValidator = 
178         dynamic_cast<const ModelAPI_FeatureValidator*>(validator(aValidatorID));
179       if (aFValidator) {
180         Events_InfoMessage anError;
181         if (!aFValidator->isValid(theFeature, anArguments, anError)) {
182           if (anError.empty())
183             anError = "Unknown error.";
184           anError = aValidatorID + ": " + anError.messageString();
185           theFeature->setError(anError.messageString(), false);
186           theFeature->data()->execState(ModelAPI_StateInvalidArgument);
187           return false;
188         }
189       }
190     }
191   }
192   // The default validator was retrned by validators() and was checked in previous cycle
193   //// check default validator
194   //std::map<std::string, ModelAPI_Validator*>::const_iterator aDefaultVal = myIDs.find(kDefaultId);
195   //if(aDefaultVal != myIDs.end()) {
196   //  static const std::list<std::string> anEmptyArgList;
197   //  const ModelAPI_FeatureValidator* aFValidator = 
198   //    dynamic_cast<const ModelAPI_FeatureValidator*>(aDefaultVal->second);
199   //  if (aFValidator) {
200   //    std::string anError;
201   //    if (!aFValidator->isValid(theFeature, anEmptyArgList, anError)) {
202   //      if (anError.empty())
203   //        anError = "Unknown error.";
204   //      anError = "Feature invalidated by \"" + kDefaultId + "\" with error: " + anError;
205   //      theFeature->setError(anError, false);
206   //      theFeature->data()->execState(ModelAPI_StateInvalidArgument);
207   //      return false;
208   //    }
209   //  }
210   //}
211
212   // check all attributes for validity
213   // Validity of data is checked by "Model_FeatureValidator" (kDefaultId)
214   // if (!aData || !aData->isValid())
215   //   return false;
216   static const std::string kAllTypes = "";
217   std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
218   std::list<std::string>::const_iterator anAttrIt = aLtAttributes.cbegin();
219   for (; anAttrIt != aLtAttributes.cend(); anAttrIt++) {
220     const std::string& anAttributeID = *anAttrIt; 
221     AttributePtr anAttribute = theFeature->data()->attribute(anAttributeID);
222
223     std::string aValidatorID;
224     Events_InfoMessage anError;
225     if (!validate(anAttribute, aValidatorID, anError)) {
226       if (anError.empty())
227         anError = "Unknown error.";
228       anError = anAttributeID + " - " + aValidatorID + ": " + anError.messageString();
229       theFeature->setError(anError.messageString(), false);
230       theFeature->data()->execState(ModelAPI_StateInvalidArgument);
231       return false;
232     } 
233   }
234
235   return true;
236 }
237
238 bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Attribute>& theAttribute,
239                                        std::string& theValidator,
240                                        Events_InfoMessage& theError) const
241 {
242   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
243   if (!aFeature.get()) {
244     theValidator = "Model_ValidatorsFactory";
245     theError = "Attribute has no feature.";
246     return false;
247   }
248
249   // skip not-case attributes, that really may be invalid (issue 671)
250   if (!const_cast<Model_ValidatorsFactory*>(this)->isCase(aFeature, theAttribute->id()))
251     return true;
252
253   Validators aValidators;
254   validators(aFeature->getKind(), theAttribute->id(), aValidators);
255
256   Validators::iterator aValidatorIt = aValidators.begin();
257   for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
258     const std::string& aValidatorID = aValidatorIt->first;
259     const std::list<std::string>& anArguments = aValidatorIt->second;
260     const ModelAPI_AttributeValidator* anAttrValidator =
261         dynamic_cast<const ModelAPI_AttributeValidator*>(validator(aValidatorID));
262     if (!anAttrValidator)
263       continue;
264     if (!anAttrValidator->isValid(theAttribute, anArguments, theError)) {
265       theValidator = aValidatorID;
266       return false;
267     } 
268   }
269
270   return true;
271 }
272
273 void Model_ValidatorsFactory::registerNotObligatory(std::string theFeature, std::string theAttribute)
274 {
275   const static std::string kDefaultId = "Model_FeatureValidator";
276   std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
277   if (it != myIDs.end()) {
278     Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
279     if (aValidator) {
280       aValidator->registerNotObligatory(theFeature, theAttribute);
281     }
282   }
283 }
284
285 bool Model_ValidatorsFactory::isNotObligatory(std::string theFeature, std::string theAttribute)
286 {
287   const static std::string kDefaultId = "Model_FeatureValidator";
288   std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
289   if (it != myIDs.end()) {
290     Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
291     if (aValidator) {
292       return aValidator->isNotObligatory(theFeature, theAttribute);
293     }
294   }
295   return false; // default
296 }
297
298 void Model_ValidatorsFactory::registerConcealment(std::string theFeature, std::string theAttribute)
299 {
300   std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
301   if (aFind == myConcealed.end()) {
302     std::set<std::string> aNewSet;
303     aNewSet.insert(theAttribute);
304     myConcealed[theFeature] = aNewSet;
305   } else {
306     aFind->second.insert(theAttribute);
307   }
308 }
309
310 bool Model_ValidatorsFactory::isConcealed(std::string theFeature, std::string theAttribute)
311 {
312   std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
313   return aFind != myConcealed.end() && aFind->second.find(theAttribute) != aFind->second.end();
314 }
315
316 void Model_ValidatorsFactory::registerCase(std::string theFeature, std::string theAttribute,
317     std::string theSwitchId, std::string theCaseId)
318 {
319   std::map<std::string, std::map<std::string, std::pair<std::string, std::set<std::string> > > >
320     ::iterator aFindFeature = myCases.find(theFeature);
321   if (aFindFeature == myCases.end()) {
322     myCases[theFeature] = std::map<std::string, std::pair<std::string, std::set<std::string> > >();
323     aFindFeature = myCases.find(theFeature);
324   }
325   std::map<std::string, std::pair<std::string, std::set<std::string> > >::iterator aFindAttrID =
326     aFindFeature->second.find(theAttribute);
327   if (aFindAttrID == aFindFeature->second.end()) {
328     aFindFeature->second[theAttribute] =
329       std::pair<std::string, std::set<std::string> >(theSwitchId, std::set<std::string>());
330     aFindAttrID = aFindFeature->second.find(theAttribute);
331   }
332   aFindAttrID->second.second.insert(theCaseId);
333 }
334
335 bool Model_ValidatorsFactory::isCase(FeaturePtr theFeature, std::string theAttribute)
336 {
337   std::map<std::string, std::map<std::string, std::pair<std::string, std::set<std::string> > > >
338     ::iterator aFindFeature = myCases.find(theFeature->getKind());
339   if (aFindFeature != myCases.end()) {
340     std::map<std::string, std::pair<std::string, std::set<std::string> > >::iterator
341       aFindAttrID = aFindFeature->second.find(theAttribute);
342     if (aFindAttrID != aFindFeature->second.end()) {
343       // the the switch-attribute that contains the case value
344       AttributeStringPtr aSwitch = theFeature->string(aFindAttrID->second.first);
345       if (aSwitch.get()) {
346          // the second has the case identifier
347         return aFindAttrID->second.second.find(aSwitch->value()) != 
348                aFindAttrID->second.second.end();
349       }
350     }
351   }
352   return true; // if no additional conditions, this attribute is the case to be validated
353 }