Salome HOME
Fix for #767: correct the internal history structure for nested features
[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_Data.h>
16 #include <ModelAPI_Feature.h>
17
18 #include <Events_Error.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_Error::send(std::string("Validator ") + theID + " is already registered");
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_Error::send(std::string("Validator ") + aValidatorsIt->first + " was not registered");
90       } else {
91         theValidators.push_back(std::make_pair(aValidatorsIt->first, aValidatorsIt->second));
92       }
93     }
94   }
95   addDefaultValidators(theValidators);
96 }
97
98 void Model_ValidatorsFactory::validators(const std::string& theFeatureID, const std::string& theAttrID,
99                                          Validators& theValidators) const
100 {
101   std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeatureIt = 
102       myAttrs.find(theFeatureID);
103   if (aFeatureIt != myAttrs.cend()) {
104     std::map<std::string, AttrValidators>::const_iterator anAttrIt = aFeatureIt->second.find(theAttrID);
105     if (anAttrIt != aFeatureIt->second.end()) {
106       AttrValidators::const_iterator aValidatorsIt = anAttrIt->second.cbegin();
107       for (; aValidatorsIt != anAttrIt->second.cend(); aValidatorsIt++) {
108         if (!validator(aValidatorsIt->first)) {
109           Events_Error::send(std::string("Validator ") + aValidatorsIt->first + " was not registered");
110         } else {
111           theValidators.push_back(std::make_pair(aValidatorsIt->first, aValidatorsIt->second));
112         }
113       }
114     }
115   }
116   addDefaultAttributeValidators(theValidators);
117 }
118
119 Model_ValidatorsFactory::Model_ValidatorsFactory()
120   : ModelAPI_ValidatorsFactory()
121 {
122   registerValidator("Model_FeatureValidator", new Model_FeatureValidator);
123   registerValidator("Model_AttributeValidator", new Model_AttributeValidator);
124 }
125
126 const ModelAPI_Validator* Model_ValidatorsFactory::validator(const std::string& theID) const
127 {
128   std::map<std::string, ModelAPI_Validator*>::const_iterator aIt = myIDs.find(theID);
129   if (aIt != myIDs.end()) {
130     return aIt->second;
131   }
132   return NULL;
133 }
134
135 void Model_ValidatorsFactory::addDefaultValidators(Validators& theValidators) const
136 {
137   const static std::string kDefaultId = "Model_FeatureValidator";
138   if (!validator(kDefaultId))
139     return;
140   theValidators.push_back(std::make_pair(kDefaultId, std::list<std::string>()));
141 }
142
143 void Model_ValidatorsFactory::addDefaultAttributeValidators(Validators& theValidators) const
144 {
145   const static std::string kDefaultId = "Model_AttributeValidator";
146   if (!validator(kDefaultId))
147     return;
148   theValidators.push_back(std::make_pair(kDefaultId, std::list<std::string>()));
149 }
150
151 bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
152 {
153   ModelAPI_ExecState anExecState = theFeature->data()->execState();
154   theFeature->setError("", false);
155   theFeature->data()->execState(anExecState);
156
157   // check feature validators first
158   Validators aValidators;
159   validators(theFeature->getKind(), aValidators);
160
161   if (!aValidators.empty()) {
162     Validators::const_iterator aValidatorIt = aValidators.cbegin();
163     for(; aValidatorIt != aValidators.cend(); aValidatorIt++) {
164       const std::string& aValidatorID = aValidatorIt->first;
165       const std::list<std::string>& anArguments = aValidatorIt->second;
166       // validators() checks invalid validator names
167       //if (!aValidator) {
168       //  Events_Error::send(std::string("Validator ") + aValidatorID + " was not registered");
169       //  continue;
170       //}
171       const ModelAPI_FeatureValidator* aFValidator = 
172         dynamic_cast<const ModelAPI_FeatureValidator*>(validator(aValidatorID));
173       if (aFValidator) {
174         std::string anError;
175         if (!aFValidator->isValid(theFeature, anArguments, anError)) {
176           if (anError.empty())
177             anError = "Unknown error.";
178           anError = "Feature invalidated by \"" + aValidatorID + "\" with error: " + anError;
179           theFeature->setError(anError, false);
180           theFeature->data()->execState(ModelAPI_StateInvalidArgument);
181           return false;
182         }
183       }
184     }
185   }
186   // The default validator was retrned by validators() and was checked in previous cycle
187   //// check default validator
188   //std::map<std::string, ModelAPI_Validator*>::const_iterator aDefaultVal = myIDs.find(kDefaultId);
189   //if(aDefaultVal != myIDs.end()) {
190   //  static const std::list<std::string> anEmptyArgList;
191   //  const ModelAPI_FeatureValidator* aFValidator = 
192   //    dynamic_cast<const ModelAPI_FeatureValidator*>(aDefaultVal->second);
193   //  if (aFValidator) {
194   //    std::string anError;
195   //    if (!aFValidator->isValid(theFeature, anEmptyArgList, anError)) {
196   //      if (anError.empty())
197   //        anError = "Unknown error.";
198   //      anError = "Feature invalidated by \"" + kDefaultId + "\" with error: " + anError;
199   //      theFeature->setError(anError, false);
200   //      theFeature->data()->execState(ModelAPI_StateInvalidArgument);
201   //      return false;
202   //    }
203   //  }
204   //}
205   
206   // check all attributes for validity
207   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
208   // Validity of data is checked by "Model_FeatureValidator" (kDefaultId)
209   // if (!aData || !aData->isValid())
210   //   return false;
211   static const std::string kAllTypes = "";
212   std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
213   std::list<std::string>::const_iterator anAttrIt = aLtAttributes.cbegin();
214   for (; anAttrIt != aLtAttributes.cend(); anAttrIt++) {
215     const std::string& anAttributeID = *anAttrIt; 
216     AttributePtr anAttribute = theFeature->data()->attribute(anAttributeID);
217
218     std::string aValidatorID;
219     std::string anError;
220     if (!validate(anAttribute, aValidatorID, anError)) {
221       if (anError.empty())
222         anError = "Unknown error.";
223       anError = "Attribute \"" + anAttributeID + "\" invalidated by \"" + aValidatorID + "\" with error: " + anError;
224       theFeature->setError(anError, false);
225       theFeature->data()->execState(ModelAPI_StateInvalidArgument);
226       return false;
227     } 
228   }
229
230   return true;
231 }
232
233 bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Attribute>& theAttribute,
234                                        std::string& theValidator,
235                                        std::string& theError) const
236 {
237   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
238   if (!aFeature.get()) {
239     theValidator = "Model_ValidatorsFactory";
240     theError = "Attribute has no feature.";
241     return false;
242   }
243
244   // skip not-case attributes, that really may be invalid (issue 671)
245   if (!const_cast<Model_ValidatorsFactory*>(this)->isCase(aFeature, theAttribute->id()))
246     return true;
247
248   Validators aValidators;
249   validators(aFeature->getKind(), theAttribute->id(), aValidators);
250
251   Validators::iterator aValidatorIt = aValidators.begin();
252   for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
253     const std::string& aValidatorID = aValidatorIt->first;
254     const std::list<std::string>& anArguments = aValidatorIt->second;
255     const ModelAPI_AttributeValidator* anAttrValidator =
256         dynamic_cast<const ModelAPI_AttributeValidator*>(validator(aValidatorID));
257     if (!anAttrValidator)
258       continue;
259     if (!anAttrValidator->isValid(theAttribute, anArguments, theError)) {
260       theValidator = aValidatorID;
261       return false;
262     } 
263   }
264
265   return true;
266 }
267
268 void Model_ValidatorsFactory::registerNotObligatory(std::string theFeature, std::string theAttribute)
269 {
270   const static std::string kDefaultId = "Model_FeatureValidator";
271   std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
272   if (it != myIDs.end()) {
273     Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
274     if (aValidator) {
275       aValidator->registerNotObligatory(theFeature, theAttribute);
276     }
277   }
278 }
279
280 bool Model_ValidatorsFactory::isNotObligatory(std::string theFeature, std::string theAttribute)
281 {
282   const static std::string kDefaultId = "Model_FeatureValidator";
283   std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
284   if (it != myIDs.end()) {
285     Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
286     if (aValidator) {
287       return aValidator->isNotObligatory(theFeature, theAttribute);
288     }
289   }
290   return false; // default
291 }
292
293 void Model_ValidatorsFactory::registerConcealment(std::string theFeature, std::string theAttribute)
294 {
295   std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
296   if (aFind == myConcealed.end()) {
297     std::set<std::string> aNewSet;
298     aNewSet.insert(theAttribute);
299     myConcealed[theFeature] = aNewSet;
300   } else {
301     aFind->second.insert(theAttribute);
302   }
303 }
304
305 bool Model_ValidatorsFactory::isConcealed(std::string theFeature, std::string theAttribute)
306 {
307   std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
308   return aFind != myConcealed.end() && aFind->second.find(theAttribute) != aFind->second.end();
309 }
310
311 void Model_ValidatorsFactory::registerCase(std::string theFeature, std::string theAttribute,
312     std::string theSwitchId, std::string theCaseId)
313 {
314   std::map<std::string, std::map<std::string, std::pair<std::string, std::string> > >::iterator 
315     aFindFeature = myCases.find(theFeature);
316   if (aFindFeature == myCases.end()) {
317     myCases[theFeature] = std::map<std::string, std::pair<std::string, std::string> >();
318     aFindFeature = myCases.find(theFeature);
319   }
320   (aFindFeature->second)[theAttribute] = std::pair<std::string, std::string>(theSwitchId, theCaseId);
321 }
322
323 bool Model_ValidatorsFactory::isCase(
324   FeaturePtr theFeature, std::string theAttribute)
325 {
326   std::map<std::string, std::map<std::string, std::pair<std::string, std::string> > >::iterator 
327     aFindFeature = myCases.find(theFeature->getKind());
328   if (aFindFeature != myCases.end()) {
329     std::map<std::string, std::pair<std::string, std::string> >::iterator
330       aFindAttr = aFindFeature->second.find(theAttribute);
331     if (aFindAttr != aFindFeature->second.end()) {
332       // the the switch-attribute that contains the case value
333       AttributeStringPtr aSwitch = theFeature->string(aFindAttr->second.first);
334       if (aSwitch.get()) {
335         return aSwitch->value() == aFindAttr->second.second; // the second is the case identifier
336       }
337     }
338   }
339   return true; // if no additional conditions, this attribute is the case to be validated
340 }