Salome HOME
Validators for Parameters values and expressions
authorsbh <sergey.belash@opencascade.com>
Thu, 9 Apr 2015 15:08:08 +0000 (18:08 +0300)
committersbh <sergey.belash@opencascade.com>
Thu, 9 Apr 2015 15:08:08 +0000 (18:08 +0300)
14 files changed:
src/Model/Model_Data.cpp
src/Model/Model_Data.h
src/Model/Model_ResultParameter.cpp
src/ModelAPI/ModelAPI_Data.h
src/ModelAPI/ModelAPI_Feature.h
src/ModuleBase/ModuleBase_ParamSpinBox.cpp
src/ModuleBase/ModuleBase_WidgetExprEditor.cpp
src/ParametersPlugin/CMakeLists.txt
src/ParametersPlugin/ParametersPlugin_Parameter.cpp
src/ParametersPlugin/ParametersPlugin_Parameter.h
src/ParametersPlugin/ParametersPlugin_Plugin.cpp
src/ParametersPlugin/ParametersPlugin_Validators.cpp [new file with mode: 0644]
src/ParametersPlugin/ParametersPlugin_Validators.h [new file with mode: 0644]
src/ParametersPlugin/plugin-Parameters.xml

index e8c1a980ea406cd429c2d6f984c399216fba90ff..e2ef4c7477e1357cfae8ae05164661d08040f092 100644 (file)
@@ -29,6 +29,7 @@
 #include <Events_Error.h>
 
 #include <TDataStd_Name.hxx>
+#include <TDataStd_AsciiString.hxx>
 #include <TDF_AttributeIterator.hxx>
 #include <TDF_ChildIterator.hxx>
 #include <TDF_RelocationTable.hxx>
@@ -239,10 +240,22 @@ ModelAPI_ExecState Model_Data::execState()
   return ModelAPI_StateMustBeUpdated; // default value
 }
 
-void Model_Data::setError(const std::string& theError)
+void Model_Data::setError(const std::string& theError, bool theSend)
 {
   execState(ModelAPI_StateExecFailed);
-  Events_Error::send(theError);
+  if (theSend) {
+    Events_Error::send(theError);
+  }
+  TDataStd_AsciiString::Set(myLab, theError.c_str());
+}
+
+std::string Model_Data::error() const
+{
+  Handle(TDataStd_AsciiString) anErrorAttr;
+  if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
+    return std::string(anErrorAttr->Get().ToCString());
+  }
+  return std::string();
 }
 
 int Model_Data::featureId() const
index 93de6977de7bb6cd633575d623c99ecb60a3d36f..4e96a5be5c65e126cc2352ca2fc2f07e72904e44 100644 (file)
@@ -163,7 +163,10 @@ class Model_Data : public ModelAPI_Data
   MODEL_EXPORT virtual ModelAPI_ExecState execState();
 
   /// Registers error during the execution, causes the ExecutionFailed state
-  MODEL_EXPORT virtual void setError(const std::string& theError);
+  MODEL_EXPORT virtual void setError(const std::string& theError, bool theSend = true);
+
+  /// Registers error during the execution, causes the ExecutionFailed state
+  MODEL_EXPORT virtual std::string error() const;
 
   /// Returns the identifier of feature-owner, unique in this document
   MODEL_EXPORT virtual int featureId() const;
index e5110e6770d87301941a1a45e1cb15773734dacc..0e1aee7fef14d253fa97524f963a729d1ea99a61 100644 (file)
@@ -17,11 +17,8 @@ void Model_ResultParameter::initAttributes()
 {
   data()->addAttribute(ModelAPI_ResultParameter::VALUE(),
                        ModelAPI_AttributeDouble::typeId());
-  data()->addAttribute(ModelAPI_ResultParameter::STATE(),
-                       ModelAPI_AttributeString::typeId());
 }
 
