Salome HOME
Issue #588 - Renaming a parameter used in an expression
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_EvalListener.cpp
1 /*
2  * ParametersPlugin_EvalListener.cpp
3  *
4  *  Created on: Apr 28, 2015
5  *      Author: sbh
6  */
7
8 #include <pyconfig.h>
9
10 #include <ParametersPlugin_EvalListener.h>
11 #include <ParametersPlugin_Parameter.h>
12 #include <ParametersPlugin_PyInterp.h>
13
14 #include <Events_Error.h>
15
16 #include <ModelAPI_AttributeString.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Tools.h>
21
22 #include <ModelAPI_AttributeDouble.h>
23 #include <GeomDataAPI_Point.h>
24 #include <GeomDataAPI_Point2D.h>
25
26 #include <string>
27 #include <sstream>
28
29 ParametersPlugin_EvalListener::ParametersPlugin_EvalListener()
30 {
31   Events_Loop* aLoop = Events_Loop::loop();
32   const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId();
33   aLoop->registerListener(this, kEvaluationEvent, NULL, true);
34   const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
35   aLoop->registerListener(this, kObjectRenamedEvent, NULL, true);
36
37   myInterp = std::shared_ptr<ParametersPlugin_PyInterp>(new ParametersPlugin_PyInterp());
38   myInterp->initialize();
39 }
40
41 ParametersPlugin_EvalListener::~ParametersPlugin_EvalListener()
42 {
43 }
44
45 void ParametersPlugin_EvalListener::processEvent(
46     const std::shared_ptr<Events_Message>& theMessage)
47 {
48   if (!theMessage.get())
49     return;
50
51   const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId();
52   const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
53   if (theMessage->eventID() == kEvaluationEvent) {
54     processEvaluationEvent(theMessage);
55   } else if (theMessage->eventID() == kObjectRenamedEvent) {
56     processObjectRenamedEvent(theMessage);
57   } else {
58     Events_Error::send(std::string("ParametersPlugin python interpreter, unhandled message caught: ")
59                        + theMessage->eventID().eventText());
60   }
61 }
62
63 double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression,
64                                                std::string& theError)
65 {
66   std::list<std::string> anExprParams = myInterp->compile(theExpression);
67   // find expression's params in the model
68   std::list<std::string> aContext;
69   std::list<std::string>::iterator it = anExprParams.begin();
70   for ( ; it != anExprParams.end(); it++) {
71     double aValue;
72     ResultParameterPtr aParamRes;
73     if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes)) continue;
74
75     std::ostringstream sstream;
76     sstream << aValue;
77     std::string aParamValue = sstream.str();
78     aContext.push_back(*it + "=" + aParamValue);
79   }
80   myInterp->extendLocalContext(aContext);
81   double result = myInterp->evaluate(theExpression, theError);
82   myInterp->clearLocalContext();
83   return result;
84 }
85
86 void ParametersPlugin_EvalListener::processEvaluationEvent(
87     const std::shared_ptr<Events_Message>& theMessage)
88 {
89   std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
90       std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
91
92   // Double
93   AttributeDoublePtr aDoubleAttribute =
94       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
95   if (aDoubleAttribute.get()) {
96     std::string anError;
97     double aValue = evaluate(aDoubleAttribute->text(), anError);
98     if (anError.empty()) {
99       aDoubleAttribute->setCalculatedValue(aValue);
100       aDoubleAttribute->setExpressionInvalid(false);
101     } else { // set feature as invalid-parameter arguments
102       aDoubleAttribute->setExpressionInvalid(true);
103     }
104   }
105
106   // Point
107   AttributePointPtr aPointAttribute =
108       std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
109   if (aPointAttribute.get()) {
110     std::string anError[3];
111     double aValue[3] = {
112         evaluate(aPointAttribute->textX(), anError[0]),
113         evaluate(aPointAttribute->textY(), anError[1]),
114         evaluate(aPointAttribute->textZ(), anError[2])
115     };
116     bool isValid[3] = {
117         anError[0].empty(),
118         anError[1].empty(),
119         anError[2].empty()
120     };
121     aPointAttribute->setExpressionInvalid(0, !isValid[0]);
122     aPointAttribute->setExpressionInvalid(1, !isValid[1]);
123     aPointAttribute->setExpressionInvalid(2, !isValid[2]);
124
125     aPointAttribute->setCalculatedValue(
126         isValid[0] ? aValue[0] : aPointAttribute->x(),
127         isValid[1] ? aValue[1] : aPointAttribute->y(),
128         isValid[2] ? aValue[2] : aPointAttribute->z()
129     );
130   }
131
132   // Point2D
133   AttributePoint2DPtr aPoint2DAttribute =
134       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
135   if (aPoint2DAttribute.get()) {
136     std::string anError[2];
137     double aValue[2] = {
138         evaluate(aPoint2DAttribute->textX(), anError[0]),
139         evaluate(aPoint2DAttribute->textY(), anError[1])
140     };
141     bool isValid[2] = {
142         anError[0].empty(),
143         anError[1].empty()
144     };
145     aPoint2DAttribute->setExpressionInvalid(0, !isValid[0]);
146     aPoint2DAttribute->setExpressionInvalid(1, !isValid[1]);
147
148     aPoint2DAttribute->setCalculatedValue(
149         isValid[0] ? aValue[0] : aPoint2DAttribute->x(),
150         isValid[1] ? aValue[1] : aPoint2DAttribute->y()
151     );
152   }
153 }
154
155 std::string ParametersPlugin_EvalListener::renameInPythonExpression(
156     const std::string& theExpression,
157     const std::string& theOldName,
158     const std::string& theNewName)
159 {
160   std::string anExpressionString = theExpression;
161
162   std::list<std::pair<int, int> > aPositions =
163       myInterp->positions(anExpressionString, theOldName);
164
165   if (aPositions.empty())
166     return anExpressionString;
167
168   std::map<int, std::list<int> > aLines;
169   std::list<std::pair<int, int> >::const_iterator it = aPositions.begin();
170   for (; it != aPositions.end(); ++it)
171     aLines[it->first].push_back(it->second);
172
173   // Start renaming from the end to keep indexes if theNewName is longer then theOldName
174   std::map<int, std::list<int> >::const_reverse_iterator ritLine = aLines.rbegin();
175   for (; ritLine != aLines.rend(); ++ritLine) {
176     // Calculate the start of the line (find the aLineNo occurrence of "\n" )
177     int aLineNo = ritLine->first - 1;
178     size_t aLineStart = 0;
179     for (int i = 0; i < aLineNo; ++i)
180       aLineStart = anExpressionString.find("\n", aLineStart) + 1;
181
182     const std::list<int>& aColOffsets = ritLine->second;
183     std::list<int>::const_reverse_iterator ritOffset = aColOffsets.rbegin();
184     for (; ritOffset != aColOffsets.rend(); ++ritOffset) {
185       int anOffset = *ritOffset;
186       anExpressionString.replace(aLineStart + anOffset, theOldName.size(), theNewName);
187     }
188   }
189
190   return anExpressionString;
191 }
192
193 void ParametersPlugin_EvalListener::renameInParameter(
194     std::shared_ptr<ParametersPlugin_Parameter> theParameter,
195     const std::string& theOldName,
196     const std::string& theNewName)
197 {
198   std::shared_ptr<ModelAPI_AttributeString> anExpressionAttribute =
199       theParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID());
200
201   std::string anExpressionString = anExpressionAttribute->value();
202   anExpressionString = renameInPythonExpression(anExpressionString,
203                                                 theOldName,
204                                                 theNewName);
205   // Issue #588. No need for reevaluating expression. 
206   // Moreover, current history may not contain necessary parameters.
207   anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(true);
208   anExpressionAttribute->setValue(anExpressionString);
209   anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(false);
210 }
211
212 void ParametersPlugin_EvalListener::renameInAttribute(
213     std::shared_ptr<ModelAPI_Attribute> theAttribute,
214     const std::string& theOldName,
215     const std::string& theNewName)
216 {
217   // Double
218   AttributeDoublePtr aDoubleAttribute =
219       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
220   if (aDoubleAttribute.get()) {
221     std::string anExpressionString = aDoubleAttribute->text();
222     anExpressionString = renameInPythonExpression(anExpressionString,
223                                                   theOldName,
224                                                   theNewName);
225     aDoubleAttribute->setText(anExpressionString);
226   }
227
228   // Point
229   AttributePointPtr aPointAttribute =
230       std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
231   if (aPointAttribute.get()) {
232     std::string anExpressionString[3] = {
233       aPointAttribute->textX(),
234       aPointAttribute->textY(),
235       aPointAttribute->textZ()
236     };
237     for (int i = 0; i < 3; ++i)
238       anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
239                                                        theOldName,
240                                                        theNewName);
241     aPointAttribute->setText(anExpressionString[0],
242                              anExpressionString[1],
243                              anExpressionString[2]);
244   }
245
246   // Point2D
247   AttributePoint2DPtr aPoint2DAttribute =
248       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
249   if (aPoint2DAttribute.get()) {
250     std::string anExpressionString[2] = {
251       aPoint2DAttribute->textX(),
252       aPoint2DAttribute->textY()
253     };
254     for (int i = 0; i < 2; ++i)
255       anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
256                                                        theOldName,
257                                                        theNewName);
258     aPoint2DAttribute->setText(anExpressionString[0],
259                                anExpressionString[1]);
260   }
261 }
262
263 void ParametersPlugin_EvalListener::processObjectRenamedEvent(
264     const std::shared_ptr<Events_Message>& theMessage)
265 {
266   std::shared_ptr<ModelAPI_ObjectRenamedMessage> aMessage =
267       std::dynamic_pointer_cast<ModelAPI_ObjectRenamedMessage>(theMessage);
268
269   if (!aMessage.get() || aMessage->oldName().empty() || aMessage->newName().empty())
270     return;
271
272   // check that the renamed object is a result 
273   std::shared_ptr<ModelAPI_ResultParameter> aResultParameter =
274       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aMessage->object());
275   if (!aResultParameter.get()) 
276     return;
277   
278   // get parameter feature for the result
279   std::shared_ptr<ModelAPI_Feature> aFeature = aResultParameter->document()->feature(aResultParameter);
280   std::shared_ptr<ParametersPlugin_Parameter> aParameter =
281       std::dynamic_pointer_cast<ParametersPlugin_Parameter>(aFeature);
282   if (!aParameter.get())
283     return;
284
285   // rename a parameter attributes
286   // short way:
287   //aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(aMessage->newName());
288   //aParameter->execute();
289   // manual way:
290   aParameter->data()->setName(aMessage->newName());
291   aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(aMessage->newName());
292
293   // List of documents to process
294   std::list<DocumentPtr> aDocList;
295   SessionPtr aSession = ModelAPI_Session::get();
296   DocumentPtr aDocument = aSession->activeDocument();
297   DocumentPtr aRootDocument = aSession->moduleDocument();
298   aDocList.push_back(aDocument);
299   if (aDocument != aRootDocument) {
300     aDocList.push_back(aRootDocument);
301   }
302
303   // Find all features
304   for (std::list<DocumentPtr>::const_iterator aDicumentIt = aDocList.begin(); 
305        aDicumentIt != aDocList.end(); ++aDicumentIt) {
306     const DocumentPtr& aDocument = *aDicumentIt;
307     std::list<FeaturePtr> aFeatures = aDocument->allFeatures();
308     std::list<FeaturePtr>::iterator aFeatureIt = aFeatures.begin();
309     for (; aFeatureIt != aFeatures.end(); ++aFeatureIt) {
310       const FeaturePtr& aFeature = *aFeatureIt;
311       
312       // If Parameter feature then rename its expression
313       std::shared_ptr<ParametersPlugin_Parameter> aParameter =
314           std::dynamic_pointer_cast<ParametersPlugin_Parameter>(aFeature);
315       if (aParameter.get()) {
316         // Rename
317         renameInParameter(aParameter, aMessage->oldName(), aMessage->newName());
318         continue;
319       }      
320
321       // Find all attributes
322       std::list<AttributePtr> anAttributes = aFeature->data()->attributes(std::string());
323       std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
324       for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
325         const AttributePtr& anAttribute = *anAttributeIt;
326
327         // Rename
328         renameInAttribute(anAttribute, aMessage->oldName(), aMessage->newName());
329       }
330     }
331   }
332 }
333