Salome HOME
Issue #903 - Parameter is not created, if expression contains not created parameter
authorspo <sergey.pokhodenko@opencascade.com>
Tue, 8 Sep 2015 11:03:01 +0000 (14:03 +0300)
committerspo <sergey.pokhodenko@opencascade.com>
Tue, 8 Sep 2015 11:43:38 +0000 (14:43 +0300)
src/Model/Model_Validator.cpp
src/ModuleBase/ModuleBase_WidgetExprEditor.cpp
src/ParametersPlugin/ParametersPlugin_EvalListener.cpp
src/ParametersPlugin/ParametersPlugin_Parameter.cpp
src/ParametersPlugin/ParametersPlugin_Parameter.h
src/ParametersPlugin/ParametersPlugin_Validators.cpp
test.squish/suite_ISSUES_SALOME/tst_903/test.py [new file with mode: 0644]

index 1fc4f71234db3b7e0b1a4d8188575f6c9f0503c6..6795e43e1b2b13cb7387d4ff8e3515534a25ac4b 100644 (file)
@@ -206,7 +206,7 @@ bool Model_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Feature>&
   //    }
   //  }
   //}
-  
+
   // check all attributes for validity
   // Validity of data is checked by "Model_FeatureValidator" (kDefaultId)
   // if (!aData || !aData->isValid())
index b8e8ca60e4872d29582e0b222d4bb4d61bbb5c51..3b3d25c0815a8b9c8c2fdf2086de0da80dcd6a34 100644 (file)
@@ -227,7 +227,7 @@ bool ModuleBase_WidgetExprEditor::storeValueCustom() const
 
   // Try to get the value
   QString aStateMsg;
-  std::string anErrorMessage = myFeature->error();
+  std::string anErrorMessage = myFeature->string("ExpressionError")->value();
   if (anErrorMessage.empty()) {
     ResultParameterPtr aParam =
       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
@@ -240,7 +240,7 @@ bool ModuleBase_WidgetExprEditor::storeValueCustom() const
       }
     }
   } else {
-    aStateMsg = QString::fromStdString(anErrorMessage);
+    aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
   }
   myResultLabel->setText(aStateMsg);
   return true;
index 03b4b11996a3d4d1d8830da95dfb2015a8126d97..029800afa4e2dcf7315b865d1cf863d20045f0b2 100644 (file)
@@ -73,6 +73,7 @@ double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression,
   for ( ; it != anExprParams.end(); it++) {
     double aValue;
     ResultParameterPtr aParamRes;
+    // If variable does not exist python interpreter will generate an error. It is OK.
     if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes, theDocument)) continue;
 
     std::ostringstream sstream;
index 54dc39581185a6892ddb35ce04310de7b19e044e..2b5ee41a5b1b3eda3afacb0c9ada50d1e898c6ef 100644 (file)
@@ -34,6 +34,11 @@ void ParametersPlugin_Parameter::initAttributes()
 {
   data()->addAttribute(VARIABLE_ID(), ModelAPI_AttributeString::typeId());
   data()->addAttribute(EXPRESSION_ID(), ModelAPI_AttributeString::typeId());
+
+  data()->addAttribute(EXPRESSION_ERROR_ID(), ModelAPI_AttributeString::typeId());
+  data()->string(EXPRESSION_ERROR_ID())->setIsArgument(false);
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXPRESSION_ERROR_ID());
+
   data()->addAttribute(ARGUMENTS_ID(), ModelAPI_AttributeRefList::typeId());
   data()->reflist(ARGUMENTS_ID())->setIsArgument(false);
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ARGUMENTS_ID());
@@ -63,27 +68,20 @@ void ParametersPlugin_Parameter::updateName()
 void ParametersPlugin_Parameter::updateExpression()
 {
   std::string anExpression = string(EXPRESSION_ID())->value();
-  if(anExpression.empty()) {
-    // clear error/result if the expression is empty
-    setError("", false);
-    return;
-  }
+
   std::string outErrorMessage;
   double aValue = evaluate(anExpression, outErrorMessage);
-  std::ostringstream sstream;
-  sstream << aValue;
-  std::string aParamValue = sstream.str();
-  // Error
+
+  data()->string(EXPRESSION_ERROR_ID())->setValue(outErrorMessage);
   if (!outErrorMessage.empty()) {
-    std::string aStateMsg("Error: " + outErrorMessage);
+    setError("Expression error.", false);
     data()->execState(ModelAPI_StateExecFailed);
-    setError(aStateMsg, false);
-  } else {
-    static const std::string anEmptyMsg(""); // it is checked in the validator by the empty message
-    setError(anEmptyMsg, false);
-    data()->execState(ModelAPI_StateDone);
+    return;
   }
 
+  setError("", false);
+  data()->execState(ModelAPI_StateDone);
+
   ResultParameterPtr aParam = document()->createParameter(data());
   AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
   aValueAttribute->setValue(aValue);
@@ -104,9 +102,11 @@ double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, st
   std::list<std::string>::iterator it = anExprParams.begin();
   std::list<ResultParameterPtr> aParamsList;
   for ( ; it != anExprParams.end(); it++) {
+    std::string& aVariableName = *it;
+
     double aValue;
     ResultParameterPtr aParamRes;
-    if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes, document())) continue;
+    if (!ModelAPI_Tools::findVariable(aVariableName, aValue, aParamRes, document())) continue;
     aParamsList.push_back(aParamRes);
 
     std::ostringstream sstream;
