2 * Parameters_VariableValidator.cpp
4 * Created on: Apr 9, 2015
8 #include <ParametersPlugin_Validators.h>
10 #include <ModelAPI_AttributeString.h>
11 #include <ModelAPI_Feature.h>
12 #include <ModelAPI_ResultParameter.h>
13 #include <ModelAPI_Tools.h>
15 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
19 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
23 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
24 const std::list<std::string>& theArguments,
25 std::string& theError) const
27 AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
28 if (!isVariable(aStrAttr->value())) {
29 theError = "Incorrect variable name.";
32 if (!isUnique(theAttribute, aStrAttr->value())) {
33 theError = "Variable name is not unique.";
39 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
41 if (theString.empty())
43 std::string::const_iterator it = theString.begin();
44 if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
47 for ( ; it != theString.end(); ++it ) {
48 if(!(isalnum(*it) || (*it) == '_')) {
55 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
56 const std::string& theString) const
58 DocumentPtr aDocument = theAttribute->owner()->document();
59 for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
60 anIndex < aSize; ++anIndex) {
61 ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
62 if (aParamObj->data()->name() != theString)
64 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
67 FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
68 if (aFeature == theAttribute->owner())
75 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
80 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
85 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
86 const std::list<std::string>& theArguments,
87 std::string& theError) const
89 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
90 ResultParameterPtr aParam =
91 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
93 AttributeStringPtr aStrAttr =
94 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
95 bool isEmptyExpr = aStrAttr->value().empty();
97 theError = "Expression is empty.";
102 theError = "Result is empty.";
106 theError = aFeature->error();
107 return aFeature->error().empty();