Salome HOME
[Code coverage ParametersPlugin]: Unit tests for parameter remove and for error treating
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_Validators.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ParametersPlugin_Validators.h>
22
23 #include <ParametersPlugin_Parameter.h>
24
25 #include <Events_InfoMessage.h>
26
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>
32
33 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
34 {
35 }
36
37 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
38 {
39 }
40
41 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
42                                                  const std::list<std::string>& theArguments,
43                                                  Events_InfoMessage& theError) const
44 {
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());
49     return false;
50   }
51   bool isEmptyExpr = aStrAttr->value().empty();
52   if (isEmptyExpr) {
53     theError = "Attribute \"%1\" value is empty.";
54     theError.arg(aStrAttr->id());
55     return false;
56   }
57   if (!ModelAPI_Expression::isVariable(aStrAttr->value())) {
58     theError = "Incorrect variable name.";
59     return false;
60   }
61   if (!isUnique(theAttribute, aStrAttr->value())) {
62     theError = "Variable name is not unique.";
63     return false;
64   }
65   return true;
66 }
67
68 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
69                                                   const std::string& theString) const
70 {
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)
76       continue;
77     ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
78     if (!aParam.get())
79       continue;
80     FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
81     if (aFeature == theAttribute->owner())
82       continue;
83     return false;
84   }
85   return true;
86 }
87
88 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
89 {
90
91 }
92
93 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
94 {
95
96 }
97
98 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
99                                                    const std::list<std::string>& theArguments,
100                                                    Events_InfoMessage& theError) const
101 {
102   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
103
104   AttributeStringPtr aStrAttr =
105       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
106   if (!aStrAttr->isInitialized()) {
107     theError = "Attribute \"%1\" is not initialized.";
108     theError.arg(aStrAttr->id());
109     return false;
110   }
111   bool isEmptyExpr = aStrAttr->value().empty();
112   if (isEmptyExpr) {
113     theError = "Expression is empty.";
114     return false;
115   }
116
117   theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
118   return theError.empty();
119 }