2 * Parameters_VariableValidator.cpp
4 * Created on: Apr 9, 2015
8 #include <ParametersPlugin_Validators.h>
10 #include <ParametersPlugin_Parameter.h>
12 #include <Events_InfoMessage.h>
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_ResultParameter.h>
17 #include <ModelAPI_Tools.h>
19 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
23 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
27 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
28 const std::list<std::string>& theArguments,
29 Events_InfoMessage& theError) const
31 AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
32 if (!aStrAttr->isInitialized()) {
33 theError = "Attribute \"" + aStrAttr->id() + "\" is not initialized.";
36 bool isEmptyExpr = aStrAttr->value().empty();
38 theError = "Attribute \"" + aStrAttr->id() + "\" value is empty.";
41 if (!isVariable(aStrAttr->value())) {
42 theError = "Incorrect variable name.";
45 if (!isUnique(theAttribute, aStrAttr->value())) {
46 theError = "Variable name is not unique.";
52 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
54 if (theString.empty())
56 std::string::const_iterator it = theString.begin();
57 if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
60 for ( ; it != theString.end(); ++it ) {
61 if(!(isalnum(*it) || (*it) == '_')) {
68 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
69 const std::string& theString) const
71 DocumentPtr aDocument = theAttribute->owner()->document();
72 for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
73 anIndex < aSize; ++anIndex) {
74 ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
75 if (aParamObj->data()->name() != theString)
77 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
80 FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
81 if (aFeature == theAttribute->owner())
88 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
93 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
98 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
99 const std::list<std::string>& theArguments,
100 Events_InfoMessage& theError) const
102 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
103 ResultParameterPtr aParam =
104 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
106 AttributeStringPtr aStrAttr =
107 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
108 if (!aStrAttr->isInitialized()) {
109 theError = "Attribute \"" + aStrAttr->id() + "\" is not initialized.";
112 bool isEmptyExpr = aStrAttr->value().empty();
114 theError = "Expression is empty.";
119 theError = "Result is empty.";
123 theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
124 return theError.empty();