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 \"%1\" is not initialized.";
34 theError.arg(aStrAttr->id());
37 bool isEmptyExpr = aStrAttr->value().empty();
39 theError = "Attribute \"%1\" value is empty.";
40 theError.arg(aStrAttr->id());
43 if (!isVariable(aStrAttr->value())) {
44 theError = "Incorrect variable name.";
47 if (!isUnique(theAttribute, aStrAttr->value())) {
48 theError = "Variable name is not unique.";
54 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
56 if (theString.empty())
58 std::string::const_iterator it = theString.begin();
59 if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
62 for ( ; it != theString.end(); ++it ) {
63 if(!(isalnum(*it) || (*it) == '_')) {
70 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
71 const std::string& theString) const
73 DocumentPtr aDocument = theAttribute->owner()->document();
74 for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
75 anIndex < aSize; ++anIndex) {
76 ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
77 if (aParamObj->data()->name() != theString)
79 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
82 FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
83 if (aFeature == theAttribute->owner())
90 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
95 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
100 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
101 const std::list<std::string>& theArguments,
102 Events_InfoMessage& theError) const
104 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
105 ResultParameterPtr aParam =
106 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
108 AttributeStringPtr aStrAttr =
109 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
110 if (!aStrAttr->isInitialized()) {
111 theError = "Attribute \"%1\" is not initialized.";
112 theError.arg(aStrAttr->id());
115 bool isEmptyExpr = aStrAttr->value().empty();
117 theError = "Expression is empty.";
122 theError = "Result is empty.";
126 theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
127 return theError.empty();