]> SALOME platform Git repositories - modules/shaper.git/blob - src/ParametersPlugin/ParametersPlugin_Validators.cpp
Salome HOME
ec550aecaad95bd2be334bddbff623d9fc6eed91
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_Validators.cpp
1 /*
2  * Parameters_VariableValidator.cpp
3  *
4  *  Created on: Apr 9, 2015
5  *      Author: sbh
6  */
7
8 #include <ParametersPlugin_Validators.h>
9
10 #include <ParametersPlugin_Parameter.h>
11
12 #include <Events_InfoMessage.h>
13
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_ResultParameter.h>
17 #include <ModelAPI_Tools.h>
18
19 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
20 {
21 }
22
23 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
24 {
25 }
26
27 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
28                                                  const std::list<std::string>& theArguments,
29                                                  Events_InfoMessage& theError) const
30 {
31   AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
32   if (!aStrAttr->isInitialized()) {
33     theError = "Attribute \"" + aStrAttr->id() + "\" is not initialized.";
34     return false;
35   }
36   bool isEmptyExpr = aStrAttr->value().empty();
37   if (isEmptyExpr) {
38     theError = "Attribute \"" + aStrAttr->id() + "\" value is empty.";
39     return false;
40   }
41   if (!isVariable(aStrAttr->value())) {
42     theError = "Incorrect variable name.";
43     return false;
44   } 
45   if (!isUnique(theAttribute, aStrAttr->value())) {
46     theError = "Variable name is not unique.";
47     return false;
48   }
49   return true;
50 }
51
52 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
53 {
54   if (theString.empty())
55     return false;
56   std::string::const_iterator it = theString.begin();
57   if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
58     return false;
59   it++;
60   for ( ; it != theString.end(); ++it ) {
61     if(!(isalnum(*it) || (*it) == '_')) {
62       return false;
63     }
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   ResultParameterPtr aParam =
104       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
105
106   AttributeStringPtr aStrAttr =
107       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
108   if (!aStrAttr->isInitialized()) {
109     theError = "Attribute \"" + aStrAttr->id() + "\" is not initialized.";
110     return false;
111   }
112   bool isEmptyExpr = aStrAttr->value().empty();
113   if (isEmptyExpr) {
114     theError = "Expression is empty.";
115     return false;
116   }
117
118   if (!aParam.get()) {
119     theError = "Result is empty.";
120     return false;
121   }
122
123   theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
124   return theError.empty();
125 }