| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Eivind Kvedalen <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <App/Application.h> |
| 25 |
#include <App/Document.h> |
| 26 |
#include <App/DocumentObject.h> |
| 27 |
#include <App/DocumentObserver.h> |
| 28 |
#include <Base/Reader.h> |
| 29 |
#include <Base/Tools.h> |
| 30 |
#include <Base/Writer.h> |
| 31 |
#include <CXX/Objects.hxx> |
| 32 |
|
| 33 |
#include "PropertyExpressionEngine.h" |
| 34 |
#include "ExpressionVisitors.h" |
| 35 |
|
| 36 |
|
| 37 |
FC_LOG_LEVEL_INIT("App", true); |
| 38 |
|
| 39 |
using namespace App; |
| 40 |
using namespace Base; |
| 41 |
using namespace boost; |
| 42 |
namespace sp = std::placeholders; |
| 43 |
|
| 44 |
TYPESYSTEM_SOURCE_ABSTRACT(App::PropertyExpressionContainer, App::PropertyXLinkContainer) |
| 45 |
|
| 46 |
static std::set<PropertyExpressionContainer*> _ExprContainers; |
| 47 |
|
| 48 |
PropertyExpressionContainer::PropertyExpressionContainer() |
| 49 |
{ |
| 50 |
static bool inited; |
| 51 |
if (!inited) { |
| 52 |
inited = true; |
| 53 |
GetApplication().signalRelabelDocument.connect( |
| 54 |
PropertyExpressionContainer::slotRelabelDocument); |
| 55 |
GetApplication().signalRenameDynamicProperty.connect( |
| 56 |
PropertyExpressionContainer::slotRenameDynamicProperty); |
| 57 |
} |
| 58 |
_ExprContainers.insert(this); |
| 59 |
} |
| 60 |
|
| 61 |
PropertyExpressionContainer::~PropertyExpressionContainer() |
| 62 |
{ |
| 63 |
_ExprContainers.erase(this); |
| 64 |
} |
| 65 |
|
| 66 |
void PropertyExpressionContainer::slotRelabelDocument(const App::Document& doc) |
| 67 |
{ |
| 68 |
// For use a private _ExprContainers to track all living |
| 69 |
// PropertyExpressionContainer including those inside undo/redo stack, |
| 70 |
// because document relabel is not undoable/redoable. |
| 71 |
|
| 72 |
if (doc.getOldLabel() != doc.Label.getValue()) { |
| 73 |
for (auto prop : _ExprContainers) { |
| 74 |
prop->onRelabeledDocument(doc); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void PropertyExpressionContainer::slotRenameDynamicProperty(const App::Property& prop, const char* oldName) |
| 80 |
{ |
| 81 |
for (auto container : _ExprContainers) { |
| 82 |
container->onRenameDynamicProperty(prop, oldName); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/////////////////////////////////////////////////////////////////////////////////////// |
| 87 |
|
| 88 |
struct PropertyExpressionEngine::Private |
| 89 |
{ |
| 90 |
// For some reason, MSVC has trouble with vector of scoped_connection if |
| 91 |
// defined in header, hence the private structure here. |
| 92 |
std::vector<boost::signals2::scoped_connection> conns; |
| 93 |
std::unordered_map<std::string, std::vector<ObjectIdentifier>> propMap; |
| 94 |
}; |
| 95 |
|
| 96 |
/////////////////////////////////////////////////////////////////////////////////////// |
| 97 |
|
| 98 |
TYPESYSTEM_SOURCE(App::PropertyExpressionEngine, App::PropertyExpressionContainer) |
| 99 |
|
| 100 |
/** |
| 101 |
* @brief Construct a new PropertyExpressionEngine object. |
| 102 |
*/ |
| 103 |
|
| 104 |
PropertyExpressionEngine::PropertyExpressionEngine() |
| 105 |
: validator(0) |
| 106 |
{} |
| 107 |
|
| 108 |
/** |
| 109 |
* @brief Destroy the PropertyExpressionEngine object. |
| 110 |
*/ |
| 111 |
|
| 112 |
PropertyExpressionEngine::~PropertyExpressionEngine() = default; |
| 113 |
|
| 114 |
/** |
| 115 |
* @brief Estimate memory size of this property. |
| 116 |
* |
| 117 |
* \fixme Should probably return something else than 0. |
| 118 |
* |
| 119 |
* @return Size of object. |
| 120 |
*/ |
| 121 |
|
| 122 |
unsigned int PropertyExpressionEngine::getMemSize() const |
| 123 |
{ |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
Property* PropertyExpressionEngine::Copy() const |
| 128 |
{ |
| 129 |
PropertyExpressionEngine* engine = new PropertyExpressionEngine(); |
| 130 |
|
| 131 |
for (const auto& it : expressions) { |
| 132 |
ExpressionInfo info; |
| 133 |
if (it.second.expression) { |
| 134 |
info.expression = std::shared_ptr<Expression>(it.second.expression->copy()); |
| 135 |
} |
| 136 |
engine->expressions[it.first] = info; |
| 137 |
} |
| 138 |
|
| 139 |
engine->validator = validator; |
| 140 |
|
| 141 |
return engine; |
| 142 |
} |
| 143 |
|
| 144 |
void PropertyExpressionEngine::hasSetValue() |
| 145 |
{ |
| 146 |
App::DocumentObject* owner = dynamic_cast<App::DocumentObject*>(getContainer()); |
| 147 |
if (!owner || !owner->isAttachedToDocument() || owner->isRestoring() |
| 148 |
|| testFlag(LinkDetached)) { |
| 149 |
PropertyExpressionContainer::hasSetValue(); |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
std::map<App::DocumentObject*, bool> deps; |
| 154 |
std::vector<std::string> labels; |
| 155 |
unregisterElementReference(); |
| 156 |
UpdateElementReferenceExpressionVisitor<PropertyExpressionEngine> v(*this); |
| 157 |
for (auto& e : expressions) { |
| 158 |
auto expr = e.second.expression; |
| 159 |
if (expr) { |
| 160 |
expr->getDepObjects(deps, &labels); |
| 161 |
if (!restoring) { |
| 162 |
expr->visit(v); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
registerLabelReferences(std::move(labels)); |
| 167 |
|
| 168 |
updateDeps(std::move(deps)); |
| 169 |
|
| 170 |
if (pimpl) { |
| 171 |
pimpl->conns.clear(); |
| 172 |
pimpl->propMap.clear(); |
| 173 |
} |
| 174 |
// check if there is any hidden references |
| 175 |
bool hasHidden = false; |
| 176 |
for (auto& v : _Deps) { |
| 177 |
if (v.second) { |
| 178 |
hasHidden = true; |
| 179 |
break; |
| 180 |
} |
| 181 |
} |
| 182 |
if (hasHidden) { |
| 183 |
if (!pimpl) { |
| 184 |
pimpl = std::make_unique<Private>(); |
| 185 |
} |
| 186 |
for (auto& e : expressions) { |
| 187 |
auto expr = e.second.expression; |
| 188 |
if (!expr) { |
| 189 |
continue; |
| 190 |
} |
| 191 |
for (auto& dep : expr->getIdentifiers()) { |
| 192 |
if (!dep.second) { |
| 193 |
continue; |
| 194 |
} |
| 195 |
const ObjectIdentifier& var = dep.first; |
| 196 |
for (auto& vdep : var.getDep(true)) { |
| 197 |
auto obj = vdep.first; |
| 198 |
auto objName = obj->getFullName() + "."; |
| 199 |
for (auto& propName : vdep.second) { |
| 200 |
std::string key = objName + propName; |
| 201 |
auto& propDeps = pimpl->propMap[key]; |
| 202 |
if (propDeps.empty()) { |
| 203 |
// NOLINTBEGIN |
| 204 |
if (!propName.empty()) { |
| 205 |
pimpl->conns.emplace_back(obj->signalChanged.connect( |
| 206 |
std::bind(&PropertyExpressionEngine::slotChangedProperty, |
| 207 |
this, |
| 208 |
sp::_1, |
| 209 |
sp::_2))); |
| 210 |
} |
| 211 |
else { |
| 212 |
pimpl->conns.emplace_back(obj->signalChanged.connect( |
| 213 |
std::bind(&PropertyExpressionEngine::slotChangedObject, |
| 214 |
this, |
| 215 |
sp::_1, |
| 216 |
sp::_2))); |
| 217 |
} |
| 218 |
// NOLINTEND |
| 219 |
} |
| 220 |
propDeps.push_back(e.first); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
PropertyExpressionContainer::hasSetValue(); |
| 228 |
} |
| 229 |
|
| 230 |
void PropertyExpressionEngine::updateHiddenReference(const std::string& key) |
| 231 |
{ |
| 232 |
if (!pimpl) { |
| 233 |
return; |
| 234 |
} |
| 235 |
auto it = pimpl->propMap.find(key); |
| 236 |
if (it == pimpl->propMap.end()) { |
| 237 |
return; |
| 238 |
} |
| 239 |
for (auto& var : it->second) { |
| 240 |
auto it = expressions.find(var); |
| 241 |
if (it == expressions.end() || it->second.busy) { |
| 242 |
continue; |
| 243 |
} |
| 244 |
Property* myProp = var.getProperty(); |
| 245 |
if (!myProp) { |
| 246 |
continue; |
| 247 |
} |
| 248 |
Base::StateLocker guard(it->second.busy); |
| 249 |
App::any value; |
| 250 |
try { |
| 251 |
value = it->second.expression->getValueAsAny(); |
| 252 |
if (!isAnyEqual(value, myProp->getPathValue(var))) { |
| 253 |
myProp->setPathValue(var, value); |
| 254 |
} |
| 255 |
} |
| 256 |
catch (Base::Exception& e) { |
| 257 |
e.reportException(); |
| 258 |
FC_ERR("Failed to evaluate property binding " << myProp->getFullName() |
| 259 |
<< " on change of " << key); |
| 260 |
} |
| 261 |
catch (std::bad_cast&) { |
| 262 |
FC_ERR("Invalid type '" << value.type().name() << "' in property binding " |
| 263 |
<< myProp->getFullName() << " on change of " << key); |
| 264 |
} |
| 265 |
catch (std::exception& e) { |
| 266 |
FC_ERR(e.what()); |
| 267 |
FC_ERR("Failed to evaluate property binding " << myProp->getFullName() |
| 268 |
<< " on change of " << key); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
void PropertyExpressionEngine::slotChangedObject(const App::DocumentObject& obj, |
| 274 |
const App::Property&) |
| 275 |
{ |
| 276 |
updateHiddenReference(obj.getFullName()); |
| 277 |
} |
| 278 |
|
| 279 |
void PropertyExpressionEngine::slotChangedProperty(const App::DocumentObject&, |
| 280 |
const App::Property& prop) |
| 281 |
{ |
| 282 |
updateHiddenReference(prop.getFullName()); |
| 283 |
} |
| 284 |
|
| 285 |
void PropertyExpressionEngine::Paste(const Property& from) |
| 286 |
{ |
| 287 |
const PropertyExpressionEngine& fromee = dynamic_cast<const PropertyExpressionEngine&>(from); |
| 288 |
|
| 289 |
AtomicPropertyChange signaller(*this); |
| 290 |
|
| 291 |
expressions.clear(); |
| 292 |
for (auto& e : fromee.expressions) { |
| 293 |
ExpressionInfo info; |
| 294 |
if (e.second.expression) { |
| 295 |
info.expression = std::shared_ptr<Expression>(e.second.expression->copy()); |
| 296 |
} |
| 297 |
expressions[e.first] = info; |
| 298 |
expressionChanged(e.first); |
| 299 |
} |
| 300 |
validator = fromee.validator; |
| 301 |
signaller.tryInvoke(); |
| 302 |
} |
| 303 |
|
| 304 |
void PropertyExpressionEngine::Save(Base::Writer& writer) const |
| 305 |
{ |
| 306 |
writer.Stream() << writer.ind() << "<ExpressionEngine count=\"" << expressions.size(); |
| 307 |
if (PropertyExpressionContainer::_XLinks.empty()) { |
| 308 |
writer.Stream() << "\">" << std::endl; |
| 309 |
writer.incInd(); |
| 310 |
} |
| 311 |
else { |
| 312 |
writer.Stream() << R"(" xlink="1">)" << std::endl; |
| 313 |
writer.incInd(); |
| 314 |
PropertyExpressionContainer::Save(writer); |
| 315 |
} |
| 316 |
for (const auto& it : expressions) { |
| 317 |
std::string expression, comment; |
| 318 |
if (it.second.expression) { |
| 319 |
expression = it.second.expression->toString(true); |
| 320 |
comment = it.second.expression->comment; |
| 321 |
} |
| 322 |
|
| 323 |
writer.Stream() << writer.ind() << "<Expression path=\"" |
| 324 |
<< Property::encodeAttribute(it.first.toString()) << "\" expression=\"" |
| 325 |
<< Property::encodeAttribute(expression) << "\""; |
| 326 |
if (!comment.empty()) { |
| 327 |
writer.Stream() << " comment=\"" << Property::encodeAttribute(comment) << "\""; |
| 328 |
} |
| 329 |
writer.Stream() << "/>" << std::endl; |
| 330 |
} |
| 331 |
writer.decInd(); |
| 332 |
writer.Stream() << writer.ind() << "</ExpressionEngine>" << std::endl; |
| 333 |
} |
| 334 |
|
| 335 |
void PropertyExpressionEngine::Restore(Base::XMLReader& reader) |
| 336 |
{ |
| 337 |
reader.readElement("ExpressionEngine"); |
| 338 |
int count = reader.getAttribute<double>("count"); |
| 339 |
|
| 340 |
if (reader.hasAttribute("xlink") && reader.getAttribute<bool>("xlink")) { |
| 341 |
PropertyExpressionContainer::Restore(reader); |
| 342 |
} |
| 343 |
|
| 344 |
restoredExpressions = std::make_unique<std::vector<RestoredExpression>>(); |
| 345 |
restoredExpressions->reserve(count); |
| 346 |
for (int i = 0; i < count; ++i) { |
| 347 |
|
| 348 |
reader.readElement("Expression"); |
| 349 |
restoredExpressions->emplace_back(); |
| 350 |
auto& info = restoredExpressions->back(); |
| 351 |
info.path = reader.getAttribute<const char*>("path"); |
| 352 |
info.expr = reader.getAttribute<const char*>("expression"); |
| 353 |
if (reader.hasAttribute("comment")) { |
| 354 |
info.comment = reader.getAttribute<const char*>("comment"); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
reader.readEndElement("ExpressionEngine"); |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* @brief Update graph structure with given path and expression. |
| 363 |
* @param path Path |
| 364 |
* @param expression Expression to query for dependencies |
| 365 |
* @param nodes Map with nodes of graph, including dependencies of 'expression' |
| 366 |
* @param revNodes Reverse map of the nodes, containing only the given paths, without dependencies. |
| 367 |
* @param edges Edges in graph |
| 368 |
*/ |
| 369 |
|
| 370 |
void PropertyExpressionEngine::buildGraphStructures( |
| 371 |
const ObjectIdentifier& path, |
| 372 |
const std::shared_ptr<Expression> expression, |
| 373 |
boost::unordered_map<ObjectIdentifier, int>& nodes, |
| 374 |
boost::unordered_map<int, ObjectIdentifier>& revNodes, |
| 375 |
std::vector<Edge>& edges) const |
| 376 |
{ |
| 377 |
/* Insert target property into nodes structure */ |
| 378 |
if (nodes.find(path) == nodes.end()) { |
| 379 |
int s = nodes.size(); |
| 380 |
|
| 381 |
revNodes[s] = path; |
| 382 |
nodes[path] = s; |
| 383 |
} |
| 384 |
else { |
| 385 |
revNodes[nodes[path]] = path; |
| 386 |
} |
| 387 |
|
| 388 |
/* Insert dependencies into nodes structure */ |
| 389 |
ExpressionDeps deps; |
| 390 |
if (expression) { |
| 391 |
deps = expression->getDeps(); |
| 392 |
} |
| 393 |
|
| 394 |
for (auto& dep : deps) { |
| 395 |
for (auto& info : dep.second) { |
| 396 |
if (info.first.empty()) { |
| 397 |
continue; |
| 398 |
} |
| 399 |
for (auto& oid : info.second) { |
| 400 |
ObjectIdentifier cPath(oid.canonicalPath()); |
| 401 |
if (nodes.find(cPath) == nodes.end()) { |
| 402 |
int s = nodes.size(); |
| 403 |
nodes[cPath] = s; |
| 404 |
} |
| 405 |
edges.emplace_back(nodes[path], nodes[cPath]); |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* @brief Create a canonical object identifier of the given object \a p. |
| 413 |
* @param p ObjectIndentifier |
| 414 |
* @return New ObjectIdentifier |
| 415 |
*/ |
| 416 |
|
| 417 |
ObjectIdentifier PropertyExpressionEngine::canonicalPath(const ObjectIdentifier& oid) const |
| 418 |
{ |
| 419 |
DocumentObject* docObj = freecad_cast<DocumentObject*>(getContainer()); |
| 420 |
|
| 421 |
// Am I owned by a DocumentObject? |
| 422 |
if (!docObj) { |
| 423 |
throw Base::RuntimeError("PropertyExpressionEngine must be owned by a DocumentObject."); |
| 424 |
} |
| 425 |
|
| 426 |
int ptype; |
| 427 |
Property* prop = oid.getProperty(&ptype); |
| 428 |
|
| 429 |
// oid pointing to a property...? |
| 430 |
if (!prop) { |
| 431 |
throw Base::RuntimeError(oid.resolveErrorString().c_str()); |
| 432 |
} |
| 433 |
|
| 434 |
if (ptype || prop->getContainer() != getContainer()) { |
| 435 |
return oid; |
| 436 |
} |
| 437 |
|
| 438 |
// In case someone calls this with p pointing to a PropertyExpressionEngine for some reason |
| 439 |
if (prop->isDerivedFrom(PropertyExpressionEngine::classTypeId)) { |
| 440 |
return oid; |
| 441 |
} |
| 442 |
|
| 443 |
// Dispatch call to actual canonicalPath implementation |
| 444 |
return oid.canonicalPath(); |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* @brief Number of expressions managed by this object. |
| 449 |
* @return Number of expressions. |
| 450 |
*/ |
| 451 |
|
| 452 |
size_t PropertyExpressionEngine::numExpressions() const |
| 453 |
{ |
| 454 |
return expressions.size(); |
| 455 |
} |
| 456 |
|
| 457 |
void PropertyExpressionEngine::afterRestore() |
| 458 |
{ |
| 459 |
DocumentObject* docObj = freecad_cast<DocumentObject*>(getContainer()); |
| 460 |
if (restoredExpressions && docObj) { |
| 461 |
Base::FlagToggler<bool> flag(restoring); |
| 462 |
AtomicPropertyChange signaller(*this); |
| 463 |
|
| 464 |
PropertyExpressionContainer::afterRestore(); |
| 465 |
ObjectIdentifier::DocumentMapper mapper(this->_DocMap); |
| 466 |
|
| 467 |
for (auto& info : *restoredExpressions) { |
| 468 |
tryRestoreExpression(docObj, info); |
| 469 |
} |
| 470 |
signaller.tryInvoke(); |
| 471 |
} |
| 472 |
restoredExpressions.reset(); |
| 473 |
} |
| 474 |
|
| 475 |
void PropertyExpressionEngine::tryRestoreExpression(DocumentObject* docObj, |
| 476 |
const RestoredExpression& info) |
| 477 |
{ |
| 478 |
try { |
| 479 |
ObjectIdentifier path = ObjectIdentifier::parse(docObj, info.path); |
| 480 |
if (!info.expr.empty()) { |
| 481 |
std::shared_ptr<Expression> expression( |
| 482 |
Expression::parse(docObj, info.expr)); |
| 483 |
if (expression) { |
| 484 |
expression->comment = info.comment; |
| 485 |
} |
| 486 |
setValue(path, expression); |
| 487 |
} |
| 488 |
} |
| 489 |
catch (const Base::Exception& e) { |
| 490 |
FC_ERR("Failed to restore " << docObj->getFullName() |
| 491 |
<< '.' |
| 492 |
<< getName() |
| 493 |
<< ": " |
| 494 |
<< e.what()); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
void PropertyExpressionEngine::onContainerRestored() |
| 499 |
{ |
| 500 |
Base::FlagToggler<bool> flag(restoring); |
| 501 |
unregisterElementReference(); |
| 502 |
UpdateElementReferenceExpressionVisitor<PropertyExpressionEngine> v(*this); |
| 503 |
for (auto& e : expressions) { |
| 504 |
auto expr = e.second.expression; |
| 505 |
if (expr) { |
| 506 |
expr->visit(v); |
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* @brief Get expression for \a path. |
| 513 |
* @param path ObjectIndentifier to query for. |
| 514 |
* @return Expression for \a path, or empty boost::any if not found. |
| 515 |
*/ |
| 516 |
|
| 517 |
const boost::any PropertyExpressionEngine::getPathValue(const App::ObjectIdentifier& path) const |
| 518 |
{ |
| 519 |
// Get a canonical path |
| 520 |
ObjectIdentifier usePath(canonicalPath(path)); |
| 521 |
|
| 522 |
auto i = expressions.find(usePath); |
| 523 |
if (i != expressions.end()) { |
| 524 |
return i->second; |
| 525 |
} |
| 526 |
|
| 527 |
return boost::any(); |
| 528 |
} |
| 529 |
|
| 530 |
/** |
| 531 |
* @brief Set expression with optional comment for \a path. |
| 532 |
* @param path Path to update |
| 533 |
* @param expr New expression |
| 534 |
* @param comment Optional comment. |
| 535 |
*/ |
| 536 |
|
| 537 |
void PropertyExpressionEngine::setValue(const ObjectIdentifier& path, |
| 538 |
std::shared_ptr<Expression> expr) |
| 539 |
{ |
| 540 |
ObjectIdentifier usePath(canonicalPath(path)); |
| 541 |
const Property* prop = usePath.getProperty(); |
| 542 |
|
| 543 |
// Try to access value; it should trigger an exception if it is not supported, or if the path is |
| 544 |
// invalid |
| 545 |
prop->getPathValue(usePath); |
| 546 |
|
| 547 |
// Check if the current expression equals the new one and do nothing if so to reduce unneeded |
| 548 |
// computations |
| 549 |
auto it = expressions.find(usePath); |
| 550 |
if (it != expressions.end() |
| 551 |
&& (expr == it->second.expression |
| 552 |
|| (expr && it->second.expression && expr->isSame(*it->second.expression)))) { |
| 553 |
return; |
| 554 |
} |
| 555 |
|
| 556 |
if (expr) { |
| 557 |
std::string error = validateExpression(usePath, expr); |
| 558 |
if (!error.empty()) { |
| 559 |
throw Base::RuntimeError(error.c_str()); |
| 560 |
} |
| 561 |
AtomicPropertyChange signaller(*this); |
| 562 |
expressions[usePath] = ExpressionInfo(expr); |
| 563 |
expressionChanged(usePath); |
| 564 |
signaller.tryInvoke(); |
| 565 |
} |
| 566 |
else if (it != expressions.end()) { |
| 567 |
AtomicPropertyChange signaller(*this); |
| 568 |
expressions.erase(it); |
| 569 |
expressionChanged(usePath); |
| 570 |
signaller.tryInvoke(); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
/** |
| 575 |
* @brief The cycle_detector struct is used by the boost graph routines to detect cycles in the |
| 576 |
* graph. |
| 577 |
*/ |
| 578 |
|
| 579 |
struct cycle_detector: public boost::dfs_visitor<> |
| 580 |
{ |
| 581 |
cycle_detector(bool& has_cycle, int& src) |
| 582 |
: _has_cycle(has_cycle) |
| 583 |
, _src(src) |
| 584 |
{} |
| 585 |
|
| 586 |
template<class Edge, class Graph> |
| 587 |
void back_edge(Edge e, Graph& g) |
| 588 |
{ |
| 589 |
_has_cycle = true; |
| 590 |
_src = source(e, g); |
| 591 |
} |
| 592 |
|
| 593 |
protected: |
| 594 |
bool& _has_cycle; |
| 595 |
int& _src; |
| 596 |
}; |
| 597 |
|
| 598 |
/** |
| 599 |
* @brief Build a graph of all expressions in \a exprs. |
| 600 |
* @param exprs Expressions to use in graph |
| 601 |
* @param revNodes Map from int[nodeid] to ObjectIndentifer. |
| 602 |
* @param g Graph to update. May contain additional nodes than in revNodes, because of outside |
| 603 |
* dependencies. |
| 604 |
*/ |
| 605 |
|
| 606 |
void PropertyExpressionEngine::buildGraph(const ExpressionMap& exprs, |
| 607 |
boost::unordered_map<int, ObjectIdentifier>& revNodes, |
| 608 |
DiGraph& g, |
| 609 |
ExecuteOption option) const |
| 610 |
{ |
| 611 |
boost::unordered_map<ObjectIdentifier, int> nodes; |
| 612 |
std::vector<Edge> edges; |
| 613 |
|
| 614 |
// Build data structure for graph |
| 615 |
for (const auto& expr : exprs) { |
| 616 |
if (option != ExecuteAll) { |
| 617 |
auto prop = expr.first.getProperty(); |
| 618 |
if (!prop) { |
| 619 |
throw Base::RuntimeError("Path does not resolve to a property."); |
| 620 |
} |
| 621 |
bool is_output = |
| 622 |
prop->testStatus(App::Property::Output) || (prop->getType() & App::Prop_Output); |
| 623 |
if ((is_output && option == ExecuteNonOutput) |
| 624 |
|| (!is_output && option == ExecuteOutput)) { |
| 625 |
continue; |
| 626 |
} |
| 627 |
if (option == ExecuteOnRestore && !prop->testStatus(Property::Transient) |
| 628 |
&& !(prop->getType() & Prop_Transient) |
| 629 |
&& !prop->testStatus(Property::EvalOnRestore)) { |
| 630 |
continue; |
| 631 |
} |
| 632 |
} |
| 633 |
buildGraphStructures(expr.first, expr.second.expression, nodes, revNodes, edges); |
| 634 |
} |
| 635 |
|
| 636 |
// Create graph |
| 637 |
g = DiGraph(nodes.size()); |
| 638 |
|
| 639 |
// Add edges to graph |
| 640 |
for (const auto& edge : edges) { |
| 641 |
add_edge(edge.first, edge.second, g); |
| 642 |
} |
| 643 |
|
| 644 |
// Check for cycles |
| 645 |
bool has_cycle = false; |
| 646 |
int src = -1; |
| 647 |
cycle_detector vis(has_cycle, src); |
| 648 |
depth_first_search(g, visitor(vis)); |
| 649 |
|
| 650 |
if (has_cycle) { |
| 651 |
std::string s = revNodes[src].toString() + " reference creates a cyclic dependency."; |
| 652 |
|
| 653 |
throw Base::RuntimeError(s.c_str()); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
/** |
| 658 |
* The code below builds a graph for all expressions in the engine, and |
| 659 |
* finds any circular dependencies. It also computes the internal evaluation |
| 660 |
* order, in case properties depends on each other. |
| 661 |
*/ |
| 662 |
|
| 663 |
std::vector<App::ObjectIdentifier> |
| 664 |
PropertyExpressionEngine::computeEvaluationOrder(ExecuteOption option) |
| 665 |
{ |
| 666 |
std::vector<App::ObjectIdentifier> evaluationOrder; |
| 667 |
boost::unordered_map<int, ObjectIdentifier> revNodes; |
| 668 |
DiGraph g; |
| 669 |
|
| 670 |
buildGraph(expressions, revNodes, g, option); |
| 671 |
|
| 672 |
/* Compute evaluation order for expressions */ |
| 673 |
std::vector<int> c; |
| 674 |
topological_sort(g, std::back_inserter(c)); |
| 675 |
|
| 676 |
for (int i : c) { |
| 677 |
// we return the evaluation order for our properties, not the dependencies |
| 678 |
// the topo sort will contain node ids for both our props and their deps |
| 679 |
if (revNodes.find(i) != revNodes.end()) { |
| 680 |
evaluationOrder.push_back(revNodes[i]); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
return evaluationOrder; |
| 685 |
} |
| 686 |
|
| 687 |
/** |
| 688 |
* @brief Compute and update values of all registered expressions. |
| 689 |
* @return StdReturn on success. |
| 690 |
*/ |
| 691 |
|
| 692 |
DocumentObjectExecReturn* App::PropertyExpressionEngine::execute(ExecuteOption option, |
| 693 |
bool* touched) |
| 694 |
{ |
| 695 |
DocumentObject* docObj = freecad_cast<DocumentObject*>(getContainer()); |
| 696 |
|
| 697 |
if (!docObj) { |
| 698 |
throw Base::RuntimeError("PropertyExpressionEngine must be owned by a DocumentObject."); |
| 699 |
} |
| 700 |
|
| 701 |
if (running) { |
| 702 |
return DocumentObject::StdReturn; |
| 703 |
} |
| 704 |
|
| 705 |
if (option == ExecuteOnRestore) { |
| 706 |
bool found = false; |
| 707 |
for (auto& e : expressions) { |
| 708 |
auto prop = e.first.getProperty(); |
| 709 |
if (!prop) { |
| 710 |
continue; |
| 711 |
} |
| 712 |
if (prop->testStatus(App::Property::Transient) |
| 713 |
|| (prop->getType() & App::Prop_Transient) |
| 714 |
|| prop->testStatus(App::Property::EvalOnRestore)) { |
| 715 |
found = true; |
| 716 |
break; |
| 717 |
} |
| 718 |
} |
| 719 |
if (!found) { |
| 720 |
return DocumentObject::StdReturn; |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
/* Resetter class, to ensure that the "running" variable gets set to false, even if |
| 725 |
* an exception is thrown. |
| 726 |
*/ |
| 727 |
|
| 728 |
class resetter |
| 729 |
{ |
| 730 |
public: |
| 731 |
explicit resetter(bool& b) |
| 732 |
: _b(b) |
| 733 |
{ |
| 734 |
_b = true; |
| 735 |
} |
| 736 |
~resetter() |
| 737 |
{ |
| 738 |
_b = false; |
| 739 |
} |
| 740 |
|
| 741 |
private: |
| 742 |
bool& _b; |
| 743 |
}; |
| 744 |
|
| 745 |
resetter r(running); |
| 746 |
|
| 747 |
// Compute evaluation order |
| 748 |
std::vector<App::ObjectIdentifier> evaluationOrder = computeEvaluationOrder(option); |
| 749 |
std::vector<ObjectIdentifier>::const_iterator it = evaluationOrder.begin(); |
| 750 |
|
| 751 |
#ifdef FC_PROPERTYEXPRESSIONENGINE_LOG |
| 752 |
std::clog << "Computing expressions for " << getName() << std::endl; |
| 753 |
#endif |
| 754 |
|
| 755 |
/* Evaluate the expressions, and update properties */ |
| 756 |
for (; it != evaluationOrder.end(); ++it) { |
| 757 |
|
| 758 |
// Get property to update |
| 759 |
Property* prop = it->getProperty(); |
| 760 |
|
| 761 |
if (!prop) { |
| 762 |
throw Base::RuntimeError("Path does not resolve to a property."); |
| 763 |
} |
| 764 |
|
| 765 |
DocumentObject* parent = freecad_cast<DocumentObject*>(prop->getContainer()); |
| 766 |
|
| 767 |
/* Make sure property belongs to the same container as this PropertyExpressionEngine */ |
| 768 |
if (parent != docObj) { |
| 769 |
throw Base::RuntimeError("Invalid property owner."); |
| 770 |
} |
| 771 |
|
| 772 |
/* Set value of property */ |
| 773 |
App::any value; |
| 774 |
try { |
| 775 |
// Evaluate expression |
| 776 |
std::shared_ptr<App::Expression> expression = expressions[*it].expression; |
| 777 |
if (expression) { |
| 778 |
value = expression->getValueAsAny(); |
| 779 |
|
| 780 |
// Enable value comparison for all expression bindings to reduce |
| 781 |
// unnecessary touch and recompute. |
| 782 |
// |
| 783 |
// This optimization is necessary for some hidden reference to |
| 784 |
// work because it introduce dependency loop. The repeativtive |
| 785 |
// recompute can be stopped if the expression evaluates the same |
| 786 |
// value. |
| 787 |
// |
| 788 |
// In the future, we can generalize the optimization to all |
| 789 |
// property modification, i.e. do not touch unless value change |
| 790 |
// |
| 791 |
// if (option == ExecuteOnRestore && prop->testStatus(Property::EvalOnRestore)) |
| 792 |
{ |
| 793 |
if (isAnyEqual(value, prop->getPathValue(*it))) { |
| 794 |
continue; |
| 795 |
} |
| 796 |
if (touched) { |
| 797 |
*touched = true; |
| 798 |
} |
| 799 |
} |
| 800 |
prop->setPathValue(*it, value); |
| 801 |
} |
| 802 |
} |
| 803 |
catch (Base::Exception& e) { |
| 804 |
std::ostringstream ss; |
| 805 |
ss << e.what() << std::endl << "in property binding '" << prop->getFullName() << "'"; |
| 806 |
e.setMessage(ss.str()); |
| 807 |
throw; |
| 808 |
} |
| 809 |
catch (std::bad_cast&) { |
| 810 |
std::ostringstream ss; |
| 811 |
ss << "Invalid type '" << value.type().name() << "'"; |
| 812 |
ss << "\nin property binding '" << prop->getFullName() << "'"; |
| 813 |
throw Base::TypeError(ss.str().c_str()); |
| 814 |
} |
| 815 |
catch (std::exception& e) { |
| 816 |
std::ostringstream ss; |
| 817 |
ss << e.what() << "\nin property binding '" << prop->getFullName() << "'"; |
| 818 |
throw Base::RuntimeError(ss.str().c_str()); |
| 819 |
} |
| 820 |
} |
| 821 |
return DocumentObject::StdReturn; |
| 822 |
} |
| 823 |
|
| 824 |
/** |
| 825 |
* @brief Find paths to document object. |
| 826 |
* @param obj Document object |
| 827 |
* @param paths Object identifier |
| 828 |
*/ |
| 829 |
|
| 830 |
void PropertyExpressionEngine::getPathsToDocumentObject( |
| 831 |
DocumentObject* obj, |
| 832 |
std::vector<App::ObjectIdentifier>& paths) const |
| 833 |
{ |
| 834 |
DocumentObject* owner = freecad_cast<DocumentObject*>(getContainer()); |
| 835 |
|
| 836 |
if (!owner || owner == obj) { |
| 837 |
return; |
| 838 |
} |
| 839 |
|
| 840 |
for (auto& v : expressions) { |
| 841 |
if (!v.second.expression) { |
| 842 |
continue; |
| 843 |
} |
| 844 |
const auto& deps = v.second.expression->getDeps(); |
| 845 |
auto it = deps.find(obj); |
| 846 |
if (it == deps.end()) { |
| 847 |
continue; |
| 848 |
} |
| 849 |
for (auto& dep : it->second) { |
| 850 |
paths.insert(paths.end(), dep.second.begin(), dep.second.end()); |
| 851 |
} |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
/** |
| 856 |
* @brief Determine whether any dependencies of any of the registered expressions have been touched. |
| 857 |
* @return True if at least on dependency has been touched. |
| 858 |
*/ |
| 859 |
|
| 860 |
bool PropertyExpressionEngine::depsAreTouched() const |
| 861 |
{ |
| 862 |
for (auto& v : _Deps) { |
| 863 |
// v.second indicates if it is a hidden reference |
| 864 |
if (!v.second && v.first->isTouched()) { |
| 865 |
return true; |
| 866 |
} |
| 867 |
} |
| 868 |
return false; |
| 869 |
} |
| 870 |
|
| 871 |
/** |
| 872 |
* @brief Validate the given path and expression. |
| 873 |
* @param path Object Identifier for expression. |
| 874 |
* @param expr Expression tree. |
| 875 |
* @return Empty string on success, error message on failure. |
| 876 |
*/ |
| 877 |
|
| 878 |
std::string |
| 879 |
PropertyExpressionEngine::validateExpression(const ObjectIdentifier& path, |
| 880 |
std::shared_ptr<const Expression> expr) const |
| 881 |
{ |
| 882 |
std::string error; |
| 883 |
ObjectIdentifier usePath(canonicalPath(path)); |
| 884 |
|
| 885 |
if (validator) { |
| 886 |
error = validator(usePath, expr); |
| 887 |
if (!error.empty()) { |
| 888 |
return error; |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
// Get document object |
| 893 |
DocumentObject* pathDocObj = usePath.getDocumentObject(); |
| 894 |
assert(pathDocObj); |
| 895 |
|
| 896 |
auto inList = pathDocObj->getInListEx(true); |
| 897 |
for (auto& v : expr->getDepObjects()) { |
| 898 |
auto docObj = v.first; |
| 899 |
if (!v.second && inList.contains(docObj)) { |
| 900 |
std::stringstream ss; |
| 901 |
ss << "cyclic reference to " << docObj->getFullName(); |
| 902 |
return ss.str(); |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
// Check for internal document object dependencies |
| 907 |
|
| 908 |
// Copy current expressions |
| 909 |
ExpressionMap newExpressions = expressions; |
| 910 |
|
| 911 |
// Add expression in question |
| 912 |
std::shared_ptr<Expression> exprClone(expr->copy()); |
| 913 |
newExpressions[usePath].expression = exprClone; |
| 914 |
|
| 915 |
// Build graph; an exception will be thrown if it is not a DAG |
| 916 |
try { |
| 917 |
boost::unordered_map<int, ObjectIdentifier> revNodes; |
| 918 |
DiGraph g; |
| 919 |
|
| 920 |
buildGraph(newExpressions, revNodes, g); |
| 921 |
} |
| 922 |
catch (const Base::Exception& e) { |
| 923 |
return e.what(); |
| 924 |
} |
| 925 |
|
| 926 |
return {}; |
| 927 |
} |
| 928 |
|
| 929 |
/** |
| 930 |
* @brief Rename paths based on \a paths. |
| 931 |
* @param paths Map with current and new object identifier. |
| 932 |
*/ |
| 933 |
|
| 934 |
void PropertyExpressionEngine::renameExpressions( |
| 935 |
const std::map<ObjectIdentifier, ObjectIdentifier>& paths) |
| 936 |
{ |
| 937 |
ExpressionMap newExpressions; |
| 938 |
std::map<ObjectIdentifier, ObjectIdentifier> canonicalPaths; |
| 939 |
|
| 940 |
/* ensure input map uses canonical paths */ |
| 941 |
for (const auto& path : paths) { |
| 942 |
canonicalPaths[canonicalPath(path.first)] = path.second; |
| 943 |
} |
| 944 |
|
| 945 |
for (auto i = expressions.begin(); i != expressions.end(); ++i) { |
| 946 |
auto j = canonicalPaths.find(i->first); |
| 947 |
|
| 948 |
// Renamed now? |
| 949 |
if (j != canonicalPaths.end()) { |
| 950 |
newExpressions[j->second] = i->second; |
| 951 |
} |
| 952 |
else { |
| 953 |
newExpressions[i->first] = i->second; |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
aboutToSetValue(); |
| 958 |
expressions = newExpressions; |
| 959 |
for (auto i = expressions.begin(); i != expressions.end(); ++i) { |
| 960 |
expressionChanged(i->first); |
| 961 |
} |
| 962 |
|
| 963 |
hasSetValue(); |
| 964 |
} |
| 965 |
|
| 966 |
/** |
| 967 |
* @brief Rename object identifiers in the registered expressions. |
| 968 |
* @param paths Map with current and new object identifiers. |
| 969 |
*/ |
| 970 |
|
| 971 |
void PropertyExpressionEngine::renameObjectIdentifiers( |
| 972 |
const std::map<ObjectIdentifier, ObjectIdentifier>& paths) |
| 973 |
{ |
| 974 |
for (const auto& it : expressions) { |
| 975 |
RenameObjectIdentifierExpressionVisitor<PropertyExpressionEngine> v(*this, paths, it.first); |
| 976 |
it.second.expression->visit(v); |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
PyObject* PropertyExpressionEngine::getPyObject() |
| 981 |
{ |
| 982 |
Py::List list; |
| 983 |
for (const auto& it : expressions) { |
| 984 |
Py::Tuple tuple(2); |
| 985 |
tuple.setItem(0, Py::String(it.first.toString())); |
| 986 |
auto expr = it.second.expression; |
| 987 |
tuple.setItem(1, expr ? Py::String(expr->toString()) : Py::None()); |
| 988 |
list.append(tuple); |
| 989 |
} |
| 990 |
return Py::new_reference_to(list); |
| 991 |
} |
| 992 |
|
| 993 |
void PropertyExpressionEngine::setPyObject(PyObject*) |
| 994 |
{ |
| 995 |
throw Base::RuntimeError("Property is read-only"); |
| 996 |
} |
| 997 |
|
| 998 |
/* The policy implemented in the following function is to auto erase binding in |
| 999 |
* case linked object is gone. I think it is better to cause error and get |
| 1000 |
* user's attention |
| 1001 |
* |
| 1002 |
void PropertyExpressionEngine::breakLink(App::DocumentObject *obj, bool clear) { |
| 1003 |
auto owner = dynamic_cast<App::DocumentObject*>(getContainer()); |
| 1004 |
if(!owner) |
| 1005 |
return; |
| 1006 |
if(_Deps.count(obj)==0 && (!clear || obj!=owner || _Deps.empty())) |
| 1007 |
return; |
| 1008 |
AtomicPropertyChange signaler(*this); |
| 1009 |
for(auto it=expressions.begin(),itNext=it;it!=expressions.end();it=itNext) { |
| 1010 |
++itNext; |
| 1011 |
const auto &deps = it->second.expression->getDepObjects(); |
| 1012 |
if(clear) { |
| 1013 |
// here means we are breaking all expression, except those that has |
| 1014 |
// no depdenecy or self dependency |
| 1015 |
if(deps.empty() || (deps.size()==1 && *deps.begin()==owner)) |
| 1016 |
continue; |
| 1017 |
}else if(!deps.count(obj)) |
| 1018 |
continue; |
| 1019 |
auto path = it->first; |
| 1020 |
expressions.erase(it); |
| 1021 |
expressionChanged(path); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
*/ |
| 1025 |
|
| 1026 |
bool PropertyExpressionEngine::adjustLink(const std::set<DocumentObject*>& inList) |
| 1027 |
{ |
| 1028 |
auto owner = dynamic_cast<App::DocumentObject*>(getContainer()); |
| 1029 |
if (!owner) { |
| 1030 |
return false; |
| 1031 |
} |
| 1032 |
bool found = false; |
| 1033 |
for (auto& v : _Deps) { |
| 1034 |
if (inList.contains(v.first)) { |
| 1035 |
found = true; |
| 1036 |
break; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
if (!found) { |
| 1040 |
return false; |
| 1041 |
} |
| 1042 |
|
| 1043 |
AtomicPropertyChange signaler(*this); |
| 1044 |
for (auto& v : expressions) { |
| 1045 |
try { |
| 1046 |
if (v.second.expression && v.second.expression->adjustLinks(inList)) { |
| 1047 |
expressionChanged(v.first); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
catch (Base::Exception& e) { |
| 1051 |
std::ostringstream ss; |
| 1052 |
ss << "Failed to adjust link for " << owner->getFullName() << " in expression " |
| 1053 |
<< v.second.expression->toString() << ": " << e.what(); |
| 1054 |
throw Base::RuntimeError(ss.str()); |
| 1055 |
} |
| 1056 |
} |
| 1057 |
return true; |
| 1058 |
} |
| 1059 |
|
| 1060 |
void PropertyExpressionEngine::updateElementReference(DocumentObject* feature, |
| 1061 |
bool reverse, |
| 1062 |
bool notify) |
| 1063 |
{ |
| 1064 |
(void)notify; |
| 1065 |
if (!feature) { |
| 1066 |
unregisterElementReference(); |
| 1067 |
} |
| 1068 |
UpdateElementReferenceExpressionVisitor<PropertyExpressionEngine> v(*this, feature, reverse); |
| 1069 |
for (auto& e : expressions) { |
| 1070 |
if (e.second.expression) { |
| 1071 |
e.second.expression->visit(v); |
| 1072 |
if (v.changed()) { |
| 1073 |
expressionChanged(e.first); |
| 1074 |
v.reset(); |
| 1075 |
} |
| 1076 |
} |
| 1077 |
} |
| 1078 |
if (feature && v.changed()) { |
| 1079 |
auto owner = dynamic_cast<App::DocumentObject*>(getContainer()); |
| 1080 |
if (owner) { |
| 1081 |
owner->onUpdateElementReference(this); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
bool PropertyExpressionEngine::referenceChanged() const |
| 1087 |
{ |
| 1088 |
return false; |
| 1089 |
} |
| 1090 |
|
| 1091 |
Property* PropertyExpressionEngine::CopyOnImportExternal( |
| 1092 |
const std::map<std::string, std::string>& nameMap) const |
| 1093 |
{ |
| 1094 |
std::unique_ptr<PropertyExpressionEngine> engine; |
| 1095 |
for (auto it = expressions.begin(); it != expressions.end(); ++it) { |
| 1096 |
#ifdef BOOST_NO_CXX11_SMART_PTR |
| 1097 |
std::shared_ptr<Expression> expr(it->second.expression->importSubNames(nameMap).release()); |
| 1098 |
#else |
| 1099 |
std::shared_ptr<Expression> expr(it->second.expression->importSubNames(nameMap)); |
| 1100 |
#endif |
| 1101 |
if (!expr && !engine) { |
| 1102 |
continue; |
| 1103 |
} |
| 1104 |
if (!engine) { |
| 1105 |
engine = std::make_unique<PropertyExpressionEngine>(); |
| 1106 |
for (auto it2 = expressions.begin(); it2 != it; ++it2) { |
| 1107 |
engine->expressions[it2->first] = |
| 1108 |
ExpressionInfo(std::shared_ptr<Expression>(it2->second.expression->copy())); |
| 1109 |
} |
| 1110 |
} |
| 1111 |
else if (!expr) { |
| 1112 |
expr = it->second.expression; |
| 1113 |
} |
| 1114 |
engine->expressions[it->first] = ExpressionInfo(expr); |
| 1115 |
} |
| 1116 |
if (!engine) { |
| 1117 |
return nullptr; |
| 1118 |
} |
| 1119 |
engine->validator = validator; |
| 1120 |
return engine.release(); |
| 1121 |
} |
| 1122 |
|
| 1123 |
Property* PropertyExpressionEngine::CopyOnLabelChange(App::DocumentObject* obj, |
| 1124 |
const std::string& ref, |
| 1125 |
const char* newLabel) const |
| 1126 |
{ |
| 1127 |
std::unique_ptr<PropertyExpressionEngine> engine; |
| 1128 |
for (auto it = expressions.begin(); it != expressions.end(); ++it) { |
| 1129 |
#ifdef BOOST_NO_CXX11_SMART_PTR |
| 1130 |
std::shared_ptr<Expression> expr( |
| 1131 |
it->second.expression->updateLabelReference(obj, ref, newLabel).release()); |
| 1132 |
#else |
| 1133 |
std::shared_ptr<Expression> expr( |
| 1134 |
it->second.expression->updateLabelReference(obj, ref, newLabel)); |
| 1135 |
#endif |
| 1136 |
if (!expr && !engine) { |
| 1137 |
continue; |
| 1138 |
} |
| 1139 |
if (!engine) { |
| 1140 |
engine = std::make_unique<PropertyExpressionEngine>(); |
| 1141 |
for (auto it2 = expressions.begin(); it2 != it; ++it2) { |
| 1142 |
ExpressionInfo info; |
| 1143 |
if (it2->second.expression) { |
| 1144 |
info.expression = std::shared_ptr<Expression>(it2->second.expression->copy()); |
| 1145 |
} |
| 1146 |
engine->expressions[it2->first] = info; |
| 1147 |
} |
| 1148 |
} |
| 1149 |
else if (!expr) { |
| 1150 |
expr = it->second.expression; |
| 1151 |
} |
| 1152 |
engine->expressions[it->first] = ExpressionInfo(expr); |
| 1153 |
} |
| 1154 |
if (!engine) { |
| 1155 |
return nullptr; |
| 1156 |
} |
| 1157 |
engine->validator = validator; |
| 1158 |
return engine.release(); |
| 1159 |
} |
| 1160 |
|
| 1161 |
Property* PropertyExpressionEngine::CopyOnLinkReplace(const App::DocumentObject* parent, |
| 1162 |
App::DocumentObject* oldObj, |
| 1163 |
App::DocumentObject* newObj) const |
| 1164 |
{ |
| 1165 |
std::unique_ptr<PropertyExpressionEngine> engine; |
| 1166 |
for (auto it = expressions.begin(); it != expressions.end(); ++it) { |
| 1167 |
#ifdef BOOST_NO_CXX11_SMART_PTR |
| 1168 |
std::shared_ptr<Expression> expr( |
| 1169 |
it->second.expression->replaceObject(parent, oldObj, newObj).release()); |
| 1170 |
#else |
| 1171 |
std::shared_ptr<Expression> expr( |
| 1172 |
it->second.expression->replaceObject(parent, oldObj, newObj)); |
| 1173 |
#endif |
| 1174 |
if (!expr && !engine) { |
| 1175 |
continue; |
| 1176 |
} |
| 1177 |
if (!engine) { |
| 1178 |
engine = std::make_unique<PropertyExpressionEngine>(); |
| 1179 |
for (auto it2 = expressions.begin(); it2 != it; ++it2) { |
| 1180 |
ExpressionInfo info; |
| 1181 |
if (it2->second.expression) { |
| 1182 |
info.expression = std::shared_ptr<Expression>(it2->second.expression->copy()); |
| 1183 |
} |
| 1184 |
engine->expressions[it2->first] = info; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
else if (!expr) { |
| 1188 |
expr = it->second.expression; |
| 1189 |
} |
| 1190 |
engine->expressions[it->first] = ExpressionInfo(expr); |
| 1191 |
} |
| 1192 |
if (!engine) { |
| 1193 |
return nullptr; |
| 1194 |
} |
| 1195 |
engine->validator = validator; |
| 1196 |
return engine.release(); |
| 1197 |
} |
| 1198 |
|
| 1199 |
std::map<App::ObjectIdentifier, const App::Expression*> |
| 1200 |
PropertyExpressionEngine::getExpressions() const |
| 1201 |
{ |
| 1202 |
std::map<App::ObjectIdentifier, const Expression*> res; |
| 1203 |
for (auto& v : expressions) { |
| 1204 |
res[v.first] = v.second.expression.get(); |
| 1205 |
} |
| 1206 |
return res; |
| 1207 |
} |
| 1208 |
|
| 1209 |
void PropertyExpressionEngine::setExpressions( |
| 1210 |
std::map<App::ObjectIdentifier, App::ExpressionPtr>&& exprs) |
| 1211 |
{ |
| 1212 |
AtomicPropertyChange signaller(*this); |
| 1213 |
#ifdef BOOST_NO_CXX11_SMART_PTR |
| 1214 |
for (auto& v : exprs) { |
| 1215 |
setValue(v.first, std::shared_ptr<Expression>(v.second.release())); |
| 1216 |
} |
| 1217 |
#else |
| 1218 |
for (auto& v : exprs) { |
| 1219 |
setValue(v.first, std::move(v.second)); |
| 1220 |
} |
| 1221 |
#endif |
| 1222 |
} |
| 1223 |
|
| 1224 |
void PropertyExpressionEngine::onRelabeledDocument(const App::Document& doc) |
| 1225 |
{ |
| 1226 |
RelabelDocumentExpressionVisitor v(doc); |
| 1227 |
for (auto& e : expressions) { |
| 1228 |
if (e.second.expression) { |
| 1229 |
e.second.expression->visit(v); |
| 1230 |
} |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
void PropertyExpressionEngine::onRenameDynamicProperty(const App::Property& prop, const char* oldName) |
| 1235 |
{ |
| 1236 |
ObjectIdentifier oldNameId = ObjectIdentifier(prop.getContainer(), std::string(oldName)); |
| 1237 |
ObjectIdentifier newNameId = ObjectIdentifier(prop); |
| 1238 |
const std::map<ObjectIdentifier, ObjectIdentifier> paths = { |
| 1239 |
{oldNameId, newNameId}, |
| 1240 |
}; |
| 1241 |
|
| 1242 |
renameObjectIdentifiers(paths); |
| 1243 |
} |
| 1244 |
|
| 1245 |
void PropertyExpressionEngine::getLinksTo(std::vector<App::ObjectIdentifier>& identifiers, |
| 1246 |
App::DocumentObject* obj, |
| 1247 |
const char* subname, |
| 1248 |
bool all) const |
| 1249 |
{ |
| 1250 |
Expression::DepOption option = |
| 1251 |
all ? Expression::DepOption::DepAll : Expression::DepOption::DepNormal; |
| 1252 |
|
| 1253 |
App::SubObjectT objT(obj, subname); |
| 1254 |
auto sobj = objT.getSubObject(); |
| 1255 |
auto subElement = objT.getOldElementName(); |
| 1256 |
|
| 1257 |
for (auto& [expressionId, expressionInfo] : expressions) { |
| 1258 |
const auto& deps = expressionInfo.expression->getDeps(option); |
| 1259 |
auto it = deps.find(obj); |
| 1260 |
if (it == deps.end()) { |
| 1261 |
continue; |
| 1262 |
} |
| 1263 |
auto [docObj, map] = *it; |
| 1264 |
for (auto& [key, paths] : map) { |
| 1265 |
if (!subname) { |
| 1266 |
identifiers.push_back(expressionId); |
| 1267 |
break; |
| 1268 |
} |
| 1269 |
if (std::ranges::any_of(paths, [subname, obj, sobj, &subElement](const auto& path) { |
| 1270 |
if (path.getSubObjectName() == subname) { |
| 1271 |
return true; |
| 1272 |
} |
| 1273 |
|
| 1274 |
App::SubObjectT sobjT(obj, path.getSubObjectName().c_str()); |
| 1275 |
return (sobjT.getSubObject() == sobj |
| 1276 |
&& sobjT.getOldElementName() == subElement); |
| 1277 |
})) { |
| 1278 |
identifiers.push_back(expressionId); |
| 1279 |
} |
| 1280 |
} |
| 1281 |
} |
| 1282 |
} |