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