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>
18 #include <ModelAPI_Expression.h>
20 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
24 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
28 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
29 const std::list<std::string>& theArguments,
30 Events_InfoMessage& theError) const
32 AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
33 if (!aStrAttr->isInitialized()) {
34 theError = "Attribute \"%1\" is not initialized.";
35 theError.arg(aStrAttr->id());
38 bool isEmptyExpr = aStrAttr->value().empty();
40 theError = "Attribute \"%1\" value is empty.";
41 theError.arg(aStrAttr->id());
44 if (!ModelAPI_Expression::isVariable(aStrAttr->value())) {
45 theError = "Incorrect variable name.";
48 if (!isUnique(theAttribute, aStrAttr->value())) {
49 theError = "Variable name is not unique.";
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 Events_InfoMessage& 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 if (!aStrAttr->isInitialized()) {
96 theError = "Attribute \"%1\" is not initialized.";
97 theError.arg(aStrAttr->id());
100 bool isEmptyExpr = aStrAttr->value().empty();
102 theError = "Expression is empty.";
107 theError = "Result is empty.";
111 theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
112 return theError.empty();