-
 Model_ResultParameter::Model_ResultParameter()
 {
   setIsConcealed(false);
index 3a922d5061e98579362ae38dc9e4aca4a221ef24..a03a06c9bf9ff40a2c7c80cf4875558780ef2653 100644 (file)
@@ -125,7 +125,10 @@ class MODELAPI_EXPORT ModelAPI_Data
   virtual ModelAPI_ExecState execState() = 0;
 
   /// Registers error during the execution, causes the ExecutionFailed state
-  virtual void setError(const std::string& theError) = 0;
+  virtual void setError(const std::string& theError, bool theSend = true) = 0;
+
+  /// Returns error, arose during the execution
+  virtual std::string error() const = 0;
 
   /// Returns the identifier of feature-owner, unique in this document
   virtual int featureId() const = 0;
index 5572bc08d4147cf49467f2fdc5563ed1ba473ab1..b80583544021bd7afc1f74b9129df94f246166f5 100644 (file)
@@ -58,8 +58,13 @@ class ModelAPI_Feature : public ModelAPI_Object
   virtual bool compute(const std::string& theAttributeId) { return false; };
 
   /// Registers error during the execution, causes the ExecutionFailed state
-  virtual void setError(const std::string& theError) {
-    data()->setError(theError);
+  virtual void setError(const std::string& theError, bool isSend = true) {
+    data()->setError(theError, isSend);
+  }
+
+  /// Returns error, arose during the execution
+  virtual std::string error() const {
+    return data()->error();
   }
 
   /// returns the current results of the feature
index cfcd5e6e0c59d0a42ea27f36287999c779520ae3..5ad7c1dcf3b88203317d95d6c4b9372e72dd5c29 100644 (file)
@@ -156,8 +156,7 @@ QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) cons
   // either a string starting with a letter, or a string starting with
   // an underscore followed by at least one alphanumeric character
   if (isAcceptVariables()) {
-    QRegExp varNameMask("(([a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|_)*)|"
-                        "(_([a-z]|[A-Z]|[0-9])+([a-z]|[A-Z]|[0-9]|_)*)");
+    QRegExp varNameMask("[_a-zA-Z][a-zA-Z0-9_]*");
     if (varNameMask.exactMatch(str))
       res = QValidator::Acceptable;
 
index 01d7ba4d5ab8b0db8e9033d0f8da0ab896eab5da..f96eec92c48a15a98066430cb477ef62d8873925 100644 (file)
 #include <ModuleBase_WidgetExprEditor.h>
 #include <ModuleBase_Tools.h>
 
-#include <ModelAPI_AttributeString.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Object.h>
 #include <ModelAPI_Validator.h>
 #include <ModelAPI_ResultParameter.h>
+#include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeDouble.h>
 
 #include <Config_WidgetAPI.h>
 
@@ -176,13 +177,24 @@ bool ModuleBase_WidgetExprEditor::storeValueCustom() const
   aStringAttr->setValue(aWidgetValue.toStdString());
   updateObject(myFeature);
 
-  if(!myFeature->firstResult().get())
-    return true;
-  
-  ResultParameterPtr aResult =
+  // Try to get the value
+  QString aStateMsg;
+  std::string anErrorMessage = myFeature->error();
+  if (anErrorMessage.empty()) {
+    ResultParameterPtr aParam =
       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
-  AttributeStringPtr aErrorAttr = aResult->data()->string(ModelAPI_ResultParameter::STATE());
-  myResultLabel->setText(QString::fromStdString(aErrorAttr->value()));
+    if(aParam.get()) {
+      AttributeDoublePtr aValueAttr =
+        aParam->data()->real(ModelAPI_ResultParameter::VALUE());
+      if (aValueAttr.get()) {
+        double aValue = aValueAttr->value();
+        aStateMsg = "Result: " + QString::number(aValue);
+      }
+    }
+  } else {
+    aStateMsg = QString::fromStdString(anErrorMessage);
+  }
+  myResultLabel->setText(aStateMsg);
   return true;
 }
 
index f6e54ea57418fac97638706cd04079ad9d982309..f10bf2e848a4cbddcc08a0144d9c5773a779753b 100644 (file)
@@ -6,12 +6,14 @@ SET(PROJECT_HEADERS
     ParametersPlugin_Plugin.h
     ParametersPlugin_Parameter.h
     ParametersPlugin_PyInterp.h
+    ParametersPlugin_Validators.h
 )
  
 SET(PROJECT_SOURCES
     ParametersPlugin_Plugin.cpp
     ParametersPlugin_Parameter.cpp
     ParametersPlugin_PyInterp.cpp
+    ParametersPlugin_Validators.cpp
 )
 
 SET(XML_RESOURCES
index b32afe718bb75b96d6e1e0997ad63f417d415b93..413e7dc9bcacf220e88d7737843b866cdf1cde7f 100644 (file)
@@ -39,22 +39,18 @@ bool ParametersPlugin_Parameter::isInHistory()
   return false;
 }
 
-void ParametersPlugin_Parameter::execute()
+void ParametersPlugin_Parameter::attributeChanged(const std::string&)
 {
   ResultParameterPtr aParam = document()->createParameter(data());
 
   std::string anExpression = string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
   if(anExpression.empty()) {
     // clear error/result if the expression is empty
-    aParam->data()->string(ModelAPI_ResultParameter::STATE())->setValue("");
+    setError("", false);
     return;
   }
-  // Value
   std::string outErrorMessage;
   double aValue = evaluate(anExpression, outErrorMessage);
-  AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
-  aValueAttribute->setValue(aValue);
-  setResult(aParam);
   // Name
   std::string aName = string(ParametersPlugin_Parameter::VARIABLE_ID())->value();
   std::ostringstream sstream;
@@ -63,17 +59,20 @@ void ParametersPlugin_Parameter::execute()
   data()->setName(aName + " ("+ aParamValue + ")");
   aParam->data()->setName(aName);
   // Error
-  AttributeStringPtr aErrorAttr = aParam->data()->string(ModelAPI_ResultParameter::STATE());
   std::string aStateMsg;
-  if (outErrorMessage.empty()) {
-    aStateMsg = "Result: " + aParamValue;
-  } else {
-    aStateMsg = "Error:\n" + outErrorMessage;
-  }
-  aErrorAttr->setValue(aStateMsg);
-  //if(!outErrorMessage.empty()) {
-  //  data()->execState(ModelAPI_StateExecFailed);
-  //}
+  if (!outErrorMessage.empty()) {
+    aStateMsg = "Error: " + outErrorMessage;
+    data()->execState(ModelAPI_StateExecFailed);
+  } 
+  setError(aStateMsg, false);
+  // Value
+  AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
+  aValueAttribute->setValue(aValue);
+  setResult(aParam);
+}
+
+void ParametersPlugin_Parameter::execute()
+{
 }
 
 double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, std::string& theError)
