1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Validator.cpp
5 // Author: Mikhail PONIKAROV
7 #include <Model_Validator.h>
8 #include <Model_FeatureValidator.h>
9 #include <ModelAPI_Feature.h>
10 #include <ModelAPI_Attribute.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_AttributeValidator.h>
13 #include <ModelAPI_AttributeString.h>
14 #include <Events_Error.h>
16 void Model_ValidatorsFactory::registerValidator(const std::string& theID,
17 ModelAPI_Validator* theValidator)
19 if (myIDs.find(theID) != myIDs.end()) {
20 Events_Error::send(std::string("Validator ") + theID + " is already registered");
22 myIDs[theID] = theValidator;
26 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
27 const std::string& theFeatureID)
29 if (myFeatures.find(theFeatureID) == myFeatures.end()) {
30 myFeatures[theFeatureID] = AttrValidators();
32 if (myFeatures[theFeatureID].find(theID) != myFeatures[theFeatureID].end()) {
33 //Events_Error::send(std::string("Validator ") + theID +
34 // " for feature " + theFeatureID + "is already registered");
36 myFeatures[theFeatureID][theID] = std::list<std::string>();
40 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
41 const std::string& theFeatureID,
42 const std::list<std::string>& theArguments)
44 if (myFeatures.find(theFeatureID) == myFeatures.end()) {
45 myFeatures[theFeatureID] = AttrValidators();
48 if (myFeatures[theFeatureID].find(theID) != myFeatures[theFeatureID].end()) {
49 //Events_Error::send(std::string("Validator ") + theID +
50 // " for feature " + theFeatureID + "is already registered");
52 myFeatures[theFeatureID][theID] = theArguments;
56 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
57 const std::string& theFeatureID,
58 const std::string& theAttrID,
59 const std::list<std::string>& theArguments)
61 // create feature-structures if not exist
62 std::map<std::string, std::map<std::string, AttrValidators> >::iterator aFeature = myAttrs.find(
64 if (aFeature == myAttrs.end()) {
65 myAttrs[theFeatureID] = std::map<std::string, AttrValidators>();
66 aFeature = myAttrs.find(theFeatureID);
68 // add attr-structure if not exist, or generate error if already exist
69 std::map<std::string, AttrValidators>::iterator anAttr = aFeature->second.find(theAttrID);
70 if (anAttr == aFeature->second.end()) {
71 aFeature->second[theAttrID] = AttrValidators();
73 aFeature->second[theAttrID][theID] = theArguments;
76 void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
77 std::list<ModelAPI_Validator*>& theResult,
78 std::list<std::list<std::string> >& theArguments) const
80 std::map<std::string, AttrValidators>::const_iterator aFeature = myFeatures.find(theFeatureID);
81 if (aFeature != myFeatures.cend()) {
82 AttrValidators::const_iterator aValIter = aFeature->second.cbegin();
83 for (; aValIter != aFeature->second.cend(); aValIter++) {
84 std::map<std::string, ModelAPI_Validator*>::const_iterator aFound =
85 myIDs.find(aValIter->first);
86 if (aFound == myIDs.end()) {
87 Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered");
89 theResult.push_back(aFound->second);
90 theArguments.push_back(aValIter->second);
94 addDefaultValidators(theResult, theArguments);
97 void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
98 const std::string& theAttrID,
99 std::list<ModelAPI_Validator*>& theValidators,
100 std::list<std::list<std::string> >& theArguments) const
102 std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeature =
103 myAttrs.find(theFeatureID);
104 if (aFeature != myAttrs.cend()) {
105 std::map<std::string, AttrValidators>::const_iterator anAttr = aFeature->second.find(theAttrID);
106 if (anAttr != aFeature->second.end()) {
107 AttrValidators::const_iterator aValIter = anAttr->second.cbegin();
108 for (; aValIter != anAttr->second.cend(); aValIter++) {
109 std::map<std::string, ModelAPI_Validator*>::const_iterator aFound = myIDs.find(
111 if (aFound == myIDs.end()) {
112 Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered");
114 theValidators.push_back(aFound->second);
115 theArguments.push_back(aValIter->second);
122 Model_ValidatorsFactory::Model_ValidatorsFactory()
123 : ModelAPI_ValidatorsFactory()
125 const static std::string kDefaultId = "Model_FeatureValidator";
126 registerValidator(kDefaultId, new Model_FeatureValidator);
129 const ModelAPI_Validator* Model_ValidatorsFactory::validator(const std::string& theID) const
131 std::map<std::string, ModelAPI_Validator*>::const_iterator aIt = myIDs.find(theID);
132 if (aIt != myIDs.end()) {
138 void Model_ValidatorsFactory::addDefaultValidators(std::list<ModelAPI_Validator*>& theValidators,
139 std::list<std::list<std::string> >& theArguments) const
141 const static std::string kDefaultId = "Model_FeatureValidator";
142 std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
143 if(it == myIDs.end())
145 theValidators.push_back(it->second);
146 theArguments.push_back(std::list<std::string>());
149 bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
151 const static std::string kDefaultId = "Model_FeatureValidator";
152 // check feature validators first
153 std::map<std::string, AttrValidators>::const_iterator aFeature =
154 myFeatures.find(theFeature->getKind());
155 if (aFeature != myFeatures.end()) {
156 AttrValidators::const_iterator aValidator = aFeature->second.begin();
157 for(; aValidator != aFeature->second.end(); aValidator++) {
158 std::map<std::string, ModelAPI_Validator*>::const_iterator aValFind =
159 myIDs.find(aValidator->first);
160 if (aValFind == myIDs.end()) {
161 Events_Error::send(std::string("Validator ") + aValidator->first + " was not registered");
164 const ModelAPI_FeatureValidator* aFValidator =
165 dynamic_cast<const ModelAPI_FeatureValidator*>(aValFind->second);
167 if (!aFValidator->isValid(theFeature, aValidator->second))
172 // check default validator
173 std::map<std::string, ModelAPI_Validator*>::const_iterator aDefaultVal = myIDs.find(kDefaultId);
174 if(aDefaultVal != myIDs.end()) {
175 static const std::list<std::string> anEmptyArgList;
176 const ModelAPI_FeatureValidator* aFValidator =
177 dynamic_cast<const ModelAPI_FeatureValidator*>(aDefaultVal->second);
179 if (!aFValidator->isValid(theFeature, anEmptyArgList))
184 // check all attributes for validity
185 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
186 // Validity of data is checked by "Model_FeatureValidator" (kDefaultId)
187 // if (!aData || !aData->isValid())
189 static const std::string kAllTypes = "";
190 std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeatureIter =
191 myAttrs.find(theFeature->getKind());
192 if (aFeatureIter != myAttrs.cend()) {
193 std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
194 std::list<std::string>::iterator anAttrIter = aLtAttributes.begin();
195 for (; anAttrIter != aLtAttributes.end(); anAttrIter++) {
196 std::map<std::string, AttrValidators>::const_iterator anAttr =
197 aFeatureIter->second.find(*anAttrIter);
198 if (anAttr != aFeatureIter->second.end()) {
199 AttrValidators::const_iterator aValIter = anAttr->second.cbegin();
200 for (; aValIter != anAttr->second.cend(); aValIter++) {
201 std::map<std::string, ModelAPI_Validator*>::const_iterator aFound = myIDs.find(
203 if (aFound == myIDs.end()) {
204 Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered");
206 const ModelAPI_AttributeValidator* anAttrValidator =
207 dynamic_cast<const ModelAPI_AttributeValidator*>(aFound->second);
208 if (anAttrValidator) {
209 AttributePtr anAttribute = theFeature->data()->attribute(*anAttrIter);
210 if (!anAttrValidator->isValid(anAttribute, aValIter->second)) {
222 void Model_ValidatorsFactory::registerNotObligatory(std::string theFeature, std::string theAttribute)
224 const static std::string kDefaultId = "Model_FeatureValidator";
225 std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
226 if (it != myIDs.end()) {
227 Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
229 aValidator->registerNotObligatory(theFeature, theAttribute);
234 bool Model_ValidatorsFactory::isNotObligatory(std::string theFeature, std::string theAttribute)
236 const static std::string kDefaultId = "Model_FeatureValidator";
237 std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
238 if (it != myIDs.end()) {
239 Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
241 return aValidator->isNotObligatory(theFeature, theAttribute);
244 return false; // default
247 void Model_ValidatorsFactory::registerConcealment(std::string theFeature, std::string theAttribute)
249 std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
250 if (aFind == myConcealed.end()) {
251 std::set<std::string> aNewSet;
252 aNewSet.insert(theAttribute);
253 myConcealed[theFeature] = aNewSet;
255 aFind->second.insert(theAttribute);
259 bool Model_ValidatorsFactory::isConcealed(std::string theFeature, std::string theAttribute)
261 std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
262 return aFind != myConcealed.end() && aFind->second.find(theAttribute) != aFind->second.end();
265 void Model_ValidatorsFactory::registerCase(std::string theFeature, std::string theAttribute,
266 std::string theSwitchId, std::string theCaseId)
268 std::map<std::string, std::map<std::string, std::pair<std::string, std::string> > >::iterator
269 aFindFeature = myCases.find(theFeature);
270 if (aFindFeature == myCases.end()) {
271 myCases[theFeature] = std::map<std::string, std::pair<std::string, std::string> >();
272 aFindFeature = myCases.find(theFeature);
274 (aFindFeature->second)[theAttribute] = std::pair<std::string, std::string>(theSwitchId, theCaseId);
277 bool Model_ValidatorsFactory::isCase(
278 FeaturePtr theFeature, std::string theAttribute)
280 std::map<std::string, std::map<std::string, std::pair<std::string, std::string> > >::iterator
281 aFindFeature = myCases.find(theFeature->getKind());
282 if (aFindFeature != myCases.end()) {
283 std::map<std::string, std::pair<std::string, std::string> >::iterator
284 aFindAttr = aFindFeature->second.find(theAttribute);
285 if (aFindAttr != aFindFeature->second.end()) {
286 // the the switch-attribute that contains the case value
287 AttributeStringPtr aSwitch = theFeature->string(aFindAttr->second.first);
289 return aSwitch->value() == aFindAttr->second.second; // the second is the case identifier
293 return true; // if no additional conditions, this attribute is the case to be validated