Salome HOME
Update line endings according to coding rules
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_EvalListener.cpp
index fcd4b0dc808076245064ae35cd336edc6675e9e7..0d61bec9ff3aa2e3e0d1a7671bb9c6cbe69b5b89 100644 (file)
@@ -1,3 +1,4 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
 /*
  * ParametersPlugin_EvalListener.cpp
  *
@@ -9,7 +10,6 @@
 
 #include <ParametersPlugin_EvalListener.h>
 #include <ParametersPlugin_Parameter.h>
-#include <ParametersPlugin_PyInterp.h>
 
 #include <Events_InfoMessage.h>
 
@@ -58,16 +58,12 @@ ParametersPlugin_EvalListener::ParametersPlugin_EvalListener()
   Events_Loop* aLoop = Events_Loop::loop();
 
   Events_ID anEvents_IDs[] = {
-      ModelAPI_AttributeEvalMessage::eventId(),
       ModelAPI_ObjectRenamedMessage::eventId(),
       ModelAPI_ReplaceParameterMessage::eventId()
   };
 
   for (int i = 0; i < sizeof(anEvents_IDs)/sizeof(anEvents_IDs[0]); ++i)
     aLoop->registerListener(this, anEvents_IDs[i], NULL, true);
-
-  myInterp = std::shared_ptr<ParametersPlugin_PyInterp>(new ParametersPlugin_PyInterp());
-  myInterp->initialize();
 }
 
 ParametersPlugin_EvalListener::~ParametersPlugin_EvalListener()
@@ -80,128 +76,13 @@ void ParametersPlugin_EvalListener::processEvent(
   if (!theMessage.get())
     return;
 
-  const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId();
   const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
   const Events_ID kReplaceParameterEvent = ModelAPI_ReplaceParameterMessage::eventId();
 
-  if (theMessage->eventID() == kEvaluationEvent) {
-    processEvaluationEvent(theMessage);
-  } else if (theMessage->eventID() == kObjectRenamedEvent) {
+  if (theMessage->eventID() == kObjectRenamedEvent) {
     processObjectRenamedEvent(theMessage);
   } else if (theMessage->eventID() == kReplaceParameterEvent) {
     processReplaceParameterEvent(theMessage);
-  } else {
-    Events_InfoMessage("ParametersPlugin_EvalListener", 
-      "ParametersPlugin python interpreter, unhandled message caught: ")
-      .arg(theMessage->eventID().eventText()).send();
-  }
-}
-
-double ParametersPlugin_EvalListener::evaluate(FeaturePtr theParameter,
-  const std::string& theExpression, std::string& theError)
-{
-  std::list<std::string> anExprParams = myInterp->compile(theExpression);
-  // find expression's params in the model
-  std::list<std::string> aContext;
-  std::list<std::string>::iterator it = anExprParams.begin();
-  for ( ; it != anExprParams.end(); it++) {
-    double aValue;
-    ResultParameterPtr aParamRes;
-    // If variable does not exist python interpreter will generate an error. It is OK.
-    // But due to the issue 1479 it should not check the history position of parameters relatively
-    // to feature that contains expression
-    if (!ModelAPI_Tools::findVariable(/*theParameter*/ FeaturePtr(), 
-      *it, aValue, aParamRes, theParameter->document()))
-      continue;
-
-    aContext.push_back(*it + "=" + toStdString(aValue));
-  }
-  myInterp->extendLocalContext(aContext);
-  double result = myInterp->evaluate(theExpression, theError);
-  myInterp->clearLocalContext();
-  return result;
-}
-
-void ParametersPlugin_EvalListener::processEvaluationEvent(
-    const std::shared_ptr<Events_Message>& theMessage)
-{
-  std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
-      std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
-
-  FeaturePtr aParamFeature = 
-    std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
-  if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
-    AttributeIntegerPtr anAttribute =
-        std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
-    std::string anError;
-    int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError);
-    bool isValid = anError.empty();
-    if (isValid)
-      anAttribute->setCalculatedValue(aValue);
-    anAttribute->setUsedParameters(isValid ? toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
-    anAttribute->setExpressionInvalid(!isValid);
-    anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
-  } else
-  if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
-    AttributeDoublePtr anAttribute =
-        std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
-    std::string anError;
-    double aValue = evaluate(aParamFeature, anAttribute->text(), anError);
-    bool isValid = anError.empty();
-    if (isValid)
-      anAttribute->setCalculatedValue(aValue);
-    anAttribute->setUsedParameters(isValid ? toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
-    anAttribute->setExpressionInvalid(!isValid);
-    anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
-  } else
-  if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
-    AttributePointPtr anAttribute =
-        std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
-    std::string aText[] = {
-      anAttribute->textX(),
-      anAttribute->textY(),
-      anAttribute->textZ()
-    };
-    double aCalculatedValue[] = {
-      anAttribute->x(),
-      anAttribute->y(),
-      anAttribute->z()
-    };
-    for (int i = 0; i < 3; ++i) {
-      std::string anError;
-      double aValue = evaluate(aParamFeature, aText[i], anError);
-      bool isValid = anError.empty();
-      if (isValid) aCalculatedValue[i] = aValue;
-      anAttribute->setUsedParameters(i, isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
-      anAttribute->setExpressionInvalid(i, !isValid);
-      anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
-    }
-    anAttribute->setCalculatedValue(aCalculatedValue[0],
-                                    aCalculatedValue[1],
-                                    aCalculatedValue[2]);
-  } else
-  if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) {
-    AttributePoint2DPtr anAttribute =
-        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
-    std::string aText[] = {
-      anAttribute->textX(),
-      anAttribute->textY()
-    };
-    double aCalculatedValue[] = {
-      anAttribute->x(),
-      anAttribute->y()
-    };
-    for (int i = 0; i < 2; ++i) {
-      std::string anError;
-      double aValue = evaluate(aParamFeature, aText[i], anError);
-      bool isValid = anError.empty();
-      if (isValid) aCalculatedValue[i] = aValue;
-      anAttribute->setUsedParameters(i, isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
-      anAttribute->setExpressionInvalid(i, !isValid);
-      anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
-    }
-    anAttribute->setCalculatedValue(aCalculatedValue[0],
-                                    aCalculatedValue[1]);
   }
 }
 