index c735c8ee7b4c2b026110ccfdc115998aa28738b8..9b029a1564b69e4c874ea63201ff1bdbf6b58cc4 100644 (file)
@@ -40,11 +40,18 @@ class ParametersPlugin_Parameter : public ModelAPI_Feature
     return MY_EXPRESSION_ID;
   }
 
+  /// attribute name of extrusion size
+  inline static const std::string& EXPRESSION_ERROR_ID()
+  {
+    static const std::string MY_EXPRESSION_ERROR_ID("ExpressionError");
+    return MY_EXPRESSION_ERROR_ID;
+  }
+
   /// list of references to the arguments of this expression
   inline static const std::string& ARGUMENTS_ID()
   {
-    static const std::string MY_VARIABLE_ID("arguments");
-    return MY_VARIABLE_ID;
+    static const std::string MY_ARGUMENTS_ID("arguments");
+    return MY_ARGUMENTS_ID;
   }
 
   /// Returns the kind of a feature
index d0b00ca8e8bff0494247b5cd4af0956a2c650333..82bd9d8d4d4d97304f65b4408f86bf19602cd1cd 100644 (file)
@@ -7,6 +7,8 @@
 
 #include <ParametersPlugin_Validators.h>
 
+#include <ParametersPlugin_Parameter.h>
+
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_ResultParameter.h>
@@ -93,16 +95,16 @@ bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttrib
   AttributeStringPtr aStrAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
   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;
   }
 
-  theError = aFeature->error();
-  return aFeature->error().empty();
+  theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
+  return theError.empty();
 }
diff --git a/test.squish/suite_ISSUES_SALOME/tst_903/test.py b/test.squish/suite_ISSUES_SALOME/tst_903/test.py
new file mode 100644 (file)
index 0000000..40b767f
--- /dev/null
@@ -0,0 +1,44 @@
+def main():
+    source(findFile("scripts", "common.py"))
+    
+    startApplication("salome_run.sh")
+   
+    activate_newgeom()
+
+    #[step] Click menu Part->Parameter    
+    activateItem(waitForObjectItem(":SALOME*_QMenuBar", "Part"))
+    activateItem(waitForObjectItem(":Part_QMenu", "Parameter"))
+    mouseClick(waitForObject(":Parameter_QLineEdit"), 79, 8, 0, Qt.LeftButton)
+    #[step] Check that feature ToolTip is: Model_FeatureValidator: Attribute "expression" is not initialized.
+    waitFor("object.exists(':Parameter_QFrame')", 20000)
+    test.compare(str(findObject(":Parameter_QFrame").toolTip), "Model_FeatureValidator: Attribute \"expression\" is not initialized.")
+    #[step] Check that name tooltip is: Errors:\nvariable - Parameters_VariableValidator: Incorrect variable name.
+    waitFor("object.exists(':Parameter_QLineEdit')", 20000)
+    test.compare(str(findObject(":Parameter_QLineEdit").toolTip), "Errors:\nvariable - Parameters_VariableValidator: Incorrect variable name.")
+    #[step] Check that expression tooltip is: Errors:\nexpression - Parameters_ExpressionValidator: Expression is empty.
+    waitFor("object.exists(':Parameter_ExpressionEditor')", 20000)
+    test.compare(str(findObject(":Parameter_ExpressionEditor").toolTip), "Errors:\nexpression - Parameters_ExpressionValidator: Expression is empty.")
+
+    #[step] Enter variable name 'a'    
+    type(waitForObject(":Parameter_QLineEdit"), "a")
+    mouseClick(waitForObject(":Parameter_ExpressionEditor"), 97, 31, 0, Qt.LeftButton)
+    
+    #[step] Enter variable expression '100+b'
+    type(waitForObject(":Parameter_ExpressionEditor"), "100+b")
+    
+    #[step] Check that expression tooltip is: Errors:\nexpression - Parameters_ExpressionValidator: name 'b' is not defined
+    waitFor("object.exists(':Parameter_ExpressionEditor')", 20000)
+    test.compare(str(findObject(":Parameter_ExpressionEditor").toolTip), "Errors:\nexpression - Parameters_ExpressionValidator: name 'b' is not defined")
+    #[step] Check that result message is: Error: unexpected EOF while parsing (<string>, line 0)
+    waitFor("object.exists(':Parameter.Result_QLabel')", 20000)
+    test.compare(str(findObject(":Parameter.Result_QLabel").text), "Error: name 'b' is not defined")
+
+    #[step] Check that feature ToolTip is: expression - Parameters_ExpressionValidator: name 'b' is not defined
+    waitFor("object.exists(':Parameter_QFrame')", 20000)
+    test.compare(str(findObject(":Parameter_QFrame").toolTip), "expression - Parameters_ExpressionValidator: name 'b' is not defined")
+
+    #[step] Check that apply button is disabled
+    waitFor("object.exists(':Parameter.property_panel_ok_QToolButton')", 20000)
+    test.compare(findObject(":Parameter.property_panel_ok_QToolButton").enabled, False)
+
+    close_application()