Salome HOME
updated copyright message
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_Validators.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ParametersPlugin_Validators.h>
21
22 #include <ParametersPlugin_Parameter.h>
23
24 #include <Events_InfoMessage.h>
25
26 #include <Locale_Convert.h>
27
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>
33
34 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
35 {
36 }
37
38 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
39 {
40 }
41
42 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
43                                                  const std::list<std::string>& /*theArguments*/,
44                                                  Events_InfoMessage& theError) const
45 {
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());
50     return false;
51   }
52   bool isEmptyExpr = aStrAttr->value().empty();
53   if (isEmptyExpr) {
54     theError = "Attribute \"%1\" value is empty.";
55     theError.arg(aStrAttr->id());
56     return false;
57   }
58   if (!ModelAPI_Expression::isVariable(aStrAttr->value())) {
59     theError = "Incorrect variable name.";
60     return false;
61   }
62   if (!isUnique(theAttribute, aStrAttr->value())) {
63     theError = "Variable name is not unique.";
64     return false;
65   }
66   return true;
67 }
68
69 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
70                                                   const std::string& theString) const
71 {
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)
77       continue;
78     ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
79     if (!aParam.get())
80       continue;
81     FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
82     if (aFeature == theAttribute->owner())
83       continue;
84     return false;
85   }
86   return true;
87 }
88
89 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
90 {
91
92 }
93
94 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
95 {
96
97 }
98
99 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
100                                                    const std::list<std::string>& /*theArguments*/,
101                                                    Events_InfoMessage& theError) const
102 {
103   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
104
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());
110     return false;
111   }
112   bool isEmptyExpr = aStrAttr->value().empty();
113   if (isEmptyExpr) {
114     theError = "Expression is empty.";
115     return false;
116   }
117
118   theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
119   return theError.empty();
120 }