Salome HOME
bos #19884 Avoid error message in terminal if SALOME is running in terminal mode
[modules/shaper.git] / src / InitializationPlugin / InitializationPlugin_EvalListener.cpp
1 // Copyright (C) 2014-2020  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 <Locale_Convert.h>
29
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeInteger.h>
32 #include <ModelAPI_AttributeRefList.h>
33 #include <ModelAPI_AttributeString.h>
34 #include <ModelAPI_AttributeValidator.h>
35 #include <ModelAPI_Document.h>
36 #include <ModelAPI_Events.h>
37 #include <ModelAPI_ResultParameter.h>
38 #include <ModelAPI_Session.h>
39 #include <ModelAPI_Tools.h>
40
41 #include <GeomDataAPI_Point.h>
42 #include <GeomDataAPI_Point2D.h>
43
44 #include <string>
45 #include <set>
46 #include <sstream>
47
48 //------------------------------------------------------------------------------
49 // Tools
50
51 std::wstring toString(double theValue)
52 {
53   std::wostringstream sstream;
54   // write value in scientific format with 16 digits,
55   // thus, not check the dot position
56   sstream.precision(16);
57   sstream << std::scientific << theValue;
58   return sstream.str();
59 }
60
61 std::set<std::wstring> toSet(const std::list<std::wstring>& theContainer)
62 {
63   return std::set<std::wstring>(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     AttributeStringPtr anExprAttr = aParam->string(ParametersPlugin_Parameter::EXPRESSION_ID());
97     std::wstring anExp = anExprAttr->isUValue() ?
98         Locale::Convert::toWString(anExprAttr->valueU()) :
99         Locale::Convert::toWString(anExprAttr->value());
100     std::string anError;
101     std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
102     double aResult = evaluate(aParam, anExp, anError, aParamsList, true);
103     aMsg->setResults(aParamsList, aResult, anError);
104   } else if (theMessage->eventID() == ModelAPI_ComputePositionsMessage::eventId()) {
105     std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
106       std::dynamic_pointer_cast<ModelAPI_ComputePositionsMessage>(theMessage);
107     aMsg->setPositions(myInterp->positions(aMsg->expression(), aMsg->parameter()));
108   }
109 }
110
111 double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter,
112   const std::wstring& theExpression, std::string& theError,
113   std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
114   const bool theIsParameter)
115 {
116   std::list<std::wstring> anExprParams = myInterp->compile(theExpression);
117   // find expression's params in the model
118   std::list<std::wstring> aContext;
119   std::list<std::wstring>::iterator it = anExprParams.begin();
120   for ( ; it != anExprParams.end(); it++) {
121     double aValue;
122     ResultParameterPtr aParamRes;
123     // If variable does not exist python interpreter will generate an error. It is OK.
124     // But due to the issue 1479 it should not check the history position of parameters relatively
125     // to feature that contains expression
126     if (!ModelAPI_Tools::findVariable(theIsParameter ? theParameter : FeaturePtr(),
127       *it, aValue, aParamRes, theParameter->document()))
128       continue;
129
130     if (theIsParameter)
131       theParamsList.push_back(aParamRes);
132     aContext.push_back(*it + L"=" + toString(aValue));
133   }
134   myInterp->extendLocalContext(aContext);
135   double result = myInterp->evaluate(theExpression, theError);
136   myInterp->clearLocalContext();
137   return result;
138 }
139
140 void InitializationPlugin_EvalListener::processEvaluationEvent(
141     const std::shared_ptr<Events_Message>& theMessage)
142 {
143   std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
144       std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
145
146   std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
147   FeaturePtr aParamFeature =
148     std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
149   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
150     AttributeIntegerPtr anAttribute =
151         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
152     std::string anError;
153     int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
154     bool isValid = anError.empty();
155     if (isValid)
156       anAttribute->setCalculatedValue(aValue);
157     anAttribute->setUsedParameters(isValid ?
158       toSet(myInterp->compile(anAttribute->text())) : std::set<std::wstring>());
159     anAttribute->setExpressionInvalid(!isValid);
160     anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
161   } else
162   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
163     AttributeDoublePtr anAttribute =
164         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
165     std::string anError;
166     double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
167     bool isValid = anError.empty();
168     if (isValid)
169       anAttribute->setCalculatedValue(aValue);
170     anAttribute->setUsedParameters(isValid ?
171       toSet(myInterp->compile(anAttribute->text())) : std::set<std::wstring>());
172     anAttribute->setExpressionInvalid(!isValid);
173     anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
174   } else
175   if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
176     AttributePointPtr anAttribute =
177         std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
178     std::wstring aText[] = {
179       anAttribute->textX(),
180       anAttribute->textY(),
181       anAttribute->textZ()
182     };
183     double aCalculatedValue[] = {
184       anAttribute->x(),
185       anAttribute->y(),
186       anAttribute->z()
187     };
188     for (int i = 0; i < 3; ++i) {
189       std::string anError;
190       double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
191       bool isValid = anError.empty();
192       if (isValid) aCalculatedValue[i] = aValue;
193       anAttribute->setUsedParameters(i,
194         isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::wstring>());
195       anAttribute->setExpressionInvalid(i, !isValid);
196       anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
197     }
198     anAttribute->setCalculatedValue(aCalculatedValue[0],
199                                     aCalculatedValue[1],
200                                     aCalculatedValue[2]);
201   } else
202   if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) {
203     AttributePoint2DPtr anAttribute =
204         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
205     std::wstring aText[] = {
206       anAttribute->textX(),
207       anAttribute->textY()
208     };
209     double aCalculatedValue[] = {
210       anAttribute->x(),
211       anAttribute->y()
212     };
213     for (int i = 0; i < 2; ++i) {
214       std::string anError;
215       double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
216       bool isValid = anError.empty();
217       if (isValid) aCalculatedValue[i] = aValue;
218       anAttribute->setUsedParameters(i,
219         isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::wstring>());
220       anAttribute->setExpressionInvalid(i, !isValid);
221       anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
222     }
223     anAttribute->setCalculatedValue(aCalculatedValue[0],
224                                     aCalculatedValue[1]);
225   }
226 }
227
228 void InitializationPlugin_EvalListener::initDataModel()
229 {
230   myInterp->runString("import salome_iapp;salome_iapp.register_module_in_study(\"Shaper\")");
231 }