Salome HOME
Case-specific attributes validation implementation
[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) const
20 {
21   static Model_ValidatorsFactory* aValidators = 
22     static_cast<Model_ValidatorsFactory*>(ModelAPI_Session::get()->validators());
23
24   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
25   // "Action" features has no data, but still valid. e.g "Remove Part"  
26   if (!aData) {
27     return theFeature->isAction();
28   }
29   if (!aData->isValid())
30     return false;
31   const std::string kAllTypes = "";
32   std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
33   std::list<std::string>::iterator it = aLtAttributes.begin();
34   for (; it != aLtAttributes.end(); it++) {
35     AttributePtr anAttr = aData->attribute(*it);
36     if (!aValidators->isCase(theFeature, anAttr->id()))
37       continue; // this attribute is not participated in the current case
38     if (!anAttr->isInitialized()) { // attribute is not initialized
39       std::map<std::string, std::set<std::string> >::const_iterator aFeatureFind = 
40         myNotObligatory.find(theFeature->getKind());
41       if (aFeatureFind == myNotObligatory.end() || // and it is obligatory for filling
42           aFeatureFind->second.find(*it) == aFeatureFind->second.end()) {
43         return false;
44       }
45     }
46   }
47   return true;
48 }
49
50 void Model_FeatureValidator::registerNotObligatory(std::string theFeature, std::string theAttribute)
51 {
52   std::set<std::string>& anAttrs = myNotObligatory[theFeature];
53   anAttrs.insert(theAttribute);
54 }
55
56 bool Model_FeatureValidator::isNotObligatory(std::string theFeature, std::string theAttribute)
57 {
58   std::set<std::string>& anAttrs = myNotObligatory[theFeature];
59   return anAttrs.find(theAttribute) != anAttrs.end();
60 }