Salome HOME
Error management -- Attribute validator returns an error.
[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   bool result = isVariable(aStrAttr->value()) && isUnique(theAttribute, aStrAttr->value());
29   return result;
30 }
31
32 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
33 {
34   if (theString.empty())
35     return false;
36   std::string::const_iterator it = theString.begin();
37   if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
38     return false;
39   it++;
40   for ( ; it != theString.end(); ++it ) {
41     if(!(isalnum(*it) || (*it) == '_')) {
42       return false;
43     }
44   }
45   return true;
46 }
47
48 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute, 
49                                                   const std::string& theString) const
50 {
51   DocumentPtr aDocument = theAttribute->owner()->document();
52   for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
53        anIndex < aSize; ++anIndex) {
54     ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
55     if (aParamObj->data()->name() != theString)
56       continue;
57     ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
58     if (!aParam.get())
59       continue;
60     FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
61     if (aFeature == theAttribute->owner())
62       continue;
63     return false;
64   }
65   return true;
66 }
67
68 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
69 {
70
71 }
72
73 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
74 {
75
76 }
77
78 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
79                                                    const std::list<std::string>& theArguments,
80                                                    std::string& theError) const
81 {
82   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
83   ResultParameterPtr aParam =
84       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
85
86   AttributeStringPtr aStrAttr =
87     std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
88   bool isEmptyExpr = aStrAttr->value().empty();
89   if(isEmptyExpr)
90     return false;
91
92   if(!aParam.get())
93     return false;
94
95   return aFeature->error().empty();
96 }