OpenLexocad  27.1
GraphBuilder.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Core/ExecObject.h>
4 #include <time.h>
5 
6 #include <algorithm>
7 #include <boost/graph/adjacency_list.hpp>
8 #include <boost/graph/depth_first_search.hpp>
9 #include <boost/graph/dijkstra_shortest_paths.hpp>
10 #include <boost/graph/graph_utility.hpp>
11 #include <boost/graph/graphviz.hpp>
12 #include <boost/graph/labeled_graph.hpp>
13 #include <boost/graph/isomorphism.hpp>
14 #include <boost/graph/properties.hpp>
15 #include <boost/graph/topological_sort.hpp>
16 #include <boost/graph/visitors.hpp>
17 #include <boost/utility.hpp>
18 #include <iostream>
19 #include <iterator>
20 #include <list>
21 #include <map>
22 #include <set>
23 #include <vector>
24 #include <string>
25 #include <optional>
26 #include <tbb/mutex.h>
27 
28 
29 typedef boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, boost::property<boost::vertex_name_t, std::string> > ObjectGraph_Type;
30 
31 
32 
33 //typedef boost::adjacency_list<boost::vecS, boost::listS, boost::bidirectionalS, boost::property<boost::vertex_index_t, size_t>> ObjectGraph_Type;
34 
35 /*
36 typedef boost::adjacency_list<
37  boost::listS,
38  boost::listS,
39  boost::bidirectionalS,
40  boost::property<boost::vertex_index_t, long,
41  boost::property<boost::vertex_name_t, std::string >>
42  > ObjectGraph_Type;
43 */
44 
45 
46 typedef ObjectGraph_Type::vertex_descriptor Object_Vertex;
47 
48 
49 
50 namespace Core
51 {
52 class LX_CORE_EXPORT ObjectGraph
53 {
54 public:
55  ObjectGraph();
56  // Copy constructor
57  ObjectGraph(const ObjectGraph& rhs);
58 
59  ObjectGraph(std::vector<Core::DocObject*> objs, bool ignoreLinksToTemporaryObjects = false);
60  ~ObjectGraph();
61 
62  void clear();
63  bool hasObject(const Core::DocObject* o) const;
64  bool addObject(const Core::DocObject* o);
65  bool removeObject(const Core::DocObject* o);
66  bool updateObject(const Core::DocObject* o);
67 
68 
69 
70  bool recreate(std::vector<const Core::DocObject*> toDelete);
71 
72  std::vector<Core::DocObject*> build_SubGraph(const std::vector<Core::DocObject*>& featureSet);
73 
74  std::vector<const Core::DocObject*> getInner(const Core::DocObject* o, std::function<bool(const Core::DocObject*)>* allowToAddObject = 0);
75  std::vector<const Core::DocObject*> getOuter(const Core::DocObject* o);
76 
77  bool checkForCycle();
78  std::vector<const Core::DocObject*> getTopologicalSorted();
79 
80  std::vector<const Core::DocObject*> getLinksToMe(const Core::DocObject* o);
81  std::vector<const Core::DocObject*> getLinksFromMe(const Core::DocObject* o);
82 
83  std::vector<const Core::DocObject*> getBackLinksToMe(const Core::DocObject* o);
84  std::vector<const Core::DocObject*> getBackLinksFromMe(const Core::DocObject* o);
85 
86 
87  std::string dump();
88  std::vector< std::string > getErrors();
89 
90 private:
91 
92  bool allLinksConnected();
93  bool updateObject_BackLink(const Core::DocObject* o);
94  bool addObjectLinks(const Core::DocObject* o);
95  std::string dumpSort(ObjectGraph_Type g);
96  ObjectGraph_Type build(const std::vector<Core::DocObject*>& featureSet, std::map<Object_Vertex, Core::DocObject*>& myFeaturemap);
97 
98  void getOuter(Object_Vertex& vertex, std::vector<const Core::DocObject*>& behindobjects);
99  void getInner(Object_Vertex& vertex,
100  std::set<const Core::DocObject*>& beforeobjects,
101  std::function<bool(const Core::DocObject*)>* allowToAddObject);
102  void getInner(Object_Vertex& vertex,
103  std::vector<const Core::DocObject*>& beforeobjects,
104  std::function<bool(const Core::DocObject*)>* allowToAddObject);
105  std::unordered_map<Object_Vertex, const Core::DocObject*> _vertex_to_object;
106  std::unordered_map<const Core::DocObject*, Object_Vertex> _object_to_vertex;
107 
108  std::unordered_map<Object_Vertex, const Core::DocObject*> _vertex_to_object_BackLink;
109  std::unordered_map<const Core::DocObject*, Object_Vertex> _object_to_vertex_BackLink;
110 
111  ObjectGraph_Type* _objectGraph;
112  ObjectGraph_Type* _objectGraphBackLinks;
113  bool _ignoreLinksToTemporaryObjects = false;
114 
115  std::vector< std::string > mErrors;
116 
117 };
118 
119 
120 struct NodeInfo{size_t id;};
121 struct EdgeInfo{size_t pos;};
122 
123 
124 /*
125 typedef boost::labeled_graph< boost::adjacency_list<
126  boost::vecS, boost::vecS, boost::undirectedS, NodeInfo, EdgeInfo>,
127  std::string> RelGraph_Type;
128  */
129 
130 
131 //typedef boost::property<boost::vertex_name_t, std::string> VertexProperty;
132 //typedef boost::property<boost::edge_name_t, long> EdgeProperty;
133 using RelGraph_Type = boost::adjacency_list<boost::listS, boost::setS, boost::bidirectionalS,NodeInfo,EdgeInfo >;
134 using RelGraph_Vertex = RelGraph_Type::vertex_descriptor;
135 using RelGraph_Edge = RelGraph_Type::edge_descriptor;
136 using RelGraph_Link = std::pair<Core::PropertyLinkBaseBase*,Core::DocObject*>;
137 
138 class LX_CORE_EXPORT RelGraph
139 {
140 public:
142  Core::PropertyLinkBaseBase* getProperty(Core::DocObject* docObj, size_t pos);
143 
144  bool hasRelationShip(PropertyLinkBaseBase* p, Core::DocObject* from, Core::DocObject* to);
145 
146 
147  void addObject( Core::DocObject* from );
148  void removeObject( Core::DocObject* obj );
149  void addRelationShip(PropertyLinkBaseBase* p, Core::DocObject* from,Core::DocObject* to);
150  void addRelationShips(PropertyLinkBaseBase* p, Core::DocObject* from,const std::list<Core::DocObject*>& linkList);
151  void addRelationShips(PropertyLinkBaseBase* p, Core::DocObject* from,const std::unordered_set<Core::DocObject*>& linkSet);
152  void removeRelationShip(PropertyLinkBaseBase* p, Core::DocObject* from,Core::DocObject* to);
153  void removeRelationShips(PropertyLinkBaseBase* p, Core::DocObject* from,const std::list<Core::DocObject*>& linkList);
154  void removeRelationShips(PropertyLinkBaseBase* p, Core::DocObject* from,const std::unordered_set<Core::DocObject*>& linkSet);
155  void test_graph2();
156  std::string dump();
157  void test_graph();
158  std::vector<RelGraph_Link> getOutEdges(Core::DocObject* docObj);
159 
160  std::vector<RelGraph_Link> RelGraph::getInEdges(Core::DocObject* docObj);
161  void breakLinks( Core::DocObject* from );
162 
163 
164 
165 private:
166  RelGraph_Vertex RelGraph::getOrAddVertex( Core::DocObject* docObj );
167  std::optional<RelGraph_Vertex> getVertex(Core::DocObject* docObj);
169  std::unordered_map<size_t,RelGraph_Vertex> _object_to_vertex;
170  RelGraph() = default;
171  Core::CoreDocument* _cDoc = nullptr;
172  tbb::mutex m_mutex;
173 
174 };
175 
176 
177 } // namespace Core
void breakLinks(Core::DocObject *from)
RelGraph * _relGraph
Definition: CoreDocument.h:841
std::pair< Core::PropertyLinkBaseBase *, Core::DocObject * > RelGraph_Link
Definition: GraphBuilder.h:136
void removeObject(Core::DocObject *e)
Removes an object from the document.
boost::adjacency_list< boost::setS, boost::vecS, boost::bidirectionalS, boost::property< boost::vertex_name_t, std::string > > ObjectGraph_Type
Definition: GraphBuilder.h:29
RelGraph_Type::vertex_descriptor RelGraph_Vertex
Definition: GraphBuilder.h:134
ObjectGraph_Type::vertex_descriptor Object_Vertex
Definition: GraphBuilder.h:46
std::vector< const Core::DocObject * > getInner(const Core::DocObject *me, std::function< bool(const Core::DocObject *)> *allowToAddObject=0)
Get ALL Links to me, also indirect.
size_t id
Definition: GraphBuilder.h:120
Definition: CoreDocument.h:210
bool addObject(Core::DocObject *e)
Adds an existing object to the document.
boost::adjacency_list< boost::listS, boost::setS, boost::bidirectionalS, NodeInfo, EdgeInfo > RelGraph_Type
Definition: GraphBuilder.h:133
size_t pos
Definition: GraphBuilder.h:121
std::vector< const Core::DocObject * > getBackLinksFromMe(const Core::DocObject *o)
Returns all objects 'o' directly linked from 'o'.
RelGraph_Type::edge_descriptor RelGraph_Edge
Definition: GraphBuilder.h:135
Definition: Base.h:12
std::vector< const Core::DocObject * > getBackLinksToMe(const Core::DocObject *o)
Returns all objects that directly linked to 'o'.
std::vector< const Core::DocObject * > getOuter(const Core::DocObject *me)
Get ALL Links from me, also indirect.
void test_graph()
Definition: GraphBuilder.h:121
Definition: GraphBuilder.h:120
Definition: GraphBuilder.h:138
std::vector< const Core::DocObject * > getLinksFromMe(const Core::DocObject *o)
Returns all objects 'o' directly linked from 'o'.
std::vector< const Core::DocObject * > getLinksToMe(const Core::DocObject *o)
Returns all objects that directly linked to 'o'.
Definition: GraphBuilder.h:52
Definition: DocObject.h:51