Salome HOME
19e9f56dda46f684efb290f3237d36526944f6f7
[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 <ModelAPI_Feature.h>
8 #include <Events_Error.h>
9
10 void Model_ValidatorsFactory::registerValidator(
11   const std::string& theID, ModelAPI_Validator* theValidator)
12 {
13   if (myIDs.find(theID) != myIDs.end()) {
14     Events_Error::send(std::string("Validator ") + theID + " is already registered");
15   } else {
16     myIDs[theID] = theValidator;
17   }
18 }
19
20 void Model_ValidatorsFactory::assignValidator(
21   const std::string& theID, const std::string& theFeatureID)
22 {
23   if (myFeatures.find(theFeatureID) == myFeatures.end()) {
24     myFeatures[theFeatureID] = std::set<std::string>();
25   }
26   myFeatures[theFeatureID].insert(theID);
27 }
28
29 void Model_ValidatorsFactory::assignValidator(const std::string& theID, 
30   const std::string& theFeatureID, const std::string& theAttrID, 
31   const std::list<std::string>& theArguments)
32 {
33   // create feature-structures if not exist
34   std::map<std::string, std::map<std::string, AttrValidators> >::iterator aFeature = 
35     myAttrs.find(theFeatureID);
36   if (aFeature == myAttrs.end()) {
37     myAttrs[theFeatureID] = std::map<std::string, AttrValidators>();
38     aFeature = myAttrs.find(theFeatureID);
39   }
40   // add attr-structure if not exist, or generate error if already exist
41   std::map<std::string, AttrValidators>::iterator anAttr = aFeature->second.find(theAttrID);
42   if (anAttr == aFeature->second.end()) {
43     aFeature->second[theAttrID] = AttrValidators();
44   }
45   aFeature->second[theAttrID].insert(
46     std::pair<std::string, std::list<std::string> >(theID, theArguments));
47 }
48
49 void Model_ValidatorsFactory::validators(
50   const std::string& theFeatureID, std::list<ModelAPI_Validator*>& theResult ) const
51 {
52   std::map<std::string, std::set<std::string> >::const_iterator aFeature =
53     myFeatures.find(theFeatureID);
54   if (aFeature != myFeatures.cend()) {
55     std::set<std::string>::const_iterator aValIter = aFeature->second.cbegin();
56     for(; aValIter != aFeature->second.cend(); aValIter++) {
57       std::map<std::string, ModelAPI_Validator*>::const_iterator aFound = myIDs.find(*aValIter);
58       if (aFound == myIDs.end()) {
59         Events_Error::send(std::string("Validator ") + *aValIter + " was not registered");
60       } else {
61         theResult.push_back(aFound->second);
62       }
63     }
64   }
65 }
66
67 void Model_ValidatorsFactory::validators(const std::string& theFeatureID, 
68   const std::string& theAttrID, std::list<ModelAPI_Validator*>& theValidators, 
69   std::list<std::list<std::string> >& theArguments) const
70 {
71   std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeature =
72     myAttrs.find(theFeatureID);
73   if (aFeature != myAttrs.cend()) {
74     std::map<std::string, AttrValidators>::const_iterator anAttr =
75       aFeature->second.find(theAttrID);
76     if (anAttr != aFeature->second.end()) {
77       AttrValidators::const_iterator aValIter = anAttr->second.cbegin();
78       for(; aValIter != anAttr->second.cend(); aValIter++) {
79         std::map<std::string, ModelAPI_Validator*>::const_iterator aFound = 
80           myIDs.find(aValIter->first);
81         if (aFound == myIDs.end()) {
82           Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered");
83         } else {
84           theValidators.push_back(aFound->second);
85           theArguments.push_back(aValIter->second);
86         }
87       }
88     }
89   }
90 }
91
92 Model_ValidatorsFactory::Model_ValidatorsFactory() : ModelAPI_ValidatorsFactory()
93 {
94   registerValidator("Model_ResultPointValidator", new Model_ResultPointValidator);
95   registerValidator("Model_ResultLineValidator", new Model_ResultLineValidator);
96   registerValidator("Model_ResultArcValidator", new Model_ResultArcValidator);
97 }
98
99
100 const ModelAPI_Validator* Model_ValidatorsFactory::validator(const std::string& theID) const
101 {
102   std::map<std::string, ModelAPI_Validator*>::const_iterator aIt = myIDs.find(theID);
103   if (aIt != myIDs.end()) {
104     return aIt->second;
105   }
106   return NULL;
107 }