From: spo Date: Mon, 27 Jul 2015 10:40:47 +0000 (+0300) Subject: Improve ModelAPI_ValidatorsFactory interface + Introduce validate(attribute) method. X-Git-Tag: V_1.4.0_beta4~431 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=1b585947597658057eedf6223a230011e20a959d;p=modules%2Fshaper.git Improve ModelAPI_ValidatorsFactory interface + Introduce validate(attribute) method. --- diff --git a/src/Model/Model_Validator.cpp b/src/Model/Model_Validator.cpp index 62715db6e..83a4e04cb 100644 --- a/src/Model/Model_Validator.cpp +++ b/src/Model/Model_Validator.cpp @@ -74,45 +74,37 @@ void Model_ValidatorsFactory::assignValidator(const std::string& theID, } void Model_ValidatorsFactory::validators(const std::string& theFeatureID, - std::list& theResult, - std::list >& theArguments) const + Validators& theValidators) const { - std::map::const_iterator aFeature = myFeatures.find(theFeatureID); - if (aFeature != myFeatures.cend()) { - AttrValidators::const_iterator aValIter = aFeature->second.cbegin(); - for (; aValIter != aFeature->second.cend(); aValIter++) { - std::map::const_iterator aFound = - myIDs.find(aValIter->first); - if (aFound == myIDs.end()) { - Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered"); + std::map::const_iterator aFeatureIt = + myFeatures.find(theFeatureID); + if (aFeatureIt != myFeatures.cend()) { + AttrValidators::const_iterator aValidatorsIt = aFeatureIt->second.cbegin(); + for (; aValidatorsIt != aFeatureIt->second.cend(); aValidatorsIt++) { + if (!validator(aValidatorsIt->first)) { + Events_Error::send(std::string("Validator ") + aValidatorsIt->first + " was not registered"); } else { - theResult.push_back(aFound->second); - theArguments.push_back(aValIter->second); + theValidators.push_back(std::make_pair(aValidatorsIt->first, aValidatorsIt->second)); } } } - addDefaultValidators(theResult, theArguments); + addDefaultValidators(theValidators); } -void Model_ValidatorsFactory::validators(const std::string& theFeatureID, - const std::string& theAttrID, - std::list& theValidators, - std::list >& theArguments) const +void Model_ValidatorsFactory::validators(const std::string& theFeatureID, const std::string& theAttrID, + Validators& theValidators) const { - std::map >::const_iterator aFeature = - myAttrs.find(theFeatureID); - if (aFeature != myAttrs.cend()) { - std::map::const_iterator anAttr = aFeature->second.find(theAttrID); - if (anAttr != aFeature->second.end()) { - AttrValidators::const_iterator aValIter = anAttr->second.cbegin(); - for (; aValIter != anAttr->second.cend(); aValIter++) { - std::map::const_iterator aFound = myIDs.find( - aValIter->first); - if (aFound == myIDs.end()) { - Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered"); + std::map >::const_iterator aFeatureIt = + myAttrs.find(theFeatureID); + if (aFeatureIt != myAttrs.cend()) { + std::map::const_iterator anAttrIt = aFeatureIt->second.find(theAttrID); + if (anAttrIt != aFeatureIt->second.end()) { + AttrValidators::const_iterator aValidatorsIt = anAttrIt->second.cbegin(); + for (; aValidatorsIt != anAttrIt->second.cend(); aValidatorsIt++) { + if (!validator(aValidatorsIt->first)) { + Events_Error::send(std::string("Validator ") + aValidatorsIt->first + " was not registered"); } else { - theValidators.push_back(aFound->second); - theArguments.push_back(aValIter->second); + theValidators.push_back(std::make_pair(aValidatorsIt->first, aValidatorsIt->second)); } } } @@ -135,15 +127,12 @@ const ModelAPI_Validator* Model_ValidatorsFactory::validator(const std::string& return NULL; } -void Model_ValidatorsFactory::addDefaultValidators(std::list& theValidators, - std::list >& theArguments) const +void Model_ValidatorsFactory::addDefaultValidators(Validators& theValidators) const { const static std::string kDefaultId = "Model_FeatureValidator"; - std::map::const_iterator it = myIDs.find(kDefaultId); - if(it == myIDs.end()) + if (!validator(kDefaultId)) return; - theValidators.push_back(it->second); - theArguments.push_back(std::list()); + theValidators.push_back(std::make_pair(kDefaultId, std::list())); } bool Model_ValidatorsFactory::validate(const std::shared_ptr& theFeature) const @@ -155,52 +144,53 @@ bool Model_ValidatorsFactory::validate(const std::shared_ptr& theFeature->data()->execState(anExecState); // check feature validators first - std::map::const_iterator aFeature = - myFeatures.find(theFeature->getKind()); - if (aFeature != myFeatures.end()) { - AttrValidators::const_iterator aValidator = aFeature->second.begin(); - for(; aValidator != aFeature->second.end(); aValidator++) { - const std::string& aValidatorID = aValidator->first; - const std::list& anArguments = aValidator->second; - std::map::const_iterator aValFind = - myIDs.find(aValidatorID); - if (aValFind == myIDs.end()) { - Events_Error::send(std::string("Validator ") + aValidatorID + " was not registered"); - continue; - } + Validators aValidators; + validators(theFeature->getKind(), aValidators); + + if (!aValidators.empty()) { + Validators::const_iterator aValidatorIt = aValidators.cbegin(); + for(; aValidatorIt != aValidators.cend(); aValidatorIt++) { + const std::string& aValidatorID = aValidatorIt->first; + const std::list& anArguments = aValidatorIt->second; + // validators() checks invalid validator names + //if (!aValidator) { + // Events_Error::send(std::string("Validator ") + aValidatorID + " was not registered"); + // continue; + //} const ModelAPI_FeatureValidator* aFValidator = - dynamic_cast(aValFind->second); + dynamic_cast(validator(aValidatorID)); if (aFValidator) { std::string anError; if (!aFValidator->isValid(theFeature, anArguments, anError)) { if (anError.empty()) anError = "Unknown error."; - theFeature->setValidationError(aValidatorID, anError); + anError = "Feature invalidated by \"" + aValidatorID + "\" with error: " + anError; + theFeature->setError(anError, false); + theFeature->data()->execState(ModelAPI_StateInvalidArgument); return false; - } else { - theFeature->setValidationError(aValidatorID, ""); } } } } - // check default validator - std::map::const_iterator aDefaultVal = myIDs.find(kDefaultId); - if(aDefaultVal != myIDs.end()) { - static const std::list anEmptyArgList; - const ModelAPI_FeatureValidator* aFValidator = - dynamic_cast(aDefaultVal->second); - if (aFValidator) { - std::string anError; - if (!aFValidator->isValid(theFeature, anEmptyArgList, anError)) { - if (anError.empty()) - anError = "Unknown error."; - theFeature->setValidationError(kDefaultId, anError); - return false; - } else { - theFeature->setValidationError(kDefaultId, ""); - } - } - } + // The default validator was retrned by validators() and was checked in previous cycle + //// check default validator + //std::map::const_iterator aDefaultVal = myIDs.find(kDefaultId); + //if(aDefaultVal != myIDs.end()) { + // static const std::list anEmptyArgList; + // const ModelAPI_FeatureValidator* aFValidator = + // dynamic_cast(aDefaultVal->second); + // if (aFValidator) { + // std::string anError; + // if (!aFValidator->isValid(theFeature, anEmptyArgList, anError)) { + // if (anError.empty()) + // anError = "Unknown error."; + // anError = "Feature invalidated by \"" + kDefaultId + "\" with error: " + anError; + // theFeature->setError(anError, false); + // theFeature->data()->execState(ModelAPI_StateInvalidArgument); + // return false; + // } + // } + //} // check all attributes for validity std::shared_ptr aData = theFeature->data(); @@ -208,46 +198,59 @@ bool Model_ValidatorsFactory::validate(const std::shared_ptr& // if (!aData || !aData->isValid()) // return false; static const std::string kAllTypes = ""; - std::map >::const_iterator aFeatureIter = - myAttrs.find(theFeature->getKind()); - if (aFeatureIter != myAttrs.cend()) { - std::list aLtAttributes = aData->attributesIDs(kAllTypes); - std::list::iterator anAttrIter = aLtAttributes.begin(); - for (; anAttrIter != aLtAttributes.end(); anAttrIter++) { - const std::string& anAttributeID = *anAttrIter; - std::map::const_iterator anAttr = - aFeatureIter->second.find(anAttributeID); - if (anAttr != aFeatureIter->second.end()) { - // skip not-case attributres, that really may be invalid (issue 671) - if (!const_cast(this)->isCase(theFeature, anAttributeID)) - continue; - AttrValidators::const_iterator aValIter = anAttr->second.cbegin(); - for (; aValIter != anAttr->second.cend(); aValIter++) { - const std::string& aValidatorID = aValIter->first; - const std::list& anArguments = aValIter->second; - std::map::const_iterator aFound = myIDs.find(aValidatorID); - if (aFound == myIDs.end()) { - Events_Error::send(std::string("Validator ") + aValidatorID + " was not registered"); - } else { - const ModelAPI_AttributeValidator* anAttrValidator = - dynamic_cast(aFound->second); - if (anAttrValidator) { - AttributePtr anAttribute = theFeature->data()->attribute(anAttributeID); - std::string anError; - if (!anAttrValidator->isValid(anAttribute, anArguments, anError)) { - if (anError.empty()) - anError = "Unknown error."; - theFeature->setAttributeValidationError(anAttributeID, aValidatorID, anError); - return false; - } else { - theFeature->setAttributeValidationError(anAttributeID, aValidatorID, ""); - } - } - } - } - } - } + std::list aLtAttributes = aData->attributesIDs(kAllTypes); + std::list::const_iterator anAttrIt = aLtAttributes.cbegin(); + for (; anAttrIt != aLtAttributes.cend(); anAttrIt++) { + const std::string& anAttributeID = *anAttrIt; + AttributePtr anAttribute = theFeature->data()->attribute(anAttributeID); + + std::string aValidatorID; + std::string anError; + if (!validate(anAttribute, aValidatorID, anError)) { + if (anError.empty()) + anError = "Unknown error."; + anError = "Attribute \"" + anAttributeID + "\" invalidated by \"" + aValidatorID + "\" with error: " + anError; + theFeature->setError(anError, false); + theFeature->data()->execState(ModelAPI_StateInvalidArgument); + return false; + } + } + + return true; +} + +bool Model_ValidatorsFactory::validate(const std::shared_ptr& theAttribute, + std::string& theValidator, + std::string& theError) const +{ + FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner()); + if (!aFeature.get()) { + theValidator = "Model_ValidatorsFactory"; + theError = "Attribute has no feature."; + return false; + } + + // skip not-case attributes, that really may be invalid (issue 671) + if (!const_cast(this)->isCase(aFeature, theAttribute->id())) + return true; + + Validators aValidators; + validators(aFeature->getKind(), theAttribute->id(), aValidators); + + Validators::iterator aValidatorIt = aValidators.begin(); + for (; aValidatorIt != aValidators.end(); ++aValidatorIt) { + const std::string& aValidatorID = aValidatorIt->first; + const std::list& anArguments = aValidatorIt->second; + const ModelAPI_AttributeValidator* anAttrValidator = + dynamic_cast(validator(aValidatorID)); + if (!anAttrValidator) + continue; + if (!anAttrValidator->isValid(theAttribute, anArguments, theError)) { + theValidator = aValidatorID; + return false; + } } + return true; } diff --git a/src/Model/Model_Validator.h b/src/Model/Model_Validator.h index 0bd242ad2..87552dd42 100644 --- a/src/Model/Model_Validator.h +++ b/src/Model/Model_Validator.h @@ -64,13 +64,10 @@ class Model_ValidatorsFactory : public ModelAPI_ValidatorsFactory /// Provides a validator for the feature, returns NULL if no validator MODEL_EXPORT virtual void validators(const std::string& theFeatureID, - std::list& theResult, - std::list >& theArguments) const; + Validators& theResult) const; /// Provides a validator for the attribute, returns NULL if no validator - MODEL_EXPORT virtual void validators(const std::string& theFeatureID, - const std::string& theAttrID, - std::list& theValidators, - std::list >& theArguments) const; + MODEL_EXPORT virtual void validators(const std::string& theFeatureID, const std::string& theAttrID, + Validators& theResult) const; /// Returns registered validator by its Id MODEL_EXPORT virtual const ModelAPI_Validator* validator(const std::string& theID) const; @@ -78,6 +75,10 @@ class Model_ValidatorsFactory : public ModelAPI_ValidatorsFactory /// Returns true if feature and all its attributes are valid. MODEL_EXPORT virtual bool validate(const std::shared_ptr& theFeature) const; + /// Returns true if the attribute is valid. + MODEL_EXPORT virtual bool validate(const std::shared_ptr& theAttribute, + std::string& theValidator, std::string& theError) const; + /// register that this attribute in feature is not obligatory for the feature execution /// so, it is not needed for the standard validation mechanism virtual void registerNotObligatory(std::string theFeature, std::string theAttribute); @@ -102,8 +103,7 @@ class Model_ValidatorsFactory : public ModelAPI_ValidatorsFactory protected: /// Adds the defualt validators that are usefull for all features. - void addDefaultValidators(std::list& theValidators, - std::list >& theArguments) const; + void addDefaultValidators(Validators& theValidators) const; /// Get instance from Session Model_ValidatorsFactory(); diff --git a/src/ModelAPI/ModelAPI_Validator.h b/src/ModelAPI/ModelAPI_Validator.h index 926ae886c..af9eb2839 100644 --- a/src/ModelAPI/ModelAPI_Validator.h +++ b/src/ModelAPI/ModelAPI_Validator.h @@ -67,14 +67,14 @@ class MODELAPI_EXPORT ModelAPI_ValidatorsFactory const std::string& theAttrID, const std::list& theArguments) = 0; + /// Validators is a list of pairs + typedef std::list > > Validators; /// Provides a validator for the feature, returns NULL if no validator virtual void validators(const std::string& theFeatureID, - std::list& theResult, - std::list >& theArguments) const = 0; + Validators& theResult) const = 0; /// Provides a validator for the attribute, returns NULL if no validator virtual void validators(const std::string& theFeatureID, const std::string& theAttrID, - std::list& theValidators, - std::list >& theArguments) const = 0; + Validators& theResult) const = 0; /// Returns registered validator by its Id virtual const ModelAPI_Validator* validator(const std::string& theID) const = 0; @@ -82,6 +82,10 @@ class MODELAPI_EXPORT ModelAPI_ValidatorsFactory /// Returns true if feature and all its attributes are valid. virtual bool validate(const std::shared_ptr& theFeature) const = 0; + /// Returns true if the attribute is valid. + virtual bool validate(const std::shared_ptr& theAttribute, + std::string& theValidator, std::string& theError) const = 0; + /// register that this attribute in feature is not obligatory for the feature execution /// so, it is not needed for the standard validation mechanism virtual void registerNotObligatory(std::string theFeature, std::string theAttribute) = 0; diff --git a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp index a18f05939..5abfea54d 100644 --- a/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFileSelector.cpp @@ -162,12 +162,11 @@ QStringList ModuleBase_WidgetFileSelector::getValidatorFormats() const SessionPtr aMgr = ModelAPI_Session::get(); ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); - std::list allValidators; - std::list > allArguments; - aFactory->validators(myFeature->getKind(), myAttributeID, allValidators, allArguments); + ModelAPI_ValidatorsFactory::Validators allValidators; + aFactory->validators(myFeature->getKind(), myAttributeID, allValidators); QStringList aResult; - std::list anArgumentList = allArguments.front(); + std::list anArgumentList = allValidators.front().second; std::list::const_iterator it = anArgumentList.begin(); for (; it != anArgumentList.end(); ++it) { QString aFormat = QString::fromStdString(*it); diff --git a/src/ModuleBase/ModuleBase_WidgetValidated.cpp b/src/ModuleBase/ModuleBase_WidgetValidated.cpp index aa774ca55..20b33095f 100644 --- a/src/ModuleBase/ModuleBase_WidgetValidated.cpp +++ b/src/ModuleBase/ModuleBase_WidgetValidated.cpp @@ -171,24 +171,9 @@ bool ModuleBase_WidgetValidated::isValidAttribute() const { SessionPtr aMgr = ModelAPI_Session::get(); ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); - std::list aValidators; - std::list > anArguments; - aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments); - - DataPtr aData = myFeature->data(); AttributePtr anAttribute = myFeature->attribute(attributeID()); - - std::list::iterator aValidator = aValidators.begin(); - std::list >::iterator aArgs = anArguments.begin(); - bool aValid = true; - for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) { - const ModelAPI_AttributeValidator* aAttrValidator = - dynamic_cast(*aValidator); - if (aAttrValidator) { - aValid = aAttrValidator->isValid(anAttribute, *aArgs); - } - } - return aValid; + std::string aValidatorID, anError; + return aFactory->validate(anAttribute, aValidatorID, anError); } bool ModuleBase_WidgetValidated::isFilterActivated() const diff --git a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp index 3e626d373..bb0bc5ba3 100644 --- a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp +++ b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp @@ -263,27 +263,8 @@ void ParametersPlugin_EvalListener::renameInAttribute( bool isValidAttribute(const AttributePtr& theAttribute) { - std::list aValidators; - std::list > anArguments; - - FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner()); - if (!aFeature.get()) - return false; - - ModelAPI_Session::get()->validators()->validators(aFeature->getKind(), - theAttribute->id(), - aValidators, anArguments); - std::list::const_iterator aValidatorIt = aValidators.begin(); - std::list >::const_iterator anArgumentIt = anArguments.begin(); - for (; aValidatorIt != aValidators.end() || anArgumentIt != anArguments.end(); ++aValidatorIt, ++anArgumentIt) { - const ModelAPI_AttributeValidator * anAttributeValidator = - dynamic_cast(*aValidatorIt); - if (!anAttributeValidator) - continue; - if (!anAttributeValidator->isValid(theAttribute, *anArgumentIt)) - return false; - } - return true; + std::string aValidator, anError; + return ModelAPI_Session::get()->validators()->validate(theAttribute, aValidator, anError); } void setParameterName(ResultParameterPtr theResultParameter, const std::string& theName) diff --git a/src/XGUI/XGUI_ActionsMgr.cpp b/src/XGUI/XGUI_ActionsMgr.cpp index 525d58892..8d2eada7c 100644 --- a/src/XGUI/XGUI_ActionsMgr.cpp +++ b/src/XGUI/XGUI_ActionsMgr.cpp @@ -156,20 +156,15 @@ void XGUI_ActionsMgr::updateOnViewSelection() ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); foreach(QString aFeatureId, aIdList) { foreach(QString aId, nestedCommands(aFeatureId)) { - std::list aValidators; - std::list > anArguments; - aFactory->validators(aId.toStdString(), aValidators, anArguments); - std::list::iterator aValidator = aValidators.begin(); - std::list >::iterator aValidatorArgs = anArguments.begin(); - for (; aValidator != aValidators.end(); aValidator++, aValidatorArgs++) { - if (!(*aValidator)) - continue; + ModelAPI_ValidatorsFactory::Validators aValidators; + aFactory->validators(aId.toStdString(), aValidators); + ModelAPI_ValidatorsFactory::Validators::iterator aValidatorIt = aValidators.begin(); + for (; aValidatorIt != aValidators.end(); ++aValidatorIt) { const ModuleBase_SelectionValidator* aSelValidator = - dynamic_cast(*aValidator); + dynamic_cast(aFactory->validator(aValidatorIt->first)); if (!aSelValidator) continue; - setActionEnabled(aId, aSelValidator->isValid(aSelection, *aValidatorArgs)); - + setActionEnabled(aId, aSelValidator->isValid(aSelection, aValidatorIt->second)); } } }