| 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 |
#ifndef EXPRESSIONENGINE_H |
| 24 |
#define EXPRESSIONENGINE_H |
| 25 |
|
| 26 |
#include <functional> |
| 27 |
#include <set> |
| 28 |
|
| 29 |
#include <boost/unordered/unordered_map.hpp> |
| 30 |
#include <boost/signals2.hpp> |
| 31 |
#include <boost_graph_adjacency_list.hpp> |
| 32 |
#include <boost/graph/topological_sort.hpp> |
| 33 |
|
| 34 |
#include <FCConfig.h> |
| 35 |
|
| 36 |
#include <App/PropertyLinks.h> |
| 37 |
|
| 38 |
|
| 39 |
namespace Base |
| 40 |
{ |
| 41 |
class Writer; |
| 42 |
class XMLReader; |
| 43 |
} // namespace Base |
| 44 |
|
| 45 |
namespace App |
| 46 |
{ |
| 47 |
|
| 48 |
class DocumentObject; |
| 49 |
class DocumentObjectExecReturn; |
| 50 |
class ObjectIdentifier; |
| 51 |
class Expression; |
| 52 |
using ExpressionPtr = std::unique_ptr<Expression>; |
| 53 |
|
| 54 |
class AppExport PropertyExpressionContainer: public App::PropertyXLinkContainer |
| 55 |
{ |
| 56 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 57 |
|
| 58 |
public: |
| 59 |
PropertyExpressionContainer(); |
| 60 |
~PropertyExpressionContainer() override; |
| 61 |
|
| 62 |
virtual std::map<App::ObjectIdentifier, const App::Expression*> getExpressions() const = 0; |
| 63 |
virtual void setExpressions(std::map<App::ObjectIdentifier, App::ExpressionPtr>&& exprs) = 0; |
| 64 |
|
| 65 |
protected: |
| 66 |
virtual void onRelabeledDocument(const App::Document& doc) = 0; |
| 67 |
virtual void onRenameDynamicProperty(const App::Property& prop, const char* oldName) = 0; |
| 68 |
|
| 69 |
private: |
| 70 |
static void slotRelabelDocument(const App::Document& doc); |
| 71 |
static void slotRenameDynamicProperty(const App::Property& prop, const char* oldName); |
| 72 |
}; |
| 73 |
|
| 74 |
class AppExport PropertyExpressionEngine |
| 75 |
: public App::PropertyExpressionContainer, |
| 76 |
private App::AtomicPropertyChangeInterface<PropertyExpressionEngine> |
| 77 |
{ |
| 78 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 79 |
|
| 80 |
public: |
| 81 |
void updateElementReference(App::DocumentObject* feature, |
| 82 |
bool reverse = false, |
| 83 |
bool notify = false) override; |
| 84 |
bool referenceChanged() const override; |
| 85 |
bool adjustLink(const std::set<App::DocumentObject*>& inList) override; |
| 86 |
Property* |
| 87 |
CopyOnImportExternal(const std::map<std::string, std::string>& nameMap) const override; |
| 88 |
Property* CopyOnLabelChange(App::DocumentObject* obj, |
| 89 |
const std::string& ref, |
| 90 |
const char* newLabel) const override; |
| 91 |
Property* CopyOnLinkReplace(const App::DocumentObject* parent, |
| 92 |
App::DocumentObject* oldObj, |
| 93 |
App::DocumentObject* newObj) const override; |
| 94 |
|
| 95 |
using ValidatorFunc = std::function<std::string(const App::ObjectIdentifier& path, |
| 96 |
std::shared_ptr<const App::Expression> expr)>; |
| 97 |
|
| 98 |
/** |
| 99 |
* @brief The ExpressionInfo struct encapsulates an expression. |
| 100 |
*/ |
| 101 |
|
| 102 |
struct ExpressionInfo |
| 103 |
{ |
| 104 |
std::shared_ptr<App::Expression> expression; /**< The actual expression tree */ |
| 105 |
bool busy; |
| 106 |
|
| 107 |
explicit ExpressionInfo( |
| 108 |
std::shared_ptr<App::Expression> expression = std::shared_ptr<App::Expression>()) |
| 109 |
{ |
| 110 |
this->expression = expression; |
| 111 |
this->busy = false; |
| 112 |
} |
| 113 |
|
| 114 |
ExpressionInfo(const ExpressionInfo&) = default; |
| 115 |
|
| 116 |
ExpressionInfo& operator=(const ExpressionInfo&) = default; |
| 117 |
}; |
| 118 |
|
| 119 |
PropertyExpressionEngine(); |
| 120 |
~PropertyExpressionEngine() override; |
| 121 |
|
| 122 |
unsigned int getMemSize() const override; |
| 123 |
|
| 124 |
std::map<App::ObjectIdentifier, const App::Expression*> getExpressions() const override; |
| 125 |
void setExpressions(std::map<App::ObjectIdentifier, App::ExpressionPtr>&& exprs) override; |
| 126 |
void onRelabeledDocument(const App::Document& doc) override; |
| 127 |
void onRenameDynamicProperty(const App::Property& prop, const char* oldName) override; |
| 128 |
|
| 129 |
void setValue() |
| 130 |
{} // Dummy |
| 131 |
|
| 132 |
Property* Copy() const override; |
| 133 |
|
| 134 |
void Paste(const Property& from) override; |
| 135 |
|
| 136 |
void Save(Base::Writer& writer) const override; |
| 137 |
|
| 138 |
void Restore(Base::XMLReader& reader) override; |
| 139 |
|
| 140 |
void setValue(const App::ObjectIdentifier& path, std::shared_ptr<App::Expression> expr); |
| 141 |
|
| 142 |
const boost::any getPathValue(const App::ObjectIdentifier& path) const override; |
| 143 |
|
| 144 |
/// Execute options |
| 145 |
enum ExecuteOption |
| 146 |
{ |
| 147 |
/// Execute all expression |
| 148 |
ExecuteAll, |
| 149 |
/// Execute only output property bindings |
| 150 |
ExecuteOutput, |
| 151 |
/// Execute only non-output property bindings |
| 152 |
ExecuteNonOutput, |
| 153 |
/// Execute on document restore |
| 154 |
ExecuteOnRestore, |
| 155 |
}; |
| 156 |
/** Evaluate the expressions |
| 157 |
* |
| 158 |
* @param option: execution option, see ExecuteOption. |
| 159 |
*/ |
| 160 |
DocumentObjectExecReturn* execute(ExecuteOption option = ExecuteAll, bool* touched = nullptr); |
| 161 |
|
| 162 |
void getPathsToDocumentObject(DocumentObject*, std::vector<App::ObjectIdentifier>& paths) const; |
| 163 |
|
| 164 |
bool depsAreTouched() const; |
| 165 |
|
| 166 |
/* Expression validator */ |
| 167 |
void setValidator(ValidatorFunc f) |
| 168 |
{ |
| 169 |
validator = f; |
| 170 |
} |
| 171 |
|
| 172 |
std::string validateExpression(const App::ObjectIdentifier& path, |
| 173 |
std::shared_ptr<const App::Expression> expr) const; |
| 174 |
|
| 175 |
void renameExpressions(const std::map<App::ObjectIdentifier, App::ObjectIdentifier>& paths); |
| 176 |
|
| 177 |
void |
| 178 |
renameObjectIdentifiers(const std::map<App::ObjectIdentifier, App::ObjectIdentifier>& paths); |
| 179 |
|
| 180 |
App::ObjectIdentifier canonicalPath(const App::ObjectIdentifier& p) const override; |
| 181 |
|
| 182 |
size_t numExpressions() const; |
| 183 |
|
| 184 |
/// signal called when an expression was changed |
| 185 |
boost::signals2::signal<void(const App::ObjectIdentifier&)> expressionChanged; |
| 186 |
|
| 187 |
void afterRestore() override; |
| 188 |
void onContainerRestored() override; |
| 189 |
|
| 190 |
void getLinksTo(std::vector<App::ObjectIdentifier>& identifiers, |
| 191 |
App::DocumentObject* obj, |
| 192 |
const char* subname = nullptr, |
| 193 |
bool all = false) const override; |
| 194 |
|
| 195 |
/* Python interface */ |
| 196 |
PyObject* getPyObject() override; |
| 197 |
void setPyObject(PyObject*) override; |
| 198 |
|
| 199 |
protected: |
| 200 |
void hasSetValue() override; |
| 201 |
|
| 202 |
private: |
| 203 |
using DiGraph = boost::adjacency_list<boost::listS, boost::vecS, boost::directedS>; |
| 204 |
using Edge = std::pair<int, int>; |
| 205 |
// Note: use std::map instead of unordered_map to keep the binding order stable |
| 206 |
#if defined(FC_OS_MACOSX) || defined(FC_OS_BSD) || defined(_LIBCPP_VERSION) |
| 207 |
using ExpressionMap = std::map<App::ObjectIdentifier, ExpressionInfo>; |
| 208 |
#else |
| 209 |
using ExpressionMap = std::map<const App::ObjectIdentifier, ExpressionInfo>; |
| 210 |
#endif |
| 211 |
|
| 212 |
std::vector<App::ObjectIdentifier> computeEvaluationOrder(ExecuteOption option); |
| 213 |
|
| 214 |
void buildGraphStructures(const App::ObjectIdentifier& path, |
| 215 |
const std::shared_ptr<Expression> expression, |
| 216 |
boost::unordered_map<App::ObjectIdentifier, int>& nodes, |
| 217 |
boost::unordered_map<int, App::ObjectIdentifier>& revNodes, |
| 218 |
std::vector<Edge>& edges) const; |
| 219 |
|
| 220 |
void buildGraph(const ExpressionMap& exprs, |
| 221 |
boost::unordered_map<int, App::ObjectIdentifier>& revNodes, |
| 222 |
DiGraph& g, |
| 223 |
ExecuteOption option = ExecuteAll) const; |
| 224 |
|
| 225 |
void slotChangedObject(const App::DocumentObject& obj, const App::Property& prop); |
| 226 |
void slotChangedProperty(const App::DocumentObject& obj, const App::Property& prop); |
| 227 |
void updateHiddenReference(const std::string& key); |
| 228 |
|
| 229 |
bool running = false; /**< Boolean used to avoid loops */ |
| 230 |
bool restoring = false; |
| 231 |
|
| 232 |
ExpressionMap expressions; /**< Stored expressions */ |
| 233 |
|
| 234 |
ValidatorFunc validator; /**< Valdiator functor */ |
| 235 |
|
| 236 |
struct RestoredExpression |
| 237 |
{ |
| 238 |
std::string path; |
| 239 |
std::string expr; |
| 240 |
std::string comment; |
| 241 |
}; |
| 242 |
/**< Expressions are read from file to this map first before they are validated and inserted |
| 243 |
* into the actual map */ |
| 244 |
std::unique_ptr<std::vector<RestoredExpression>> restoredExpressions; |
| 245 |
|
| 246 |
void tryRestoreExpression(DocumentObject* docObj, const RestoredExpression& info); |
| 247 |
|
| 248 |
struct Private; |
| 249 |
std::unique_ptr<Private> pimpl; |
| 250 |
|
| 251 |
friend class AtomicPropertyChange; |
| 252 |
}; |
| 253 |
|
| 254 |
} // namespace App |
| 255 |
|
| 256 |
#endif // EXPRESSIONENGINE_H |