1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 * Parameters_VariableValidator.cpp
5 * Created on: Apr 9, 2015
9 #include <ParametersPlugin_Validators.h>
11 #include <ParametersPlugin_Parameter.h>
13 #include <Events_InfoMessage.h>
15 #include <ModelAPI_AttributeString.h>
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_ResultParameter.h>
18 #include <ModelAPI_Tools.h>
19 #include <ModelAPI_Expression.h>
21 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
25 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
29 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
30 const std::list<std::string>& theArguments,
31 Events_InfoMessage& theError) const
33 AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
34 if (!aStrAttr->isInitialized()) {
35 theError = "Attribute \"%1\" is not initialized.";
36 theError.arg(aStrAttr->id());
39 bool isEmptyExpr = aStrAttr->value().empty();
41 theError = "Attribute \"%1\" value is empty.";
42 theError.arg(aStrAttr->id());
45 if (!ModelAPI_Expression::isVariable(aStrAttr->value())) {
46 theError = "Incorrect variable name.";
49 if (!isUnique(theAttribute, aStrAttr->value())) {
50 theError = "Variable name is not unique.";
56 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
57 const std::string& theString) const
59 DocumentPtr aDocument = theAttribute->owner()->document();
60 for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
61 anIndex < aSize; ++anIndex) {
62 ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
63 if (aParamObj->data()->name() != theString)
65 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
68 FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
69 if (aFeature == theAttribute->owner())
76 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
81 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
86 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
87 const std::list<std::string>& theArguments,
88 Events_InfoMessage& theError) const
90 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
91 ResultParameterPtr aParam =
92 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
94 AttributeStringPtr aStrAttr =
95 std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
96 if (!aStrAttr->isInitialized()) {
97 theError = "Attribute \"%1\" is not initialized.";
98 theError.arg(aStrAttr->id());
101 bool isEmptyExpr = aStrAttr->value().empty();
103 theError = "Expression is empty.";
108 theError = "Result is empty.";
112 theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
113 return theError.empty();