1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ParametersPlugin_Validators.h>
22 #include <ParametersPlugin_Parameter.h>
24 #include <Events_InfoMessage.h>
26 #include <Locale_Convert.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_ResultParameter.h>
31 #include <ModelAPI_Tools.h>
32 #include <ModelAPI_Expression.h>
34 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
38 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
42 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
43 const std::list<std::string>& theArguments,
44 Events_InfoMessage& theError) const
46 AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
47 if (!aStrAttr->isInitialized()) {
48 theError = "Attribute \"%1\" is not initialized.";
49 theError.arg(aStrAttr->id());
52 bool isEmptyExpr = aStrAttr->value().empty();
54 theError = "Attribute \"%1\" value is empty.";
55 theError.arg(aStrAttr->id());
58 if (!ModelAPI_Expression::isVariable(aStrAttr->value())) {
59 theError = "Incorrect variable name.";
62 if (!isUnique(theAttribute, aStrAttr->value())) {
63 theError = "Variable name is not unique.";
69 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
70 const std::string& theString) const
72 DocumentPtr aDocument = theAttribute->owner()->document();
73 for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
74 anIndex < aSize; ++anIndex) {
75 ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
76 if (Locale::Convert::toString(aParamObj->data()->name()) != theString)
78 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
81 FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
82 if (aFeature == theAttribute->owner())
89 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
94 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
99 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
100 const std::list<std::string>& theArguments,
101 Events_InfoMessage& theError) const
103 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
105 AttributeStringPtr aStrAttr =
106 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
107 if (!aStrAttr->isInitialized()) {
108 theError = "Attribute \"%1\" is not initialized.";
109 theError.arg(aStrAttr->id());
112 bool isEmptyExpr = aStrAttr->value().empty();
114 theError = "Expression is empty.";
118 theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
119 return theError.empty();