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(EXPRESSION_ERROR_ID(), ModelAPI_AttributeString::typeId());
39 data()->string(EXPRESSION_ERROR_ID())->setIsArgument(false);
40 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXPRESSION_ERROR_ID());
42 data()->addAttribute(ARGUMENTS_ID(), ModelAPI_AttributeRefList::typeId());
43 data()->reflist(ARGUMENTS_ID())->setIsArgument(false);
44 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ARGUMENTS_ID());
47 bool ParametersPlugin_Parameter::isInHistory()
52 void ParametersPlugin_Parameter::attributeChanged(const std::string& theID)
54 if (theID == EXPRESSION_ID())
58 void ParametersPlugin_Parameter::updateName()
60 std::string aName = string(VARIABLE_ID())->value();
61 data()->setName(aName);
63 ResultParameterPtr aParam = document()->createParameter(data());
64 aParam->data()->setName(aName);
68 bool ParametersPlugin_Parameter::updateExpression()
70 std::string anExpression = string(EXPRESSION_ID())->value();
72 std::string outErrorMessage;
73 double aValue = evaluate(anExpression, outErrorMessage);
75 data()->string(EXPRESSION_ERROR_ID())->setValue(outErrorMessage);
76 if (!outErrorMessage.empty())
79 ResultParameterPtr aParam = document()->createParameter(data());
80 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
81 aValueAttribute->setValue(aValue);
86 void ParametersPlugin_Parameter::execute()
89 if (!updateExpression())
90 setError("Expression error.", false);
93 double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, std::string& theError)
95 std::list<std::string> anExprParams = myInterp->compile(theExpression);
96 // find expression's params in the model
97 std::list<std::string> aContext;
98 std::list<std::string>::iterator it = anExprParams.begin();
99 std::list<ResultParameterPtr> aParamsList;
100 for ( ; it != anExprParams.end(); it++) {
101 std::string& aVariableName = *it;
103 // parameter with the same name should be searched in the parent document
104 // currently there is no way to get parent document, so we get PartSet doc
105 DocumentPtr aDocument = document();
106 if (data()->name() == aVariableName)
107 aDocument = ModelAPI_Session::get()->moduleDocument();
110 ResultParameterPtr aParamRes;
111 if (!ModelAPI_Tools::findVariable(aVariableName, aValue, aParamRes, aDocument)) continue;
112 aParamsList.push_back(aParamRes);
114 std::ostringstream sstream;
116 std::string aParamValue = sstream.str();
117 aContext.push_back(*it + "=" + aParamValue);
119 // compare the list of parameters to store if changed
120 AttributeRefListPtr aParams = reflist(ARGUMENTS_ID());
121 bool aDifferent = aParams->size() != aParamsList.size();
123 std::list<ResultParameterPtr>::iterator aNewIter = aParamsList.begin();
124 std::list<ObjectPtr> anOldList = aParams->list();
125 std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
126 for(; !aDifferent && aNewIter != aParamsList.end(); aNewIter++, anOldIter++) {
127 if (*aNewIter != *anOldIter)
133 std::list<ResultParameterPtr>::iterator aNewIter = aParamsList.begin();
134 for(; aNewIter != aParamsList.end(); aNewIter++) {
135 aParams->append(*aNewIter);
139 myInterp->extendLocalContext(aContext);
140 double result = myInterp->evaluate(theExpression, theError);
141 myInterp->clearLocalContext();
145 bool ParametersPlugin_Parameter::isPreviewNeeded() const