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) const
26 AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
27 bool result = isVariable(aStrAttr->value()) && isUnique(theAttribute, aStrAttr->value());
31 bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const
33 if (theString.empty())
35 std::string::const_iterator it = theString.begin();
36 if (!(isalpha(*it) || (*it) == '_') || it == theString.end())
39 for ( ; it != theString.end(); ++it ) {
40 if(!(isalnum(*it) || (*it) == '_')) {
47 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
48 const std::string& theString) const
50 DocumentPtr aDocument = theAttribute->owner()->document();
51 for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
52 anIndex < aSize; ++anIndex) {
53 ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
54 if (aParamObj->data()->name() != theString)
56 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
59 FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
60 if (aFeature == theAttribute->owner())
67 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
72 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
77 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
78 const std::list<std::string>& theArguments) const
80 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
81 ResultParameterPtr aParam =
82 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
84 AttributeStringPtr aStrAttr =
85 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
86 bool isEmptyExpr = aStrAttr->value().empty();
93 return aFeature->error().empty();