1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: ParametersPlugin_Parameter.cpp
4 // Created: 23 MArch 2015
7 #include "ParametersPlugin_Parameter.h"
8 #include <ParametersPlugin_PyInterp.h>
10 #include <ModelAPI_AttributeString.h>
11 #include <ModelAPI_ResultParameter.h>
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_Tools.h>
18 ParametersPlugin_Parameter::ParametersPlugin_Parameter()
20 myInterp = new ParametersPlugin_PyInterp();
23 ParametersPlugin_Parameter::~ParametersPlugin_Parameter()
28 void ParametersPlugin_Parameter::initAttributes()
30 data()->addAttribute(ParametersPlugin_Parameter::VARIABLE_ID(),
31 ModelAPI_AttributeString::typeId());
32 data()->addAttribute(ParametersPlugin_Parameter::EXPRESSION_ID(),
33 ModelAPI_AttributeString::typeId());
36 bool ParametersPlugin_Parameter::isInHistory()
41 void ParametersPlugin_Parameter::attributeChanged(const std::string&)
43 ResultParameterPtr aParam = document()->createParameter(data());
45 std::string anExpression = string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
46 if(anExpression.empty()) {
47 // clear error/result if the expression is empty
51 std::string outErrorMessage;
52 double aValue = evaluate(anExpression, outErrorMessage);
54 std::string aName = string(ParametersPlugin_Parameter::VARIABLE_ID())->value();
55 std::ostringstream sstream;
57 std::string aParamValue = sstream.str();
58 data()->setName(aName + " ("+ aParamValue + ")");
59 aParam->data()->setName(aName);
61 std::string aStateMsg;
62 if (!outErrorMessage.empty()) {
63 aStateMsg = "Error: " + outErrorMessage;
64 data()->execState(ModelAPI_StateExecFailed);
66 setError(aStateMsg, false);
68 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
69 aValueAttribute->setValue(aValue);
73 void ParametersPlugin_Parameter::execute()
77 double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, std::string& theError)
79 myInterp->initialize();
80 std::list<std::string> anExprParams = myInterp->compile(theExpression);
81 // find expression's params in the model
82 std::list<std::string> aContext;
83 std::list<std::string>::iterator it = anExprParams.begin();
84 for ( ; it != anExprParams.end(); it++) {
86 if (!ModelAPI_Tools::findVariable(*it, aValue)) continue;
88 std::ostringstream sstream;
90 std::string aParamValue = sstream.str();
91 aContext.push_back(*it + "=" + aParamValue);
93 myInterp->extendLocalContext(aContext);
94 double result = myInterp->evaluate(theExpression, theError);