1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include <InitializationPlugin_EvalListener.h>
23 #include <ParametersPlugin_Parameter.h>
24 #include <InitializationPlugin_PyInterp.h>
26 #include <Events_InfoMessage.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_AttributeInteger.h>
30 #include <ModelAPI_AttributeRefList.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_AttributeValidator.h>
33 #include <ModelAPI_Document.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_ResultParameter.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Tools.h>
39 #include <GeomDataAPI_Point.h>
40 #include <GeomDataAPI_Point2D.h>
46 //------------------------------------------------------------------------------
49 std::string toStdString(double theValue)
51 std::ostringstream sstream;
53 size_t aPos = sstream.str().find(".");
54 std::string aPnt = "";
55 if (aPos == std::string::npos)
57 return sstream.str() + aPnt;
60 std::set<std::string> toSet(const std::list<std::string>& theContainer)
62 return std::set<std::string>(theContainer.begin(), theContainer.end());
65 //------------------------------------------------------------------------------
67 InitializationPlugin_EvalListener::InitializationPlugin_EvalListener()
69 Events_Loop* aLoop = Events_Loop::loop();
71 aLoop->registerListener(this, ModelAPI_AttributeEvalMessage::eventId(), NULL, true);
72 aLoop->registerListener(this, ModelAPI_ParameterEvalMessage::eventId(), NULL, true);
73 aLoop->registerListener(this, ModelAPI_ComputePositionsMessage::eventId(), NULL, true);
75 myInterp = std::shared_ptr<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
76 myInterp->initialize();
79 InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener()
83 void InitializationPlugin_EvalListener::processEvent(
84 const std::shared_ptr<Events_Message>& theMessage)
86 if (!theMessage.get())
89 if (theMessage->eventID() == ModelAPI_AttributeEvalMessage::eventId()) {
90 processEvaluationEvent(theMessage);
91 } else if (theMessage->eventID() == ModelAPI_ParameterEvalMessage::eventId()) {
92 std::shared_ptr<ModelAPI_ParameterEvalMessage> aMsg =
93 std::dynamic_pointer_cast<ModelAPI_ParameterEvalMessage>(theMessage);
94 FeaturePtr aParam = aMsg->parameter();
95 std::string anExp = aParam->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
97 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
98 double aResult = evaluate(aParam, anExp, anError, aParamsList, true);
99 aMsg->setResults(aParamsList, aResult, anError);
100 } else if (theMessage->eventID() == ModelAPI_ComputePositionsMessage::eventId()) {
101 std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
102 std::dynamic_pointer_cast<ModelAPI_ComputePositionsMessage>(theMessage);
103 aMsg->setPositions(myInterp->positions(aMsg->expression(), aMsg->parameter()));
107 double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter,
108 const std::string& theExpression, std::string& theError,
109 std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
110 const bool theIsParameter)
112 std::list<std::string> anExprParams = myInterp->compile(theExpression);
113 // find expression's params in the model
114 std::list<std::string> aContext;
115 std::list<std::string>::iterator it = anExprParams.begin();
116 for ( ; it != anExprParams.end(); it++) {
118 ResultParameterPtr aParamRes;
119 // If variable does not exist python interpreter will generate an error. It is OK.
120 // But due to the issue 1479 it should not check the history position of parameters relatively
121 // to feature that contains expression
122 if (!ModelAPI_Tools::findVariable(theIsParameter ? theParameter : FeaturePtr(),
123 *it, aValue, aParamRes, theParameter->document()))
127 theParamsList.push_back(aParamRes);
128 aContext.push_back(*it + "=" + toStdString(aValue));
130 myInterp->extendLocalContext(aContext);
131 double result = myInterp->evaluate(theExpression, theError);
132 myInterp->clearLocalContext();
136 void InitializationPlugin_EvalListener::processEvaluationEvent(
137 const std::shared_ptr<Events_Message>& theMessage)
139 std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
140 std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
142 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
143 FeaturePtr aParamFeature =
144 std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
145 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
146 AttributeIntegerPtr anAttribute =
147 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
149 int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
150 bool isValid = anError.empty();
152 anAttribute->setCalculatedValue(aValue);
153 anAttribute->setUsedParameters(isValid ?
154 toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
155 anAttribute->setExpressionInvalid(!isValid);
156 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
158 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
159 AttributeDoublePtr anAttribute =
160 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
162 double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
163 bool isValid = anError.empty();
165 anAttribute->setCalculatedValue(aValue);
166 anAttribute->setUsedParameters(isValid ?
167 toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
168 anAttribute->setExpressionInvalid(!isValid);
169 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
171 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
172 AttributePointPtr anAttribute =
173 std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
174 std::string aText[] = {
175 anAttribute->textX(),
176 anAttribute->textY(),
179 double aCalculatedValue[] = {
184 for (int i = 0; i < 3; ++i) {
186 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
187 bool isValid = anError.empty();
188 if (isValid) aCalculatedValue[i] = aValue;
189 anAttribute->setUsedParameters(i,
190 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
191 anAttribute->setExpressionInvalid(i, !isValid);
192 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
194 anAttribute->setCalculatedValue(aCalculatedValue[0],
196 aCalculatedValue[2]);
198 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) {
199 AttributePoint2DPtr anAttribute =
200 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
201 std::string aText[] = {
202 anAttribute->textX(),
205 double aCalculatedValue[] = {
209 for (int i = 0; i < 2; ++i) {
211 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
212 bool isValid = anError.empty();
213 if (isValid) aCalculatedValue[i] = aValue;
214 anAttribute->setUsedParameters(i,
215 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
216 anAttribute->setExpressionInvalid(i, !isValid);
217 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
219 anAttribute->setCalculatedValue(aCalculatedValue[0],
220 aCalculatedValue[1]);