Salome HOME
a1741e3b7a81333723483afbb3d5f707f44fdefc
[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_FeatureValidator.h>
7 #include <ModelAPI_Feature.h>
8 #include <Events_Error.h>
9
10 void Model_ValidatorsFactory::registerValidator(const std::string& theID,
11                                                 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(const std::string& theID,
21                                               const std::string& theFeatureID)
22 {
23   if (myFeatures.find(theFeatureID) == myFeatures.end()) {
24     myFeatures[theFeatureID] = AttrValidators();
25   }
26   if (myFeatures[theFeatureID].find(theID) != myFeatures[theFeatureID].end()) {
27     //Events_Error::send(std::string("Validator ") + theID + 
28     //  " for feature " + theFeatureID + "is already registered");
29   } else {
30     myFeatures[theFeatureID][theID] = std::list<std::string>();
31   }
32 }
33
34 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
35                                               const std::string& theFeatureID,
36                                               const std::list<std::string>& theArguments)
37 {
38   if (myFeatures.find(theFeatureID) == myFeatures.end()) {
39     myFeatures[theFeatureID] = AttrValidators();
40   }
41
42   if (myFeatures[theFeatureID].find(theID) != myFeatures[theFeatureID].end()) {
43     //Events_Error::send(std::string("Validator ") + theID + 
44     //  " for feature " + theFeatureID + "is already registered");
45   } else {
46     myFeatures[theFeatureID][theID] = theArguments;
47   }
48 }
49
50 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
51                                               const std::string& theFeatureID,
52                                               const std::string& theAttrID,
53                                               const std::list<std::string>& theArguments)
54 {
55   // create feature-structures if not exist
56   std::map<std::string, std::map<std::string, AttrValidators> >::iterator aFeature = myAttrs.find(
57       theFeatureID);
58   if (aFeature == myAttrs.end()) {
59     myAttrs[theFeatureID] = std::map<std::string, AttrValidators>();
60     aFeature = myAttrs.find(theFeatureID);
61   }
62   // add attr-structure if not exist, or generate error if already exist
63   std::map<std::string, AttrValidators>::iterator anAttr = aFeature->second.find(theAttrID);
64   if (anAttr == aFeature->second.end()) {
65     aFeature->second[theAttrID] = AttrValidators();
66   }
67   aFeature->second[theAttrID][theID] = theArguments;
68 }
69
70 void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
71                                          std::list<ModelAPI_Validator*>& theResult,
72                                          std::list<std::list<std::string> >& theArguments) const
73 {
74   std::map<std::string, AttrValidators>::const_iterator aFeature = myFeatures.find(theFeatureID);
75   if (aFeature != myFeatures.cend()) {
76     AttrValidators::const_iterator aValIter = aFeature->second.cbegin();
77     for (; aValIter != aFeature->second.cend(); aValIter++) {
78       std::map<std::string, ModelAPI_Validator*>::const_iterator aFound = 
79         myIDs.find(aValIter->first);
80       if (aFound == myIDs.end()) {
81         Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered");
82       } else {
83         theResult.push_back(aFound->second);
84         theArguments.push_back(aValIter->second);
85       }
86     }
87   }
88   addDefaultValidators(theResult);
89 }
90
91 void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
92                                          const std::string& theAttrID,
93                                          std::list<ModelAPI_Validator*>& theValidators,
94                                          std::list<std::list<std::string> >& theArguments) const
95 {
96   std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeature = myAttrs
97       .find(theFeatureID);
98   if (aFeature != myAttrs.cend()) {
99     std::map<std::string, AttrValidators>::const_iterator anAttr = aFeature->second.find(theAttrID);
100     if (anAttr != aFeature->second.end()) {
101       AttrValidators::const_iterator aValIter = anAttr->second.cbegin();
102       for (; aValIter != anAttr->second.cend(); aValIter++) {
103         std::map<std::string, ModelAPI_Validator*>::const_iterator aFound = myIDs.find(
104             aValIter->first);
105         if (aFound == myIDs.end()) {
106           Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered");
107         } else {
108           theValidators.push_back(aFound->second);
109           theArguments.push_back(aValIter->second);
110         }
111       }
112     }
113   }
114 }
115
116 Model_ValidatorsFactory::Model_ValidatorsFactory()
117     : ModelAPI_ValidatorsFactory()
118 {
119   registerValidator("Model_FeatureValidator", new Model_FeatureValidator);
120 }
121
122 const ModelAPI_Validator* Model_ValidatorsFactory::validator(const std::string& theID) const
123 {
124   std::map<std::string, ModelAPI_Validator*>::const_iterator aIt = myIDs.find(theID);
125   if (aIt != myIDs.end()) {
126     return aIt->second;
127   }
128   return NULL;
129 }
130
131 void Model_ValidatorsFactory::addDefaultValidators(std::list<ModelAPI_Validator*>& theValidators) const
132 {
133   std::string anId = "Model_FeatureValidator";
134   std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(anId);
135   if(it == myIDs.end())
136     return;
137   theValidators.push_back(it->second);
138 }