2 * ParametersPlugin_EvalListener.cpp
4 * Created on: Apr 28, 2015
10 #include <ParametersPlugin_EvalListener.h>
11 #include <ParametersPlugin_Parameter.h>
12 #include <ParametersPlugin_PyInterp.h>
14 #include <Events_Error.h>
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 #include <ModelAPI_AttributeValidator.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <GeomDataAPI_Point.h>
25 #include <GeomDataAPI_Point2D.h>
30 ParametersPlugin_EvalListener::ParametersPlugin_EvalListener()
32 Events_Loop* aLoop = Events_Loop::loop();
33 const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId();
34 aLoop->registerListener(this, kEvaluationEvent, NULL, true);
35 const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
36 aLoop->registerListener(this, kObjectRenamedEvent, NULL, true);
38 myInterp = std::shared_ptr<ParametersPlugin_PyInterp>(new ParametersPlugin_PyInterp());
39 myInterp->initialize();
42 ParametersPlugin_EvalListener::~ParametersPlugin_EvalListener()
46 void ParametersPlugin_EvalListener::processEvent(
47 const std::shared_ptr<Events_Message>& theMessage)
49 if (!theMessage.get())
52 const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId();
53 const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
54 if (theMessage->eventID() == kEvaluationEvent) {
55 processEvaluationEvent(theMessage);
56 } else if (theMessage->eventID() == kObjectRenamedEvent) {
57 processObjectRenamedEvent(theMessage);
59 Events_Error::send(std::string("ParametersPlugin python interpreter, unhandled message caught: ")
60 + theMessage->eventID().eventText());
64 double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression,
65 std::string& theError)
67 std::list<std::string> anExprParams = myInterp->compile(theExpression);
68 // find expression's params in the model
69 std::list<std::string> aContext;
70 std::list<std::string>::iterator it = anExprParams.begin();
71 for ( ; it != anExprParams.end(); it++) {
73 ResultParameterPtr aParamRes;
74 if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes)) continue;
76 std::ostringstream sstream;
78 std::string aParamValue = sstream.str();
79 aContext.push_back(*it + "=" + aParamValue);
81 myInterp->extendLocalContext(aContext);
82 double result = myInterp->evaluate(theExpression, theError);
83 myInterp->clearLocalContext();
87 void ParametersPlugin_EvalListener::processEvaluationEvent(
88 const std::shared_ptr<Events_Message>& theMessage)
90 std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
91 std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
94 AttributeDoublePtr aDoubleAttribute =
95 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
96 if (aDoubleAttribute.get()) {
98 double aValue = evaluate(aDoubleAttribute->text(), anError);
99 if (anError.empty()) {
100 aDoubleAttribute->setCalculatedValue(aValue);
101 aDoubleAttribute->setExpressionInvalid(false);
102 } else { // set feature as invalid-parameter arguments
103 aDoubleAttribute->setExpressionInvalid(true);
108 AttributePointPtr aPointAttribute =
109 std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
110 if (aPointAttribute.get()) {
111 std::string anError[3];
113 evaluate(aPointAttribute->textX(), anError[0]),
114 evaluate(aPointAttribute->textY(), anError[1]),
115 evaluate(aPointAttribute->textZ(), anError[2])
122 aPointAttribute->setExpressionInvalid(0, !isValid[0]);
123 aPointAttribute->setExpressionInvalid(1, !isValid[1]);
124 aPointAttribute->setExpressionInvalid(2, !isValid[2]);
126 aPointAttribute->setCalculatedValue(
127 isValid[0] ? aValue[0] : aPointAttribute->x(),
128 isValid[1] ? aValue[1] : aPointAttribute->y(),
129 isValid[2] ? aValue[2] : aPointAttribute->z()
134 AttributePoint2DPtr aPoint2DAttribute =
135 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
136 if (aPoint2DAttribute.get()) {
137 std::string anError[2];
139 evaluate(aPoint2DAttribute->textX(), anError[0]),
140 evaluate(aPoint2DAttribute->textY(), anError[1])
146 aPoint2DAttribute->setExpressionInvalid(0, !isValid[0]);
147 aPoint2DAttribute->setExpressionInvalid(1, !isValid[1]);
149 aPoint2DAttribute->setCalculatedValue(
150 isValid[0] ? aValue[0] : aPoint2DAttribute->x(),
151 isValid[1] ? aValue[1] : aPoint2DAttribute->y()
156 std::string ParametersPlugin_EvalListener::renameInPythonExpression(
157 const std::string& theExpression,
158 const std::string& theOldName,
159 const std::string& theNewName)
161 std::string anExpressionString = theExpression;
163 std::list<std::pair<int, int> > aPositions =
164 myInterp->positions(anExpressionString, theOldName);
166 if (aPositions.empty())
167 return anExpressionString;
169 std::map<int, std::list<int> > aLines;
170 std::list<std::pair<int, int> >::const_iterator it = aPositions.begin();
171 for (; it != aPositions.end(); ++it)
172 aLines[it->first].push_back(it->second);
174 // Start renaming from the end to keep indexes if theNewName is longer then theOldName
175 std::map<int, std::list<int> >::const_reverse_iterator ritLine = aLines.rbegin();
176 for (; ritLine != aLines.rend(); ++ritLine) {
177 // Calculate the start of the line (find the aLineNo occurrence of "\n" )
178 int aLineNo = ritLine->first - 1;
179 size_t aLineStart = 0;
180 for (int i = 0; i < aLineNo; ++i)
181 aLineStart = anExpressionString.find("\n", aLineStart) + 1;
183 const std::list<int>& aColOffsets = ritLine->second;
184 std::list<int>::const_reverse_iterator ritOffset = aColOffsets.rbegin();
185 for (; ritOffset != aColOffsets.rend(); ++ritOffset) {
186 int anOffset = *ritOffset;
187 anExpressionString.replace(aLineStart + anOffset, theOldName.size(), theNewName);
191 return anExpressionString;
194 void ParametersPlugin_EvalListener::renameInParameter(
195 std::shared_ptr<ParametersPlugin_Parameter> theParameter,
196 const std::string& theOldName,
197 const std::string& theNewName)
199 std::shared_ptr<ModelAPI_AttributeString> anExpressionAttribute =
200 theParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID());
202 std::string anExpressionString = anExpressionAttribute->value();
203 anExpressionString = renameInPythonExpression(anExpressionString,
206 // Issue #588. No need for reevaluating expression.
207 // Moreover, current history may not contain necessary parameters.
208 anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(true);
209 anExpressionAttribute->setValue(anExpressionString);
210 anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(false);
213 void ParametersPlugin_EvalListener::renameInAttribute(
214 std::shared_ptr<ModelAPI_Attribute> theAttribute,
215 const std::string& theOldName,
216 const std::string& theNewName)
219 AttributeDoublePtr aDoubleAttribute =
220 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
221 if (aDoubleAttribute.get()) {
222 std::string anExpressionString = aDoubleAttribute->text();
223 anExpressionString = renameInPythonExpression(anExpressionString,
226 aDoubleAttribute->setText(anExpressionString);
230 AttributePointPtr aPointAttribute =
231 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
232 if (aPointAttribute.get()) {
233 std::string anExpressionString[3] = {
234 aPointAttribute->textX(),
235 aPointAttribute->textY(),
236 aPointAttribute->textZ()
238 for (int i = 0; i < 3; ++i)
239 anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
242 aPointAttribute->setText(anExpressionString[0],
243 anExpressionString[1],
244 anExpressionString[2]);
248 AttributePoint2DPtr aPoint2DAttribute =
249 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
250 if (aPoint2DAttribute.get()) {
251 std::string anExpressionString[2] = {
252 aPoint2DAttribute->textX(),
253 aPoint2DAttribute->textY()
255 for (int i = 0; i < 2; ++i)
256 anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
259 aPoint2DAttribute->setText(anExpressionString[0],
260 anExpressionString[1]);
264 bool isValidAttribute(const AttributePtr& theAttribute)
266 std::string aValidator, anError;
267 return ModelAPI_Session::get()->validators()->validate(theAttribute, aValidator, anError);
270 void setParameterName(ResultParameterPtr theResultParameter, const std::string& theName)
272 theResultParameter->data()->blockSendAttributeUpdated(true);
273 theResultParameter->data()->setName(theName);
274 theResultParameter->data()->blockSendAttributeUpdated(false);
276 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
277 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
278 ModelAPI_Feature::feature(theResultParameter));
280 aParameter->data()->blockSendAttributeUpdated(true);
281 aParameter->data()->setName(theName);
282 aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(theName);
283 aParameter->data()->blockSendAttributeUpdated(false);
286 void ParametersPlugin_EvalListener::processObjectRenamedEvent(
287 const std::shared_ptr<Events_Message>& theMessage)
289 std::shared_ptr<ModelAPI_ObjectRenamedMessage> aMessage =
290 std::dynamic_pointer_cast<ModelAPI_ObjectRenamedMessage>(theMessage);
292 if (!aMessage.get() || aMessage->oldName().empty() || aMessage->newName().empty())
295 // check if the renamed object is a result perameter
296 ResultParameterPtr aResultParameter =
297 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aMessage->object());
298 if (!aResultParameter.get())
301 // get parameter feature for the result
302 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
303 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
304 ModelAPI_Feature::feature(aResultParameter));
305 if (!aParameter.get())
308 // try to update the parameter feature according the new name
309 setParameterName(aResultParameter, aMessage->newName());
310 // TODO(spo): replace with ModelAPI_Session::get()->validators()->validate(aParameter, ParametersPlugin_Parameter::VARIABLE_ID())
311 // when ModelAPI_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::string& theAttribute) const
313 if (!isValidAttribute(aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID()))) {
314 setParameterName(aResultParameter, aMessage->oldName());
318 // List of documents to process
319 std::list<DocumentPtr> aDocList;
320 SessionPtr aSession = ModelAPI_Session::get();
321 DocumentPtr aDocument = aSession->activeDocument();
322 DocumentPtr aRootDocument = aSession->moduleDocument();
323 aDocList.push_back(aDocument);
324 if (aDocument != aRootDocument) {
325 aDocList.push_back(aRootDocument);
329 for (std::list<DocumentPtr>::const_iterator aDicumentIt = aDocList.begin();
330 aDicumentIt != aDocList.end(); ++aDicumentIt) {
331 const DocumentPtr& aDocument = *aDicumentIt;
332 std::list<FeaturePtr> aFeatures = aDocument->allFeatures();
333 std::list<FeaturePtr>::iterator aFeatureIt = aFeatures.begin();
334 for (; aFeatureIt != aFeatures.end(); ++aFeatureIt) {
335 const FeaturePtr& aFeature = *aFeatureIt;
337 // If Parameter feature then rename its expression
338 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
339 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(aFeature);
340 if (aParameter.get()) {
342 renameInParameter(aParameter, aMessage->oldName(), aMessage->newName());
346 // Find all attributes
347 std::list<AttributePtr> anAttributes = aFeature->data()->attributes(std::string());
348 std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
349 for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
350 const AttributePtr& anAttribute = *anAttributeIt;
353 renameInAttribute(anAttribute, aMessage->oldName(), aMessage->newName());