1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ParametersPlugin_Validators.h>
23 #include <ParametersPlugin_Parameter.h>
25 #include <Events_InfoMessage.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_ResultParameter.h>
30 #include <ModelAPI_Tools.h>
31 #include <ModelAPI_Expression.h>
33 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
37 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
41 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
42 const std::list<std::string>& theArguments,
43 Events_InfoMessage& theError) const
45 AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
46 if (!aStrAttr->isInitialized()) {
47 theError = "Attribute \"%1\" is not initialized.";
48 theError.arg(aStrAttr->id());
51 bool isEmptyExpr = aStrAttr->value().empty();
53 theError = "Attribute \"%1\" value is empty.";
54 theError.arg(aStrAttr->id());
57 if (!ModelAPI_Expression::isVariable(aStrAttr->value())) {
58 theError = "Incorrect variable name.";
61 if (!isUnique(theAttribute, aStrAttr->value())) {
62 theError = "Variable name is not unique.";
68 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
69 const std::string& theString) const
71 DocumentPtr aDocument = theAttribute->owner()->document();
72 for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
73 anIndex < aSize; ++anIndex) {
74 ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
75 if (aParamObj->data()->name() != theString)
77 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
80 FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
81 if (aFeature == theAttribute->owner())
88 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
93 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
98 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
99 const std::list<std::string>& theArguments,
100 Events_InfoMessage& theError) const
102 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
103 ResultParameterPtr aParam =
104 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
106 AttributeStringPtr aStrAttr =
107 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
108 if (!aStrAttr->isInitialized()) {
109 theError = "Attribute \"%1\" is not initialized.";
110 theError.arg(aStrAttr->id());
113 bool isEmptyExpr = aStrAttr->value().empty();
115 theError = "Expression is empty.";
120 theError = "Result is empty.";
124 theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
125 return theError.empty();