From: sbh Date: Thu, 9 Apr 2015 15:08:08 +0000 (+0300) Subject: Validators for Parameters values and expressions X-Git-Tag: V_1.1.0~53 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=df901d4270af49bacb374dcb862596e2f88b0c0f;p=modules%2Fshaper.git Validators for Parameters values and expressions --- diff --git a/src/Model/Model_Data.cpp b/src/Model/Model_Data.cpp index e8c1a980e..e2ef4c747 100644 --- a/src/Model/Model_Data.cpp +++ b/src/Model/Model_Data.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -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 diff --git a/src/Model/Model_Data.h b/src/Model/Model_Data.h index 93de6977d..4e96a5be5 100644 --- a/src/Model/Model_Data.h +++ b/src/Model/Model_Data.h @@ -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; diff --git a/src/Model/Model_ResultParameter.cpp b/src/Model/Model_ResultParameter.cpp index e5110e677..0e1aee7fe 100644 --- a/src/Model/Model_ResultParameter.cpp +++ b/src/Model/Model_ResultParameter.cpp @@ -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); diff --git a/src/ModelAPI/ModelAPI_Data.h b/src/ModelAPI/ModelAPI_Data.h index 3a922d506..a03a06c9b 100644 --- a/src/ModelAPI/ModelAPI_Data.h +++ b/src/ModelAPI/ModelAPI_Data.h @@ -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; diff --git a/src/ModelAPI/ModelAPI_Feature.h b/src/ModelAPI/ModelAPI_Feature.h index 5572bc08d..b80583544 100644 --- a/src/ModelAPI/ModelAPI_Feature.h +++ b/src/ModelAPI/ModelAPI_Feature.h @@ -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 diff --git a/src/ModuleBase/ModuleBase_ParamSpinBox.cpp b/src/ModuleBase/ModuleBase_ParamSpinBox.cpp index cfcd5e6e0..5ad7c1dcf 100644 --- a/src/ModuleBase/ModuleBase_ParamSpinBox.cpp +++ b/src/ModuleBase/ModuleBase_ParamSpinBox.cpp @@ -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; diff --git a/src/ModuleBase/ModuleBase_WidgetExprEditor.cpp b/src/ModuleBase/ModuleBase_WidgetExprEditor.cpp index 01d7ba4d5..f96eec92c 100644 --- a/src/ModuleBase/ModuleBase_WidgetExprEditor.cpp +++ b/src/ModuleBase/ModuleBase_WidgetExprEditor.cpp @@ -10,11 +10,12 @@ #include #include -#include #include #include #include #include +#include +#include #include @@ -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(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; } diff --git a/src/ParametersPlugin/CMakeLists.txt b/src/ParametersPlugin/CMakeLists.txt index f6e54ea57..f10bf2e84 100644 --- a/src/ParametersPlugin/CMakeLists.txt +++ b/src/ParametersPlugin/CMakeLists.txt @@ -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 diff --git a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp index b32afe718..413e7dc9b 100644 --- a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp +++ b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp @@ -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) diff --git a/src/ParametersPlugin/ParametersPlugin_Parameter.h b/src/ParametersPlugin/ParametersPlugin_Parameter.h index 9eea5e160..c36d7764f 100644 --- a/src/ParametersPlugin/ParametersPlugin_Parameter.h +++ b/src/ParametersPlugin/ParametersPlugin_Parameter.h @@ -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(); diff --git a/src/ParametersPlugin/ParametersPlugin_Plugin.cpp b/src/ParametersPlugin/ParametersPlugin_Plugin.cpp index 9affdd7a1..97fa7a9fd 100644 --- a/src/ParametersPlugin/ParametersPlugin_Plugin.cpp +++ b/src/ParametersPlugin/ParametersPlugin_Plugin.cpp @@ -2,8 +2,10 @@ #include #include +#include #include +#include #include @@ -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 index 000000000..dc7eb066c --- /dev/null +++ b/src/ParametersPlugin/ParametersPlugin_Validators.cpp @@ -0,0 +1,58 @@ +/* + * Parameters_VariableValidator.cpp + * + * Created on: Apr 9, 2015 + * Author: sbh + */ + +#include + +#include +#include +#include + +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& theArguments) const +{ + AttributeStringPtr aStrAttr = std::dynamic_pointer_cast(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& theArguments) const +{ + FeaturePtr aFeature = std::dynamic_pointer_cast(theAttribute->owner()); + ResultParameterPtr aParam = + std::dynamic_pointer_cast(aFeature->firstResult()); + + AttributeStringPtr aStrAttr = + std::dynamic_pointer_cast(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 index 000000000..3a4db55c8 --- /dev/null +++ b/src/ParametersPlugin/ParametersPlugin_Validators.h @@ -0,0 +1,49 @@ +/* + * Parameters_VariableValidator.h + * + * Created on: Apr 9, 2015 + * Author: sbh + */ + +#ifndef PARAMETERSPLUGIN_VARIABLEVALIDATOR_H_ +#define PARAMETERSPLUGIN_VARIABLEVALIDATOR_H_ + +#include +#include + +#include + +#include +#include + +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& 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& theArguments) const; +}; + + +#endif /* PARAMETERSPLUGIN_VARIABLEVALIDATOR_H_ */ diff --git a/src/ParametersPlugin/plugin-Parameters.xml b/src/ParametersPlugin/plugin-Parameters.xml index 027f50c24..0a84294b5 100644 --- a/src/ParametersPlugin/plugin-Parameters.xml +++ b/src/ParametersPlugin/plugin-Parameters.xml @@ -4,8 +4,12 @@ - - + + + + + +