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 <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_ResultParameter.h>
29 #include <ModelAPI_Tools.h>
30 #include <ModelAPI_Expression.h>
32 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
36 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
40 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
41 const std::list<std::string>& theArguments,
42 Events_InfoMessage& theError) const
44 AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
45 if (!aStrAttr->isInitialized()) {
46 theError = "Attribute \"%1\" is not initialized.";
47 theError.arg(aStrAttr->id());
50 bool isEmptyExpr = aStrAttr->value().empty();
52 theError = "Attribute \"%1\" value is empty.";
53 theError.arg(aStrAttr->id());
56 if (!ModelAPI_Expression::isVariable(aStrAttr->value())) {
57 theError = "Incorrect variable name.";
60 if (!isUnique(theAttribute, aStrAttr->value())) {
61 theError = "Variable name is not unique.";
67 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
68 const std::string& theString) const
70 DocumentPtr aDocument = theAttribute->owner()->document();
71 for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
72 anIndex < aSize; ++anIndex) {
73 ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
74 if (aParamObj->data()->name() != theString)
76 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
79 FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
80 if (aFeature == theAttribute->owner())
87 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
92 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
97 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
98 const std::list<std::string>& theArguments,
99 Events_InfoMessage& theError) const
101 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
103 AttributeStringPtr aStrAttr =
104 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
105 if (!aStrAttr->isInitialized()) {
106 theError = "Attribute \"%1\" is not initialized.";
107 theError.arg(aStrAttr->id());
110 bool isEmptyExpr = aStrAttr->value().empty();
112 theError = "Expression is empty.";
116 theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
117 return theError.empty();