Salome HOME
Add dumping named of copied entities in the sketch (issue #1924)
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_Validators.cpp
index 788e163980f6308e9a1740ee45418c016db22b9c..c5804aeaed66fa06eed53648552635101f3e7b3a 100644 (file)
@@ -1,3 +1,4 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
 /*
  * Parameters_VariableValidator.cpp
  *
@@ -7,9 +8,15 @@
 
 #include <ParametersPlugin_Validators.h>
 
+#include <ParametersPlugin_Parameter.h>
+
+#include <Events_InfoMessage.h>
+
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_ResultParameter.h>
+#include <ModelAPI_Tools.h>
+#include <ModelAPI_Expression.h>
 
 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
 {
@@ -20,23 +27,48 @@ ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
 }
 
 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
-                                                 const std::list<std::string>& theArguments) const
+                                                 const std::list<std::string>& theArguments,
+                                                 Events_InfoMessage& theError) const
 {
   AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
-  bool result = isVariable(aStrAttr->value());
-  return result;
+  if (!aStrAttr->isInitialized()) {
+    theError = "Attribute \"%1\" is not initialized.";
+    theError.arg(aStrAttr->id());
+    return false;
+  }
+  bool isEmptyExpr = aStrAttr->value().empty();
+  if (isEmptyExpr) {
+    theError = "Attribute \"%1\" value is empty.";
+    theError.arg(aStrAttr->id());
+    return false;
+  }
+  if (!ModelAPI_Expression::isVariable(aStrAttr->value())) {
+    theError = "Incorrect variable name.";
+    return false;
+  }
+  if (!isUnique(theAttribute, aStrAttr->value())) {
+    theError = "Variable name is not unique.";
+    return false;
+  }
+  return true;
 }
 
-bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
+bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
+                                                  const std::string& theString) const
 {
-  std::string::const_iterator it = theString.begin();
-  if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
+  DocumentPtr aDocument = theAttribute->owner()->document();
+  for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
+       anIndex < aSize; ++anIndex) {
+    ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
+    if (aParamObj->data()->name() != theString)
+      continue;
+    ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
+    if (!aParam.get())
+      continue;
+    FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
+    if (aFeature == theAttribute->owner())
+      continue;
     return false;
-  it++;
-  for ( ; it != theString.end(); ++it ) {
-    if(!(isalnum(*it) || (*it) == '_')) {
-      return false;
-    }
   }
   return true;
 }
@@ -52,20 +84,31 @@ ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
 }
 
 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
-                                                  const std::list<std::string>& theArguments) const
+                                                   const std::list<std::string>& theArguments,
+                                                   Events_InfoMessage& theError) const
 {
   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
   ResultParameterPtr aParam =
       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
 
   AttributeStringPtr aStrAttr =
-    std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
+      std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
+  if (!aStrAttr->isInitialized()) {
+    theError = "Attribute \"%1\" is not initialized.";
+    theError.arg(aStrAttr->id());
+    return false;
+  }
   bool isEmptyExpr = aStrAttr->value().empty();
-  if(isEmptyExpr)
+  if (isEmptyExpr) {
+    theError = "Expression is empty.";
     return false;
+  }
 
-  if(!aParam.get())
+  if (!aParam.get()) {
+    theError = "Result is empty.";
     return false;
+  }
 
-  return aFeature->error().empty();
+  theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
+  return theError.empty();
 }