Graph.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file Graph.cpp
22 
23  \brief This is the main graph implementation, that uses a
24  cache policy anda graph loader to get all elements
25  inside a data source.
26 
27  All methods to access a graph element (vertex or edge)
28  will use the GraphData instance, if not found the element,
29  the class GraphCache will be consulted.
30 */
31 
32 // Terralib Includes
33 #include "../../common/STLUtils.h"
34 #include "../../common/StringUtils.h"
35 #include "../../core/translator/Translator.h"
36 #include "../core/Edge.h"
37 #include "../core/EdgeProperty.h"
38 #include "../core/GraphData.h"
39 #include "../core/GraphDataManager.h"
40 #include "../core/GraphCache.h"
41 #include "../core/GraphMetadata.h"
42 #include "../core/Vertex.h"
43 #include "../core/VertexProperty.h"
44 #include "../loader/AbstractGraphLoaderStrategy.h"
45 #include "../Config.h"
46 #include "../Exception.h"
47 #include "Graph.h"
48 
49 // STL Includes
50 #include <cassert>
51 
53  : m_dataManager(nullptr),
54  m_graphCache(nullptr),
55  m_metadata(nullptr),
56  m_graphData(nullptr)
57 {
58 }
59 
61  :
62 
63  m_dataManager(nullptr),
64  m_graphCache(nullptr),
65  m_metadata(metadata)
66 {
67  assert(metadata);
68 
71 }
72 
75  :
76 
77  m_dataManager(nullptr),
78  m_graphCache(nullptr),
79  m_metadata(nullptr),
80  m_graphData(nullptr)
81 {
82  assert(ls);
83 
85 
87 
89 
90  m_metadata = ls->getMetadata();
91 }
92 
94 {
95  flush();
96 
97  delete m_graphCache;
98  delete m_dataManager;
99 }
100 
102 {
103  if(!m_graphData)
104  {
106  }
107 
109 
111  {
112  m_graphData = nullptr;
113  }
114 }
115 
117 {
118  v->setDirty(true);
119 
120  m_graphData->setDirty(true);
121 }
122 
124 {
125  bool res = m_graphData->removeVertex(id);
126 
127  if(!res && !m_metadata->m_memoryGraph)
128  {
130 
131  if(m_graphData)
133  }
134 
135  if(m_dataManager)
136  {
138  }
139 }
140 
142 {
143  te::graph::Vertex* v = nullptr;
144 
145  if(m_graphData)
146  {
147  v = m_graphData->getVertex(id);
148  }
149 
150  if(!v && !m_metadata->m_memoryGraph)
151  {
153 
154  if(m_graphData)
155  v = m_graphData->getVertex(id);
156  }
157 
158  return v;
159 }
160 
162 {
163  if(m_metadata)
164  {
166  }
167 }
168 
170 {
171  if(m_metadata)
172  {
174  }
175 }
176 
178 {
179  if(m_metadata)
180  {
181  return m_metadata->getVertexProperty(idx);
182  }
183 
184  return nullptr;
185 }
186 
188 {
189  if(m_metadata)
190  {
192  }
193 
194  return 0;
195 }
196 
198 {
199  if(!m_graphData)
200  {
202  }
203 
204  m_graphData->addEdge(e);
205 
207  {
208  m_graphData = nullptr;
209  }
210 }
211 
213 {
214  e->setDirty(true);
215 
216  m_graphData->setDirty(true);
217 }
218 
220 {
221  bool res = m_graphData->removeEdge(id);
222 
223  if(!res && !m_metadata->m_memoryGraph)
224  {
226 
227  if(m_graphData)
228  m_graphData->removeEdge(id);
229  }
230 
231  if(m_dataManager)
232  {
234  }
235 }
236 
238 {
239  te::graph::Edge* e = nullptr;
240 
241  if(m_graphData)
242  {
243  e = m_graphData->getEdge(id);
244  }
245 
246  if(!e && !m_metadata->m_memoryGraph)
247  {
249 
250  if(m_graphData)
251  e = m_graphData->getEdge(id);
252  }
253 
254  return e;
255 }
256 
258 {
259  if(m_metadata)
260  {
262  }
263 }
264 
266 {
267  if(m_metadata)
268  {
270  }
271 }
272 
274 {
275  if(m_metadata)
276  {
277  return m_metadata->getEdgeProperty(idx);
278  }
279 
280  return nullptr;
281 }
282 
284 {
285  if(m_metadata)
286  {
288  }
289 
290  return 0;
291 }
292 
294 {
295  return m_metadata;
296 }
297 
299 {
301  {
302  delete m_graphData;
303  m_graphData = nullptr;
304  }
305  else
306  {
307  m_graphData = nullptr;
309  }
310 }
Vertex * getVertex(int id)
Definition: GraphData.cpp:77
void clearCache()
Used to remove from memory all elements loaded.
Definition: GraphCache.cpp:222
GraphMetadata * m_metadata
Graph Data loader strategy.
Definition: Graph.h:294
te::graph::GraphMetadata * getMetadata()
It returns a pointer to a class that describes the graph metadata.
virtual void removeEdgeProperty(int idx)
Remove a property associated to the edge element.
Definition: Graph.cpp:265
VertexMap & getVertexMap()
It returns the the vertex map.
Definition: GraphData.cpp:89
virtual te::dt::Property * getVertexProperty(int idx)
Get a vertex property given a index.
GraphData * getGraphDataByEdgeId(int id)
Get a graph data from vector using a edge id information. if not found a new graph data has to be loa...
Definition: GraphCache.cpp:92
virtual te::dt::Property * getEdgeProperty(int idx)
Get a edge property given a index.
void removeEdge(int id)
Function used to remove a edge from data source.
virtual int getVertexPropertySize()
Used to verify the number of properties associated to vertex elements.
Edge * getEdge(int id)
Definition: GraphData.cpp:121
virtual void removeEdge(int id)
This function removes the edge element from graph, also was removed in data source.
Definition: Graph.cpp:219
virtual void addVertexProperty(te::dt::Property *p)
Add a new property associated to the vertex element.
void setLoaderStrategy(AbstractGraphLoaderStrategy *loaderStrategy)
Function used to set a current loader strategy.
virtual void addEdgeProperty(te::dt::Property *p)
Add a new property associated to the edge element.
Definition: Graph.cpp:257
virtual te::graph::Edge * getEdge(int id)
It returns the edge element if it&#39;s exist.
Definition: Graph.cpp:237
bool m_memoryGraph
Flag used to indicate if the graph is a memory graph.
Graph()
constructor.
Definition: Graph.cpp:52
It models a property definition.
Definition: Property.h:59
void addEdge(Edge *e)
Definition: GraphData.cpp:99
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
This class is used to set the main functions of a cache policy.
void setDirty(bool status)
Flag used to define the graph data state.
Definition: GraphData.cpp:143
GraphData * getGraphData()
Get a graph data.
Definition: GraphCache.cpp:124
bool removeVertex(int id)
Definition: GraphData.cpp:65
GraphCache * m_graphCache
Class used to keep all graph data loaded.
Definition: Graph.h:292
GraphDataManager * m_dataManager
Used to load and save GraphData information from a DataSource.
Definition: Graph.h:290
virtual te::dt::Property * getVertexProperty(int idx)
Get a vertex property given a index.
Definition: Graph.cpp:177
virtual int getVertexPropertySize()
Used to verify the number of properties associated to vertex elements.
Definition: Graph.cpp:187
size_t m_maxCacheSize
Attribute used to set the max cache size.
virtual void flush()
Function used to clear the memory cache, all elements was released from memory, if any element was ch...
Definition: Graph.cpp:298
void addVertex(Vertex *v)
Definition: GraphData.cpp:55
te::gm::Polygon * p
void setDirty(bool flag)
Flag used to indicate that this element was changed.
Definition: Vertex.cpp:116
This class define the main functions necessary to save and load the graph data and metadata informati...
virtual void add(Vertex *v)
Add a new vertex element to a graph.
Definition: Graph.cpp:101
virtual void removeVertexProperty(int idx)
Remove a property associated to the vertex element.
Definition: Graph.cpp:169
EdgeMap & getEdgeMap()
It returns the the edge map.
Definition: GraphData.cpp:133
void setDirty(bool flag)
Flag used to indicate that this element was changed.
Definition: Edge.cpp:108
This is the main graph implementation, that uses a cache policy anda graph loader to get all elements...
Class used to manager the graph data elements. This class uses a cache policy to control the elements...
Definition: GraphCache.h:60
bool removeEdge(int id)
Definition: GraphData.cpp:109
void removeVertex(int id)
Function used to remove a vertex from data source.
This class define a important struct used to group a map of vertex and edges. A flag is used to indic...
Definition: GraphData.h:55
GraphData * m_graphData
This class has the graph data and properties.
Definition: Graph.h:298
~Graph()
Virtual destructor.
Definition: Graph.cpp:93
virtual void removeVertexProperty(int idx)
Remove a property associated to the vertex element.
virtual void addVertexProperty(te::dt::Property *p)
Add a new property associated to the vertex element.
Definition: Graph.cpp:161
virtual void removeEdgeProperty(int idx)
Remove a property associated to the edge element.
virtual int getEdgePropertySize()
Used to verify the number of properties associated to edge elements.
Definition: Graph.cpp:283
virtual te::dt::Property * getEdgeProperty(int idx)
Get a edge property given a index.
Definition: Graph.cpp:273
virtual int getEdgePropertySize()
Used to verify the number of properties associated to edge elements.
virtual te::graph::Vertex * getVertex(int id)
It returns the vertex element if it&#39;s exist.
Definition: Graph.cpp:141
virtual te::graph::GraphMetadata * getMetadata()
Function used to access the graph metadata.
Definition: Graph.cpp:293
GraphData * getGraphDataByVertexId(int id)
Get a graph data from vector using a vertex id information. if not found a new graph data has to be l...
Definition: GraphCache.cpp:60
virtual void update(Vertex *v)
Update the vertex element.
Definition: Graph.cpp:116
virtual void removeVertex(int id)
This function removes the vertex element from graph, also was removed in data source.
Definition: Graph.cpp:123
virtual void addEdgeProperty(te::dt::Property *p)
Add a new property associated to the edge element.
Class used to define the graph metadata informations.
Definition: GraphMetadata.h:56