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 bool result = isVariable(aStrAttr->value()) && isUnique(theAttribute, aStrAttr->value());
32 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
34 if (theString.empty())
36 std::string::const_iterator it = theString.begin();
37 if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
40 for ( ; it != theString.end(); ++it ) {
41 if(!(isalnum(*it) || (*it) == '_')) {
48 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
49 const std::string& theString) const
51 DocumentPtr aDocument = theAttribute->owner()->document();
52 for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
53 anIndex < aSize; ++anIndex) {
54 ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
55 if (aParamObj->data()->name() != theString)
57 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
60 FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
61 if (aFeature == theAttribute->owner())
68 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
73 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
78 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
79 const std::list<std::string>& theArguments,
80 std::string& theError) const
82 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
83 ResultParameterPtr aParam =
84 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
86 AttributeStringPtr aStrAttr =
87 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
88 bool isEmptyExpr = aStrAttr->value().empty();
95 return aFeature->error().empty();