Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / InitializationPlugin / InitializationPlugin_EvalListener.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2 /*
3  * InitializationPlugin_EvalListener.cpp
4  *
5  *  Created on: Apr 28, 2015
6  *      Author: sbh
7  */
8
9 #include <pyconfig.h>
10
11 #include <InitializationPlugin_EvalListener.h>
12 #include <ParametersPlugin_Parameter.h>
13 #include <InitializationPlugin_PyInterp.h>
14
15 #include <Events_InfoMessage.h>
16
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>
27
28 #include <GeomDataAPI_Point.h>
29 #include <GeomDataAPI_Point2D.h>
30
31 #include <string>
32 #include <set>
33 #include <sstream>
34
35 //------------------------------------------------------------------------------
36 // Tools
37
38 std::string toStdString(double theValue)
39 {
40   std::ostringstream sstream;
41   sstream << theValue;
42   size_t aPos = sstream.str().find(".");
43   std::string aPnt = "";
44   if (aPos == std::string::npos)
45     aPnt = ".";
46   return sstream.str() + aPnt;
47 }
48
49 std::set<std::string> toSet(const std::list<std::string>& theContainer)
50 {
51   return std::set<std::string>(theContainer.begin(), theContainer.end());
52 }
53
54 //------------------------------------------------------------------------------
55
56 InitializationPlugin_EvalListener::InitializationPlugin_EvalListener()
57 {
58   Events_Loop* aLoop = Events_Loop::loop();
59
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);
63
64   myInterp = std::shared_ptr<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
65   myInterp->initialize();
66 }
67
68 InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener()
69 {
70 }
71
72 void InitializationPlugin_EvalListener::processEvent(
73     const std::shared_ptr<Events_Message>& theMessage)
74 {
75   if (!theMessage.get())
76     return;
77
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();
85     std::string anError;
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()));
93   }
94 }
95
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)
100 {
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++) {
106     double aValue;
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()))
113       continue;
114
115     if (theIsParameter)
116       theParamsList.push_back(aParamRes);
117     aContext.push_back(*it + "=" + toStdString(aValue));
118   }
119   myInterp->extendLocalContext(aContext);
120   double result = myInterp->evaluate(theExpression, theError);
121   myInterp->clearLocalContext();
122   return result;
123 }
124
125 void InitializationPlugin_EvalListener::processEvaluationEvent(
126     const std::shared_ptr<Events_Message>& theMessage)
127 {
128   std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
129       std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
130
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());
137     std::string anError;
138     int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
139     bool isValid = anError.empty();
140     if (isValid)
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);
146   } else
147   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
148     AttributeDoublePtr anAttribute =
149         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
150     std::string anError;
151     double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
152     bool isValid = anError.empty();
153     if (isValid)
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);
159   } else
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(),
166       anAttribute->textZ()
167     };
168     double aCalculatedValue[] = {
169       anAttribute->x(),
170       anAttribute->y(),
171       anAttribute->z()
172     };
173     for (int i = 0; i < 3; ++i) {
174       std::string anError;
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);
182     }
183     anAttribute->setCalculatedValue(aCalculatedValue[0],
184                                     aCalculatedValue[1],
185                                     aCalculatedValue[2]);
186   } else
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(),
192       anAttribute->textY()
193     };
194     double aCalculatedValue[] = {
195       anAttribute->x(),
196       anAttribute->y()
197     };
198     for (int i = 0; i < 2; ++i) {
199       std::string anError;
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);
207     }
208     anAttribute->setCalculatedValue(aCalculatedValue[0],
209                                     aCalculatedValue[1]);
210   }
211 }