2 * Parameters_VariableValidator.cpp
4 * Created on: Apr 9, 2015
8 #include <ParametersPlugin_Validators.h>
10 #include <ParametersPlugin_Parameter.h>
12 #include <ModelAPI_AttributeString.h>
13 #include <ModelAPI_Feature.h>
14 #include <ModelAPI_ResultParameter.h>
15 #include <ModelAPI_Tools.h>
17 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
21 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
25 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
26 const std::list<std::string>& theArguments,
27 std::string& theError) const
29 AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
30 if (!aStrAttr->isInitialized()) {
31 theError = "Attribute \"" + aStrAttr->id() + "\" is not initialized.";
34 bool isEmptyExpr = aStrAttr->value().empty();
36 theError = "Attribute \"" + aStrAttr->id() + "\" value is empty.";
39 if (!isVariable(aStrAttr->value())) {
40 theError = "Incorrect variable name.";
43 if (!isUnique(theAttribute, aStrAttr->value())) {
44 theError = "Variable name is not unique.";
50 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
52 if (theString.empty())
54 std::string::const_iterator it = theString.begin();
55 if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
58 for ( ; it != theString.end(); ++it ) {
59 if(!(isalnum(*it) || (*it) == '_')) {
66 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
67 const std::string& theString) const
69 DocumentPtr aDocument = theAttribute->owner()->document();
70 for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
71 anIndex < aSize; ++anIndex) {
72 ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
73 if (aParamObj->data()->name() != theString)
75 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
78 FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
79 if (aFeature == theAttribute->owner())
86 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
91 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
96 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
97 const std::list<std::string>& theArguments,
98 std::string& theError) const
100 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
101 ResultParameterPtr aParam =
102 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
104 AttributeStringPtr aStrAttr =
105 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
106 if (!aStrAttr->isInitialized()) {
107 theError = "Attribute \"" + aStrAttr->id() + "\" is not initialized.";
110 bool isEmptyExpr = aStrAttr->value().empty();
112 theError = "Expression is empty.";
117 theError = "Result is empty.";
121 theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
122 return theError.empty();