1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Validator.cpp
5 // Author: Mikhail PONIKAROV
7 #include "Model_Validator.h"
9 #include "Model_AttributeValidator.h"
10 #include "Model_FeatureValidator.h"
12 #include <ModelAPI_Attribute.h>
13 #include <ModelAPI_AttributeString.h>
14 #include <ModelAPI_AttributeValidator.h>
15 #include <ModelAPI_Feature.h>
16 #include <Model_Data.h>
18 #include <Events_Error.h>
20 void Model_ValidatorsFactory::registerValidator(const std::string& theID,
21 ModelAPI_Validator* theValidator)
23 if (myIDs.find(theID) != myIDs.end()) {
24 Events_Error::send(std::string("Validator ") + theID + " is already registered");
26 myIDs[theID] = theValidator;
30 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
31 const std::string& theFeatureID)
33 if (myFeatures.find(theFeatureID) == myFeatures.end()) {
34 myFeatures[theFeatureID] = AttrValidators();
36 if (myFeatures[theFeatureID].find(theID) != myFeatures[theFeatureID].end()) {
37 //Events_Error::send(std::string("Validator ") + theID +
38 // " for feature " + theFeatureID + "is already registered");
40 myFeatures[theFeatureID][theID] = std::list<std::string>();
44 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
45 const std::string& theFeatureID,
46 const std::list<std::string>& theArguments)
48 if (myFeatures.find(theFeatureID) == myFeatures.end()) {
49 myFeatures[theFeatureID] = AttrValidators();
52 if (myFeatures[theFeatureID].find(theID) != myFeatures[theFeatureID].end()) {
53 //Events_Error::send(std::string("Validator ") + theID +
54 // " for feature " + theFeatureID + "is already registered");
56 myFeatures[theFeatureID][theID] = theArguments;
60 void Model_ValidatorsFactory::assignValidator(const std::string& theID,
61 const std::string& theFeatureID,
62 const std::string& theAttrID,
63 const std::list<std::string>& theArguments)
65 // create feature-structures if not exist
66 std::map<std::string, std::map<std::string, AttrValidators> >::iterator aFeature = myAttrs.find(
68 if (aFeature == myAttrs.end()) {
69 myAttrs[theFeatureID] = std::map<std::string, AttrValidators>();
70 aFeature = myAttrs.find(theFeatureID);
72 // add attr-structure if not exist, or generate error if already exist
73 std::map<std::string, AttrValidators>::iterator anAttr = aFeature->second.find(theAttrID);
74 if (anAttr == aFeature->second.end()) {
75 aFeature->second[theAttrID] = AttrValidators();
77 aFeature->second[theAttrID][theID] = theArguments;
80 void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
81 Validators& theValidators) const
83 std::map<std::string, AttrValidators>::const_iterator aFeatureIt =
84 myFeatures.find(theFeatureID);
85 if (aFeatureIt != myFeatures.cend()) {
86 AttrValidators::const_iterator aValidatorsIt = aFeatureIt->second.cbegin();
87 for (; aValidatorsIt != aFeatureIt->second.cend(); aValidatorsIt++) {
88 if (!validator(aValidatorsIt->first)) {
89 Events_Error::send(std::string("Validator ") + aValidatorsIt->first + " was not registered");
91 theValidators.push_back(std::make_pair(aValidatorsIt->first, aValidatorsIt->second));
95 addDefaultValidators(theValidators);
98 void Model_ValidatorsFactory::validators(const std::string& theFeatureID, const std::string& theAttrID,
99 Validators& theValidators) const
101 std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeatureIt =
102 myAttrs.find(theFeatureID);
103 if (aFeatureIt != myAttrs.cend()) {
104 std::map<std::string, AttrValidators>::const_iterator anAttrIt = aFeatureIt->second.find(theAttrID);
105 if (anAttrIt != aFeatureIt->second.end()) {
106 AttrValidators::const_iterator aValidatorsIt = anAttrIt->second.cbegin();
107 for (; aValidatorsIt != anAttrIt->second.cend(); aValidatorsIt++) {
108 if (!validator(aValidatorsIt->first)) {
109 Events_Error::send(std::string("Validator ") + aValidatorsIt->first + " was not registered");
111 theValidators.push_back(std::make_pair(aValidatorsIt->first, aValidatorsIt->second));
116 addDefaultAttributeValidators(theValidators);
119 Model_ValidatorsFactory::Model_ValidatorsFactory()
120 : ModelAPI_ValidatorsFactory()
122 registerValidator("Model_FeatureValidator", new Model_FeatureValidator);
123 registerValidator("Model_AttributeValidator", new Model_AttributeValidator);
126 const ModelAPI_Validator* Model_ValidatorsFactory::validator(const std::string& theID) const
128 std::map<std::string, ModelAPI_Validator*>::const_iterator aIt = myIDs.find(theID);
129 if (aIt != myIDs.end()) {
135 void Model_ValidatorsFactory::addDefaultValidators(Validators& theValidators) const
137 const static std::string kDefaultId = "Model_FeatureValidator";
138 if (!validator(kDefaultId))
140 theValidators.push_back(std::make_pair(kDefaultId, std::list<std::string>()));
143 void Model_ValidatorsFactory::addDefaultAttributeValidators(Validators& theValidators) const
145 const static std::string kDefaultId = "Model_AttributeValidator";
146 if (!validator(kDefaultId))
148 theValidators.push_back(std::make_pair(kDefaultId, std::list<std::string>()));
151 bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
153 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
154 if (aData.get() && aData->isValid()) {
155 if (aData->execState() == ModelAPI_StateDone)
156 aData->eraseErrorString(); // no error => erase the information string
158 return false; // feature is broken, already not presented in the data structure
161 // check feature validators first
162 Validators aValidators;
163 validators(theFeature->getKind(), aValidators);
165 if (!aValidators.empty()) {
166 Validators::const_iterator aValidatorIt = aValidators.cbegin();
167 for(; aValidatorIt != aValidators.cend(); aValidatorIt++) {
168 const std::string& aValidatorID = aValidatorIt->first;
169 const std::list<std::string>& anArguments = aValidatorIt->second;
170 // validators() checks invalid validator names
172 // Events_Error::send(std::string("Validator ") + aValidatorID + " was not registered");
175 const ModelAPI_FeatureValidator* aFValidator =
176 dynamic_cast<const ModelAPI_FeatureValidator*>(validator(aValidatorID));
179 if (!aFValidator->isValid(theFeature, anArguments, anError)) {
181 anError = "Unknown error.";
182 anError = aValidatorID + ": " + anError;
183 theFeature->setError(anError, false);
184 theFeature->data()->execState(ModelAPI_StateInvalidArgument);
190 // The default validator was retrned by validators() and was checked in previous cycle
191 //// check default validator
192 //std::map<std::string, ModelAPI_Validator*>::const_iterator aDefaultVal = myIDs.find(kDefaultId);
193 //if(aDefaultVal != myIDs.end()) {
194 // static const std::list<std::string> anEmptyArgList;
195 // const ModelAPI_FeatureValidator* aFValidator =
196 // dynamic_cast<const ModelAPI_FeatureValidator*>(aDefaultVal->second);
197 // if (aFValidator) {
198 // std::string anError;
199 // if (!aFValidator->isValid(theFeature, anEmptyArgList, anError)) {
200 // if (anError.empty())
201 // anError = "Unknown error.";
202 // anError = "Feature invalidated by \"" + kDefaultId + "\" with error: " + anError;
203 // theFeature->setError(anError, false);
204 // theFeature->data()->execState(ModelAPI_StateInvalidArgument);
210 // check all attributes for validity
211 // Validity of data is checked by "Model_FeatureValidator" (kDefaultId)
212 // if (!aData || !aData->isValid())
214 static const std::string kAllTypes = "";
215 std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
216 std::list<std::string>::const_iterator anAttrIt = aLtAttributes.cbegin();
217 for (; anAttrIt != aLtAttributes.cend(); anAttrIt++) {
218 const std::string& anAttributeID = *anAttrIt;
219 AttributePtr anAttribute = theFeature->data()->attribute(anAttributeID);
221 std::string aValidatorID;
223 if (!validate(anAttribute, aValidatorID, anError)) {
225 anError = "Unknown error.";
226 anError = anAttributeID + " - " + aValidatorID + ": " + anError;
227 theFeature->setError(anError, false);
228 theFeature->data()->execState(ModelAPI_StateInvalidArgument);
236 bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Attribute>& theAttribute,
237 std::string& theValidator,
238 std::string& theError) const
240 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
241 if (!aFeature.get()) {
242 theValidator = "Model_ValidatorsFactory";
243 theError = "Attribute has no feature.";
247 // skip not-case attributes, that really may be invalid (issue 671)
248 if (!const_cast<Model_ValidatorsFactory*>(this)->isCase(aFeature, theAttribute->id()))
251 Validators aValidators;
252 validators(aFeature->getKind(), theAttribute->id(), aValidators);
254 Validators::iterator aValidatorIt = aValidators.begin();
255 for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
256 const std::string& aValidatorID = aValidatorIt->first;
257 const std::list<std::string>& anArguments = aValidatorIt->second;
258 const ModelAPI_AttributeValidator* anAttrValidator =
259 dynamic_cast<const ModelAPI_AttributeValidator*>(validator(aValidatorID));
260 if (!anAttrValidator)
262 if (!anAttrValidator->isValid(theAttribute, anArguments, theError)) {
263 theValidator = aValidatorID;
271 void Model_ValidatorsFactory::registerNotObligatory(std::string theFeature, std::string theAttribute)
273 const static std::string kDefaultId = "Model_FeatureValidator";
274 std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
275 if (it != myIDs.end()) {
276 Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
278 aValidator->registerNotObligatory(theFeature, theAttribute);
283 bool Model_ValidatorsFactory::isNotObligatory(std::string theFeature, std::string theAttribute)
285 const static std::string kDefaultId = "Model_FeatureValidator";
286 std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
287 if (it != myIDs.end()) {
288 Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
290 return aValidator->isNotObligatory(theFeature, theAttribute);
293 return false; // default
296 void Model_ValidatorsFactory::registerConcealment(std::string theFeature, std::string theAttribute)
298 std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
299 if (aFind == myConcealed.end()) {
300 std::set<std::string> aNewSet;
301 aNewSet.insert(theAttribute);
302 myConcealed[theFeature] = aNewSet;
304 aFind->second.insert(theAttribute);
308 bool Model_ValidatorsFactory::isConcealed(std::string theFeature, std::string theAttribute)
310 std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
311 return aFind != myConcealed.end() && aFind->second.find(theAttribute) != aFind->second.end();
314 void Model_ValidatorsFactory::registerCase(std::string theFeature, std::string theAttribute,
315 std::string theSwitchId, std::string theCaseId)
317 std::map<std::string, std::map<std::string, std::pair<std::string, std::set<std::string> > > >
318 ::iterator aFindFeature = myCases.find(theFeature);
319 if (aFindFeature == myCases.end()) {
320 myCases[theFeature] = std::map<std::string, std::pair<std::string, std::set<std::string> > >();
321 aFindFeature = myCases.find(theFeature);
323 std::map<std::string, std::pair<std::string, std::set<std::string> > >::iterator aFindAttrID =
324 aFindFeature->second.find(theAttribute);
325 if (aFindAttrID == aFindFeature->second.end()) {
326 aFindFeature->second[theAttribute] =
327 std::pair<std::string, std::set<std::string> >(theSwitchId, std::set<std::string>());
328 aFindAttrID = aFindFeature->second.find(theAttribute);
330 aFindAttrID->second.second.insert(theCaseId);
333 bool Model_ValidatorsFactory::isCase(FeaturePtr theFeature, std::string theAttribute)
335 std::map<std::string, std::map<std::string, std::pair<std::string, std::set<std::string> > > >
336 ::iterator aFindFeature = myCases.find(theFeature->getKind());
337 if (aFindFeature != myCases.end()) {
338 std::map<std::string, std::pair<std::string, std::set<std::string> > >::iterator
339 aFindAttrID = aFindFeature->second.find(theAttribute);
340 if (aFindAttrID != aFindFeature->second.end()) {
341 // the the switch-attribute that contains the case value
342 AttributeStringPtr aSwitch = theFeature->string(aFindAttrID->second.first);
344 // the second has the case identifier
345 return aFindAttrID->second.second.find(aSwitch->value()) !=
346 aFindAttrID->second.second.end();
350 return true; // if no additional conditions, this attribute is the case to be validated