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 (!isVariable(aStrAttr->value())) {
31 theError = "Incorrect variable name.";
34 if (!isUnique(theAttribute, aStrAttr->value())) {
35 theError = "Variable name is not unique.";
41 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
43 if (theString.empty())
45 std::string::const_iterator it = theString.begin();
46 if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
49 for ( ; it != theString.end(); ++it ) {
50 if(!(isalnum(*it) || (*it) == '_')) {
57 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
58 const std::string& theString) const
60 DocumentPtr aDocument = theAttribute->owner()->document();
61 for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
62 anIndex < aSize; ++anIndex) {
63 ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
64 if (aParamObj->data()->name() != theString)
66 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
69 FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
70 if (aFeature == theAttribute->owner())
77 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
82 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
87 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
88 const std::list<std::string>& theArguments,
89 std::string& theError) const
91 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
92 ResultParameterPtr aParam =
93 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
95 AttributeStringPtr aStrAttr =
96 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
97 bool isEmptyExpr = aStrAttr->value().empty();
99 theError = "Expression is empty.";
104 theError = "Result is empty.";
108 theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
109 return theError.empty();