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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
23 #include <InitializationPlugin_EvalListener.h>
24 #include <ParametersPlugin_Parameter.h>
25 #include <InitializationPlugin_PyInterp.h>
27 #include <Events_InfoMessage.h>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_AttributeInteger.h>
31 #include <ModelAPI_AttributeRefList.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_AttributeValidator.h>
34 #include <ModelAPI_Document.h>
35 #include <ModelAPI_Events.h>
36 #include <ModelAPI_ResultParameter.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_Tools.h>
40 #include <GeomDataAPI_Point.h>
41 #include <GeomDataAPI_Point2D.h>
47 //------------------------------------------------------------------------------
50 std::string toStdString(double theValue)
52 std::ostringstream sstream;
54 size_t aPos = sstream.str().find(".");
55 std::string aPnt = "";
56 if (aPos == std::string::npos)
58 return sstream.str() + aPnt;
61 std::set<std::string> toSet(const std::list<std::string>& theContainer)
63 return std::set<std::string>(theContainer.begin(), theContainer.end());
66 //------------------------------------------------------------------------------
68 InitializationPlugin_EvalListener::InitializationPlugin_EvalListener()
70 Events_Loop* aLoop = Events_Loop::loop();
72 aLoop->registerListener(this, ModelAPI_AttributeEvalMessage::eventId(), NULL, true);
73 aLoop->registerListener(this, ModelAPI_ParameterEvalMessage::eventId(), NULL, true);
74 aLoop->registerListener(this, ModelAPI_ComputePositionsMessage::eventId(), NULL, true);
76 myInterp = std::shared_ptr<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
77 myInterp->initialize();
80 InitializationPlugin_EvalListener::~InitializationPlugin_EvalListener()
84 void InitializationPlugin_EvalListener::processEvent(
85 const std::shared_ptr<Events_Message>& theMessage)
87 if (!theMessage.get())
90 if (theMessage->eventID() == ModelAPI_AttributeEvalMessage::eventId()) {
91 processEvaluationEvent(theMessage);
92 } else if (theMessage->eventID() == ModelAPI_ParameterEvalMessage::eventId()) {
93 std::shared_ptr<ModelAPI_ParameterEvalMessage> aMsg =
94 std::dynamic_pointer_cast<ModelAPI_ParameterEvalMessage>(theMessage);
95 FeaturePtr aParam = aMsg->parameter();
96 std::string anExp = aParam->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
98 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
99 double aResult = evaluate(aParam, anExp, anError, aParamsList, true);
100 aMsg->setResults(aParamsList, aResult, anError);
101 } else if (theMessage->eventID() == ModelAPI_ComputePositionsMessage::eventId()) {
102 std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
103 std::dynamic_pointer_cast<ModelAPI_ComputePositionsMessage>(theMessage);
104 aMsg->setPositions(myInterp->positions(aMsg->expression(), aMsg->parameter()));
108 double InitializationPlugin_EvalListener::evaluate(FeaturePtr theParameter,
109 const std::string& theExpression, std::string& theError,
110 std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
111 const bool theIsParameter)
113 std::list<std::string> anExprParams = myInterp->compile(theExpression);
114 // find expression's params in the model
115 std::list<std::string> aContext;
116 std::list<std::string>::iterator it = anExprParams.begin();
117 for ( ; it != anExprParams.end(); it++) {
119 ResultParameterPtr aParamRes;
120 // If variable does not exist python interpreter will generate an error. It is OK.
121 // But due to the issue 1479 it should not check the history position of parameters relatively
122 // to feature that contains expression
123 if (!ModelAPI_Tools::findVariable(theIsParameter ? theParameter : FeaturePtr(),
124 *it, aValue, aParamRes, theParameter->document()))
128 theParamsList.push_back(aParamRes);
129 aContext.push_back(*it + "=" + toStdString(aValue));
131 myInterp->extendLocalContext(aContext);
132 double result = myInterp->evaluate(theExpression, theError);
133 myInterp->clearLocalContext();
137 void InitializationPlugin_EvalListener::processEvaluationEvent(
138 const std::shared_ptr<Events_Message>& theMessage)
140 std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
141 std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
143 std::list<std::shared_ptr<ModelAPI_ResultParameter> > aParamsList;
144 FeaturePtr aParamFeature =
145 std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
146 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
147 AttributeIntegerPtr anAttribute =
148 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
150 int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
151 bool isValid = anError.empty();
153 anAttribute->setCalculatedValue(aValue);
154 anAttribute->setUsedParameters(isValid ?
155 toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
156 anAttribute->setExpressionInvalid(!isValid);
157 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
159 if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
160 AttributeDoublePtr anAttribute =
161 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
163 double aValue = evaluate(aParamFeature, anAttribute->text(), anError, aParamsList);
164 bool isValid = anError.empty();
166 anAttribute->setCalculatedValue(aValue);
167 anAttribute->setUsedParameters(isValid ?
168 toSet(myInterp->compile(anAttribute->text())) : std::set<std::string>());
169 anAttribute->setExpressionInvalid(!isValid);
170 anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
172 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) {
173 AttributePointPtr anAttribute =
174 std::dynamic_pointer_cast<GeomDataAPI_Point>(aMessage->attribute());
175 std::string aText[] = {
176 anAttribute->textX(),
177 anAttribute->textY(),
180 double aCalculatedValue[] = {
185 for (int i = 0; i < 3; ++i) {
187 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
188 bool isValid = anError.empty();
189 if (isValid) aCalculatedValue[i] = aValue;
190 anAttribute->setUsedParameters(i,
191 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
192 anAttribute->setExpressionInvalid(i, !isValid);
193 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
195 anAttribute->setCalculatedValue(aCalculatedValue[0],
197 aCalculatedValue[2]);
199 if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) {
200 AttributePoint2DPtr anAttribute =
201 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aMessage->attribute());
202 std::string aText[] = {
203 anAttribute->textX(),
206 double aCalculatedValue[] = {
210 for (int i = 0; i < 2; ++i) {
212 double aValue = evaluate(aParamFeature, aText[i], anError, aParamsList);
213 bool isValid = anError.empty();
214 if (isValid) aCalculatedValue[i] = aValue;
215 anAttribute->setUsedParameters(i,
216 isValid ? toSet(myInterp->compile(aText[i])) : std::set<std::string>());
217 anAttribute->setExpressionInvalid(i, !isValid);
218 anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError);
220 anAttribute->setCalculatedValue(aCalculatedValue[0],
221 aCalculatedValue[1]);