Salome HOME
Fix for the issue #1051 the obsolete results were not taken into account, so, sometim...
[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 (!isVariable(aStrAttr->value())) {
31     theError = "Incorrect variable name.";
32     return false;
33   } 
34   if (!isUnique(theAttribute, aStrAttr->value())) {
35     theError = "Variable name is not unique.";
36     return false;
37   }
38   return true;
39 }
40
41 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
42 {
43   if (theString.empty())
44     return false;
45   std::string::const_iterator it = theString.begin();
46   if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
47     return false;
48   it++;
49   for ( ; it != theString.end(); ++it ) {
50     if(!(isalnum(*it) || (*it) == '_')) {
51       return false;
52     }
53   }
54   return true;
55 }
56
57 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
58                                                   const std::string& theString) const
59 {
60   DocumentPtr aDocument = theAttribute->owner()->document();
61   for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
62        anIndex < aSize; ++anIndex) {
63     ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
64     if (aParamObj->data()->name() != theString)
65       continue;
66     ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
67     if (!aParam.get())
68       continue;
69     FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
70     if (aFeature == theAttribute->owner())
71       continue;
72     return false;
73   }
74   return true;
75 }
76
77 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
78 {
79
80 }
81
82 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
83 {
84
85 }
86
87 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
88                                                    const std::list<std::string>& theArguments,
89                                                    std::string& theError) const
90 {
91   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
92   ResultParameterPtr aParam =
93       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
94
95   AttributeStringPtr aStrAttr =
96       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
97   bool isEmptyExpr = aStrAttr->value().empty();
98   if (isEmptyExpr) {
99     theError = "Expression is empty.";
100     return false;
101   }
102
103   if (!aParam.get()) {
104     theError = "Result is empty.";
105     return false;
106   }
107
108   theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
109   return theError.empty();
110 }