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