Salome HOME
Merge with PythonAPI branch
[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 <ModelAPI_AttributeString.h>
13 #include <ModelAPI_Feature.h>
14 #include <ModelAPI_ResultParameter.h>
15 #include <ModelAPI_Tools.h>
16
17 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
18 {
19 }
20
21 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
22 {
23 }
24
25 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
26                                                  const std::list<std::string>& theArguments,
27                                                  std::string& theError) const
28 {
29   AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
30   if (!aStrAttr->isInitialized()) {
31     theError = "Attribute \"" + aStrAttr->id() + "\" is not initialized.";
32     return false;
33   }
34   bool isEmptyExpr = aStrAttr->value().empty();
35   if (isEmptyExpr) {
36     theError = "Attribute \"" + aStrAttr->id() + "\" value is empty.";
37     return false;
38   }
39   if (!isVariable(aStrAttr->value())) {
40     theError = "Incorrect variable name.";
41     return false;
42   } 
43   if (!isUnique(theAttribute, aStrAttr->value())) {
44     theError = "Variable name is not unique.";
45     return false;
46   }
47   return true;
48 }
49
50 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
51 {
52   if (theString.empty())
53     return false;
54   std::string::const_iterator it = theString.begin();
55   if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
56     return false;
57   it++;
58   for ( ; it != theString.end(); ++it ) {
59     if(!(isalnum(*it) || (*it) == '_')) {
60       return false;
61     }
62   }
63   return true;
64 }
65
66 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
67                                                   const std::string& theString) const
68 {
69   DocumentPtr aDocument = theAttribute->owner()->document();
70   for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
71        anIndex < aSize; ++anIndex) {
72     ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
73     if (aParamObj->data()->name() != theString)
74       continue;
75     ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
76     if (!aParam.get())
77       continue;
78     FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
79     if (aFeature == theAttribute->owner())
80       continue;
81     return false;
82   }
83   return true;
84 }
85
86 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
87 {
88
89 }
90
91 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
92 {
93
94 }
95
96 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
97                                                    const std::list<std::string>& theArguments,
98                                                    std::string& theError) const
99 {
100   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
101   ResultParameterPtr aParam =
102       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
103
104   AttributeStringPtr aStrAttr =
105       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
106   if (!aStrAttr->isInitialized()) {
107     theError = "Attribute \"" + aStrAttr->id() + "\" is not initialized.";
108     return false;
109   }
110   bool isEmptyExpr = aStrAttr->value().empty();
111   if (isEmptyExpr) {
112     theError = "Expression is empty.";
113     return false;
114   }
115
116   if (!aParam.get()) {
117     theError = "Result is empty.";
118     return false;
119   }
120
121   theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
122   return theError.empty();
123 }