Salome HOME
Issue #1315 Middle point constraint problem
[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_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   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
154   if (aData.get() && aData->isValid()) {
155     if (aData->execState() == ModelAPI_StateDone)
156       aData->eraseErrorString(); // no error => erase the information string
157   } else {
158     return false; // feature is broken, already not presented in the data structure
159   }
160
161   // check feature validators first
162   Validators aValidators;
163   validators(theFeature->getKind(), aValidators);
164
165   if (!aValidators.empty()) {
166     Validators::const_iterator aValidatorIt = aValidators.cbegin();
167     for(; aValidatorIt != aValidators.cend(); aValidatorIt++) {
168       const std::string& aValidatorID = aValidatorIt->first;
169       const std::list<std::string>& anArguments = aValidatorIt->second;
170       // validators() checks invalid validator names
171       //if (!aValidator) {
172       //  Events_Error::send(std::string("Validator ") + aValidatorID + " was not registered");
173       //  continue;
174       //}
175       const ModelAPI_FeatureValidator* aFValidator = 
176         dynamic_cast<const ModelAPI_FeatureValidator*>(validator(aValidatorID));
177       if (aFValidator) {
178         std::string anError;
179         if (!aFValidator->isValid(theFeature, anArguments, anError)) {
180           if (anError.empty())
181             anError = "Unknown error.";
182           anError = aValidatorID + ": " + anError;
183           theFeature->setError(anError, false);
184           theFeature->data()->execState(ModelAPI_StateInvalidArgument);
185           return false;
186         }
187       }
188     }
189   }
190   // The default validator was retrned by validators() and was checked in previous cycle
191   //// check default validator
192   //std::map<std::string, ModelAPI_Validator*>::const_iterator aDefaultVal = myIDs.find(kDefaultId);
193   //if(aDefaultVal != myIDs.end()) {
194   //  static const std::list<std::string> anEmptyArgList;
195   //  const ModelAPI_FeatureValidator* aFValidator = 
196   //    dynamic_cast<const ModelAPI_FeatureValidator*>(aDefaultVal->second);
197   //  if (aFValidator) {
198   //    std::string anError;
199   //    if (!aFValidator->isValid(theFeature, anEmptyArgList, anError)) {
200   //      if (anError.empty())
201   //        anError = "Unknown error.";
202   //      anError = "Feature invalidated by \"" + kDefaultId + "\" with error: " + anError;
203   //      theFeature->setError(anError, false);
204   //      theFeature->data()->execState(ModelAPI_StateInvalidArgument);
205   //      return false;
206   //    }
207   //  }
208   //}
209
210   // check all attributes for validity
211   // Validity of data is checked by "Model_FeatureValidator" (kDefaultId)
212   // if (!aData || !aData->isValid())
213   //   return false;
214   static const std::string kAllTypes = "";
215   std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
216   std::list<std::string>::const_iterator anAttrIt = aLtAttributes.cbegin();
217   for (; anAttrIt != aLtAttributes.cend(); anAttrIt++) {
218     const std::string& anAttributeID = *anAttrIt; 
219     AttributePtr anAttribute = theFeature->data()->attribute(anAttributeID);
220
221     std::string aValidatorID;
222     std::string anError;
223     if (!validate(anAttribute, aValidatorID, anError)) {
224       if (anError.empty())
225         anError = "Unknown error.";
226       anError = anAttributeID + " - " + aValidatorID + ": " + anError;
227       theFeature->setError(anError, false);
228       theFeature->data()->execState(ModelAPI_StateInvalidArgument);
229       return false;
230     } 
231   }
232
233   return true;
234 }
235
236 bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Attribute>& theAttribute,
237                                        std::string& theValidator,
238                                        std::string& theError) const
239 {
240   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
241   if (!aFeature.get()) {
242     theValidator = "Model_ValidatorsFactory";
243     theError = "Attribute has no feature.";
244     return false;
245   }
246
247   // skip not-case attributes, that really may be invalid (issue 671)
248   if (!const_cast<Model_ValidatorsFactory*>(this)->isCase(aFeature, theAttribute->id()))
249     return true;
250
251   Validators aValidators;
252   validators(aFeature->getKind(), theAttribute->id(), aValidators);
253
254   Validators::iterator aValidatorIt = aValidators.begin();
255   for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
256     const std::string& aValidatorID = aValidatorIt->first;
257     const std::list<std::string>& anArguments = aValidatorIt->second;
258     const ModelAPI_AttributeValidator* anAttrValidator =
259         dynamic_cast<const ModelAPI_AttributeValidator*>(validator(aValidatorID));
260     if (!anAttrValidator)
261       continue;
262     if (!anAttrValidator->isValid(theAttribute, anArguments, theError)) {
263       theValidator = aValidatorID;
264       return false;
265     } 
266   }
267
268   return true;
269 }
270
271 void Model_ValidatorsFactory::registerNotObligatory(std::string theFeature, std::string theAttribute)
272 {
273   const static std::string kDefaultId = "Model_FeatureValidator";
274   std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
275   if (it != myIDs.end()) {
276     Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
277     if (aValidator) {
278       aValidator->registerNotObligatory(theFeature, theAttribute);
279     }
280   }
281 }
282
283 bool Model_ValidatorsFactory::isNotObligatory(std::string theFeature, std::string theAttribute)
284 {
285   const static std::string kDefaultId = "Model_FeatureValidator";
286   std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
287   if (it != myIDs.end()) {
288     Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
289     if (aValidator) {
290       return aValidator->isNotObligatory(theFeature, theAttribute);
291     }
292   }
293   return false; // default
294 }
295
296 void Model_ValidatorsFactory::registerConcealment(std::string theFeature, std::string theAttribute)
297 {
298   std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
299   if (aFind == myConcealed.end()) {
300     std::set<std::string> aNewSet;
301     aNewSet.insert(theAttribute);
302     myConcealed[theFeature] = aNewSet;
303   } else {
304     aFind->second.insert(theAttribute);
305   }
306 }
307
308 bool Model_ValidatorsFactory::isConcealed(std::string theFeature, std::string theAttribute)
309 {
310   std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
311   return aFind != myConcealed.end() && aFind->second.find(theAttribute) != aFind->second.end();
312 }
313
314 void Model_ValidatorsFactory::registerCase(std::string theFeature, std::string theAttribute,
315     std::string theSwitchId, std::string theCaseId)
316 {
317   std::map<std::string, std::map<std::string, std::pair<std::string, std::set<std::string> > > >
318     ::iterator aFindFeature = myCases.find(theFeature);
319   if (aFindFeature == myCases.end()) {
320     myCases[theFeature] = std::map<std::string, std::pair<std::string, std::set<std::string> > >();
321     aFindFeature = myCases.find(theFeature);
322   }
323   std::map<std::string, std::pair<std::string, std::set<std::string> > >::iterator aFindAttrID =
324     aFindFeature->second.find(theAttribute);
325   if (aFindAttrID == aFindFeature->second.end()) {
326     aFindFeature->second[theAttribute] =
327       std::pair<std::string, std::set<std::string> >(theSwitchId, std::set<std::string>());
328     aFindAttrID = aFindFeature->second.find(theAttribute);
329   }
330   aFindAttrID->second.second.insert(theCaseId);
331 }
332
333 bool Model_ValidatorsFactory::isCase(FeaturePtr theFeature, std::string theAttribute)
334 {
335   std::map<std::string, std::map<std::string, std::pair<std::string, std::set<std::string> > > >
336     ::iterator aFindFeature = myCases.find(theFeature->getKind());
337   if (aFindFeature != myCases.end()) {
338     std::map<std::string, std::pair<std::string, std::set<std::string> > >::iterator
339       aFindAttrID = aFindFeature->second.find(theAttribute);
340     if (aFindAttrID != aFindFeature->second.end()) {
341       // the the switch-attribute that contains the case value
342       AttributeStringPtr aSwitch = theFeature->string(aFindAttrID->second.first);
343       if (aSwitch.get()) {
344          // the second has the case identifier
345         return aFindAttrID->second.second.find(aSwitch->value()) != 
346                aFindAttrID->second.second.end();
347       }
348     }
349   }
350   return true; // if no additional conditions, this attribute is the case to be validated
351 }