| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2002 Jürgen Riegel <[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 <boost/graph/graphviz.hpp> |
| 25 |
#include <random> |
| 26 |
|
| 27 |
#include "Application.h" |
| 28 |
#include "Document.h" |
| 29 |
#include "private/DocumentP.h" |
| 30 |
#include "DocumentObject.h" |
| 31 |
#include "ExpressionParser.h" |
| 32 |
#include "GeoFeatureGroupExtension.h" |
| 33 |
#include "Origin.h" |
| 34 |
#include "OriginGroupExtension.h" |
| 35 |
#include "ObjectIdentifier.h" |
| 36 |
|
| 37 |
using namespace App; |
| 38 |
using namespace boost; |
| 39 |
|
| 40 |
void Document::writeDependencyGraphViz(std::ostream& out) |
| 41 |
{ |
| 42 |
// // caching vertex to DocObject |
| 43 |
// std::map<Vertex,DocumentObject*> VertexMap; |
| 44 |
// for(std::map<DocumentObject*,Vertex>::const_iterator It1= _DepConMap.begin();It1 != |
| 45 |
// _DepConMap.end(); ++It1) |
| 46 |
// VertexMap[It1->second] = It1->first; |
| 47 |
|
| 48 |
out << "digraph G {" << std::endl; |
| 49 |
out << "\tordering=out;" << std::endl; |
| 50 |
out << "\tnode [shape = box];" << std::endl; |
| 51 |
|
| 52 |
for (const auto& It : d->objectMap) { |
| 53 |
out << "\t" << It.first << ";" << std::endl; |
| 54 |
std::vector<DocumentObject*> OutList = It.second->getOutList(); |
| 55 |
for (const auto& It2 : OutList) { |
| 56 |
if (It2) { |
| 57 |
out << "\t" << It.first << "->" << It2->getNameInDocument() << ";" << std::endl; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/* |
| 63 |
graph_traits<DependencyList>::edge_iterator ei, ei_end; |
| 64 |
for (tie(ei,ei_end) = edges(_DepList); ei != ei_end; ++ei) |
| 65 |
out << "\t" |
| 66 |
<< VertexMap[source(*ei, _DepList)]->getNameInDocument() |
| 67 |
<< " -> " |
| 68 |
<< VertexMap[target(*ei, _DepList)]->getNameInDocument() |
| 69 |
<< ";" << endl; |
| 70 |
*/ |
| 71 |
out << "}" << std::endl; |
| 72 |
} |
| 73 |
|
| 74 |
void Document::exportGraphviz(std::ostream& out) const |
| 75 |
{ |
| 76 |
/* Type defs for a graph with graphviz attributes */ |
| 77 |
using GraphvizAttributes = std::map<std::string, std::string>; |
| 78 |
using Graph = boost::subgraph<adjacency_list< |
| 79 |
vecS, |
| 80 |
vecS, |
| 81 |
directedS, |
| 82 |
property<vertex_attribute_t, GraphvizAttributes>, |
| 83 |
property<edge_index_t, int, property<edge_attribute_t, GraphvizAttributes>>, |
| 84 |
property<graph_name_t, |
| 85 |
std::string, |
| 86 |
property<graph_graph_attribute_t, |
| 87 |
GraphvizAttributes, |
| 88 |
property<graph_vertex_attribute_t, |
| 89 |
GraphvizAttributes, |
| 90 |
property<graph_edge_attribute_t, GraphvizAttributes>>>>>>; |
| 91 |
|
| 92 |
/** |
| 93 |
* @brief The GraphCreator class |
| 94 |
* |
| 95 |
* This class creates the dependency graph for a document. |
| 96 |
* |
| 97 |
*/ |
| 98 |
|
| 99 |
class GraphCreator |
| 100 |
{ |
| 101 |
public: |
| 102 |
explicit GraphCreator(struct DocumentP* _d) |
| 103 |
: d(_d) |
| 104 |
, seed(std::random_device()()) |
| 105 |
, distribution(0, 255) |
| 106 |
{ |
| 107 |
build(); |
| 108 |
} |
| 109 |
|
| 110 |
const Graph& getGraph() const |
| 111 |
{ |
| 112 |
return DepList; |
| 113 |
} |
| 114 |
|
| 115 |
private: |
| 116 |
void build() |
| 117 |
{ |
| 118 |
// Set attribute(s) for main graph |
| 119 |
get_property(DepList, graph_graph_attribute)["compound"] = "true"; |
| 120 |
|
| 121 |
addSubgraphs(); |
| 122 |
buildAdjacencyList(); |
| 123 |
addEdges(); |
| 124 |
markCycles(); |
| 125 |
markOutOfScopeLinks(); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @brief getId returns a canonical string for a DocumentObject. |
| 130 |
* @param docObj Document object to get an ID from |
| 131 |
* @return A string |
| 132 |
*/ |
| 133 |
|
| 134 |
std::string getId(const DocumentObject* docObj) |
| 135 |
{ |
| 136 |
std::string id; |
| 137 |
if (docObj->isAttachedToDocument()) { |
| 138 |
auto doc = docObj->getDocument(); |
| 139 |
id.append(doc->getName()); |
| 140 |
id.append("#"); |
| 141 |
id.append(docObj->getNameInDocument()); |
| 142 |
} |
| 143 |
return id; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* @brief getId returns a canonical string for an ObjectIdentifier; |
| 148 |
* @param path |
| 149 |
* @return A string |
| 150 |
*/ |
| 151 |
|
| 152 |
std::string getId(const ObjectIdentifier& path) |
| 153 |
{ |
| 154 |
DocumentObject* docObj = path.getDocumentObject(); |
| 155 |
if (!docObj) { |
| 156 |
return {}; |
| 157 |
} |
| 158 |
|
| 159 |
return std::string((docObj)->getDocument()->getName()) + "#" |
| 160 |
+ docObj->getNameInDocument() + "." + path.getPropertyName() + path.getSubPathStr(); |
| 161 |
} |
| 162 |
|
| 163 |
std::string getClusterName(const DocumentObject* docObj) const |
| 164 |
{ |
| 165 |
return std::string("cluster") + docObj->getNameInDocument(); |
| 166 |
} |
| 167 |
|
| 168 |
void setGraphLabel(Graph& g, const DocumentObject* obj) const |
| 169 |
{ |
| 170 |
std::string name(obj->getNameInDocument()); |
| 171 |
std::string label(obj->Label.getValue()); |
| 172 |
if (name == label) { |
| 173 |
get_property(g, graph_graph_attribute)["label"] = name; |
| 174 |
} |
| 175 |
else { |
| 176 |
get_property(g, graph_graph_attribute)["label"] = name + "\n(" + label + ")"; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* @brief setGraphAttributes Set graph attributes on a subgraph for a DocumentObject node. |
| 182 |
* @param obj DocumentObject |
| 183 |
*/ |
| 184 |
|
| 185 |
void setGraphAttributes(const DocumentObject* obj) |
| 186 |
{ |
| 187 |
assert(GraphList.find(obj) != GraphList.end()); |
| 188 |
get_property(*GraphList[obj], graph_name) = getClusterName(obj); |
| 189 |
|
| 190 |
get_property(*GraphList[obj], graph_graph_attribute)["bgcolor"] = "#e0e0e0"; |
| 191 |
|
| 192 |
get_property(*GraphList[obj], graph_graph_attribute)["style"] = "rounded,filled"; |
| 193 |
setGraphLabel(*GraphList[obj], obj); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* @brief setPropertyVertexAttributes Set vertex attributes for a Property node in a graph. |
| 198 |
* @param g Graph |
| 199 |
* @param vertex Property node |
| 200 |
* @param name Name of node |
| 201 |
*/ |
| 202 |
|
| 203 |
void setPropertyVertexAttributes(Graph& g, Vertex vertex, const std::string& name) |
| 204 |
{ |
| 205 |
get(vertex_attribute, g)[vertex]["label"] = name; |
| 206 |
get(vertex_attribute, g)[vertex]["shape"] = "box"; |
| 207 |
get(vertex_attribute, g)[vertex]["style"] = "dashed"; |
| 208 |
get(vertex_attribute, g)[vertex]["fontsize"] = "8pt"; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* @brief addExpressionSubgraphIfNeeded Add a subgraph to the main graph if it is needed, |
| 213 |
* i.e. there are defined at least one expression in the document object, or other objects |
| 214 |
* are referencing properties in it. |
| 215 |
* @param obj DocumentObject to assess. |
| 216 |
* @param CSSubgraphs Boolean if the GeoFeatureGroups are created as subgraphs |
| 217 |
*/ |
| 218 |
|
| 219 |
void addExpressionSubgraphIfNeeded(DocumentObject* obj, bool CSsubgraphs) |
| 220 |
{ |
| 221 |
|
| 222 |
auto expressions = obj->ExpressionEngine.getExpressions(); |
| 223 |
|
| 224 |
if (!expressions.empty()) { |
| 225 |
|
| 226 |
Graph* graph = nullptr; |
| 227 |
graph = &DepList; |
| 228 |
if (CSsubgraphs) { |
| 229 |
auto group = GeoFeatureGroupExtension::getGroupOfObject(obj); |
| 230 |
if (group) { |
| 231 |
auto it = GraphList.find(group); |
| 232 |
if (it != GraphList.end()) { |
| 233 |
graph = it->second; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
// If documentObject has an expression, create a subgraph for it |
| 239 |
if (graph && !GraphList[obj]) { |
| 240 |
GraphList[obj] = &graph->create_subgraph(); |
| 241 |
setGraphAttributes(obj); |
| 242 |
} |
| 243 |
|
| 244 |
// Create subgraphs for all document objects that it depends on; it will depend on |
| 245 |
// some property there |
| 246 |
for (const auto& expr : expressions) { |
| 247 |
std::map<ObjectIdentifier, bool> deps; |
| 248 |
|
| 249 |
expr.second->getIdentifiers(deps); |
| 250 |
|
| 251 |
for (const auto& dep : deps) { |
| 252 |
if (dep.second) { |
| 253 |
continue; |
| 254 |
} |
| 255 |
DocumentObject* o = dep.first.getDocumentObject(); |
| 256 |
|
| 257 |
// Doesn't exist already? |
| 258 |
if (o && !GraphList[o]) { |
| 259 |
|
| 260 |
if (CSsubgraphs) { |
| 261 |
auto group = GeoFeatureGroupExtension::getGroupOfObject(o); |
| 262 |
auto graph2 = group ? GraphList[group] : &DepList; |
| 263 |
if (graph2) { |
| 264 |
GraphList[o] = &graph2->create_subgraph(); |
| 265 |
setGraphAttributes(o); |
| 266 |
} |
| 267 |
} |
| 268 |
else if (graph) { |
| 269 |
GraphList[o] = &graph->create_subgraph(); |
| 270 |
setGraphAttributes(o); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* @brief add Add @docObj to the graph, including all expressions (and dependencies) it |
| 280 |
* includes. |
| 281 |
* @param docObj The document object to add. |
| 282 |
* @param name Name of node. |
| 283 |
*/ |
| 284 |
|
| 285 |
void add(DocumentObject* docObj, |
| 286 |
const std::string& name, |
| 287 |
const std::string& label, |
| 288 |
bool CSSubgraphs) |
| 289 |
{ |
| 290 |
|
| 291 |
// don't add objects twice |
| 292 |
if (objects.contains(docObj)) { |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
// find the correct graph to add the vertex to. Check first expression graphs, |
| 297 |
// afterwards the parent CS and origin graphs |
| 298 |
Graph* sgraph = GraphList[docObj]; |
| 299 |
if (CSSubgraphs) { |
| 300 |
if (!sgraph) { |
| 301 |
auto group = GeoFeatureGroupExtension::getGroupOfObject(docObj); |
| 302 |
if (group) { |
| 303 |
if (docObj->isDerivedFrom<App::DatumElement>()) { |
| 304 |
sgraph = GraphList[group->getExtensionByType<OriginGroupExtension>() |
| 305 |
->Origin.getValue()]; |
| 306 |
} |
| 307 |
else { |
| 308 |
sgraph = GraphList[group]; |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
if (!sgraph) { |
| 313 |
if (docObj->isDerivedFrom<DatumElement>()) { |
| 314 |
auto* lcs = static_cast<DatumElement*>(docObj)->getLCS(); |
| 315 |
if (lcs) { |
| 316 |
sgraph = GraphList[lcs]; |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
if (!sgraph) { |
| 322 |
sgraph = &DepList; |
| 323 |
} |
| 324 |
|
| 325 |
// Keep a list of all added document objects. |
| 326 |
objects.insert(docObj); |
| 327 |
|
| 328 |
// Add vertex to graph. Track global and local index |
| 329 |
LocalVertexList[getId(docObj)] = add_vertex(*sgraph); |
| 330 |
GlobalVertexList[getId(docObj)] = vertex_no++; |
| 331 |
|
| 332 |
// If node is in main graph, style it with rounded corners. If not, make it invisible. |
| 333 |
if (!GraphList[docObj]) { |
| 334 |
get(vertex_attribute, *sgraph)[LocalVertexList[getId(docObj)]]["style"] = "filled"; |
| 335 |
get(vertex_attribute, *sgraph)[LocalVertexList[getId(docObj)]]["shape"] = "Mrecord"; |
| 336 |
// Set node label |
| 337 |
if (name == label) { |
| 338 |
get(vertex_attribute, *sgraph)[LocalVertexList[getId(docObj)]]["label"] = name; |
| 339 |
} |
| 340 |
else { |
| 341 |
get(vertex_attribute, *sgraph)[LocalVertexList[getId(docObj)]]["label"] = |
| 342 |
name + "\n(" + label + ")"; |
| 343 |
} |
| 344 |
} |
| 345 |
else { |
| 346 |
get(vertex_attribute, *sgraph)[LocalVertexList[getId(docObj)]]["style"] = "invis"; |
| 347 |
get(vertex_attribute, *sgraph)[LocalVertexList[getId(docObj)]]["fixedsize"] = |
| 348 |
"true"; |
| 349 |
get(vertex_attribute, *sgraph)[LocalVertexList[getId(docObj)]]["width"] = "0"; |
| 350 |
get(vertex_attribute, *sgraph)[LocalVertexList[getId(docObj)]]["height"] = "0"; |
| 351 |
} |
| 352 |
|
| 353 |
// Add expressions and its dependencies |
| 354 |
auto expressions {docObj->ExpressionEngine.getExpressions()}; |
| 355 |
for (const auto& expr : expressions) { |
| 356 |
auto found = std::as_const(GlobalVertexList).find(getId(expr.first)); |
| 357 |
if (found == GlobalVertexList.end()) { |
| 358 |
int vid = LocalVertexList[getId(expr.first)] = add_vertex(*sgraph); |
| 359 |
GlobalVertexList[getId(expr.first)] = vertex_no++; |
| 360 |
setPropertyVertexAttributes(*sgraph, vid, expr.first.toString()); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
// Add all dependencies |
| 365 |
for (const auto& expression : expressions) { |
| 366 |
// Get dependencies |
| 367 |
std::map<ObjectIdentifier, bool> deps; |
| 368 |
expression.second->getIdentifiers(deps); |
| 369 |
|
| 370 |
// Create subgraphs for all documentobjects that it depends on; it will depend on |
| 371 |
// some property there |
| 372 |
for (const auto& dep : deps) { |
| 373 |
if (dep.second) { |
| 374 |
continue; |
| 375 |
} |
| 376 |
DocumentObject* depObjDoc = dep.first.getDocumentObject(); |
| 377 |
auto found = GlobalVertexList.find(getId(dep.first)); |
| 378 |
|
| 379 |
if (found == GlobalVertexList.end()) { |
| 380 |
Graph* depSgraph = GraphList[depObjDoc] ? GraphList[depObjDoc] : &DepList; |
| 381 |
|
| 382 |
LocalVertexList[getId(dep.first)] = add_vertex(*depSgraph); |
| 383 |
GlobalVertexList[getId(dep.first)] = vertex_no++; |
| 384 |
setPropertyVertexAttributes(*depSgraph, |
| 385 |
LocalVertexList[getId(dep.first)], |
| 386 |
dep.first.getPropertyName() |
| 387 |
+ dep.first.getSubPathStr()); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
void recursiveCSSubgraphs(DocumentObject* cs, DocumentObject* parent) |
| 394 |
{ |
| 395 |
|
| 396 |
auto graph = parent ? GraphList[parent] : &DepList; |
| 397 |
// check if the value for the key 'parent' is null |
| 398 |
if (!graph) { |
| 399 |
return; |
| 400 |
} |
| 401 |
auto& sub = graph->create_subgraph(); |
| 402 |
GraphList[cs] = ⊂ |
| 403 |
get_property(sub, graph_name) = getClusterName(cs); |
| 404 |
|
| 405 |
// build random color string |
| 406 |
std::stringstream stream; |
| 407 |
stream << "#" << std::setfill('0') << std::setw(2) << std::hex << distribution(seed) |
| 408 |
<< std::setfill('0') << std::setw(2) << std::hex << distribution(seed) |
| 409 |
<< std::setfill('0') << std::setw(2) << std::hex << distribution(seed) << 80; |
| 410 |
std::string result(stream.str()); |
| 411 |
|
| 412 |
get_property(sub, graph_graph_attribute)["bgcolor"] = result; |
| 413 |
get_property(sub, graph_graph_attribute)["style"] = "rounded,filled"; |
| 414 |
setGraphLabel(sub, cs); |
| 415 |
|
| 416 |
for (auto obj : cs->getOutList()) { |
| 417 |
if (obj->hasExtension(GeoFeatureGroupExtension::getExtensionClassTypeId())) { |
| 418 |
// in case of dependencies loops check if obj is already part of the |
| 419 |
// map to avoid infinite recursions |
| 420 |
auto it = GraphList.find(obj); |
| 421 |
if (it == GraphList.end()) { |
| 422 |
recursiveCSSubgraphs(obj, cs); |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
// setup the origin if available |
| 428 |
if (cs->hasExtension(App::OriginGroupExtension::getExtensionClassTypeId())) { |
| 429 |
auto origin = cs->getExtensionByType<OriginGroupExtension>()->Origin.getValue(); |
| 430 |
if (!origin) { |
| 431 |
std::cerr << "Origin feature not found" << std::endl; |
| 432 |
return; |
| 433 |
} |
| 434 |
auto& osub = sub.create_subgraph(); |
| 435 |
GraphList[origin] = &osub; |
| 436 |
get_property(osub, graph_name) = getClusterName(origin); |
| 437 |
get_property(osub, graph_graph_attribute)["bgcolor"] = "none"; |
| 438 |
setGraphLabel(osub, origin); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
void addSubgraphs() |
| 443 |
{ |
| 444 |
|
| 445 |
ParameterGrp::handle depGrp = App::GetApplication().GetParameterGroupByPath( |
| 446 |
"User parameter:BaseApp/Preferences/DependencyGraph"); |
| 447 |
bool CSSubgraphs = depGrp->GetBool("GeoFeatureSubgraphs", true); |
| 448 |
|
| 449 |
if (CSSubgraphs) { |
| 450 |
// first build up the coordinate system subgraphs |
| 451 |
for (auto objectIt : d->objectArray) { |
| 452 |
// ignore groups inside other groups, these will be processed in one of the next |
| 453 |
// recursive calls. App::Origin now has the GeoFeatureGroupExtension but it |
| 454 |
// should not move its group symbol outside its parent |
| 455 |
if (!objectIt->isDerivedFrom<Origin>() |
| 456 |
&& objectIt->hasExtension( |
| 457 |
GeoFeatureGroupExtension::getExtensionClassTypeId()) |
| 458 |
&& GeoFeatureGroupExtension::getGroupOfObject(objectIt) == nullptr) { |
| 459 |
recursiveCSSubgraphs(objectIt, nullptr); |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
// Internal document objects |
| 465 |
for (const auto& It : d->objectMap) { |
| 466 |
addExpressionSubgraphIfNeeded(It.second, CSSubgraphs); |
| 467 |
} |
| 468 |
|
| 469 |
// Add external document objects |
| 470 |
for (const auto& it : d->objectMap) { |
| 471 |
std::vector<DocumentObject*> OutList = it.second->getOutList(); |
| 472 |
for (auto obj : OutList) { |
| 473 |
if (obj) { |
| 474 |
std::map<std::string, Vertex>::const_iterator item = |
| 475 |
GlobalVertexList.find(getId(obj)); |
| 476 |
|
| 477 |
if (item == GlobalVertexList.end()) { |
| 478 |
addExpressionSubgraphIfNeeded(obj, CSSubgraphs); |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
// Filling up the adjacency List |
| 486 |
void buildAdjacencyList() |
| 487 |
{ |
| 488 |
|
| 489 |
ParameterGrp::handle depGrp = App::GetApplication().GetParameterGroupByPath( |
| 490 |
"User parameter:BaseApp/Preferences/DependencyGraph"); |
| 491 |
bool CSSubgraphs = depGrp->GetBool("GeoFeatureSubgraphs", true); |
| 492 |
|
| 493 |
// Add internal document objects |
| 494 |
for (const auto& It : d->objectMap) { |
| 495 |
add(It.second, |
| 496 |
It.second->getNameInDocument(), |
| 497 |
It.second->Label.getValue(), |
| 498 |
CSSubgraphs); |
| 499 |
} |
| 500 |
|
| 501 |
// Add external document objects |
| 502 |
for (const auto& It : d->objectMap) { |
| 503 |
std::vector<DocumentObject*> OutList = It.second->getOutList(); |
| 504 |
for (auto obj : OutList) { |
| 505 |
if (obj) { |
| 506 |
std::map<std::string, Vertex>::const_iterator item = |
| 507 |
GlobalVertexList.find(getId(obj)); |
| 508 |
|
| 509 |
if (item == GlobalVertexList.end()) { |
| 510 |
if (obj->isAttachedToDocument()) { |
| 511 |
add(obj, |
| 512 |
std::string(obj->getDocument()->getName()) + "#" |
| 513 |
+ obj->getNameInDocument(), |
| 514 |
std::string(obj->getDocument()->getName()) + "#" |
| 515 |
+ obj->Label.getValue(), |
| 516 |
CSSubgraphs); |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
void addEdges() |
| 525 |
{ |
| 526 |
// Get edge properties for main graph |
| 527 |
const boost::property_map<Graph, boost::edge_attribute_t>::type& edgeAttrMap = |
| 528 |
boost::get(boost::edge_attribute, DepList); |
| 529 |
|
| 530 |
// Track edges between document objects connected by expression dependencies |
| 531 |
std::set<std::pair<const DocumentObject*, const DocumentObject*>> existingEdges; |
| 532 |
|
| 533 |
// Add edges between properties |
| 534 |
for (const auto& docObj : objects) { |
| 535 |
|
| 536 |
// Add expressions and its dependencies |
| 537 |
auto expressions = docObj->ExpressionEngine.getExpressions(); |
| 538 |
for (const auto& expr : expressions) { |
| 539 |
std::map<ObjectIdentifier, bool> deps; |
| 540 |
expr.second->getIdentifiers(deps); |
| 541 |
|
| 542 |
// Create subgraphs for all documentobjects that it depends on; it will depend |
| 543 |
// on some property there |
| 544 |
for (const auto& dep : deps) { |
| 545 |
if (dep.second) { |
| 546 |
continue; |
| 547 |
} |
| 548 |
DocumentObject* depObjDoc = dep.first.getDocumentObject(); |
| 549 |
Edge edge; |
| 550 |
bool inserted; |
| 551 |
|
| 552 |
tie(edge, inserted) = add_edge(GlobalVertexList[getId(expr.first)], |
| 553 |
GlobalVertexList[getId(dep.first)], |
| 554 |
DepList); |
| 555 |
|
| 556 |
// Add this edge to the set of all expression generated edges |
| 557 |
existingEdges.insert(std::make_pair(docObj, depObjDoc)); |
| 558 |
|
| 559 |
// Edges between properties should be a bit smaller, and dashed |
| 560 |
edgeAttrMap[edge]["arrowsize"] = "0.5"; |
| 561 |
edgeAttrMap[edge]["style"] = "dashed"; |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
ParameterGrp::handle depGrp = App::GetApplication().GetParameterGroupByPath( |
| 567 |
"User parameter:BaseApp/Preferences/DependencyGraph"); |
| 568 |
bool omitGeoFeatureGroups = depGrp->GetBool("GeoFeatureSubgraphs", true); |
| 569 |
|
| 570 |
// Add edges between document objects |
| 571 |
for (const auto& It : d->objectMap) { |
| 572 |
|
| 573 |
if (omitGeoFeatureGroups && It.second->isDerivedFrom<Origin>()) { |
| 574 |
continue; |
| 575 |
} |
| 576 |
|
| 577 |
std::map<DocumentObject*, int> dups; |
| 578 |
std::vector<DocumentObject*> OutList = It.second->getOutList(); |
| 579 |
const DocumentObject* docObj = It.second; |
| 580 |
const bool docObj_is_group = |
| 581 |
docObj->hasExtension(GeoFeatureGroupExtension::getExtensionClassTypeId()); |
| 582 |
|
| 583 |
for (auto obj : OutList) { |
| 584 |
if (obj) { |
| 585 |
if (omitGeoFeatureGroups && docObj_is_group |
| 586 |
&& GeoFeatureGroupExtension::getGroupOfObject(obj) == docObj) { |
| 587 |
continue; |
| 588 |
} |
| 589 |
// Count duplicate edges |
| 590 |
bool inserted = edge(GlobalVertexList[getId(docObj)], |
| 591 |
GlobalVertexList[getId(obj)], |
| 592 |
DepList) |
| 593 |
.second; |
| 594 |
if (inserted) { |
| 595 |
dups[obj]++; |
| 596 |
continue; |
| 597 |
} |
| 598 |
|
| 599 |
// Skip edge if an expression edge already exists |
| 600 |
if (existingEdges.find(std::make_pair(docObj, obj)) |
| 601 |
!= existingEdges.end()) { |
| 602 |
continue; |
| 603 |
} |
| 604 |
|
| 605 |
// Add edge |
| 606 |
|
| 607 |
Edge edge; |
| 608 |
|
| 609 |
tie(edge, inserted) = add_edge(GlobalVertexList[getId(docObj)], |
| 610 |
GlobalVertexList[getId(obj)], |
| 611 |
DepList); |
| 612 |
|
| 613 |
// Set properties to make arrows go between subgraphs if needed |
| 614 |
if (GraphList[docObj]) { |
| 615 |
edgeAttrMap[edge]["ltail"] = getClusterName(docObj); |
| 616 |
} |
| 617 |
if (GraphList[obj]) { |
| 618 |
edgeAttrMap[edge]["lhead"] = getClusterName(obj); |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
// Set labels for duplicate edges |
| 624 |
for (const auto& dup : dups) { |
| 625 |
Edge e(edge(GlobalVertexList[getId(It.second)], |
| 626 |
GlobalVertexList[getId(dup.first)], |
| 627 |
DepList) |
| 628 |
.first); |
| 629 |
std::stringstream s; |
| 630 |
s << " " << (dup.second + 1) << "x"; |
| 631 |
edgeAttrMap[e]["label"] = s.str(); |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
using EdgeMap = std::unordered_multimap<Vertex, Edge>; |
| 637 |
|
| 638 |
void removeEdges(EdgeMap& in_edges, |
| 639 |
EdgeMap& out_edges, |
| 640 |
std::pair<EdgeMap::iterator, EdgeMap::iterator> i_pair, |
| 641 |
std::function<Vertex(const Edge&)> select_vertex) |
| 642 |
{ |
| 643 |
auto i = i_pair.first; |
| 644 |
|
| 645 |
while (i != i_pair.second) { |
| 646 |
// Remove from in edges in other nodes |
| 647 |
auto in_i_pair = in_edges.equal_range(select_vertex(i->second)); |
| 648 |
auto in_i = in_i_pair.first; |
| 649 |
|
| 650 |
while (in_i != in_i_pair.second) { |
| 651 |
if (in_i->second == i->second) { |
| 652 |
in_i = in_edges.erase(in_i); |
| 653 |
} |
| 654 |
else { |
| 655 |
++in_i; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
// Remove node from out_edges |
| 660 |
i = out_edges.erase(i); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
#if defined(__clang__) |
| 665 |
#elif defined(__GNUC__) |
| 666 |
#pragma GCC diagnostic push |
| 667 |
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
| 668 |
#endif |
| 669 |
|
| 670 |
void markCycles() |
| 671 |
{ |
| 672 |
bool changed = true; |
| 673 |
std::unordered_set<Vertex> in_use; |
| 674 |
EdgeMap in_edges; |
| 675 |
EdgeMap out_edges; |
| 676 |
|
| 677 |
// Add all vertices to the in_use set |
| 678 |
graph_traits<Graph>::vertex_iterator vi, vi_end; |
| 679 |
tie(vi, vi_end) = vertices(DepList); |
| 680 |
for (; vi != vi_end; ++vi) { |
| 681 |
in_use.insert(*vi); |
| 682 |
} |
| 683 |
|
| 684 |
// Add all edges to the in_edges and out_edges multimaps |
| 685 |
graph_traits<Graph>::edge_iterator ei, ei_end; |
| 686 |
tie(ei, ei_end) = edges(DepList); |
| 687 |
for (; ei != ei_end; ++ei) { |
| 688 |
in_edges.insert(std::make_pair(target(*ei, DepList), *ei)); |
| 689 |
out_edges.insert(std::make_pair(source(*ei, DepList), *ei)); |
| 690 |
} |
| 691 |
|
| 692 |
// Go through dependency graph and remove nodes with either no input or output |
| 693 |
// A normal DAG without any cycles will get all its edges removed. |
| 694 |
// If one or more cycles exist in the graph, there will remain nodes with |
| 695 |
// both in and out edges. |
| 696 |
|
| 697 |
while (changed) { |
| 698 |
auto uvi = in_use.begin(); |
| 699 |
auto uvi_end = in_use.end(); |
| 700 |
|
| 701 |
// Flag that no changes has occurred so far. If the loop goes through |
| 702 |
// without this flag being set to true, we are done. |
| 703 |
changed = false; |
| 704 |
|
| 705 |
while (uvi != uvi_end) { |
| 706 |
auto i_in_deg_pair = in_edges.equal_range(*uvi); |
| 707 |
auto i_out_deg_pair = out_edges.equal_range(*uvi); |
| 708 |
|
| 709 |
if (i_in_deg_pair.first == in_edges.end() |
| 710 |
&& i_out_deg_pair.first == out_edges.end()) { |
| 711 |
uvi = in_use.erase(uvi); |
| 712 |
continue; |
| 713 |
} |
| 714 |
|
| 715 |
// Remove out edges of nodes that don't have a single edge in |
| 716 |
if (i_in_deg_pair.first == in_edges.end()) { |
| 717 |
removeEdges(in_edges, out_edges, i_out_deg_pair, [&](Edge e) { |
| 718 |
return target(e, DepList); |
| 719 |
}); |
| 720 |
changed = true; |
| 721 |
i_out_deg_pair = out_edges.equal_range(*uvi); |
| 722 |
} |
| 723 |
|
| 724 |
// Remove in edges of nodes that don't have a single edge out |
| 725 |
if (i_out_deg_pair.first == out_edges.end()) { |
| 726 |
removeEdges(out_edges, in_edges, i_in_deg_pair, [&](Edge e) { |
| 727 |
return source(e, DepList); |
| 728 |
}); |
| 729 |
changed = true; |
| 730 |
} |
| 731 |
|
| 732 |
++uvi; |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
// Update colors in graph |
| 737 |
const boost::property_map<Graph, boost::edge_attribute_t>::type& edgeAttrMap = |
| 738 |
boost::get(boost::edge_attribute, DepList); |
| 739 |
for (auto ei : out_edges) { |
| 740 |
edgeAttrMap[ei.second]["color"] = "red"; |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
#if defined(__clang__) |
| 745 |
#elif defined(__GNUC__) |
| 746 |
#pragma GCC diagnostic pop |
| 747 |
#endif |
| 748 |
|
| 749 |
void markOutOfScopeLinks() |
| 750 |
{ |
| 751 |
const boost::property_map<Graph, boost::edge_attribute_t>::type& edgeAttrMap = |
| 752 |
boost::get(boost::edge_attribute, DepList); |
| 753 |
|
| 754 |
for (auto obj : objects) { |
| 755 |
|
| 756 |
std::vector<App::DocumentObject*> invalids; |
| 757 |
GeoFeatureGroupExtension::getInvalidLinkObjects(obj, invalids); |
| 758 |
// isLinkValid returns true for non-link properties |
| 759 |
for (auto linkedObj : invalids) { |
| 760 |
|
| 761 |
auto res = edge(GlobalVertexList[getId(obj)], |
| 762 |
GlobalVertexList[getId(linkedObj)], |
| 763 |
DepList); |
| 764 |
if (res.second) { |
| 765 |
edgeAttrMap[res.first]["color"] = "orange"; |
| 766 |
} |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
const struct DocumentP* d; |
| 772 |
Graph DepList; |
| 773 |
int vertex_no {0}; |
| 774 |
std::map<std::string, Vertex> LocalVertexList; |
| 775 |
std::map<std::string, Vertex> GlobalVertexList; |
| 776 |
std::set<const DocumentObject*> objects; |
| 777 |
std::map<const DocumentObject*, Graph*> GraphList; |
| 778 |
// random color generation |
| 779 |
std::mt19937 seed; |
| 780 |
std::uniform_int_distribution<int> distribution; |
| 781 |
}; |
| 782 |
|
| 783 |
GraphCreator g(d); |
| 784 |
|
| 785 |
boost::write_graphviz(out, g.getGraph()); |
| 786 |
} |