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_InfoMessage.h>
20 void Model_ValidatorsFactory::registerValidator(const std::string& theID,
21 ModelAPI_Validator* theValidator)
23 if (myIDs.find(theID) != myIDs.end()) {
24 Events_InfoMessage("Model_Validator", "Validator %1 is already registered").arg(theID).send();
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_InfoMessage("Model_Validator", "Validator %1 was not registered")
90 .arg(aValidatorsIt->first).send();
92 theValidators.push_back(std::make_pair(aValidatorsIt->first, aValidatorsIt->second));
96 addDefaultValidators(theValidators);
99 void Model_ValidatorsFactory::validators(const std::string& theFeatureID, const std::string& theAttrID,
100 Validators& theValidators) const
102 std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeatureIt =
103 myAttrs.find(theFeatureID);
104 if (aFeatureIt != myAttrs.cend()) {
105 std::map<std::string, AttrValidators>::const_iterator anAttrIt = aFeatureIt->second.find(theAttrID);
106 if (anAttrIt != aFeatureIt->second.end()) {
107 AttrValidators::const_iterator aValidatorsIt = anAttrIt->second.cbegin();
108 for (; aValidatorsIt != anAttrIt->second.cend(); aValidatorsIt++) {
109 if (!validator(aValidatorsIt->first)) {
110 Events_InfoMessage("Model_Validator", "Validator %1 was not registered")
111 .arg(aValidatorsIt->first).send();
113 theValidators.push_back(std::make_pair(aValidatorsIt->first, aValidatorsIt->second));
118 addDefaultAttributeValidators(theValidators);
121 Model_ValidatorsFactory::Model_ValidatorsFactory()
122 : ModelAPI_ValidatorsFactory()
124 registerValidator("Model_FeatureValidator", new Model_FeatureValidator);
125 registerValidator("Model_AttributeValidator", new Model_AttributeValidator);
128 const ModelAPI_Validator* Model_ValidatorsFactory::validator(const std::string& theID) const
130 std::map<std::string, ModelAPI_Validator*>::const_iterator aIt = myIDs.find(theID);
131 if (aIt != myIDs.end()) {
137 void Model_ValidatorsFactory::addDefaultValidators(Validators& theValidators) const
139 const static std::string kDefaultId = "Model_FeatureValidator";
140 if (!validator(kDefaultId))
142 theValidators.push_back(std::make_pair(kDefaultId, std::list<std::string>()));
145 void Model_ValidatorsFactory::addDefaultAttributeValidators(Validators& theValidators) const
147 const static std::string kDefaultId = "Model_AttributeValidator";
148 if (!validator(kDefaultId))
150 theValidators.push_back(std::make_pair(kDefaultId, std::list<std::string>()));
153 bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
155 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
156 if (aData.get() && aData->isValid()) {
157 if (aData->execState() == ModelAPI_StateDone)
158 aData->eraseErrorString(); // no error => erase the information string
160 return false; // feature is broken, already not presented in the data structure
163 // check feature validators first
164 Validators aValidators;
165 validators(theFeature->getKind(), aValidators);
167 if (!aValidators.empty()) {
168 Validators::const_iterator aValidatorIt = aValidators.cbegin();
169 for(; aValidatorIt != aValidators.cend(); aValidatorIt++) {
170 const std::string& aValidatorID = aValidatorIt->first;
171 const std::list<std::string>& anArguments = aValidatorIt->second;
172 // validators() checks invalid validator names
174 // Events_Error::send(std::string("Validator ") + aValidatorID + " was not registered");
177 const ModelAPI_FeatureValidator* aFValidator =
178 dynamic_cast<const ModelAPI_FeatureValidator*>(validator(aValidatorID));
181 if (!aFValidator->isValid(theFeature, anArguments, anError)) {
183 anError = "Unknown error.";
184 anError = aValidatorID + ": " + anError;
185 theFeature->setError(anError, false);
186 theFeature->data()->execState(ModelAPI_StateInvalidArgument);
192 // The default validator was retrned by validators() and was checked in previous cycle
193 //// check default validator
194 //std::map<std::string, ModelAPI_Validator*>::const_iterator aDefaultVal = myIDs.find(kDefaultId);
195 //if(aDefaultVal != myIDs.end()) {
196 // static const std::list<std::string> anEmptyArgList;
197 // const ModelAPI_FeatureValidator* aFValidator =
198 // dynamic_cast<const ModelAPI_FeatureValidator*>(aDefaultVal->second);
199 // if (aFValidator) {
200 // std::string anError;
201 // if (!aFValidator->isValid(theFeature, anEmptyArgList, anError)) {
202 // if (anError.empty())
203 // anError = "Unknown error.";
204 // anError = "Feature invalidated by \"" + kDefaultId + "\" with error: " + anError;
205 // theFeature->setError(anError, false);
206 // theFeature->data()->execState(ModelAPI_StateInvalidArgument);
212 // check all attributes for validity
213 // Validity of data is checked by "Model_FeatureValidator" (kDefaultId)
214 // if (!aData || !aData->isValid())
216 static const std::string kAllTypes = "";
217 std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
218 std::list<std::string>::const_iterator anAttrIt = aLtAttributes.cbegin();
219 for (; anAttrIt != aLtAttributes.cend(); anAttrIt++) {
220 const std::string& anAttributeID = *anAttrIt;
221 AttributePtr anAttribute = theFeature->data()->attribute(anAttributeID);
223 std::string aValidatorID;
225 if (!validate(anAttribute, aValidatorID, anError)) {
227 anError = "Unknown error.";
228 anError = anAttributeID + " - " + aValidatorID + ": " + anError;
229 theFeature->setError(anError, false);
230 theFeature->data()->execState(ModelAPI_StateInvalidArgument);
238 bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Attribute>& theAttribute,
239 std::string& theValidator,
240 std::string& theError) const
242 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
243 if (!aFeature.get()) {
244 theValidator = "Model_ValidatorsFactory";
245 theError = "Attribute has no feature.";
249 // skip not-case attributes, that really may be invalid (issue 671)
250 if (!const_cast<Model_ValidatorsFactory*>(this)->isCase(aFeature, theAttribute->id()))
253 Validators aValidators;
254 validators(aFeature->getKind(), theAttribute->id(), aValidators);
256 Validators::iterator aValidatorIt = aValidators.begin();
257 for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
258 const std::string& aValidatorID = aValidatorIt->first;
259 const std::list<std::string>& anArguments = aValidatorIt->second;
260 const ModelAPI_AttributeValidator* anAttrValidator =
261 dynamic_cast<const ModelAPI_AttributeValidator*>(validator(aValidatorID));
262 if (!anAttrValidator)
264 if (!anAttrValidator->isValid(theAttribute, anArguments, theError)) {
265 theValidator = aValidatorID;
273 void Model_ValidatorsFactory::registerNotObligatory(std::string theFeature, std::string theAttribute)
275 const static std::string kDefaultId = "Model_FeatureValidator";
276 std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
277 if (it != myIDs.end()) {
278 Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
280 aValidator->registerNotObligatory(theFeature, theAttribute);
285 bool Model_ValidatorsFactory::isNotObligatory(std::string theFeature, std::string theAttribute)
287 const static std::string kDefaultId = "Model_FeatureValidator";
288 std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
289 if (it != myIDs.end()) {
290 Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
292 return aValidator->isNotObligatory(theFeature, theAttribute);
295 return false; // default
298 void Model_ValidatorsFactory::registerConcealment(std::string theFeature, std::string theAttribute)
300 std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
301 if (aFind == myConcealed.end()) {
302 std::set<std::string> aNewSet;
303 aNewSet.insert(theAttribute);
304 myConcealed[theFeature] = aNewSet;
306 aFind->second.insert(theAttribute);
310 bool Model_ValidatorsFactory::isConcealed(std::string theFeature, std::string theAttribute)
312 std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
313 return aFind != myConcealed.end() && aFind->second.find(theAttribute) != aFind->second.end();
316 void Model_ValidatorsFactory::registerCase(std::string theFeature, std::string theAttribute,
317 std::string theSwitchId, std::string theCaseId)
319 std::map<std::string, std::map<std::string, std::pair<std::string, std::set<std::string> > > >
320 ::iterator aFindFeature = myCases.find(theFeature);
321 if (aFindFeature == myCases.end()) {
322 myCases[theFeature] = std::map<std::string, std::pair<std::string, std::set<std::string> > >();
323 aFindFeature = myCases.find(theFeature);
325 std::map<std::string, std::pair<std::string, std::set<std::string> > >::iterator aFindAttrID =
326 aFindFeature->second.find(theAttribute);
327 if (aFindAttrID == aFindFeature->second.end()) {
328 aFindFeature->second[theAttribute] =
329 std::pair<std::string, std::set<std::string> >(theSwitchId, std::set<std::string>());
330 aFindAttrID = aFindFeature->second.find(theAttribute);
332 aFindAttrID->second.second.insert(theCaseId);
335 bool Model_ValidatorsFactory::isCase(FeaturePtr theFeature, std::string theAttribute)
337 std::map<std::string, std::map<std::string, std::pair<std::string, std::set<std::string> > > >
338 ::iterator aFindFeature = myCases.find(theFeature->getKind());
339 if (aFindFeature != myCases.end()) {
340 std::map<std::string, std::pair<std::string, std::set<std::string> > >::iterator
341 aFindAttrID = aFindFeature->second.find(theAttribute);
342 if (aFindAttrID != aFindFeature->second.end()) {
343 // the the switch-attribute that contains the case value
344 AttributeStringPtr aSwitch = theFeature->string(aFindAttrID->second.first);
346 // the second has the case identifier
347 return aFindAttrID->second.second.find(aSwitch->value()) !=
348 aFindAttrID->second.second.end();
352 return true; // if no additional conditions, this attribute is the case to be validated