X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModel%2FModel_Validator.cpp;h=9875f74274a9af41843c9140761b3273f224734d;hb=f7a976b98d8cadadcb54a61e42ddb66e00759689;hp=a1741e3b7a81333723483afbb3d5f707f44fdefc;hpb=cc917b5dfaca1fcba1a25c1e54d6bd1ef0b4c057;p=modules%2Fshaper.git diff --git a/src/Model/Model_Validator.cpp b/src/Model/Model_Validator.cpp index a1741e3b7..9875f7427 100644 --- a/src/Model/Model_Validator.cpp +++ b/src/Model/Model_Validator.cpp @@ -1,3 +1,5 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + // File: Model_Validator.cpp // Created: 2 Jul 2014 // Author: Mikhail PONIKAROV @@ -5,10 +7,14 @@ #include #include #include +#include +#include +#include +#include #include 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 +24,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 +38,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 +54,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 +74,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()) { @@ -85,23 +91,23 @@ void Model_ValidatorsFactory::validators(const std::string& theFeatureID, } } } - addDefaultValidators(theResult); + addDefaultValidators(theResult, theArguments); } 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,9 +120,10 @@ void Model_ValidatorsFactory::validators(const std::string& theFeatureID, } Model_ValidatorsFactory::Model_ValidatorsFactory() - : ModelAPI_ValidatorsFactory() + : ModelAPI_ValidatorsFactory() { - registerValidator("Model_FeatureValidator", new Model_FeatureValidator); + const static std::string kDefaultId = "Model_FeatureValidator"; + registerValidator(kDefaultId, new Model_FeatureValidator); } const ModelAPI_Validator* Model_ValidatorsFactory::validator(const std::string& theID) const @@ -128,11 +135,160 @@ const ModelAPI_Validator* Model_ValidatorsFactory::validator(const std::string& return NULL; } -void Model_ValidatorsFactory::addDefaultValidators(std::list& theValidators) const +void Model_ValidatorsFactory::addDefaultValidators(std::list& theValidators, + std::list >& theArguments) const { - std::string anId = "Model_FeatureValidator"; - std::map::const_iterator it = myIDs.find(anId); + const static std::string kDefaultId = "Model_FeatureValidator"; + std::map::const_iterator it = myIDs.find(kDefaultId); if(it == myIDs.end()) return; theValidators.push_back(it->second); + theArguments.push_back(std::list()); +} + +bool Model_ValidatorsFactory::validate(const std::shared_ptr& theFeature) const +{ + const static std::string kDefaultId = "Model_FeatureValidator"; + // 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(kDefaultId); + 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 + std::shared_ptr aData = theFeature->data(); + // Validity of data is checked by "Model_FeatureValidator" (kDefaultId) + // 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++) { + 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) { + AttributePtr anAttribute = theFeature->data()->attribute(*anAttrIter); + if (!anAttrValidator->isValid(anAttribute, aValIter->second)) { + return false; + } + } + } + } + } + } + } + return true; +} + +void Model_ValidatorsFactory::registerNotObligatory(std::string theFeature, std::string theAttribute) +{ + const static std::string kDefaultId = "Model_FeatureValidator"; + std::map::const_iterator it = myIDs.find(kDefaultId); + if (it != myIDs.end()) { + Model_FeatureValidator* aValidator = dynamic_cast(it->second); + if (aValidator) { + aValidator->registerNotObligatory(theFeature, theAttribute); + } + } +} + +bool Model_ValidatorsFactory::isNotObligatory(std::string theFeature, std::string theAttribute) +{ + const static std::string kDefaultId = "Model_FeatureValidator"; + std::map::const_iterator it = myIDs.find(kDefaultId); + if (it != myIDs.end()) { + Model_FeatureValidator* aValidator = dynamic_cast(it->second); + if (aValidator) { + return aValidator->isNotObligatory(theFeature, theAttribute); + } + } + return false; // default +} + +void Model_ValidatorsFactory::registerConcealment(std::string theFeature, std::string theAttribute) +{ + std::map >::iterator aFind = myConcealed.find(theFeature); + if (aFind == myConcealed.end()) { + std::set aNewSet; + aNewSet.insert(theAttribute); + myConcealed[theFeature] = aNewSet; + } else { + aFind->second.insert(theAttribute); + } +} + +bool Model_ValidatorsFactory::isConcealed(std::string theFeature, std::string theAttribute) +{ + std::map >::iterator aFind = myConcealed.find(theFeature); + return aFind != myConcealed.end() && aFind->second.find(theAttribute) != aFind->second.end(); +} + +void Model_ValidatorsFactory::registerCase(std::string theFeature, std::string theAttribute, + std::string theSwitchId, std::string theCaseId) +{ + std::map > >::iterator + aFindFeature = myCases.find(theFeature); + if (aFindFeature == myCases.end()) { + myCases[theFeature] = std::map >(); + aFindFeature = myCases.find(theFeature); + } + (aFindFeature->second)[theAttribute] = std::pair(theSwitchId, theCaseId); +} + +bool Model_ValidatorsFactory::isCase( + FeaturePtr theFeature, std::string theAttribute) +{ + std::map > >::iterator + aFindFeature = myCases.find(theFeature->getKind()); + if (aFindFeature != myCases.end()) { + std::map >::iterator + aFindAttr = aFindFeature->second.find(theAttribute); + if (aFindAttr != aFindFeature->second.end()) { + // the the switch-attribute that contains the case value + AttributeStringPtr aSwitch = theFeature->string(aFindAttr->second.first); + if (aSwitch.get()) { + return aSwitch->value() == aFindAttr->second.second; // the second is the case identifier + } + } + } + return true; // if no additional conditions, this attribute is the case to be validated }