index 9eea5e160dc0cb39fa0c7377ae04aeb6de4c991d..c36d7764fa3e5d1059cf4bac2168b0ff4af43bb8 100644 (file)
@@ -52,7 +52,8 @@ class ParametersPlugin_Parameter : public ModelAPI_Feature
   PARAMETERSPLUGIN_EXPORT virtual void initAttributes();
 
   PARAMETERSPLUGIN_EXPORT virtual bool isInHistory(); //false
-  //bool isI
+
+  PARAMETERSPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID);
 
   /// Use plugin manager for features creation
   ParametersPlugin_Parameter();
index 9affdd7a1297dd255121805386702dd92ca4f40e..97fa7a9fdd795f27fd0e09d3323f7bce50bebffe 100644 (file)
@@ -2,8 +2,10 @@
 
 #include <ParametersPlugin_Plugin.h>
 #include <ParametersPlugin_Parameter.h>
+#include <ParametersPlugin_Validators.h>
 
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Validator.h>
 
 #include <memory>
 
@@ -15,6 +17,12 @@ ParametersPlugin_Plugin::ParametersPlugin_Plugin()
   // register this plugin
   SessionPtr aSession = ModelAPI_Session::get();
   aSession->registerPlugin(this);
+
+  ModelAPI_ValidatorsFactory* aFactory = aSession->validators();
+  aFactory->registerValidator("Parameters_VariableValidator",
+                              new ParametersPlugin_VariableValidator);
+  aFactory->registerValidator("Parameters_ExpressionValidator",
+                              new ParametersPlugin_ExpressionValidator);
 }
 
 FeaturePtr ParametersPlugin_Plugin::createFeature(std::string theFeatureID)
