Salome HOME
Issue #2052: Modification of parameters don't work (sketch, extrusion)
[modules/shaper.git] / src / Model / Model_Validator.cpp
index ed7ffac935fbe369404336b7f315d183b0077ea8..7314b48aaadd89a409aca0ab582003e22add29ce 100644 (file)
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_AttributeValidator.h>
 #include <ModelAPI_Feature.h>
+#include <ModelAPI_Result.h>
 #include <Model_Data.h>
 
+#include <Config_Translator.h>
+
 #include <Events_InfoMessage.h>
 
 void Model_ValidatorsFactory::registerValidator(const std::string& theID,
@@ -34,7 +37,7 @@ void Model_ValidatorsFactory::assignValidator(const std::string& theID,
     myFeatures[theFeatureID] = AttrValidators();
   }
   if (myFeatures[theFeatureID].find(theID) != myFeatures[theFeatureID].end()) {
-    //Events_Error::send(std::string("Validator ") + theID + 
+    //Events_Error::send(std::string("Validator ") + theID +
     //  " for feature " + theFeatureID + "is already registered");
   } else {
     myFeatures[theFeatureID][theID] = std::list<std::string>();
@@ -50,7 +53,7 @@ void Model_ValidatorsFactory::assignValidator(const std::string& theID,
   }
 
   if (myFeatures[theFeatureID].find(theID) != myFeatures[theFeatureID].end()) {
-    //Events_Error::send(std::string("Validator ") + theID + 
+    //Events_Error::send(std::string("Validator ") + theID +
     //  " for feature " + theFeatureID + "is already registered");
   } else {
     myFeatures[theFeatureID][theID] = theArguments;
@@ -80,7 +83,7 @@ void Model_ValidatorsFactory::assignValidator(const std::string& theID,
 void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
                                          Validators& theValidators) const
 {
-  std::map<std::string, AttrValidators>::const_iterator aFeatureIt = 
+  std::map<std::string, AttrValidators>::const_iterator aFeatureIt =
       myFeatures.find(theFeatureID);
   if (aFeatureIt != myFeatures.cend()) {
     AttrValidators::const_iterator aValidatorsIt = aFeatureIt->second.cbegin();
@@ -96,13 +99,15 @@ void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
   addDefaultValidators(theValidators);
 }
 
-void Model_ValidatorsFactory::validators(const std::string& theFeatureID, const std::string& theAttrID,
+void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
+                                         const std::string& theAttrID,
                                          Validators& theValidators) const
 {
-  std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeatureIt = 
+  std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeatureIt =
       myAttrs.find(theFeatureID);
   if (aFeatureIt != myAttrs.cend()) {
-    std::map<std::string, AttrValidators>::const_iterator anAttrIt = aFeatureIt->second.find(theAttrID);
+    std::map<std::string, AttrValidators>::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++) {
@@ -169,55 +174,30 @@ bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Feature>&
     for(; aValidatorIt != aValidators.cend(); aValidatorIt++) {
       const std::string& aValidatorID = aValidatorIt->first;
       const std::list<std::string>& 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 = 
+      const ModelAPI_FeatureValidator* aFValidator =
         dynamic_cast<const ModelAPI_FeatureValidator*>(validator(aValidatorID));
       if (aFValidator) {
         Events_InfoMessage anError;
         if (!aFValidator->isValid(theFeature, anArguments, anError)) {
           if (anError.empty())
             anError = "Unknown error.";
-          anError = aValidatorID + ": " + anError.messageString();
-          theFeature->setError(anError.messageString(), false);
+          if (anError.context().empty()) {
+            anError.setContext(theFeature->getKind() + ":" + aValidatorID);
+          }
+          theFeature->setError(Config_Translator::translate(anError), false, false);
           theFeature->data()->execState(ModelAPI_StateInvalidArgument);
           return false;
         }
       }
     }
   }
-  // The default validator was retrned by validators() and was checked in previous cycle
-  //// check default validator
-  //std::map<std::string, ModelAPI_Validator*>::const_iterator aDefaultVal = myIDs.find(kDefaultId);
-  //if(aDefaultVal != myIDs.end()) {
-  //  static const std::list<std::string> anEmptyArgList;
-  //  const ModelAPI_FeatureValidator* aFValidator = 
-  //    dynamic_cast<const ModelAPI_FeatureValidator*>(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
-  // Validity of data is checked by "Model_FeatureValidator" (kDefaultId)
-  // if (!aData || !aData->isValid())
-  //   return false;
   static const std::string kAllTypes = "";
   std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
   std::list<std::string>::const_iterator anAttrIt = aLtAttributes.cbegin();
   for (; anAttrIt != aLtAttributes.cend(); anAttrIt++) {
-    const std::string& anAttributeID = *anAttrIt; 
+    const std::string& anAttributeID = *anAttrIt;
     AttributePtr anAttribute = theFeature->data()->attribute(anAttributeID);
 
     std::string aValidatorID;
@@ -225,11 +205,13 @@ bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Feature>&
     if (!validate(anAttribute, aValidatorID, anError)) {
       if (anError.empty())
         anError = "Unknown error.";
-      anError = anAttributeID + " - " + aValidatorID + ": " + anError.messageString();
-      theFeature->setError(anError.messageString(), false);
+      if (anError.context().empty()) {
+        anError.setContext(theFeature->getKind() + ":" + anAttributeID + ":" + aValidatorID);
+      }
+      theFeature->setError(Config_Translator::translate(anError), false, false);
       theFeature->data()->execState(ModelAPI_StateInvalidArgument);
       return false;
-    } 
+    }
   }
 
   return true;
@@ -264,13 +246,14 @@ bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Attribute>
     if (!anAttrValidator->isValid(theAttribute, anArguments, theError)) {
       theValidator = aValidatorID;
       return false;
-    } 
+    }
   }
 
   return true;
 }
 
-void Model_ValidatorsFactory::registerNotObligatory(std::string theFeature, std::string theAttribute)
+void Model_ValidatorsFactory::registerNotObligatory(std::string theFeature,
+                                                    std::string theAttribute)
 {
   const static std::string kDefaultId = "Model_FeatureValidator";
   std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(kDefaultId);
@@ -331,7 +314,7 @@ void Model_ValidatorsFactory::registerCase(std::string theFeature, std::string t
     aFindAttrID = aFindFeature->second.find(theAttribute);
   }
   std::list<std::pair<std::string, std::string> >::const_iterator aCasesIt = theCases.begin(),
-                                                                        aCasesLast = theCases.end();
+                                                                       aCasesLast = theCases.end();
   std::map<std::string, std::set<std::string> > aFindCases = aFindAttrID->second;
   for (; aCasesIt != aCasesLast; aCasesIt++) {
     std::pair<std::string, std::string> aCasePair = *aCasesIt;