1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 * InitializationPlugin_EvalListener.cpp
5 * Created on: Apr 28, 2015
11 #include <InitializationPlugin_EvalListener.h>
12 #include <ParametersPlugin_Parameter.h>
13 #include <InitializationPlugin_PyInterp.h>
15 #include <Events_InfoMessage.h>
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_AttributeInteger.h>
19 #include <ModelAPI_AttributeRefList.h>
20 #include <ModelAPI_AttributeString.h>
21 #include <ModelAPI_AttributeValidator.h>
22 #include <ModelAPI_Document.h>
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_ResultParameter.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Tools.h>
28 #include <GeomDataAPI_Point.h>
29 #include <GeomDataAPI_Point2D.h>
35 //------------------------------------------------------------------------------
38 std::string toStdString(double theValue)
40 std::ostringstream sstream;
42 size_t aPos = sstream.str().find(".");
43 std::string aPnt = "";
44 if (aPos == std::string::npos)
46 return sstream.str() + aPnt;
49 std::set<std::string> toSet(const std::list<std::string>& theContainer)
51 return std::set<std::string>(theContainer.begin(), theContainer.end());
54 //------------------------------------------------------------------------------
56 InitializationPlugin_EvalListener::InitializationPlugin_EvalListener()
58 Events_Loop* aLoop = Events_Loop::loop();
60 aLoop->registerListener(this, ModelAPI_AttributeEvalMessage::eventId(), NULL, true);
61 aLoop->registerListener(this, ModelAPI_ParameterEvalMessage::eventId(), NULL, true);
62 aLoop->registerListener(this, ModelAPI_ComputePositionsMessage::eventId(), NULL, true);
64 myInterp = std::shared_ptr<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
65 myInterp->initialize();
68 InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener()
72 void InitializationPlugin_EvalListener::processEvent(
73 const std::shared_ptr<Events_Message>& theMessage)
75 if (!theMessage.get())
78 if (theMessage->eventID() == ModelAPI_AttributeEvalMessage::eventId()) {
79 processEvaluationEvent(theMessage);
80 } else if (theMessage->eventID() == ModelAPI_ParameterEvalMessage::eventId()) {
81 std::shared_ptr<ModelAPI_ParameterEvalMessage> aMsg =
82 std::dynamic_pointer_cast<ModelAPI_ParameterEvalMessage>(theMessage);
83 FeaturePtr aParam = aMsg->parameter();
84 std::string anExp = aParam->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
86 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
87 double aResult = evaluate(aParam, anExp, anError, aParamsList, true);
88 aMsg->setResults(aParamsList, aResult, anError);
89 } else if (theMessage->eventID() == ModelAPI_ComputePositionsMessage::eventId()) {
90 std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
91 std::dynamic_pointer_cast<ModelAPI_ComputePositionsMessage>(theMessage);
92 aMsg->setPositions(myInterp->positions(aMsg->expression(), aMsg->parameter()));
96 double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter,
97 const std::string& theExpression, std::string& theError,
98 std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
99 const bool theIsParameter)
101 std::list<std::string> anExprParams = myInterp->compile(theExpression);
102 // find expression's params in the model
103 std::list<std::string> aContext;
104 std::list<std::string>::iterator it = anExprParams.begin();
105 for ( ; it != anExprParams.end(); it++) {
107 ResultParameterPtr aParamRes;
108 // If variable does not exist python interpreter will generate an error. It is OK.
109 // But due to the issue 1479 it should not check the history position of parameters relatively
110 // to feature that contains expression
111 if (!ModelAPI_Tools::findVariable(theIsParameter ? theParameter : FeaturePtr(),
112 *it, aValue, aParamRes, theParameter->document()))
116 theParamsList.push_back(aParamRes);
117 aContext.push_back(*it + "=" + toStdString(aValue));
119 myInterp->extendLocalContext(aContext);
120 double result = myInterp->evaluate(theExpression, theError);
121 myInterp->clearLocalContext();
125 void InitializationPlugin_EvalListener::processEvaluationEvent(
126 const std::shared_ptr<Events_Message>& theMessage)
128 std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
129 std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
131 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
132 FeaturePtr aParamFeature =
133 std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
134 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
135 AttributeIntegerPtr anAttribute =
136 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
138 int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
139 bool isValid = anError.empty();
141 anAttribute->setCalculatedValue(aValue);
142 anAttribute->setUsedParameters(isValid ?
143 toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
144 anAttribute->setExpressionInvalid(!isValid);
145 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
147 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
148 AttributeDoublePtr anAttribute =
149 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
151 double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
152 bool isValid = anError.empty();
154 anAttribute->setCalculatedValue(aValue);
155 anAttribute->setUsedParameters(isValid ?
156 toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
157 anAttribute->setExpressionInvalid(!isValid);
158 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
160 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
161 AttributePointPtr anAttribute =
162 std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
163 std::string aText[] = {
164 anAttribute->textX(),
165 anAttribute->textY(),
168 double aCalculatedValue[] = {
173 for (int i = 0; i < 3; ++i) {
175 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
176 bool isValid = anError.empty();
177 if (isValid) aCalculatedValue[i] = aValue;
178 anAttribute->setUsedParameters(i,
179 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
180 anAttribute->setExpressionInvalid(i, !isValid);
181 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
183 anAttribute->setCalculatedValue(aCalculatedValue[0],
185 aCalculatedValue[2]);
187 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) {
188 AttributePoint2DPtr anAttribute =
189 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
190 std::string aText[] = {
191 anAttribute->textX(),
194 double aCalculatedValue[] = {
198 for (int i = 0; i < 2; ++i) {
200 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
201 bool isValid = anError.empty();
202 if (isValid) aCalculatedValue[i] = aValue;
203 anAttribute->setUsedParameters(i,
204 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
205 anAttribute->setExpressionInvalid(i, !isValid);
206 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
208 anAttribute->setCalculatedValue(aCalculatedValue[0],
209 aCalculatedValue[1]);