Salome HOME
Issue #1834: Fix length of lines
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_EvalListener.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2 /*
3  * ParametersPlugin_EvalListener.cpp
4  *
5  *  Created on: Apr 28, 2015
6  *      Author: sbh
7  */
8
9 #include <pyconfig.h>
10
11 #include <ParametersPlugin_EvalListener.h>
12 #include <ParametersPlugin_Parameter.h>
13 #include <ParametersPlugin_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 <QMessageBox>
31
32 #include <string>
33 #include <set>
34 #include <sstream>
35
36 //------------------------------------------------------------------------------
37 // Tools
38
39 std::string toStdString(double theValue)
40 {
41   std::ostringstream sstream;
42   sstream << theValue;
43   size_t aPos = sstream.str().find(".");
44   std::string aPnt = "";
45   if (aPos == std::string::npos)
46     aPnt = ".";
47   return sstream.str() + aPnt;
48 }
49
50 std::set<std::string> toSet(const std::list<std::string>& theContainer)
51 {
52   return std::set<std::string>(theContainer.begin(), theContainer.end());
53 }
54
55 //------------------------------------------------------------------------------
56
57 ParametersPlugin_EvalListener::ParametersPlugin_EvalListener()
58 {
59   Events_Loop* aLoop = Events_Loop::loop();
60
61   Events_ID anEvents_IDs[] = {
62       ModelAPI_AttributeEvalMessage::eventId(),
63       ModelAPI_ObjectRenamedMessage::eventId(),
64       ModelAPI_ReplaceParameterMessage::eventId()
65   };
66
67   for (int i = 0; i < sizeof(anEvents_IDs)/sizeof(anEvents_IDs[0]); ++i)
68     aLoop->registerListener(this, anEvents_IDs[i], NULL, true);
69
70   myInterp = std::shared_ptr<ParametersPlugin_PyInterp>(new ParametersPlugin_PyInterp());
71   myInterp->initialize();
72 }
73
74 ParametersPlugin_EvalListener::~ParametersPlugin_EvalListener()
75 {
76 }
77
78 void ParametersPlugin_EvalListener::processEvent(
79     const std::shared_ptr<Events_Message>& theMessage)
80 {
81   if (!theMessage.get())
82     return;
83
84   const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId();
85   const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
86   const Events_ID kReplaceParameterEvent = ModelAPI_ReplaceParameterMessage::eventId();
87
88   if (theMessage->eventID() == kEvaluationEvent) {
89     processEvaluationEvent(theMessage);
90   } else if (theMessage->eventID() == kObjectRenamedEvent) {
91     processObjectRenamedEvent(theMessage);
92   } else if (theMessage->eventID() == kReplaceParameterEvent) {
93     processReplaceParameterEvent(theMessage);
94   } else {
95     Events_InfoMessage("ParametersPlugin_EvalListener", 
96       "ParametersPlugin python interpreter, unhandled message caught: ")
97       .arg(theMessage->eventID().eventText()).send();
98   }
99 }
100
101 double ParametersPlugin_EvalListener::evaluate(FeaturePtr theParameter,
102   const std::string& theExpression, std::string& theError)
103 {
104   std::list<std::string> anExprParams = myInterp->compile(theExpression);
105   // find expression's params in the model
106   std::list<std::string> aContext;
107   std::list<std::string>::iterator it = anExprParams.begin();
108   for ( ; it != anExprParams.end(); it++) {
109     double aValue;
110     ResultParameterPtr aParamRes;
111     // If variable does not exist python interpreter will generate an error. It is OK.
112     // But due to the issue 1479 it should not check the history position of parameters relatively
113     // to feature that contains expression
114     if (!ModelAPI_Tools::findVariable(/*theParameter*/ FeaturePtr(), 
115       *it, aValue, aParamRes, theParameter->document()))
116       continue;
117
118     aContext.push_back(*it + "=" + toStdString(aValue));
119   }
120   myInterp->extendLocalContext(aContext);
121   double result = myInterp->evaluate(theExpression, theError);
122   myInterp->clearLocalContext();
123   return result;
124 }
125
126 void ParametersPlugin_EvalListener::processEvaluationEvent(
127     const std::shared_ptr<Events_Message>& theMessage)
128 {
129   std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
130       std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
131
132   FeaturePtr aParamFeature = 
133     std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
134   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
135     AttributeIntegerPtr anAttribute =
136         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
137     std::string anError;
138     int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError);
139     bool isValid = anError.empty();
140     if (isValid)
141       anAttribute->setCalculatedValue(aValue);
142     anAttribute->setUsedParameters(isValid ? 
143       toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
144     anAttribute->setExpressionInvalid(!isValid);
145     anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
146   } else
147   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
148     AttributeDoublePtr anAttribute =
149         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
150     std::string anError;
151     double aValue = evaluate(aParamFeature, anAttribute->text(), anError);
152     bool isValid = anError.empty();
153     if (isValid)
154       anAttribute->setCalculatedValue(aValue);
155     anAttribute->setUsedParameters(isValid ? 
156       toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
157     anAttribute->setExpressionInvalid(!isValid);
158     anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
159   } else
160   if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
161     AttributePointPtr anAttribute =
162         std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
163     std::string aText[] = {
164       anAttribute->textX(),
165       anAttribute->textY(),
166       anAttribute->textZ()
167     };
168     double aCalculatedValue[] = {
169       anAttribute->x(),
170       anAttribute->y(),
171       anAttribute->z()
172     };
173     for (int i = 0; i < 3; ++i) {
174       std::string anError;
175       double aValue = evaluate(aParamFeature, aText[i], anError);
176       bool isValid = anError.empty();
177       if (isValid) aCalculatedValue[i] = aValue;
178       anAttribute->setUsedParameters(i, 
179         isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
180       anAttribute->setExpressionInvalid(i, !isValid);
181       anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
182     }
183     anAttribute->setCalculatedValue(aCalculatedValue[0],
184                                     aCalculatedValue[1],
185                                     aCalculatedValue[2]);
186   } else
187   if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) {
188     AttributePoint2DPtr anAttribute =
189         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
190     std::string aText[] = {
191       anAttribute->textX(),
192       anAttribute->textY()
193     };
194     double aCalculatedValue[] = {
195       anAttribute->x(),
196       anAttribute->y()
197     };
198     for (int i = 0; i < 2; ++i) {
199       std::string anError;
200       double aValue = evaluate(aParamFeature, aText[i], anError);
201       bool isValid = anError.empty();
202       if (isValid) aCalculatedValue[i] = aValue;
203       anAttribute->setUsedParameters(i, 
204         isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
205       anAttribute->setExpressionInvalid(i, !isValid);
206       anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
207     }
208     anAttribute->setCalculatedValue(aCalculatedValue[0],
209                                     aCalculatedValue[1]);
210   }
211 }
212
213 std::string ParametersPlugin_EvalListener::renameInPythonExpression(
214     const std::string& theExpression,
215     const std::string& theOldName,
216     const std::string& theNewName)
217 {
218   std::string anExpressionString = theExpression;
219
220   std::list<std::pair<int, int> > aPositions =
221       myInterp->positions(anExpressionString, theOldName);
222
223   if (aPositions.empty())
224     return anExpressionString;
225
226   std::map<int, std::list<int> > aLines;
227   std::list<std::pair<int, int> >::const_iterator it = aPositions.begin();
228   for (; it != aPositions.end(); ++it)
229     aLines[it->first].push_back(it->second);
230
231   // Start renaming from the end to keep indexes if theNewName is longer then theOldName
232   std::map<int, std::list<int> >::const_reverse_iterator ritLine = aLines.rbegin();
233   for (; ritLine != aLines.rend(); ++ritLine) {
234     // Calculate the start of the line (find the aLineNo occurrence of "\n" )
235     int aLineNo = ritLine->first - 1;
236     size_t aLineStart = 0;
237     for (int i = 0; i < aLineNo; ++i)
238       aLineStart = anExpressionString.find("\n", aLineStart) + 1;
239
240     const std::list<int>& aColOffsets = ritLine->second;
241     std::list<int>::const_reverse_iterator ritOffset = aColOffsets.rbegin();
242     for (; ritOffset != aColOffsets.rend(); ++ritOffset) {
243       int anOffset = *ritOffset;
244       anExpressionString.replace(aLineStart + anOffset, theOldName.size(), theNewName);
245     }
246   }
247
248   return anExpressionString;
249 }
250
251 void ParametersPlugin_EvalListener::renameInParameter(
252     std::shared_ptr<ParametersPlugin_Parameter> theParameter,
253     const std::string& theOldName,
254     const std::string& theNewName)
255 {
256   std::shared_ptr<ModelAPI_AttributeString> anExpressionAttribute =
257       theParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID());
258
259   std::string anExpressionString = anExpressionAttribute->value();
260   anExpressionString = renameInPythonExpression(anExpressionString,
261                                                 theOldName,
262                                                 theNewName);
263   // Issue #588. No need for reevaluating expression. 
264   // Moreover, current history may not contain necessary parameters.
265   anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(true);
266   anExpressionAttribute->setValue(anExpressionString);
267   anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(false);
268 }
269
270 void ParametersPlugin_EvalListener::renameInAttribute(
271     std::shared_ptr<ModelAPI_Attribute> theAttribute,
272     const std::string& theOldName,
273     const std::string& theNewName)
274 {
275   if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
276     AttributeIntegerPtr anAttribute =
277         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
278     std::string anExpressionString = anAttribute->text();
279     anExpressionString = renameInPythonExpression(anExpressionString,
280                                                   theOldName, theNewName);
281     anAttribute->setText(anExpressionString);
282   } else
283   if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
284     AttributeDoublePtr anAttribute =
285         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
286     std::string anExpressionString = anAttribute->text();
287     anExpressionString = renameInPythonExpression(anExpressionString,
288                                                   theOldName, theNewName);
289     anAttribute->setText(anExpressionString);
290   } else
291   if (theAttribute->attributeType() == GeomDataAPI_Point::typeId()) {
292     AttributePointPtr anAttribute =
293         std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
294     std::string anExpressionString[3] = {
295       anAttribute->textX(),
296       anAttribute->textY(),
297       anAttribute->textZ()
298     };
299     for (int i = 0; i < 3; ++i)
300       anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
301                                                        theOldName, theNewName);
302     anAttribute->setText(anExpressionString[0],
303                          anExpressionString[1],
304                          anExpressionString[2]);
305   } else
306   if (theAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
307     AttributePoint2DPtr anAttribute =
308         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
309     std::string anExpressionString[2] = {
310       anAttribute->textX(),
311       anAttribute->textY()
312     };
313     for (int i = 0; i < 2; ++i)
314       anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
315                                                        theOldName, theNewName);
316     anAttribute->setText(anExpressionString[0],
317                          anExpressionString[1]);
318   }
319 }
320
321 void ParametersPlugin_EvalListener::renameInDependents(
322               std::shared_ptr<ModelAPI_ResultParameter> theResultParameter,
323               const std::string& theOldName,
324               const std::string& theNewName)
325 {
326   std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
327       theResultParameter->data()->refsToMe();
328   std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anAttributeIt =
329       anAttributes.cbegin();
330   for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) {
331     const AttributePtr& anAttribute = *anAttributeIt;
332     if (anAttribute->attributeType() == ModelAPI_AttributeRefList::typeId()) {
333       std::shared_ptr<ParametersPlugin_Parameter> aParameter =
334           std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
335               anAttribute->owner());
336       if (aParameter.get())
337         // Rename
338         renameInParameter(aParameter, theOldName, theNewName);
339     } else
340         // Rename
341         renameInAttribute(anAttribute, theOldName, theNewName);
342   }
343 }
344
345 bool isValidAttribute(const AttributePtr& theAttribute)
346 {
347   std::string aValidator;
348   Events_InfoMessage anError;
349   return ModelAPI_Session::get()->validators()->validate(theAttribute, aValidator, anError);
350 }
351
352 void setParameterName(ResultParameterPtr theResultParameter, const std::string& theName)
353 {
354   theResultParameter->data()->blockSendAttributeUpdated(true);
355   theResultParameter->data()->setName(theName);
356   theResultParameter->data()->blockSendAttributeUpdated(false, false);
357
358   std::shared_ptr<ParametersPlugin_Parameter> aParameter = 
359       std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
360           ModelAPI_Feature::feature(theResultParameter));
361
362   aParameter->data()->blockSendAttributeUpdated(true);
363   aParameter->data()->setName(theName);
364   aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(theName);
365   aParameter->data()->blockSendAttributeUpdated(false);
366 }
367
368 void ParametersPlugin_EvalListener::processObjectRenamedEvent(
369     const std::shared_ptr<Events_Message>& theMessage)
370 {
371   std::shared_ptr<ModelAPI_ObjectRenamedMessage> aMessage =
372       std::dynamic_pointer_cast<ModelAPI_ObjectRenamedMessage>(theMessage);
373
374   // Empty new name is not available too but it will be rejected by
375   // name validator in isValidAttribute.
376   if (!aMessage.get() || aMessage->oldName().empty())
377     return;
378
379   // check if the renamed object is a result parameter
380   ResultParameterPtr aResultParameter =
381       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aMessage->object());
382   if (!aResultParameter.get()) 
383     return;
384
385   // get parameter feature for the result
386   std::shared_ptr<ParametersPlugin_Parameter> aParameter =
387       std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
388           ModelAPI_Feature::feature(aResultParameter));
389   if (!aParameter.get())
390     return;
391
392   std::string aNotActivatedNames;
393   if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
394     QMessageBox::StandardButton aRes = QMessageBox::warning(0, QObject::tr("Warning"),
395                QObject::tr("Selected objects can be used in Part documents which are not loaded: \
396 %1. Whould you like to continue?").arg(aNotActivatedNames.c_str()),
397                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
398     if (aRes != QMessageBox::Yes) {
399       setParameterName(aResultParameter, aMessage->oldName());
400       return;
401     }
402   }
403
404   // try to update the parameter feature according the new name
405   setParameterName(aResultParameter, aMessage->newName());
406   // TODO(spo): replace with 
407   // ModelAPI_Session::get()->validators()->validate(aParameter, 
408   //                                                 ParametersPlugin_Parameter::VARIABLE_ID())
409   // when ModelAPI_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Feature>& theFeature,
410   //                                           const std::string& theAttribute) const
411   // is ready
412   if (!isValidAttribute(aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID()))) {
413     setParameterName(aResultParameter, aMessage->oldName());
414     return;
415   }
416
417   renameInDependents(aResultParameter, aMessage->oldName(), aMessage->newName());
418 }
419
420 void ParametersPlugin_EvalListener::processReplaceParameterEvent(
421     const std::shared_ptr<Events_Message>& theMessage)
422 {
423   std::shared_ptr<ModelAPI_ReplaceParameterMessage> aMessage =
424       std::dynamic_pointer_cast<ModelAPI_ReplaceParameterMessage>(theMessage);
425
426   // get parameter feature for the object
427   std::shared_ptr<ParametersPlugin_Parameter> aParameter =
428       std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
429           ModelAPI_Feature::feature(aMessage->object()));
430   if (!aParameter.get())
431     return;
432
433   ResultParameterPtr aResultParameter =
434       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(
435           aParameter->firstResult());
436   if (!aResultParameter.get())
437     return;
438
439   double aRealValue = aResultParameter->data()->real(ModelAPI_ResultParameter::VALUE())->value();
440   std::string aValue = toStdString(aRealValue);
441
442   renameInDependents(aResultParameter, aResultParameter->data()->name(), aValue);
443 }