Salome HOME
2.17. Improved management of overconstraint situation: Processing added arguments...
[modules/shaper.git] / src / Model / Model_FeatureValidator.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_FeatureValidator.cpp
4 // Created:     8 Jul 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include <Model_FeatureValidator.h>
8 #include <Model_Validator.h>
9 #include <ModelAPI_Attribute.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Feature.h>
12 #include <ModelAPI_Object.h>
13 #include <ModelAPI_Session.h>
14
15 #include <list>
16 #include <memory>
17
18 bool Model_FeatureValidator::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
19                                      const std::list<std::string>& theArguments,
20                                      std::string& theError) const
21 {
22   static Model_ValidatorsFactory* aValidators = 
23     static_cast<Model_ValidatorsFactory*>(ModelAPI_Session::get()->validators());
24
25   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
26   // "Action" features has no data, but still valid. e.g "Remove Part"  
27   if (!aData->isValid()) {
28     if (!theFeature->isAction())
29       theError = "There is no data.";
30     return theFeature->isAction();
31   }
32   const std::string kAllTypes = "";
33   std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
34   std::list<std::string>::iterator it = aLtAttributes.begin();
35   for (; it != aLtAttributes.end(); it++) {
36     AttributePtr anAttr = aData->attribute(*it);
37     if (!aValidators->isCase(theFeature, anAttr->id()))
38       continue; // this attribute is not participated in the current case
39     if (!anAttr->isInitialized()) { // attribute is not initialized
40       std::map<std::string, std::set<std::string> >::const_iterator aFeatureFind = 
41         myNotObligatory.find(theFeature->getKind());
42       if (aFeatureFind == myNotObligatory.end() || // and it is obligatory for filling
43           aFeatureFind->second.find(*it) == aFeatureFind->second.end()) {
44         theError = "Attribute \"" + anAttr->id() + "\" is not initialized.";
45         return false;
46       }
47     }
48   }
49   return true;
50 }
51
52 void Model_FeatureValidator::registerNotObligatory(std::string theFeature, std::string theAttribute)
53 {
54   std::set<std::string>& anAttrs = myNotObligatory[theFeature];
55   anAttrs.insert(theAttribute);
56 }
57
58 bool Model_FeatureValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
59 {
60   std::set<std::string>& anAttrs = myNotObligatory[theFeature];
61   return anAttrs.find(theAttribute) != anAttrs.end();
62 }