All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Graph.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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_graphData(0),
54  m_dataManager(0),
55  m_graphCache(0),
56  m_metadata(0)
57 {
58 }
59 
61  AbstractGraph(),
62  m_graphData(0),
63  m_dataManager(0),
64  m_graphCache(0),
65  m_metadata(0)
66 {
67  assert(ls);
68 
70 
72 
74 
75  m_metadata = ls->getMetadata();
76 }
77 
79 {
80  flush();
81 
82  delete m_graphCache;
83  delete m_dataManager;
84 }
85 
87 {
88  if(!m_graphData)
89  {
90  m_graphData = m_graphCache->getGraphData();
91  }
92 
93  m_graphData->getVertexMap().insert(te::graph::GraphData::VertexMap::value_type(v->getId(), v));
94 
95  if(v->isDirty() || v->isNew())
96  {
97  m_graphData->setDirty(true);
98  }
99 
100  if(m_graphData->getVertexMap().size() >= m_metadata->m_maxCacheSize)
101  {
102  m_graphData = 0;
103  }
104 }
105 
107 {
108  v->setDirty(true);
109 
110  m_graphData->setDirty(true);
111 }
112 
114 {
115  te::graph::GraphData::VertexMap::iterator it = m_graphData->getVertexMap().find(id);
116 
117  if(it != m_graphData->getVertexMap().end())
118  {
119  m_graphData->getVertexMap().erase(it);
120  }
121  else
122  {
123  m_graphData = m_graphCache->getGraphDataByVertexId(id);
124 
125  if(m_graphData)
126  {
127  it = m_graphData->getVertexMap().find(id);
128 
129  if(it != m_graphData->getVertexMap().end())
130  {
131  m_graphData->getVertexMap().erase(it);
132  }
133  }
134  }
135 
136  if(m_dataManager)
137  {
138  m_dataManager->removeVertex(id);
139  }
140 }
141 
143 {
144  if(m_graphData)
145  {
146  te::graph::GraphData::VertexMap::iterator it = m_graphData->getVertexMap().find(id);
147 
148  if(it != m_graphData->getVertexMap().end())
149  {
150  return it->second;
151  }
152  }
153 
154  m_graphData = m_graphCache->getGraphDataByVertexId(id);
155 
156  if(m_graphData)
157  {
158  te::graph::GraphData::VertexMap::iterator it = m_graphData->getVertexMap().find(id);
159 
160  if(it != m_graphData->getVertexMap().end())
161  {
162  return it->second;
163  }
164  }
165 
166  return 0;
167 }
168 
170 {
171  if(m_metadata)
172  {
173  m_metadata->addVertexProperty(p);
174  }
175 }
176 
178 {
179  if(m_metadata)
180  {
181  m_metadata->removeVertexProperty(idx);
182  }
183 }
184 
186 {
187  if(m_metadata)
188  {
189  return m_metadata->getVertexProperty(idx);
190  }
191 
192  return 0;
193 }
194 
196 {
197  if(m_metadata)
198  {
199  return m_metadata->getVertexPropertySize();
200  }
201 
202  return 0;
203 }
204 
206 {
207  if(!m_graphData)
208  {
209  m_graphData = m_graphCache->getGraphData();
210  }
211 
212  m_graphData->getEdgeMap().insert(te::graph::GraphData::EdgeMap::value_type(e->getId(), e));
213 
214  if(e->isDirty() || e->isNew())
215  {
216  m_graphData->setDirty(true);
217  }
218 
219  if(m_graphData->getEdgeMap().size() >= m_metadata->m_maxCacheSize)
220  {
221  m_graphData = 0;
222  }
223 }
224 
226 {
227  e->setDirty(true);
228 
229  m_graphData->setDirty(true);
230 }
231 
233 {
234  te::graph::GraphData::EdgeMap::iterator it = m_graphData->getEdgeMap().find(id);
235 
236  if(it != m_graphData->getEdgeMap().end())
237  {
238  m_graphData->getEdgeMap().erase(it);
239  }
240  else
241  {
242  m_graphData = m_graphCache->getGraphDataByEdgeId(id);
243 
244  if(m_graphData)
245  {
246  it = m_graphData->getEdgeMap().find(id);
247 
248  if(it != m_graphData->getEdgeMap().end())
249  {
250  m_graphData->getEdgeMap().erase(it);
251  }
252  }
253  }
254 
255  if(m_dataManager)
256  {
257  m_dataManager->removeEdge(id);
258  }
259 }
260 
262 {
263  if(m_graphData)
264  {
265  te::graph::GraphData::EdgeMap::iterator it = m_graphData->getEdgeMap().find(id);
266 
267  if(it != m_graphData->getEdgeMap().end())
268  {
269  return it->second;
270  }
271  }
272 
273  m_graphData = m_graphCache->getGraphDataByEdgeId(id);
274 
275  if(m_graphData)
276  {
277  te::graph::GraphData::EdgeMap::iterator it = m_graphData->getEdgeMap().find(id);
278 
279  if(it != m_graphData->getEdgeMap().end())
280  {
281  return it->second;
282  }
283  }
284 
285  return 0;
286 }
287 
289 {
290  if(m_metadata)
291  {
292  m_metadata->addEdgeProperty(p);
293  }
294 }
295 
297 {
298  if(m_metadata)
299  {
300  m_metadata->removeEdgeProperty(idx);
301  }
302 }
303 
305 {
306  if(m_metadata)
307  {
308  return m_metadata->getEdgeProperty(idx);
309  }
310 
311  return 0;
312 }
313 
315 {
316  if(m_metadata)
317  {
318  return m_metadata->getEdgePropertySize();
319  }
320 
321  return 0;
322 }
323 
325 {
326  return m_metadata;
327 }
328 
330 {
331  m_graphData = 0;
332 
333  m_graphCache->clearCache();
334 }
GraphDataManager * m_dataManager
Used to load and save GraphData information from a DataSource.
Definition: Graph.h:284
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:56
void setDirty(bool flag)
Flag used to indicate that this element was changed.
Definition: Edge.cpp:108
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 define the main functions necessary to save and load the graph data and metadata informati...
bool isDirty()
Used to verify the vertex state.
Definition: Vertex.cpp:121
virtual int getEdgePropertySize()
Used to verify the number of properties associated to edge elements.
Definition: Graph.cpp:314
Class used to manager the graph data elements. This class uses a cache policy to control the elements...
Definition: GraphCache.h:60
Graph()
constructor.
Definition: Graph.cpp:52
bool isNew()
Flag used to indicate that this element was a new one.
Definition: Vertex.cpp:126
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
virtual te::dt::Property * getEdgeProperty(int idx)
Get a edge property given a index.
Definition: Graph.cpp:304
bool isDirty()
Used to verify the edge state.
Definition: Edge.cpp:113
virtual void removeVertexProperty(int idx)
Remove a property associated to the vertex element.
Definition: Graph.cpp:177
~Graph()
Virtual destructor.
Definition: Graph.cpp:78
virtual void removeVertex(int id)
This function removes the vertex element from graph, also was removed in data source.
Definition: Graph.cpp:113
bool isNew()
Flag used to indicate that this element was a new one.
Definition: Edge.cpp:118
te::graph::GraphMetadata * getMetadata()
It returns a pointer to a class that describes the graph metadata.
GraphCache * m_graphCache
Class used to keep all graph data loaded.
Definition: Graph.h:286
This is the main graph implementation, that uses a cache policy anda graph loader to get all elements...
virtual int getVertexPropertySize()
Used to verify the number of properties associated to vertex elements.
Definition: Graph.cpp:195
virtual te::graph::GraphMetadata * getMetadata()
Function used to access the graph metadata.
Definition: Graph.cpp:324
void setLoaderStrategy(AbstractGraphLoaderStrategy *loaderStrategy)
Function used to set a current loader strategy.
int getId()
It returns the vertex id.
Definition: Vertex.cpp:69
virtual te::dt::Property * getVertexProperty(int idx)
Get a vertex property given a index.
Definition: Graph.cpp:185
int getId()
It returns the edge identification.
Definition: Edge.cpp:66
virtual void removeEdgeProperty(int idx)
Remove a property associated to the edge element.
Definition: Graph.cpp:296
virtual te::graph::Edge * getEdge(int id)
It returns the edge element if it&#39;s exist.
Definition: Graph.cpp:261
It models a property definition.
Definition: Property.h:59
This class is used to set the main functions of a cache policy.
virtual void addEdgeProperty(te::dt::Property *p)
Add a new property associated to the edge element.
Definition: Graph.cpp:288
Class used to define the graph metadata informations.
Definition: GraphMetadata.h:56
void setDirty(bool flag)
Flag used to indicate that this element was changed.
Definition: Vertex.cpp:116
virtual void addVertexProperty(te::dt::Property *p)
Add a new property associated to the vertex element.
Definition: Graph.cpp:169
virtual void removeEdge(int id)
This function removes the edge element from graph, also was removed in data source.
Definition: Graph.cpp:232
virtual void flush()
Function used to clear the memory cache, all elements was released from memory, if any element was ch...
Definition: Graph.cpp:329
virtual te::graph::Vertex * getVertex(int id)
It returns the vertex element if it&#39;s exist.
Definition: Graph.cpp:142
virtual void add(Vertex *v)
Add a new vertex element to a graph.
Definition: Graph.cpp:86
GraphMetadata * m_metadata
Graph Data loader strategy.
Definition: Graph.h:288
virtual void update(Vertex *v)
Update the vertex element.
Definition: Graph.cpp:106