Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / InitializationPlugin / InitializationPlugin_EvalListener.cpp
1 // Copyright (C) 2014-2017  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<mailto:webmaster.salome@opencascade.com>
18 //
19
20 /*
21  * InitializationPlugin_EvalListener.cpp
22  *
23  *  Created on: Apr 28, 2015
24  *      Author: sbh
25  */
26
27 #include <pyconfig.h>
28
29 #include <InitializationPlugin_EvalListener.h>
30 #include <ParametersPlugin_Parameter.h>
31 #include <InitializationPlugin_PyInterp.h>
32
33 #include <Events_InfoMessage.h>
34
35 #include <ModelAPI_AttributeDouble.h>
36 #include <ModelAPI_AttributeInteger.h>
37 #include <ModelAPI_AttributeRefList.h>
38 #include <ModelAPI_AttributeString.h>
39 #include <ModelAPI_AttributeValidator.h>
40 #include <ModelAPI_Document.h>
41 #include <ModelAPI_Events.h>
42 #include <ModelAPI_ResultParameter.h>
43 #include <ModelAPI_Session.h>
44 #include <ModelAPI_Tools.h>
45
46 #include <GeomDataAPI_Point.h>
47 #include <GeomDataAPI_Point2D.h>
48
49 #include <string>
50 #include <set>
51 #include <sstream>
52
53 //------------------------------------------------------------------------------
54 // Tools
55
56 std::string toStdString(double theValue)
57 {
58   std::ostringstream sstream;
59   sstream << theValue;
60   size_t aPos = sstream.str().find(".");
61   std::string aPnt = "";
62   if (aPos == std::string::npos)
63     aPnt = ".";
64   return sstream.str() + aPnt;
65 }
66
67 std::set<std::string> toSet(const std::list<std::string>& theContainer)
68 {
69   return std::set<std::string>(theContainer.begin(), theContainer.end());
70 }
71
72 //------------------------------------------------------------------------------
73
74 InitializationPlugin_EvalListener::InitializationPlugin_EvalListener()
75 {
76   Events_Loop* aLoop = Events_Loop::loop();
77
78   aLoop->registerListener(this, ModelAPI_AttributeEvalMessage::eventId(), NULL, true);
79   aLoop->registerListener(this, ModelAPI_ParameterEvalMessage::eventId(), NULL, true);
80   aLoop->registerListener(this, ModelAPI_ComputePositionsMessage::eventId(), NULL, true);
81
82   myInterp = std::shared_ptr<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
83   myInterp->initialize();
84 }
85
86 InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener()
87 {
88 }
89
90 void InitializationPlugin_EvalListener::processEvent(
91     const std::shared_ptr<Events_Message>& theMessage)
92 {
93   if (!theMessage.get())
94     return;
95
96   if (theMessage->eventID() == ModelAPI_AttributeEvalMessage::eventId()) {
97     processEvaluationEvent(theMessage);
98   } else if (theMessage->eventID() == ModelAPI_ParameterEvalMessage::eventId()) {
99     std::shared_ptr<ModelAPI_ParameterEvalMessage> aMsg =
100       std::dynamic_pointer_cast<ModelAPI_ParameterEvalMessage>(theMessage);
101     FeaturePtr aParam = aMsg->parameter();
102     std::string anExp = aParam->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
103     std::string anError;
104     std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
105     double aResult = evaluate(aParam, anExp, anError, aParamsList, true);
106     aMsg->setResults(aParamsList, aResult, anError);
107   } else if (theMessage->eventID() == ModelAPI_ComputePositionsMessage::eventId()) {
108     std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
109       std::dynamic_pointer_cast<ModelAPI_ComputePositionsMessage>(theMessage);
110     aMsg->setPositions(myInterp->positions(aMsg->expression(), aMsg->parameter()));
111   }
112 }
113
114 double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter,
115   const std::string& theExpression, std::string& theError,
116   std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
117   const bool theIsParameter)
118 {
119   std::list<std::string> anExprParams = myInterp->compile(theExpression);
120   // find expression's params in the model
121   std::list<std::string> aContext;
122   std::list<std::string>::iterator it = anExprParams.begin();
123   for ( ; it != anExprParams.end(); it++) {
124     double aValue;
125     ResultParameterPtr aParamRes;
126     // If variable does not exist python interpreter will generate an error. It is OK.
127     // But due to the issue 1479 it should not check the history position of parameters relatively
128     // to feature that contains expression
129     if (!ModelAPI_Tools::findVariable(theIsParameter ? theParameter : FeaturePtr(),
130       *it, aValue, aParamRes, theParameter->document()))
131       continue;
132
133     if (theIsParameter)
134       theParamsList.push_back(aParamRes);
135     aContext.push_back(*it + "=" + toStdString(aValue));
136   }
137   myInterp->extendLocalContext(aContext);
138   double result = myInterp->evaluate(theExpression, theError);
139   myInterp->clearLocalContext();
140   return result;
141 }
142
143 void InitializationPlugin_EvalListener::processEvaluationEvent(
144     const std::shared_ptr<Events_Message>& theMessage)
145 {
146   std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
147       std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
148
149   std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
150   FeaturePtr aParamFeature =
151     std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
152   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
153     AttributeIntegerPtr anAttribute =
154         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
155     std::string anError;
156     int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
157     bool isValid = anError.empty();
158     if (isValid)
159       anAttribute->setCalculatedValue(aValue);
160     anAttribute->setUsedParameters(isValid ?
161       toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
162     anAttribute->setExpressionInvalid(!isValid);
163     anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
164   } else
165   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
166     AttributeDoublePtr anAttribute =
167         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
168     std::string anError;
169     double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
170     bool isValid = anError.empty();
171     if (isValid)
172       anAttribute->setCalculatedValue(aValue);
173     anAttribute->setUsedParameters(isValid ?
174       toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
175     anAttribute->setExpressionInvalid(!isValid);
176     anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
177   } else
178   if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
179     AttributePointPtr anAttribute =
180         std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
181     std::string aText[] = {
182       anAttribute->textX(),
183       anAttribute->textY(),
184       anAttribute->textZ()
185     };
186     double aCalculatedValue[] = {
187       anAttribute->x(),
188       anAttribute->y(),
189       anAttribute->z()
190     };
191     for (int i = 0; i < 3; ++i) {
192       std::string anError;
193       double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
194       bool isValid = anError.empty();
195       if (isValid) aCalculatedValue[i] = aValue;
196       anAttribute->setUsedParameters(i,
197         isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
198       anAttribute->setExpressionInvalid(i, !isValid);
199       anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
200     }
201     anAttribute->setCalculatedValue(aCalculatedValue[0],
202                                     aCalculatedValue[1],
203                                     aCalculatedValue[2]);
204   } else
205   if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) {
206     AttributePoint2DPtr anAttribute =
207         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
208     std::string aText[] = {
209       anAttribute->textX(),
210       anAttribute->textY()
211     };
212     double aCalculatedValue[] = {
213       anAttribute->x(),
214       anAttribute->y()
215     };
216     for (int i = 0; i < 2; ++i) {
217       std::string anError;
218       double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
219       bool isValid = anError.empty();
220       if (isValid) aCalculatedValue[i] = aValue;
221       anAttribute->setUsedParameters(i,
222         isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
223       anAttribute->setExpressionInvalid(i, !isValid);
224       anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
225     }
226     anAttribute->setCalculatedValue(aCalculatedValue[0],
227                                     aCalculatedValue[1]);
228   }
229 }