All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../../common/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(0),
54  m_graphCache(0),
55  m_metadata(0),
56  m_graphData(0)
57 {
58 }
59 
61  AbstractGraph(),
62  m_dataManager(0),
63  m_graphCache(0),
64  m_metadata(metadata)
65 {
66  assert(metadata);
67 
70 }
71 
73  AbstractGraph(),
74  m_dataManager(0),
75  m_graphCache(0),
76  m_metadata(0),
77  m_graphData(0)
78 {
79  assert(ls);
80 
82 
84 
86 
87  m_metadata = ls->getMetadata();
88 }
89 
91 {
92  flush();
93 
94  delete m_graphCache;
95  delete m_dataManager;
96 }
97 
99 {
100  if(!m_graphData)
101  {
102  m_graphData = m_graphCache->getGraphData();
103  }
104 
105  m_graphData->addVertex(v);
106 
107  if(m_graphData->getVertexMap().size() >= m_metadata->m_maxCacheSize && !m_metadata->m_memoryGraph)
108  {
109  m_graphData = 0;
110  }
111 }
112 
114 {
115  v->setDirty(true);
116 
117  m_graphData->setDirty(true);
118 }
119 
121 {
122  bool res = m_graphData->removeVertex(id);
123 
124  if(!res && !m_metadata->m_memoryGraph)
125  {
126  m_graphData = m_graphCache->getGraphDataByVertexId(id);
127 
128  if(m_graphData)
129  m_graphData->removeVertex(id);
130  }
131 
132  if(m_dataManager)
133  {
134  m_dataManager->removeVertex(id);
135  }
136 }
137 
139 {
140  te::graph::Vertex* v = 0;
141 
142  if(m_graphData)
143  {
144  v = m_graphData->getVertex(id);
145  }
146 
147  if(!v && !m_metadata->m_memoryGraph)
148  {
149  m_graphData = m_graphCache->getGraphDataByVertexId(id);
150 
151  if(m_graphData)
152  v = m_graphData->getVertex(id);
153  }
154 
155  return v;
156 }
157 
159 {
160  if(m_metadata)
161  {
162  m_metadata->addVertexProperty(p);
163  }
164 }
165 
167 {
168  if(m_metadata)
169  {
170  m_metadata->removeVertexProperty(idx);
171  }
172 }
173 
175 {
176  if(m_metadata)
177  {
178  return m_metadata->getVertexProperty(idx);
179  }
180 
181  return 0;
182 }
183 
185 {
186  if(m_metadata)
187  {
188  return m_metadata->getVertexPropertySize();
189  }
190 
191  return 0;
192 }
193 
195 {
196  if(!m_graphData)
197  {
198  m_graphData = m_graphCache->getGraphData();
199  }
200 
201  m_graphData->addEdge(e);
202 
203  if(m_graphData->getEdgeMap().size() >= m_metadata->m_maxCacheSize && !m_metadata->m_memoryGraph)
204  {
205  m_graphData = 0;
206  }
207 }
208 
210 {
211  e->setDirty(true);
212 
213  m_graphData->setDirty(true);
214 }
215 
217 {
218  bool res = m_graphData->removeEdge(id);
219 
220  if(!res && !m_metadata->m_memoryGraph)
221  {
222  m_graphData = m_graphCache->getGraphDataByEdgeId(id);
223 
224  if(m_graphData)
225  m_graphData->removeEdge(id);
226  }
227 
228  if(m_dataManager)
229  {
230  m_dataManager->removeEdge(id);
231  }
232 }
233 
235 {
236  te::graph::Edge* e = 0;
237 
238  if(m_graphData)
239  {
240  e = m_graphData->getEdge(id);
241  }
242 
243  if(!e && !m_metadata->m_memoryGraph)
244  {
245  m_graphData = m_graphCache->getGraphDataByEdgeId(id);
246 
247  if(m_graphData)
248  e = m_graphData->getEdge(id);
249  }
250 
251  return e;
252 }
253 
255 {
256  if(m_metadata)
257  {
258  m_metadata->addEdgeProperty(p);
259  }
260 }
261 
263 {
264  if(m_metadata)
265  {
266  m_metadata->removeEdgeProperty(idx);
267  }
268 }
269 
271 {
272  if(m_metadata)
273  {
274  return m_metadata->getEdgeProperty(idx);
275  }
276 
277  return 0;
278 }
279 
281 {
282  if(m_metadata)
283  {
284  return m_metadata->getEdgePropertySize();
285  }
286 
287  return 0;
288 }
289 
291 {
292  return m_metadata;
293 }
294 
296 {
297  if(m_metadata->m_memoryGraph)
298  {
299  delete m_graphData;
300  m_graphData = 0;
301  }
302  else
303  {
304  m_graphData = 0;
305  m_graphCache->clearCache();
306  }
307 }
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:262
virtual void removeEdge(int id)
This function removes the edge element from graph, also was removed in data source.
Definition: Graph.cpp:216
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:254
virtual te::graph::Edge * getEdge(int id)
It returns the edge element if it's exist.
Definition: Graph.cpp:234
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
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.
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:174
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
virtual int getVertexPropertySize()
Used to verify the number of properties associated to vertex elements.
Definition: Graph.cpp:184
virtual void flush()
Function used to clear the memory cache, all elements was released from memory, if any element was ch...
Definition: Graph.cpp:295
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:98
virtual void removeVertexProperty(int idx)
Remove a property associated to the vertex element.
Definition: Graph.cpp:166
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
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:90
virtual void addVertexProperty(te::dt::Property *p)
Add a new property associated to the vertex element.
Definition: Graph.cpp:158
virtual int getEdgePropertySize()
Used to verify the number of properties associated to edge elements.
Definition: Graph.cpp:280
virtual te::dt::Property * getEdgeProperty(int idx)
Get a edge property given a index.
Definition: Graph.cpp:270
virtual te::graph::Vertex * getVertex(int id)
It returns the vertex element if it's exist.
Definition: Graph.cpp:138
virtual te::graph::GraphMetadata * getMetadata()
Function used to access the graph metadata.
Definition: Graph.cpp:290
virtual void update(Vertex *v)
Update the vertex element.
Definition: Graph.cpp:113
virtual void removeVertex(int id)
This function removes the vertex element from graph, also was removed in data source.
Definition: Graph.cpp:120
Class used to define the graph metadata informations.
Definition: GraphMetadata.h:56