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