1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: ParametersPlugin_Parameter.cpp
4 // Created: 23 MArch 2015
9 #include "ParametersPlugin_Parameter.h"
10 #include <ParametersPlugin_PyInterp.h>
12 #include <ModelAPI_AttributeString.h>
13 #include <ModelAPI_ResultParameter.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeRefList.h>
16 #include <ModelAPI_Tools.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_Validator.h>
23 ParametersPlugin_Parameter::ParametersPlugin_Parameter()
25 myInterp = std::shared_ptr<ParametersPlugin_PyInterp>(new ParametersPlugin_PyInterp());
26 myInterp->initialize();
29 ParametersPlugin_Parameter::~ParametersPlugin_Parameter()
33 void ParametersPlugin_Parameter::initAttributes()
35 data()->addAttribute(VARIABLE_ID(), ModelAPI_AttributeString::typeId());
36 data()->addAttribute(EXPRESSION_ID(), ModelAPI_AttributeString::typeId());
38 data()->addAttribute(COMMENT_ID(), ModelAPI_AttributeString::typeId());
39 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), COMMENT_ID());
41 data()->addAttribute(EXPRESSION_ERROR_ID(), ModelAPI_AttributeString::typeId());
42 data()->string(EXPRESSION_ERROR_ID())->setIsArgument(false);
43 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXPRESSION_ERROR_ID());
45 data()->addAttribute(ARGUMENTS_ID(), ModelAPI_AttributeRefList::typeId());
46 data()->reflist(ARGUMENTS_ID())->setIsArgument(false);
47 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ARGUMENTS_ID());
50 bool ParametersPlugin_Parameter::isInHistory()
55 void ParametersPlugin_Parameter::attributeChanged(const std::string& theID)
57 if (theID == EXPRESSION_ID())
60 data()->execState(ModelAPI_StateMustBeUpdated);
63 void ParametersPlugin_Parameter::updateName()
65 std::string aName = string(VARIABLE_ID())->value();
66 data()->setName(aName);
68 ResultParameterPtr aParam = document()->createParameter(data());
69 aParam->data()->setName(aName);
73 bool ParametersPlugin_Parameter::updateExpression()
75 std::string anExpression = string(EXPRESSION_ID())->value();
77 std::string outErrorMessage;
78 double aValue = evaluate(anExpression, outErrorMessage);
80 data()->string(EXPRESSION_ERROR_ID())->setValue(outErrorMessage);
81 if (!outErrorMessage.empty())
84 ResultParameterPtr aParam = document()->createParameter(data());
85 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
86 aValueAttribute->setValue(aValue);
91 void ParametersPlugin_Parameter::execute()
94 if (!updateExpression())
95 setError("Expression error.", false);
98 double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, std::string& theError)
100 std::list<std::string> anExprParams = myInterp->compile(theExpression);
101 // find expression's params in the model
102 std::list<std::string> aContext;
103 std::list<std::string>::iterator it = anExprParams.begin();
104 std::list<ResultParameterPtr> aParamsList;
105 for ( ; it != anExprParams.end(); it++) {
106 std::string& aVariableName = *it;
108 // Parameter with the same name should be searched in the parent document.
109 // For the PartSet assume that the parameter is absent.
110 // Currently there is no way to get parent document, so we get PartSet for all.
111 DocumentPtr aDocument = document();
112 if (data()->name() == aVariableName) {
113 if (aDocument == ModelAPI_Session::get()->moduleDocument())
115 aDocument = ModelAPI_Session::get()->moduleDocument();
119 ResultParameterPtr aParamRes;
120 if (!ModelAPI_Tools::findVariable(std::dynamic_pointer_cast<ModelAPI_Feature>(data()->owner()),
121 aVariableName, aValue, aParamRes, aDocument)) continue;
122 aParamsList.push_back(aParamRes);
124 std::ostringstream sstream;
126 std::string aParamValue = sstream.str();
127 size_t aPos = aParamValue.find(".");
128 std::string aPnt = "";
129 if (aPos == std::string::npos)
131 aContext.push_back(*it + "=" + aParamValue + aPnt);
133 // compare the list of parameters to store if changed
134 AttributeRefListPtr aParams = reflist(ARGUMENTS_ID());
135 bool aDifferent = aParams->size() != aParamsList.size();
137 std::list<ResultParameterPtr>::iterator aNewIter = aParamsList.begin();
138 std::list<ObjectPtr> anOldList = aParams->list();
139 std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
140 for(; !aDifferent && aNewIter != aParamsList.end(); aNewIter++, anOldIter++) {
141 if (*aNewIter != *anOldIter)
147 std::list<ResultParameterPtr>::iterator aNewIter = aParamsList.begin();
148 for(; aNewIter != aParamsList.end(); aNewIter++) {
149 aParams->append(*aNewIter);
153 myInterp->extendLocalContext(aContext);
154 double result = myInterp->evaluate(theExpression, theError);
155 myInterp->clearLocalContext();
159 bool ParametersPlugin_Parameter::isPreviewNeeded() const