Salome HOME
Update copyrights
[modules/shaper.git] / src / InitializationPlugin / InitializationPlugin_EvalListener.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <pyconfig.h>
21
22 #include <InitializationPlugin_EvalListener.h>
23 #include <ParametersPlugin_Parameter.h>
24 #include <InitializationPlugin_PyInterp.h>
25
26 #include <Events_InfoMessage.h>
27
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_AttributeInteger.h>
30 #include <ModelAPI_AttributeRefList.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_AttributeValidator.h>
33 #include <ModelAPI_Document.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_ResultParameter.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Tools.h>
38
39 #include <GeomDataAPI_Point.h>
40 #include <GeomDataAPI_Point2D.h>
41
42 #include <string>
43 #include <set>
44 #include <sstream>
45
46 //------------------------------------------------------------------------------
47 // Tools
48
49 std::string toStdString(double theValue)
50 {
51   std::ostringstream sstream;
52   sstream << theValue;
53   size_t aPos = sstream.str().find(".");
54   std::string aPnt = "";
55   if (aPos == std::string::npos)
56     aPnt = ".";
57   return sstream.str() + aPnt;
58 }
59
60 std::set<std::string> toSet(const std::list<std::string>& theContainer)
61 {
62   return std::set<std::string>(theContainer.begin(), theContainer.end());
63 }
64
65 //------------------------------------------------------------------------------
66
67 InitializationPlugin_EvalListener::InitializationPlugin_EvalListener()
68 {
69   Events_Loop* aLoop = Events_Loop::loop();
70
71   aLoop->registerListener(this, ModelAPI_AttributeEvalMessage::eventId(), NULL, true);
72   aLoop->registerListener(this, ModelAPI_ParameterEvalMessage::eventId(), NULL, true);
73   aLoop->registerListener(this, ModelAPI_ComputePositionsMessage::eventId(), NULL, true);
74
75   myInterp = std::shared_ptr<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
76   myInterp->initialize();
77 }
78
79 InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener()
80 {
81 }
82
83 void InitializationPlugin_EvalListener::processEvent(
84     const std::shared_ptr<Events_Message>& theMessage)
85 {
86   if (!theMessage.get())
87     return;
88
89   if (theMessage->eventID() == ModelAPI_AttributeEvalMessage::eventId()) {
90     processEvaluationEvent(theMessage);
91   } else if (theMessage->eventID() == ModelAPI_ParameterEvalMessage::eventId()) {
92     std::shared_ptr<ModelAPI_ParameterEvalMessage> aMsg =
93       std::dynamic_pointer_cast<ModelAPI_ParameterEvalMessage>(theMessage);
94     FeaturePtr aParam = aMsg->parameter();
95     std::string anExp = aParam->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
96     std::string anError;
97     std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
98     double aResult = evaluate(aParam, anExp, anError, aParamsList, true);
99     aMsg->setResults(aParamsList, aResult, anError);
100   } else if (theMessage->eventID() == ModelAPI_ComputePositionsMessage::eventId()) {
101     std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
102       std::dynamic_pointer_cast<ModelAPI_ComputePositionsMessage>(theMessage);
103     aMsg->setPositions(myInterp->positions(aMsg->expression(), aMsg->parameter()));
104   }
105 }
106
107 double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter,
108   const std::string& theExpression, std::string& theError,
109   std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
110   const bool theIsParameter)
111 {
112   std::list<std::string> anExprParams = myInterp->compile(theExpression);
113   // find expression's params in the model
114   std::list<std::string> aContext;
115   std::list<std::string>::iterator it = anExprParams.begin();
116   for ( ; it != anExprParams.end(); it++) {
117     double aValue;
118     ResultParameterPtr aParamRes;
119     // If variable does not exist python interpreter will generate an error. It is OK.
120     // But due to the issue 1479 it should not check the history position of parameters relatively
121     // to feature that contains expression
122     if (!ModelAPI_Tools::findVariable(theIsParameter ? theParameter : FeaturePtr(),
123       *it, aValue, aParamRes, theParameter->document()))
124       continue;
125
126     if (theIsParameter)
127       theParamsList.push_back(aParamRes);
128     aContext.push_back(*it + "=" + toStdString(aValue));
129   }
130   myInterp->extendLocalContext(aContext);
131   double result = myInterp->evaluate(theExpression, theError);
132   myInterp->clearLocalContext();
133   return result;
134 }
135
136 void InitializationPlugin_EvalListener::processEvaluationEvent(
137     const std::shared_ptr<Events_Message>& theMessage)
138 {
139   std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
140       std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
141
142   std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
143   FeaturePtr aParamFeature =
144     std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
145   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
146     AttributeIntegerPtr anAttribute =
147         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
148     std::string anError;
149     int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
150     bool isValid = anError.empty();
151     if (isValid)
152       anAttribute->setCalculatedValue(aValue);
153     anAttribute->setUsedParameters(isValid ?
154       toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
155     anAttribute->setExpressionInvalid(!isValid);
156     anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
157   } else
158   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
159     AttributeDoublePtr anAttribute =
160         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
161     std::string anError;
162     double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
163     bool isValid = anError.empty();
164     if (isValid)
165       anAttribute->setCalculatedValue(aValue);
166     anAttribute->setUsedParameters(isValid ?
167       toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
168     anAttribute->setExpressionInvalid(!isValid);
169     anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
170   } else
171   if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
172     AttributePointPtr anAttribute =
173         std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
174     std::string aText[] = {
175       anAttribute->textX(),
176       anAttribute->textY(),
177       anAttribute->textZ()
178     };
179     double aCalculatedValue[] = {
180       anAttribute->x(),
181       anAttribute->y(),
182       anAttribute->z()
183     };
184     for (int i = 0; i < 3; ++i) {
185       std::string anError;
186       double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
187       bool isValid = anError.empty();
188       if (isValid) aCalculatedValue[i] = aValue;
189       anAttribute->setUsedParameters(i,
190         isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
191       anAttribute->setExpressionInvalid(i, !isValid);
192       anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
193     }
194     anAttribute->setCalculatedValue(aCalculatedValue[0],
195                                     aCalculatedValue[1],
196                                     aCalculatedValue[2]);
197   } else
198   if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) {
199     AttributePoint2DPtr anAttribute =
200         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
201     std::string aText[] = {
202       anAttribute->textX(),
203       anAttribute->textY()
204     };
205     double aCalculatedValue[] = {
206       anAttribute->x(),
207       anAttribute->y()
208     };
209     for (int i = 0; i < 2; ++i) {
210       std::string anError;
211       double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
212       bool isValid = anError.empty();
213       if (isValid) aCalculatedValue[i] = aValue;
214       anAttribute->setUsedParameters(i,
215         isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
216       anAttribute->setExpressionInvalid(i, !isValid);
217       anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
218     }
219     anAttribute->setCalculatedValue(aCalculatedValue[0],
220                                     aCalculatedValue[1]);
221   }
222 }