Salome HOME
Issue #1806 : evaluation of expressions if no parameters defined.
[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_Session.h>
25 #include <ModelAPI_Tools.h>
26
27 #include <GeomDataAPI_Point.h>
28 #include <GeomDataAPI_Point2D.h>
29
30 #include <string>
31 #include <set>
32 #include <sstream>
33
34 //------------------------------------------------------------------------------
35 // Tools
36
37 std::string toStdString(double theValue)
38 {
39   std::ostringstream sstream;
40   sstream << theValue;
41   size_t aPos = sstream.str().find(".");
42   std::string aPnt = "";
43   if (aPos == std::string::npos)
44     aPnt = ".";
45   return sstream.str() + aPnt;
46 }
47
48 std::set<std::string> toSet(const std::list<std::string>& theContainer)
49 {
50   return std::set<std::string>(theContainer.begin(), theContainer.end());
51 }
52
53 //------------------------------------------------------------------------------
54
55 InitializationPlugin_EvalListener::InitializationPlugin_EvalListener()
56 {
57   Events_Loop* aLoop = Events_Loop::loop();
58
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);
62
63   myInterp = std::shared_ptr<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
64   myInterp->initialize();
65 }
66
67 InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener()
68 {
69 }
70
71 void InitializationPlugin_EvalListener::processEvent(
72     const std::shared_ptr<Events_Message>& theMessage)
73 {
74   if (!theMessage.get())
75     return;
76
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();
84     std::string anError;
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()));
92   }
93 }
94
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)
99 {
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++) {
105     double aValue;
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()))
112       continue;
113
114     if (theIsParameter)
115       theParamsList.push_back(aParamRes);
116     aContext.push_back(*it + "=" + toStdString(aValue));
117   }
118   myInterp->extendLocalContext(aContext);
119   double result = myInterp->evaluate(theExpression, theError);
120   myInterp->clearLocalContext();
121   return result;
122 }
123
124 void InitializationPlugin_EvalListener::processEvaluationEvent(
125     const std::shared_ptr<Events_Message>& theMessage)
126 {
127   std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
128       std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
129
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());
136     std::string anError;
137     int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
138     bool isValid = anError.empty();
139     if (isValid)
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);
145   } else
146   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
147     AttributeDoublePtr anAttribute =
148         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
149     std::string anError;
150     double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
151     bool isValid = anError.empty();
152     if (isValid)
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);
158   } else
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(),
165       anAttribute->textZ()
166     };
167     double aCalculatedValue[] = {
168       anAttribute->x(),
169       anAttribute->y(),
170       anAttribute->z()
171     };
172     for (int i = 0; i < 3; ++i) {
173       std::string anError;
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);
181     }
182     anAttribute->setCalculatedValue(aCalculatedValue[0],
183                                     aCalculatedValue[1],
184                                     aCalculatedValue[2]);
185   } else
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(),
191       anAttribute->textY()
192     };
193     double aCalculatedValue[] = {
194       anAttribute->x(),
195       anAttribute->y()
196     };
197     for (int i = 0; i < 2; ++i) {
198       std::string anError;
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);
206     }
207     anAttribute->setCalculatedValue(aCalculatedValue[0],
208                                     aCalculatedValue[1]);
209   }
210 }