Salome HOME
Issue #1860: fix end lines with spaces
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_Validators.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2 /*
3  * Parameters_VariableValidator.cpp
4  *
5  *  Created on: Apr 9, 2015
6  *      Author: sbh
7  */
8
9 #include <ParametersPlugin_Validators.h>
10
11 #include <ParametersPlugin_Parameter.h>
12
13 #include <Events_InfoMessage.h>
14
15 #include <ModelAPI_AttributeString.h>
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_ResultParameter.h>
18 #include <ModelAPI_Tools.h>
19 #include <ModelAPI_Expression.h>
20
21 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
22 {
23 }
24
25 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
26 {
27 }
28
29 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
30                                                  const std::list<std::string>& theArguments,
31                                                  Events_InfoMessage& theError) const
32 {
33   AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
34   if (!aStrAttr->isInitialized()) {
35     theError = "Attribute \"%1\" is not initialized.";
36     theError.arg(aStrAttr->id());
37     return false;
38   }
39   bool isEmptyExpr = aStrAttr->value().empty();
40   if (isEmptyExpr) {
41     theError = "Attribute \"%1\" value is empty.";
42     theError.arg(aStrAttr->id());
43     return false;
44   }
45   if (!ModelAPI_Expression::isVariable(aStrAttr->value())) {
46     theError = "Incorrect variable name.";
47     return false;
48   }
49   if (!isUnique(theAttribute, aStrAttr->value())) {
50     theError = "Variable name is not unique.";
51     return false;
52   }
53   return true;
54 }
55
56 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
57                                                   const std::string& theString) const
58 {
59   DocumentPtr aDocument = theAttribute->owner()->document();
60   for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
61        anIndex < aSize; ++anIndex) {
62     ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
63     if (aParamObj->data()->name() != theString)
64       continue;
65     ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
66     if (!aParam.get())
67       continue;
68     FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
69     if (aFeature == theAttribute->owner())
70       continue;
71     return false;
72   }
73   return true;
74 }
75
76 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
77 {
78
79 }
80
81 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
82 {
83
84 }
85
86 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
87                                                    const std::list<std::string>& theArguments,
88                                                    Events_InfoMessage& theError) const
89 {
90   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
91   ResultParameterPtr aParam =
92       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
93
94   AttributeStringPtr aStrAttr =
95       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
96   if (!aStrAttr->isInitialized()) {
97     theError = "Attribute \"%1\" is not initialized.";
98     theError.arg(aStrAttr->id());
99     return false;
100   }
101   bool isEmptyExpr = aStrAttr->value().empty();
102   if (isEmptyExpr) {
103     theError = "Expression is empty.";
104     return false;
105   }
106
107   if (!aParam.get()) {
108     theError = "Result is empty.";
109     return false;
110   }
111
112   theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
113   return theError.empty();
114 }