@@ -212,8 +93,10 @@ std::string ParametersPlugin_EvalListener::renameInPythonExpression(
 {
   std::string anExpressionString = theExpression;
 
-  std::list<std::pair<int, int> > aPositions =
-      myInterp->positions(anExpressionString, theOldName);
+  // ask interpreter to compute the positions in the expression
+  std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
+    ModelAPI_ComputePositionsMessage::send(theExpression, theOldName, this);
+  const std::list<std::pair<int, int> >& aPositions = aMsg->positions();
 
   if (aPositions.empty())
     return anExpressionString;
@@ -255,7 +138,7 @@ void ParametersPlugin_EvalListener::renameInParameter(
   anExpressionString = renameInPythonExpression(anExpressionString,
                                                 theOldName,
                                                 theNewName);
-  // Issue #588. No need for reevaluating expression. 
+  // Issue #588. No need for reevaluating expression.
   // Moreover, current history may not contain necessary parameters.
   anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(true);
   anExpressionAttribute->setValue(anExpressionString);
@@ -313,9 +196,10 @@ void ParametersPlugin_EvalListener::renameInAttribute(
   }
 }
 
-void ParametersPlugin_EvalListener::renameInDependents(std::shared_ptr<ModelAPI_ResultParameter> theResultParameter,
-                                                       const std::string& theOldName,
-                                                       const std::string& theNewName)
+void ParametersPlugin_EvalListener::renameInDependents(
+              std::shared_ptr<ModelAPI_ResultParameter> theResultParameter,
+              const std::string& theOldName,
+              const std::string& theNewName)
 {
   std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
       theResultParameter->data()->refsToMe();
@@ -338,7 +222,8 @@ void ParametersPlugin_EvalListener::renameInDependents(std::shared_ptr<ModelAPI_
 
 bool isValidAttribute(const AttributePtr& theAttribute)
 {
-  std::string aValidator, anError;
+  std::string aValidator;
+  Events_InfoMessage anError;
   return ModelAPI_Session::get()->validators()->validate(theAttribute, aValidator, anError);
 }
 
@@ -346,9 +231,9 @@ void setParameterName(ResultParameterPtr theResultParameter, const std::string&
 {
   theResultParameter->data()->blockSendAttributeUpdated(true);
   theResultParameter->data()->setName(theName);
-  theResultParameter->data()->blockSendAttributeUpdated(false);
+  theResultParameter->data()->blockSendAttributeUpdated(false, false);
 
-  std::shared_ptr<ParametersPlugin_Parameter> aParameter = 
+  std::shared_ptr<ParametersPlugin_Parameter> aParameter =
       std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
           ModelAPI_Feature::feature(theResultParameter));
 
@@ -372,7 +257,7 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent(
   // check if the renamed object is a result parameter
   ResultParameterPtr aResultParameter =
       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aMessage->object());
-  if (!aResultParameter.get()) 
+  if (!aResultParameter.get())
     return;
 
   // get parameter feature for the result
@@ -385,8 +270,8 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent(
   std::string aNotActivatedNames;
   if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
     QMessageBox::StandardButton aRes = QMessageBox::warning(0, QObject::tr("Warning"),
-               QObject::tr("Selected objects can be used in Part documents which are not loaded: \
-%1. Whould you like to continue?").arg(aNotActivatedNames.c_str()),
+               QObject::tr("Selected objects can be used in Part documents which are not loaded: "
+                           "%1. Whould you like to continue?").arg(aNotActivatedNames.c_str()),
                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
     if (aRes != QMessageBox::Yes) {
       setParameterName(aResultParameter, aMessage->oldName());
@@ -396,8 +281,11 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent(
 
   // try to update the parameter feature according the new name
   setParameterName(aResultParameter, aMessage->newName());
-  // TODO(spo): replace with ModelAPI_Session::get()->validators()->validate(aParameter, ParametersPlugin_Parameter::VARIABLE_ID())
-  // when ModelAPI_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Feature>& theFeature, const std::string& theAttribute) const
+  // TODO(spo): replace with
+  // ModelAPI_Session::get()->validators()->validate(aParameter,
+  //                                                 ParametersPlugin_Parameter::VARIABLE_ID())
+  // when ModelAPI_ValidatorsFactory::validate(const std::shared_ptr<ModelAPI_Feature>& theFeature,
+  //                                           const std::string& theAttribute) const
   // is ready
   if (!isValidAttribute(aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID()))) {
     setParameterName(aResultParameter, aMessage->oldName());