diff --git a/src/ParametersPlugin/ParametersPlugin_Validators.cpp b/src/ParametersPlugin/ParametersPlugin_Validators.cpp
new file mode 100644 (file)
index 0000000..dc7eb06
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Parameters_VariableValidator.cpp
+ *
+ *  Created on: Apr 9, 2015
+ *      Author: sbh
+ */
+
+#include <ParametersPlugin_Validators.h>
+
+#include <ModelAPI_AttributeString.h>
+#include <ModelAPI_Feature.h>
+#include <ModelAPI_ResultParameter.h>
+
+ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
+{
+  myPyVariableRegex = std::regex("[_a-zA-Z][a-zA-Z0-9_]*");
+}
+
+ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
+{
+}
+
+bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
+                                                 const std::list<std::string>& theArguments) const
+{
+  AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
+  bool result = std::regex_match(aStrAttr->value(), myPyVariableRegex);
+  return result;
+}
+
+ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
+{
+
+}
+
+ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
+{
+
+}
+
+bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
+                                                  const std::list<std::string>& theArguments) 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);
+  bool isEmptyExpr = aStrAttr->value().empty();
+  if(isEmptyExpr)
+    return false;
+
+  if(!aParam.get())
+    return false;
+
+  return aFeature->error().empty();
+}
diff --git a/src/ParametersPlugin/ParametersPlugin_Validators.h b/src/ParametersPlugin/ParametersPlugin_Validators.h
new file mode 100644 (file)
index 0000000..3a4db55
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Parameters_VariableValidator.h
+ *
+ *  Created on: Apr 9, 2015
+ *      Author: sbh
+ */
+
+#ifndef PARAMETERSPLUGIN_VARIABLEVALIDATOR_H_
+#define PARAMETERSPLUGIN_VARIABLEVALIDATOR_H_
+
+#include <ParametersPlugin.h>
+#include <ParametersPlugin_Validators.h>
+
+#include <ModelAPI_AttributeValidator.h>
+
+#include <regex>
+#include <memory>
+
+class ParametersPlugin_VariableValidator : public ModelAPI_AttributeValidator
+{
+ public:
+  PARAMETERSPLUGIN_EXPORT ParametersPlugin_VariableValidator();
+  PARAMETERSPLUGIN_EXPORT virtual ~ParametersPlugin_VariableValidator();
+
+  //! returns true if attribute is valid
+  //! \param theAttribute the checked attribute
+  //! \param theArguments arguments of the attribute
+  PARAMETERSPLUGIN_EXPORT virtual bool isValid(const AttributePtr& theAttribute,
+                                               const std::list<std::string>& theArguments) const;
+
+ private:
+  std::regex myPyVariableRegex;
+};
+
+class ParametersPlugin_ExpressionValidator: public ModelAPI_AttributeValidator
+{
+ public:
+  PARAMETERSPLUGIN_EXPORT ParametersPlugin_ExpressionValidator();
+  PARAMETERSPLUGIN_EXPORT virtual ~ParametersPlugin_ExpressionValidator();
+
+  //! returns true if attribute is valid
+  //! \param theAttribute the checked attribute
+  //! \param theArguments arguments of the attribute
+  PARAMETERSPLUGIN_EXPORT virtual bool isValid(const AttributePtr& theAttribute,
+                                               const std::list<std::string>& theArguments) const;
+};
+
+
+#endif /* PARAMETERSPLUGIN_VARIABLEVALIDATOR_H_ */
index 027f50c246e8de829531bd99f0f5f3f8190583ac..0a84294b5deb7c9c87a6349c2ad1a0705e9abb7f 100644 (file)
@@ -4,8 +4,12 @@
   <workbench id="Part">
     <group id="Parameters">
       <feature id="Parameter" title="New Variable" tooltip="Creates a variable" icon=":pictures/expression.png">
-        <stringvalue id="variable" label="Name" icon=":pictures/expression.png"/>
-        <expr_editor id="expression"/>
+        <stringvalue id="variable" label="Name" icon=":pictures/expression.png">
+          <validator id="Parameters_VariableValidator"/>
+        </stringvalue>
+        <expr_editor id="expression">
+          <validator id="Parameters_ExpressionValidator"/>
+        </expr_editor>
       </feature>
     </group>
   </workbench>