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_Data.h>
16 #include <ModelAPI_Feature.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 ModelAPI_ExecState anExecState = theFeature->data()->execState();
154 theFeature->setError("", false);
155 theFeature->data()->execState(anExecState);
157 // check feature validators first
158 Validators aValidators;
159 validators(theFeature->getKind(), aValidators);
161 if (!aValidators.empty()) {
162 Validators::const_iterator aValidatorIt = aValidators.cbegin();
163 for(; aValidatorIt != aValidators.cend(); aValidatorIt++) {
164 const std::string& aValidatorID = aValidatorIt->first;
165 const std::list<std::string>& anArguments = aValidatorIt->second;
166 // validators() checks invalid validator names
168 // Events_Error::send(std::string("Validator ") + aValidatorID + " was not registered");
171 const ModelAPI_FeatureValidator* aFValidator =
172 dynamic_cast<const ModelAPI_FeatureValidator*>(validator(aValidatorID));
175 if (!aFValidator->isValid(theFeature, anArguments, anError)) {
177 anError = "Unknown error.";
178 anError = "Feature invalidated by \"" + aValidatorID + "\" with error: " + anError;
179 theFeature->setError(anError, false);
180 theFeature->data()->execState(ModelAPI_StateInvalidArgument);
186 // The default validator was retrned by validators() and was checked in previous cycle
187 //// check default validator
188 //std::map<std::string, ModelAPI_Validator*>::const_iterator aDefaultVal = myIDs.find(kDefaultId);
189 //if(aDefaultVal != myIDs.end()) {
190 // static const std::list<std::string> anEmptyArgList;
191 // const ModelAPI_FeatureValidator* aFValidator =
192 // dynamic_cast<const ModelAPI_FeatureValidator*>(aDefaultVal->second);
193 // if (aFValidator) {
194 // std::string anError;
195 // if (!aFValidator->isValid(theFeature, anEmptyArgList, anError)) {
196 // if (anError.empty())
197 // anError = "Unknown error.";
198 // anError = "Feature invalidated by \"" + kDefaultId + "\" with error: " + anError;
199 // theFeature->setError(anError, false);
200 // theFeature->data()->execState(ModelAPI_StateInvalidArgument);
206 // check all attributes for validity
207 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
208 // Validity of data is checked by "Model_FeatureValidator" (kDefaultId)
209 // if (!aData || !aData->isValid())
211 static const std::string kAllTypes = "";
212 std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
213 std::list<std::string>::const_iterator anAttrIt = aLtAttributes.cbegin();
214 for (; anAttrIt != aLtAttributes.cend(); anAttrIt++) {
215 const std::string& anAttributeID = *anAttrIt;
216 AttributePtr anAttribute = theFeature->data()->attribute(anAttributeID);
218 std::string aValidatorID;
220 if (!validate(anAttribute, aValidatorID, anError)) {
222 anError = "Unknown error.";
223 anError = "Attribute \"" + anAttributeID + "\" invalidated by \"" + aValidatorID + "\" with error: " + anError;
224 theFeature->setError(anError, false);
225 theFeature->data()->execState(ModelAPI_StateInvalidArgument);
233 bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Attribute>& theAttribute,
234 std::string& theValidator,
235 std::string& theError) const
237 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
238 if (!aFeature.get()) {
239 theValidator = "Model_ValidatorsFactory";
240 theError = "Attribute has no feature.";
244 // skip not-case attributes, that really may be invalid (issue 671)
245 if (!const_cast<Model_ValidatorsFactory*>(this)->isCase(aFeature, theAttribute->id()))
248 Validators aValidators;
249 validators(aFeature->getKind(), theAttribute->id(), aValidators);
251 Validators::iterator aValidatorIt = aValidators.begin();
252 for (; aValidatorIt != aValidators.end(); ++aValidatorIt) {
253 const std::string& aValidatorID = aValidatorIt->first;
254 const std::list<std::string>& anArguments = aValidatorIt->second;
255 const ModelAPI_AttributeValidator* anAttrValidator =
256 dynamic_cast<const ModelAPI_AttributeValidator*>(validator(aValidatorID));
257 if (!anAttrValidator)
259 if (!anAttrValidator->isValid(theAttribute, anArguments, theError)) {
260 theValidator = aValidatorID;
268 void Model_ValidatorsFactory::registerNotObligatory(std::string theFeature, std::string theAttribute)
270 const static std::string kDefaultId = "Model_FeatureValidator";
271 std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
272 if (it != myIDs.end()) {
273 Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
275 aValidator->registerNotObligatory(theFeature, theAttribute);
280 bool Model_ValidatorsFactory::isNotObligatory(std::string theFeature, std::string theAttribute)
282 const static std::string kDefaultId = "Model_FeatureValidator";
283 std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
284 if (it != myIDs.end()) {
285 Model_FeatureValidator* aValidator = dynamic_cast<Model_FeatureValidator*>(it->second);
287 return aValidator->isNotObligatory(theFeature, theAttribute);
290 return false; // default
293 void Model_ValidatorsFactory::registerConcealment(std::string theFeature, std::string theAttribute)
295 std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
296 if (aFind == myConcealed.end()) {
297 std::set<std::string> aNewSet;
298 aNewSet.insert(theAttribute);
299 myConcealed[theFeature] = aNewSet;
301 aFind->second.insert(theAttribute);
305 bool Model_ValidatorsFactory::isConcealed(std::string theFeature, std::string theAttribute)
307 std::map<std::string, std::set<std::string> >::iterator aFind = myConcealed.find(theFeature);
308 return aFind != myConcealed.end() && aFind->second.find(theAttribute) != aFind->second.end();
311 void Model_ValidatorsFactory::registerCase(std::string theFeature, std::string theAttribute,
312 std::string theSwitchId, std::string theCaseId)
314 std::map<std::string, std::map<std::string, std::pair<std::string, std::string> > >::iterator
315 aFindFeature = myCases.find(theFeature);
316 if (aFindFeature == myCases.end()) {
317 myCases[theFeature] = std::map<std::string, std::pair<std::string, std::string> >();
318 aFindFeature = myCases.find(theFeature);
320 (aFindFeature->second)[theAttribute] = std::pair<std::string, std::string>(theSwitchId, theCaseId);
323 bool Model_ValidatorsFactory::isCase(
324 FeaturePtr theFeature, std::string theAttribute)
326 std::map<std::string, std::map<std::string, std::pair<std::string, std::string> > >::iterator
327 aFindFeature = myCases.find(theFeature->getKind());
328 if (aFindFeature != myCases.end()) {
329 std::map<std::string, std::pair<std::string, std::string> >::iterator
330 aFindAttr = aFindFeature->second.find(theAttribute);
331 if (aFindAttr != aFindFeature->second.end()) {
332 // the the switch-attribute that contains the case value
333 AttributeStringPtr aSwitch = theFeature->string(aFindAttr->second.first);
335 return aSwitch->value() == aFindAttr->second.second; // the second is the case identifier
339 return true; // if no additional conditions, this attribute is the case to be validated