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_Session.h>
25 #include <ModelAPI_Tools.h>
27 #include <GeomDataAPI_Point.h>
28 #include <GeomDataAPI_Point2D.h>
34 //------------------------------------------------------------------------------
37 std::string toStdString(double theValue)
39 std::ostringstream sstream;
41 size_t aPos = sstream.str().find(".");
42 std::string aPnt = "";
43 if (aPos == std::string::npos)
45 return sstream.str() + aPnt;
48 std::set<std::string> toSet(const std::list<std::string>& theContainer)
50 return std::set<std::string>(theContainer.begin(), theContainer.end());
53 //------------------------------------------------------------------------------
55 InitializationPlugin_EvalListener::InitializationPlugin_EvalListener()
57 Events_Loop* aLoop = Events_Loop::loop();
59 aLoop->registerListener(this, ModelAPI_AttributeEvalMessage::eventId(), NULL, true);
60 aLoop->registerListener(this, ModelAPI_ParameterEvalMessage::eventId(), NULL, true);
61 aLoop->registerListener(this, ModelAPI_ComputePositionsMessage::eventId(), NULL, true);
63 myInterp = std::shared_ptr<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
64 myInterp->initialize();
67 InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener()
71 void InitializationPlugin_EvalListener::processEvent(
72 const std::shared_ptr<Events_Message>& theMessage)
74 if (!theMessage.get())
77 if (theMessage->eventID() == ModelAPI_AttributeEvalMessage::eventId()) {
78 processEvaluationEvent(theMessage);
79 } else if (theMessage->eventID() == ModelAPI_ParameterEvalMessage::eventId()) {
80 std::shared_ptr<ModelAPI_ParameterEvalMessage> aMsg =
81 std::dynamic_pointer_cast<ModelAPI_ParameterEvalMessage>(theMessage);
82 FeaturePtr aParam = aMsg->parameter();
83 std::string anExp = aParam->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
85 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
86 double aResult = evaluate(aParam, anExp, anError, aParamsList, true);
87 aMsg->setResults(aParamsList, aResult, anError);
88 } else if (theMessage->eventID() == ModelAPI_ComputePositionsMessage::eventId()) {
89 std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
90 std::dynamic_pointer_cast<ModelAPI_ComputePositionsMessage>(theMessage);
91 aMsg->setPositions(myInterp->positions(aMsg->expression(), aMsg->parameter()));
95 double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter,
96 const std::string& theExpression, std::string& theError,
97 std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
98 const bool theIsParameter)
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 for ( ; it != anExprParams.end(); it++) {
106 ResultParameterPtr aParamRes;
107 // If variable does not exist python interpreter will generate an error. It is OK.
108 // But due to the issue 1479 it should not check the history position of parameters relatively
109 // to feature that contains expression
110 if (!ModelAPI_Tools::findVariable(theIsParameter ? theParameter : FeaturePtr(),
111 *it, aValue, aParamRes, theParameter->document()))
115 theParamsList.push_back(aParamRes);
116 aContext.push_back(*it + "=" + toStdString(aValue));
118 myInterp->extendLocalContext(aContext);
119 double result = myInterp->evaluate(theExpression, theError);
120 myInterp->clearLocalContext();
124 void InitializationPlugin_EvalListener::processEvaluationEvent(
125 const std::shared_ptr<Events_Message>& theMessage)
127 std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
128 std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
130 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
131 FeaturePtr aParamFeature =
132 std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
133 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
134 AttributeIntegerPtr anAttribute =
135 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
137 int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
138 bool isValid = anError.empty();
140 anAttribute->setCalculatedValue(aValue);
141 anAttribute->setUsedParameters(isValid ?
142 toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
143 anAttribute->setExpressionInvalid(!isValid);
144 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
146 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
147 AttributeDoublePtr anAttribute =
148 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
150 double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
151 bool isValid = anError.empty();
153 anAttribute->setCalculatedValue(aValue);
154 anAttribute->setUsedParameters(isValid ?
155 toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
156 anAttribute->setExpressionInvalid(!isValid);
157 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
159 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
160 AttributePointPtr anAttribute =
161 std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
162 std::string aText[] = {
163 anAttribute->textX(),
164 anAttribute->textY(),
167 double aCalculatedValue[] = {
172 for (int i = 0; i < 3; ++i) {
174 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
175 bool isValid = anError.empty();
176 if (isValid) aCalculatedValue[i] = aValue;
177 anAttribute->setUsedParameters(i,
178 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
179 anAttribute->setExpressionInvalid(i, !isValid);
180 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
182 anAttribute->setCalculatedValue(aCalculatedValue[0],
184 aCalculatedValue[2]);
186 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) {
187 AttributePoint2DPtr anAttribute =
188 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
189 std::string aText[] = {
190 anAttribute->textX(),
193 double aCalculatedValue[] = {
197 for (int i = 0; i < 2; ++i) {
199 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
200 bool isValid = anError.empty();
201 if (isValid) aCalculatedValue[i] = aValue;
202 anAttribute->setUsedParameters(i,
203 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
204 anAttribute->setExpressionInvalid(i, !isValid);
205 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
207 anAttribute->setCalculatedValue(aCalculatedValue[0],
208 aCalculatedValue[1]);