]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Validator.cpp
Salome HOME
Treat "Model_FeatureValidator" as default validator (no need to declare it in an...
[modules/shaper.git] / src / Model / Model_Validator.cpp
1 // File:        Model_Validator.cpp
2 // Created:     2 Jul 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_Validator.h>
6 #include <Model_ResultValidators.h>
7 #include <Model_FeatureValidator.h>
8 #include <ModelAPI_Feature.h>
9 #include <Events_Error.h>
10
11 void Model_ValidatorsFactory::registerValidator(const std::string& theID,
12                                                 ModelAPI_Validator* theValidator)
13 {
14   if (myIDs.find(theID) != myIDs.end()) {
15     Events_Error::send(std::string("Validator ") + theID + " is already registered");
16   } else {
17     myIDs[theID] = theValidator;
18   }
19 }
20
21 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
22                                               const std::string& theFeatureID)
23 {
24   if (myFeatures.find(theFeatureID) == myFeatures.end()) {
25     myFeatures[theFeatureID] = std::set<std::string>();
26   }
27   myFeatures[theFeatureID].insert(theID);
28 }
29
30 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
31                                               const std::string& theFeatureID,
32                                               const std::string& theAttrID,
33                                               const std::list<std::string>& theArguments)
34 {
35   // create feature-structures if not exist
36   std::map<std::string, std::map<std::string, AttrValidators> >::iterator aFeature = myAttrs.find(
37       theFeatureID);
38   if (aFeature == myAttrs.end()) {
39     myAttrs[theFeatureID] = std::map<std::string, AttrValidators>();
40     aFeature = myAttrs.find(theFeatureID);
41   }
42   // add attr-structure if not exist, or generate error if already exist
43   std::map<std::string, AttrValidators>::iterator anAttr = aFeature->second.find(theAttrID);
44   if (anAttr == aFeature->second.end()) {
45     aFeature->second[theAttrID] = AttrValidators();
46   }
47   aFeature->second[theAttrID].insert(
48       std::pair<std::string, std::list<std::string> >(theID, theArguments));
49 }
50
51 void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
52                                          std::list<ModelAPI_Validator*>& theResult) const
53 {
54   std::map<std::string, std::set<std::string> >::const_iterator aFeature = myFeatures.find(
55       theFeatureID);
56   if (aFeature != myFeatures.cend()) {
57     std::set<std::string>::const_iterator aValIter = aFeature->second.cbegin();
58     for (; aValIter != aFeature->second.cend(); aValIter++) {
59       std::map<std::string, ModelAPI_Validator*>::const_iterator aFound = myIDs.find(*aValIter);
60       if (aFound == myIDs.end()) {
61         Events_Error::send(std::string("Validator ") + *aValIter + " was not registered");
62       } else {
63         theResult.push_back(aFound->second);
64       }
65     }
66   }
67   addDefaultValidators(theResult);
68 }
69
70 void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
71                                          const std::string& theAttrID,
72                                          std::list<ModelAPI_Validator*>& theValidators,
73                                          std::list<std::list<std::string> >& theArguments) const
74 {
75   std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeature = myAttrs
76       .find(theFeatureID);
77   if (aFeature != myAttrs.cend()) {
78     std::map<std::string, AttrValidators>::const_iterator anAttr = aFeature->second.find(theAttrID);
79     if (anAttr != aFeature->second.end()) {
80       AttrValidators::const_iterator aValIter = anAttr->second.cbegin();
81       for (; aValIter != anAttr->second.cend(); aValIter++) {
82         std::map<std::string, ModelAPI_Validator*>::const_iterator aFound = myIDs.find(
83             aValIter->first);
84         if (aFound == myIDs.end()) {
85           Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered");
86         } else {
87           theValidators.push_back(aFound->second);
88           theArguments.push_back(aValIter->second);
89         }
90       }
91     }
92   }
93 }
94
95 Model_ValidatorsFactory::Model_ValidatorsFactory()
96     : ModelAPI_ValidatorsFactory()
97 {
98   registerValidator("Model_ResultPointValidator", new Model_ResultPointValidator);
99   registerValidator("Model_ResultLineValidator", new Model_ResultLineValidator);
100   registerValidator("Model_ResultArcValidator", new Model_ResultArcValidator);
101   registerValidator("Model_FeatureValidator", new Model_FeatureValidator);
102 }
103
104 const ModelAPI_Validator* Model_ValidatorsFactory::validator(const std::string& theID) const
105 {
106   std::map<std::string, ModelAPI_Validator*>::const_iterator aIt = myIDs.find(theID);
107   if (aIt != myIDs.end()) {
108     return aIt->second;
109   }
110   return NULL;
111 }
112
113 void Model_ValidatorsFactory::addDefaultValidators(std::list<ModelAPI_Validator*>& theValidators) const
114 {
115   std::string anId = "Model_FeatureValidator";
116   std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(anId);
117   if(it == myIDs.end())
118     return;
119   theValidators.push_back(it->second);
120 }