From: mpv Date: Thu, 4 Sep 2014 09:45:38 +0000 (+0400) Subject: Make general validation mechanism also for attributes the validate method for feature... X-Git-Tag: V_0.4.4~85 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=cb7a02e2d7e5f4c675571888b8119a8c58069a0d;p=modules%2Fshaper.git Make general validation mechanism also for attributes the validate method for feature in validators factory --- diff --git a/src/ExchangePlugin/ExchangePlugin_Validators.cpp b/src/ExchangePlugin/ExchangePlugin_Validators.cpp index 99e14bea9..2560fb621 100644 --- a/src/ExchangePlugin/ExchangePlugin_Validators.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Validators.cpp @@ -11,9 +11,8 @@ #include -bool ExchangePlugin_ImportFormatValidator::isValid(const FeaturePtr& theFeature, - const std::list& theArguments, - const ObjectPtr& theObject) const +bool ExchangePlugin_ImportFormatValidator::isValid( + const AttributePtr& theAttribute, const std::list& theArguments) const { PluginManagerPtr aMgr = ModelAPI_PluginManager::get(); ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); diff --git a/src/ExchangePlugin/ExchangePlugin_Validators.h b/src/ExchangePlugin/ExchangePlugin_Validators.h index c4400fc9f..21d72765b 100644 --- a/src/ExchangePlugin/ExchangePlugin_Validators.h +++ b/src/ExchangePlugin/ExchangePlugin_Validators.h @@ -11,9 +11,8 @@ class ExchangePlugin_ImportFormatValidator : public ModelAPI_AttributeValidator { public: - virtual bool isValid(const FeaturePtr& theFeature, - const std::list& theArguments, - const ObjectPtr& theObject) const; + virtual bool isValid( + const AttributePtr& theAttribute, const std::list& theArguments) const; }; diff --git a/src/Model/Model_Data.cpp b/src/Model/Model_Data.cpp index d97975153..7813c461f 100644 --- a/src/Model/Model_Data.cpp +++ b/src/Model/Model_Data.cpp @@ -251,7 +251,8 @@ bool Model_Data::isValid() std::list > Model_Data::attributes(const std::string& theType) { std::list > aResult; - std::map >::iterator anAttrsIter = myAttrs.begin(); + std::map >::iterator anAttrsIter = + myAttrs.begin(); for (; anAttrsIter != myAttrs.end(); anAttrsIter++) { if (theType.empty() || anAttrsIter->second->attributeType() == theType) { aResult.push_back(anAttrsIter->second); @@ -260,6 +261,19 @@ std::list > Model_Data::attributes(const s return aResult; } +std::list Model_Data::attributesIDs(const std::string& theType) +{ + std::list aResult; + std::map >::iterator anAttrsIter = + myAttrs.begin(); + for (; anAttrsIter != myAttrs.end(); anAttrsIter++) { + if (theType.empty() || anAttrsIter->second->attributeType() == theType) { + aResult.push_back(anAttrsIter->first); + } + } + return aResult; +} + void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr) { theAttr->setInitialized(); diff --git a/src/Model/Model_Data.h b/src/Model/Model_Data.h index 31ff67ff2..f8ad2f0a7 100644 --- a/src/Model/Model_Data.h +++ b/src/Model/Model_Data.h @@ -87,10 +87,13 @@ class Model_Data : public ModelAPI_Data /// Returns the generic attribute by identifier /// \param theID identifier of the attribute MODEL_EXPORT virtual boost::shared_ptr attribute(const std::string& theID); - /// Returns all attributes ofthe feature of the given type + /// Returns all attributes of the feature of the given type /// or all attributes if "theType" is empty MODEL_EXPORT virtual std::list > attributes(const std::string& theType); + /// Returns all attributes ids of the feature of the given type + /// or all attributes if "theType" is empty + MODEL_EXPORT virtual std::list attributesIDs(const std::string& theType); /// Identifier by the id (not fast, iteration by map) /// \param theAttr attribute already created in this data diff --git a/src/Model/Model_FeatureValidator.cpp b/src/Model/Model_FeatureValidator.cpp index b4b4fb8cc..2f9894dce 100644 --- a/src/Model/Model_FeatureValidator.cpp +++ b/src/Model/Model_FeatureValidator.cpp @@ -11,7 +11,8 @@ #include #include -bool Model_FeatureValidator::isValid(const boost::shared_ptr& theFeature) const +bool Model_FeatureValidator::isValid(const boost::shared_ptr& theFeature, + const std::list& theArguments) const { boost::shared_ptr aData = theFeature->data(); if (!aData->isValid()) diff --git a/src/Model/Model_FeatureValidator.h b/src/Model/Model_FeatureValidator.h index 042621b28..901f48ef7 100644 --- a/src/Model/Model_FeatureValidator.h +++ b/src/Model/Model_FeatureValidator.h @@ -16,7 +16,8 @@ class Model_FeatureValidator : public ModelAPI_FeatureValidator public: /// Returns true if feature and/or attributes are valid /// \param theFeature the validated feature - MODEL_EXPORT virtual bool isValid(const boost::shared_ptr& theFeature) const; + MODEL_EXPORT virtual bool isValid(const boost::shared_ptr& theFeature, + const std::list& theArguments) const; }; #endif diff --git a/src/Model/Model_Validator.cpp b/src/Model/Model_Validator.cpp index a1741e3b7..408b20cf5 100644 --- a/src/Model/Model_Validator.cpp +++ b/src/Model/Model_Validator.cpp @@ -5,10 +5,15 @@ #include #include #include +#include +#include +#include #include +const static std::string DefaultId = "Model_FeatureValidator"; + void Model_ValidatorsFactory::registerValidator(const std::string& theID, - ModelAPI_Validator* theValidator) + ModelAPI_Validator* theValidator) { if (myIDs.find(theID) != myIDs.end()) { Events_Error::send(std::string("Validator ") + theID + " is already registered"); @@ -18,7 +23,7 @@ void Model_ValidatorsFactory::registerValidator(const std::string& theID, } void Model_ValidatorsFactory::assignValidator(const std::string& theID, - const std::string& theFeatureID) + const std::string& theFeatureID) { if (myFeatures.find(theFeatureID) == myFeatures.end()) { myFeatures[theFeatureID] = AttrValidators(); @@ -32,8 +37,8 @@ void Model_ValidatorsFactory::assignValidator(const std::string& theID, } void Model_ValidatorsFactory::assignValidator(const std::string& theID, - const std::string& theFeatureID, - const std::list& theArguments) + const std::string& theFeatureID, + const std::list& theArguments) { if (myFeatures.find(theFeatureID) == myFeatures.end()) { myFeatures[theFeatureID] = AttrValidators(); @@ -48,13 +53,13 @@ void Model_ValidatorsFactory::assignValidator(const std::string& theID, } void Model_ValidatorsFactory::assignValidator(const std::string& theID, - const std::string& theFeatureID, - const std::string& theAttrID, - const std::list& theArguments) + const std::string& theFeatureID, + const std::string& theAttrID, + const std::list& theArguments) { // create feature-structures if not exist std::map >::iterator aFeature = myAttrs.find( - theFeatureID); + theFeatureID); if (aFeature == myAttrs.end()) { myAttrs[theFeatureID] = std::map(); aFeature = myAttrs.find(theFeatureID); @@ -68,8 +73,8 @@ void Model_ValidatorsFactory::assignValidator(const std::string& theID, } void Model_ValidatorsFactory::validators(const std::string& theFeatureID, - std::list& theResult, - std::list >& theArguments) const + std::list& theResult, + std::list >& theArguments) const { std::map::const_iterator aFeature = myFeatures.find(theFeatureID); if (aFeature != myFeatures.cend()) { @@ -89,19 +94,19 @@ void Model_ValidatorsFactory::validators(const std::string& theFeatureID, } void Model_ValidatorsFactory::validators(const std::string& theFeatureID, - const std::string& theAttrID, - std::list& theValidators, - std::list >& theArguments) const + const std::string& theAttrID, + std::list& theValidators, + std::list >& theArguments) const { - std::map >::const_iterator aFeature = myAttrs - .find(theFeatureID); + 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); + aValIter->first); if (aFound == myIDs.end()) { Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered"); } else { @@ -114,7 +119,7 @@ void Model_ValidatorsFactory::validators(const std::string& theFeatureID, } Model_ValidatorsFactory::Model_ValidatorsFactory() - : ModelAPI_ValidatorsFactory() + : ModelAPI_ValidatorsFactory() { registerValidator("Model_FeatureValidator", new Model_FeatureValidator); } @@ -130,9 +135,78 @@ const ModelAPI_Validator* Model_ValidatorsFactory::validator(const std::string& void Model_ValidatorsFactory::addDefaultValidators(std::list& theValidators) const { - std::string anId = "Model_FeatureValidator"; - std::map::const_iterator it = myIDs.find(anId); + std::map::const_iterator it = myIDs.find(DefaultId); if(it == myIDs.end()) return; theValidators.push_back(it->second); } + +bool Model_ValidatorsFactory::validate(const boost::shared_ptr& theFeature) const +{ + // 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++) { + std::map::const_iterator aValFind = + myIDs.find(aValidator->first); + if (aValFind == myIDs.end()) { + Events_Error::send(std::string("Validator ") + aValidator->first + " was not registered"); + continue; + } + const ModelAPI_FeatureValidator* aFValidator = + dynamic_cast(aValFind->second); + if (aFValidator) { + if (!aFValidator->isValid(theFeature, aValidator->second)) + return false; + } + } + } + // check default validator + std::map::const_iterator aDefaultVal = myIDs.find(DefaultId); + if(aDefaultVal != myIDs.end()) { + static const std::list anEmptyArgList; + const ModelAPI_FeatureValidator* aFValidator = + dynamic_cast(aDefaultVal->second); + if (aFValidator) { + if (!aFValidator->isValid(theFeature, anEmptyArgList)) + return false; + } + } + // check all attributes for validity + boost::shared_ptr aData = theFeature->data(); + if (!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++) { + std::map::const_iterator anAttr = + aFeatureIter->second.find(*anAttrIter); + if (anAttr != aFeatureIter->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"); + } else { + const ModelAPI_AttributeValidator* anAttrValidator = + dynamic_cast(aFound->second); + if (anAttrValidator) { + if (!anAttrValidator->isValid(theFeature->data()->attribute(*anAttrIter), + aValIter->second)) { + return false; + } + } + } + } + } + } + } + return true; +} diff --git a/src/Model/Model_Validator.h b/src/Model/Model_Validator.h index 645c4bdc6..f6f42b3cf 100644 --- a/src/Model/Model_Validator.h +++ b/src/Model/Model_Validator.h @@ -63,12 +63,10 @@ class Model_ValidatorsFactory : public ModelAPI_ValidatorsFactory std::list >& theArguments) const; /// Returns registered validator by its Id - virtual const ModelAPI_Validator* validator(const std::string& theID) const; + MODEL_EXPORT virtual const ModelAPI_Validator* validator(const std::string& theID) const; - /// Returns the result of "validate" method for attribute of validator. - /// If validator is not exists, returns true: everything is valid by default. - //MODEL_EXPORT virtual bool validate( - // const boost::shared_ptr& theFeature, const std::string& theAttrID) const; + /// Returns true if feature and all its attributes are valid. + MODEL_EXPORT virtual bool validate(const boost::shared_ptr& theFeature) const; protected: void addDefaultValidators(std::list& theValidators) const; diff --git a/src/ModelAPI/CMakeLists.txt b/src/ModelAPI/CMakeLists.txt index d7493bb18..ccac1a79e 100644 --- a/src/ModelAPI/CMakeLists.txt +++ b/src/ModelAPI/CMakeLists.txt @@ -28,15 +28,16 @@ SET(PROJECT_HEADERS ModelAPI_ResultConstruction.h ModelAPI_ResultPart.h ModelAPI_ResultParameters.h - ModelAPI_ResultValidator.h - ModelAPI_AttributeValidator.h - ModelAPI_Tools.h + ModelAPI_ResultValidator.h + ModelAPI_AttributeValidator.h + ModelAPI_Tools.h + ModelAPI_RefAttrValidator.h ) SET(PROJECT_SOURCES ModelAPI_Feature.cpp ModelAPI_PluginManager.cpp - ModelAPI_Tools.cpp + ModelAPI_Tools.cpp ) SET(PROJECT_LIBRARIES diff --git a/src/ModelAPI/ModelAPI_Attribute.h b/src/ModelAPI/ModelAPI_Attribute.h index 8e2051ac2..a73044d4a 100644 --- a/src/ModelAPI/ModelAPI_Attribute.h +++ b/src/ModelAPI/ModelAPI_Attribute.h @@ -40,7 +40,7 @@ class ModelAPI_Attribute } /// Returns the owner of this attribute - MODELAPI_EXPORT const boost::shared_ptr& owner() + MODELAPI_EXPORT const boost::shared_ptr& owner() const { return myObject; } diff --git a/src/ModelAPI/ModelAPI_AttributeValidator.h b/src/ModelAPI/ModelAPI_AttributeValidator.h index 15b8514ec..63a1b9e6f 100644 --- a/src/ModelAPI/ModelAPI_AttributeValidator.h +++ b/src/ModelAPI/ModelAPI_AttributeValidator.h @@ -1,19 +1,24 @@ // File: ModelAPI_AttributeValidator.h -// Created: 5 Aug 2014 -// Author: Vitaly SMETANNIKOV +// Created: 4 Sep 2014 +// Author: Mikhail PONIKAROV #ifndef ModelAPI_AttributeValidator_H #define ModelAPI_AttributeValidator_H -#include -#include +#include #include +/** + * Generic validator for any attribute of a feature. + */ class ModelAPI_AttributeValidator : public ModelAPI_Validator { - public: - virtual bool isValid(const FeaturePtr& theFeature, const std::list& theArguments, - const ObjectPtr& theObject) const = 0; +public: + //! returns true if attribute is valid + //! \param theAttribute the checked attribute + //! \param theArguments arguments of the attribute + virtual bool isValid( + const AttributePtr& theAttribute, const std::list& theArguments) const = 0; }; #endif diff --git a/src/ModelAPI/ModelAPI_Data.h b/src/ModelAPI/ModelAPI_Data.h index ba9691136..c4fe4712f 100644 --- a/src/ModelAPI/ModelAPI_Data.h +++ b/src/ModelAPI/ModelAPI_Data.h @@ -58,10 +58,13 @@ class MODELAPI_EXPORT ModelAPI_Data /// Returns the generic attribute by identifier /// \param theID identifier of the attribute virtual boost::shared_ptr attribute(const std::string& theID) = 0; - /// Returns all attributes ofthe feature of the given type + /// Returns all attributes of the feature of the given type /// or all attributes if "theType" is empty virtual std::list > attributes(const std::string& theType) = 0; + /// Returns all attributes ids of the feature of the given type + /// or all attributes if "theType" is empty + virtual std::list attributesIDs(const std::string& theType) = 0; /// Identifier by the id (not fast, iteration by map) /// \param theAttr attribute already created in this data virtual const std::string& id(const boost::shared_ptr& theAttr) = 0; diff --git a/src/ModelAPI/ModelAPI_FeatureValidator.h b/src/ModelAPI/ModelAPI_FeatureValidator.h index 7508c7ad3..3dafdf813 100644 --- a/src/ModelAPI/ModelAPI_FeatureValidator.h +++ b/src/ModelAPI/ModelAPI_FeatureValidator.h @@ -15,7 +15,8 @@ class ModelAPI_FeatureValidator : public ModelAPI_Validator /// \param theFeature the validated feature /// \param theAttr the validated attribute ID, empty string of feature is validated /// \param theArguments list of string, feature attribute names: dependent attributes - virtual bool isValid(const boost::shared_ptr& theFeature) const = 0; + virtual bool isValid(const boost::shared_ptr& theFeature, + const std::list& theArguments) const = 0; }; #endif diff --git a/src/ModelAPI/ModelAPI_RefAttrValidator.h b/src/ModelAPI/ModelAPI_RefAttrValidator.h new file mode 100644 index 000000000..e5b1b6690 --- /dev/null +++ b/src/ModelAPI/ModelAPI_RefAttrValidator.h @@ -0,0 +1,23 @@ +// File: ModelAPI_RefAttrValidator.h +// Created: 5 Aug 2014 +// Author: Vitaly SMETANNIKOV + +#ifndef ModelAPI_RefAttrValidator_H +#define ModelAPI_RefAttrValidator_H + +#include +#include +#include + +/* + * Used for filtering out the object that can be used for reference attribute value + */ +class ModelAPI_RefAttrValidator : public ModelAPI_AttributeValidator +{ +public: + //! Returns true if object is good for the feature attribute + virtual bool isValid(const FeaturePtr& theFeature, const std::list& theArguments, + const ObjectPtr& theObject) const = 0; +}; + +#endif diff --git a/src/ModelAPI/ModelAPI_Validator.h b/src/ModelAPI/ModelAPI_Validator.h index 78754d074..a3e378856 100644 --- a/src/ModelAPI/ModelAPI_Validator.h +++ b/src/ModelAPI/ModelAPI_Validator.h @@ -83,10 +83,8 @@ class MODELAPI_EXPORT ModelAPI_ValidatorsFactory /// Returns registered validator by its Id virtual const ModelAPI_Validator* validator(const std::string& theID) const = 0; - /// Returns the result of "validate" method for attribute of validator. - /// If validator is not exists, returns true: everything is valid by default. - //virtual bool validate( - // const boost::shared_ptr& theFeature, const std::string& theAttrID) const = 0; + /// Returns true if feature and all its attributes are valid. + virtual bool validate(const boost::shared_ptr& theFeature) const = 0; protected: /// Get instance from PluginManager diff --git a/src/ModuleBase/ModuleBase_WidgetFeature.cpp b/src/ModuleBase/ModuleBase_WidgetFeature.cpp index 08cb49f00..e7c510a83 100644 --- a/src/ModuleBase/ModuleBase_WidgetFeature.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFeature.cpp @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include #include @@ -98,8 +98,8 @@ bool ModuleBase_WidgetFeature::setObject(const ObjectPtr& theObject, bool theSen aValidator = aValidators.begin(); std::list >::iterator aArgs = anArguments.begin(); for (; aValidator != aValidators.end(); aValidator++, aArgs++) { - const ModelAPI_AttributeValidator* aAttrValidator = - dynamic_cast(*aValidator); + const ModelAPI_RefAttrValidator* aAttrValidator = + dynamic_cast(*aValidator); if (aAttrValidator) { if (!aAttrValidator->isValid(myFeature, *aArgs, theObject)) { return false; diff --git a/src/SketchPlugin/SketchPlugin_ResultValidators.cpp b/src/SketchPlugin/SketchPlugin_ResultValidators.cpp index e40022f7c..0e39fcb03 100644 --- a/src/SketchPlugin/SketchPlugin_ResultValidators.cpp +++ b/src/SketchPlugin/SketchPlugin_ResultValidators.cpp @@ -39,4 +39,3 @@ bool SketchPlugin_ResultArcValidator::isValid(const ObjectPtr theObject) const boost::shared_ptr aShape = ModelAPI_Tools::shape(aResult); return aShape && aShape->isEdge() && GeomAPI_Curve(aShape).isCircle(); } - diff --git a/src/SketchPlugin/SketchPlugin_Validators.cpp b/src/SketchPlugin/SketchPlugin_Validators.cpp index 50af2be89..4115d11a4 100644 --- a/src/SketchPlugin/SketchPlugin_Validators.cpp +++ b/src/SketchPlugin/SketchPlugin_Validators.cpp @@ -30,3 +30,15 @@ bool SketchPlugin_DistanceAttrValidator::isValid(const FeaturePtr& theFeature, return false; } +bool SketchPlugin_DistanceAttrValidator::isValid( + const AttributePtr& theAttribute, const std::list& theArguments ) const +{ + boost::shared_ptr anAttr = + boost::dynamic_pointer_cast(theAttribute); + if (anAttr) { + const ObjectPtr& anObj = theAttribute->owner(); + const FeaturePtr aFeature = boost::dynamic_pointer_cast(anObj); + return isValid(aFeature, theArguments, anAttr->object()); + } + return true; // it may be not reference attribute, in this case, it is OK +} diff --git a/src/SketchPlugin/SketchPlugin_Validators.h b/src/SketchPlugin/SketchPlugin_Validators.h index 38ba02916..4da0f73e9 100644 --- a/src/SketchPlugin/SketchPlugin_Validators.h +++ b/src/SketchPlugin/SketchPlugin_Validators.h @@ -6,12 +6,17 @@ #define SketchPlugin_Validators_H #include "SketchPlugin.h" -//#include -#include +#include class SketchPlugin_DistanceAttrValidator : public ModelAPI_AttributeValidator { public: + //! returns true if attribute is valid + //! \param theAttribute the checked attribute + //! \param theArguments arguments of the attribute + virtual bool isValid( + const AttributePtr& theAttribute, const std::list& theArguments) const; + //! Returns true if object is good for the feature attribute virtual bool isValid(const FeaturePtr& theFeature, const std::list& theArguments, const ObjectPtr& theObject) const; diff --git a/src/XGUI/XGUI_OperationMgr.cpp b/src/XGUI/XGUI_OperationMgr.cpp index c5972b9b9..88e625e36 100644 --- a/src/XGUI/XGUI_OperationMgr.cpp +++ b/src/XGUI/XGUI_OperationMgr.cpp @@ -84,22 +84,8 @@ void XGUI_OperationMgr::validateOperation(ModuleBase_Operation* theOperation) //Get validators for the Id PluginManagerPtr aMgr = ModelAPI_PluginManager::get(); ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); - std::list aValidators; - std::list > anArguments; - aFactory->validators(anOperationId.toStdString(), aValidators, anArguments); - // - std::list::iterator it = aValidators.begin(); - bool isValid = true; - for (; it != aValidators.end(); it++) { - const ModelAPI_FeatureValidator* aFeatureValidator = - dynamic_cast(*it); - if (!aFeatureValidator) - continue; - if (!aFeatureValidator->isValid(aFeature)) { - isValid = false; - break; - } - } + + bool isValid = aFactory->validate(aFeature); emit operationValidated(isValid); }