1 // Copyright (C) 2014-2017 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<mailto:webmaster.salome@opencascade.com>
21 * InitializationPlugin_EvalListener.cpp
23 * Created on: Apr 28, 2015
29 #include <InitializationPlugin_EvalListener.h>
30 #include <ParametersPlugin_Parameter.h>
31 #include <InitializationPlugin_PyInterp.h>
33 #include <Events_InfoMessage.h>
35 #include <ModelAPI_AttributeDouble.h>
36 #include <ModelAPI_AttributeInteger.h>
37 #include <ModelAPI_AttributeRefList.h>
38 #include <ModelAPI_AttributeString.h>
39 #include <ModelAPI_AttributeValidator.h>
40 #include <ModelAPI_Document.h>
41 #include <ModelAPI_Events.h>
42 #include <ModelAPI_ResultParameter.h>
43 #include <ModelAPI_Session.h>
44 #include <ModelAPI_Tools.h>
46 #include <GeomDataAPI_Point.h>
47 #include <GeomDataAPI_Point2D.h>
53 //------------------------------------------------------------------------------
56 std::string toStdString(double theValue)
58 std::ostringstream sstream;
60 size_t aPos = sstream.str().find(".");
61 std::string aPnt = "";
62 if (aPos == std::string::npos)
64 return sstream.str() + aPnt;
67 std::set<std::string> toSet(const std::list<std::string>& theContainer)
69 return std::set<std::string>(theContainer.begin(), theContainer.end());
72 //------------------------------------------------------------------------------
74 InitializationPlugin_EvalListener::InitializationPlugin_EvalListener()
76 Events_Loop* aLoop = Events_Loop::loop();
78 aLoop->registerListener(this, ModelAPI_AttributeEvalMessage::eventId(), NULL, true);
79 aLoop->registerListener(this, ModelAPI_ParameterEvalMessage::eventId(), NULL, true);
80 aLoop->registerListener(this, ModelAPI_ComputePositionsMessage::eventId(), NULL, true);
82 myInterp = std::shared_ptr<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
83 myInterp->initialize();
86 InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener()
90 void InitializationPlugin_EvalListener::processEvent(
91 const std::shared_ptr<Events_Message>& theMessage)
93 if (!theMessage.get())
96 if (theMessage->eventID() == ModelAPI_AttributeEvalMessage::eventId()) {
97 processEvaluationEvent(theMessage);
98 } else if (theMessage->eventID() == ModelAPI_ParameterEvalMessage::eventId()) {
99 std::shared_ptr<ModelAPI_ParameterEvalMessage> aMsg =
100 std::dynamic_pointer_cast<ModelAPI_ParameterEvalMessage>(theMessage);
101 FeaturePtr aParam = aMsg->parameter();
102 std::string anExp = aParam->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
104 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
105 double aResult = evaluate(aParam, anExp, anError, aParamsList, true);
106 aMsg->setResults(aParamsList, aResult, anError);
107 } else if (theMessage->eventID() == ModelAPI_ComputePositionsMessage::eventId()) {
108 std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
109 std::dynamic_pointer_cast<ModelAPI_ComputePositionsMessage>(theMessage);
110 aMsg->setPositions(myInterp->positions(aMsg->expression(), aMsg->parameter()));
114 double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter,
115 const std::string& theExpression, std::string& theError,
116 std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
117 const bool theIsParameter)
119 std::list<std::string> anExprParams = myInterp->compile(theExpression);
120 // find expression's params in the model
121 std::list<std::string> aContext;
122 std::list<std::string>::iterator it = anExprParams.begin();
123 for ( ; it != anExprParams.end(); it++) {
125 ResultParameterPtr aParamRes;
126 // If variable does not exist python interpreter will generate an error. It is OK.
127 // But due to the issue 1479 it should not check the history position of parameters relatively
128 // to feature that contains expression
129 if (!ModelAPI_Tools::findVariable(theIsParameter ? theParameter : FeaturePtr(),
130 *it, aValue, aParamRes, theParameter->document()))
134 theParamsList.push_back(aParamRes);
135 aContext.push_back(*it + "=" + toStdString(aValue));
137 myInterp->extendLocalContext(aContext);
138 double result = myInterp->evaluate(theExpression, theError);
139 myInterp->clearLocalContext();
143 void InitializationPlugin_EvalListener::processEvaluationEvent(
144 const std::shared_ptr<Events_Message>& theMessage)
146 std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
147 std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
149 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
150 FeaturePtr aParamFeature =
151 std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
152 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
153 AttributeIntegerPtr anAttribute =
154 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
156 int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
157 bool isValid = anError.empty();
159 anAttribute->setCalculatedValue(aValue);
160 anAttribute->setUsedParameters(isValid ?
161 toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
162 anAttribute->setExpressionInvalid(!isValid);
163 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
165 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
166 AttributeDoublePtr anAttribute =
167 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
169 double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
170 bool isValid = anError.empty();
172 anAttribute->setCalculatedValue(aValue);
173 anAttribute->setUsedParameters(isValid ?
174 toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
175 anAttribute->setExpressionInvalid(!isValid);
176 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
178 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
179 AttributePointPtr anAttribute =
180 std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
181 std::string aText[] = {
182 anAttribute->textX(),
183 anAttribute->textY(),
186 double aCalculatedValue[] = {
191 for (int i = 0; i < 3; ++i) {
193 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
194 bool isValid = anError.empty();
195 if (isValid) aCalculatedValue[i] = aValue;
196 anAttribute->setUsedParameters(i,
197 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
198 anAttribute->setExpressionInvalid(i, !isValid);
199 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
201 anAttribute->setCalculatedValue(aCalculatedValue[0],
203 aCalculatedValue[2]);
205 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) {
206 AttributePoint2DPtr anAttribute =
207 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
208 std::string aText[] = {
209 anAttribute->textX(),
212 double aCalculatedValue[] = {
216 for (int i = 0; i < 2; ++i) {
218 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
219 bool isValid = anError.empty();
220 if (isValid) aCalculatedValue[i] = aValue;
221 anAttribute->setUsedParameters(i,
222 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
223 anAttribute->setExpressionInvalid(i, !isValid);
224 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
226 anAttribute->setCalculatedValue(aCalculatedValue[0],
227 aCalculatedValue[1]);