All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AbstractGraphLoaderStrategy.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 AbstractGraphLoaderStrategy.h
22 
23  \brief This class define the main functions necessary to
24  save and load the graph data and metadata information
25  using the Graph Data and Graph Cache conceptions.
26 */
27 
28 
29 // Terralib Includes
30 #include "../../common/STLUtils.h"
31 #include "../../common/StringUtils.h"
32 #include "../../common/Translator.h"
33 #include "../../dataaccess/dataset/DataSet.h"
34 #include "../../dataaccess/dataset/DataSetType.h"
35 #include "../../dataaccess/datasource/DataSource.h"
36 #include "../../dataaccess/query_h.h"
37 #include "../../memory/DataSet.h"
38 #include "../../memory/DataSetItem.h"
39 #include "../core/AbstractGraph.h"
40 #include "../core/Edge.h"
41 #include "../core/GraphCache.h"
42 #include "../core/EdgeProperty.h"
43 #include "../core/GraphData.h"
44 #include "../core/GraphMetadata.h"
45 #include "../core/Vertex.h"
46 #include "../core/VertexProperty.h"
47 #include "../Config.h"
48 #include "../Globals.h"
49 #include "../Exception.h"
51 
52 
54 {
55 }
56 
58 {
59  delete m_graphMetadata;
60 }
61 
63 {
64  return m_graphMetadata;
65 }
66 
68 {
69  if(m_graphMetadata == 0 || m_graphMetadata->getDataSource() == 0)
70  {
71  throw Exception(TE_TR(""));
72  }
73 
74  //save graph topology
75  if(m_graphMetadata->getStorageMode() == te::graph::Edge_List)
76  {
77  saveGraphEdgeList(data);
78  }
79 }
80 
81 
83 {
84  if(m_graphMetadata == 0 || m_graphMetadata->getDataSource() == 0)
85  {
86  throw Exception(TE_TR(""));
87  }
88 }
89 
90 
92 {
93  if(m_graphMetadata == 0 || m_graphMetadata->getDataSource() == 0)
94  {
95  throw Exception(TE_TR(""));
96  }
97 }
98 
99 
101 {
102  if(data->isDirty() == false)
103  {
104  return;
105  }
106 
107  std::string tableName = m_graphMetadata->getEdgeTableName();
108 
109  //get dataset type
110  std::auto_ptr<te::da::DataSetType> dsType(m_graphMetadata->getDataSource()->getDataSetType(tableName));
111 
112  //create outputs data sets
113  std::auto_ptr<te::mem::DataSet> outDataSet(new te::mem::DataSet(dsType.get()));
114  std::auto_ptr<te::mem::DataSet> outDataSetUpdate(new te::mem::DataSet(dsType.get()));
115 
116  te::graph::GraphData::EdgeMap::iterator it = data->getEdgeMap().begin();
117 
118  bool hasNewObjects = false;
119  bool hasUpdatedObjects = false;
120 
121  while(it != data->getEdgeMap().end())
122  {
123  if(it->second->isNew()) //new element
124  {
125  //create new item
126  te::mem::DataSetItem* dsItem = new te::mem::DataSetItem(outDataSet.get());
127 
128  dsItem->setInt32(0, it->second->getId());
129  dsItem->setInt32(1, it->second->getIdFrom());
130  dsItem->setInt32(2, it->second->getIdTo());
131 
132  int pos = 3; // number of attributes already defined in dsItem.
133 
134  //check for edge properties
135  if(m_graphMetadata->getEdgePropertySize() != 0)
136  {
137  for(size_t i = 0; i < it->second->getAttributes().size(); ++i)
138  {
139  dsItem->setValue(pos + i, it->second->getAttributes()[i]->clone());
140  }
141  }
142 
143  outDataSet->add(dsItem);
144 
145  hasNewObjects = true;
146  }
147  else if(it->second->isDirty()) //update element
148  {
149  //create new item
150  te::mem::DataSetItem* dsItem = new te::mem::DataSetItem(outDataSetUpdate.get());
151 
152  dsItem->setInt32(0, it->second->getId());
153  dsItem->setInt32(1, it->second->getIdFrom());
154  dsItem->setInt32(2, it->second->getIdTo());
155 
156  int pos = 3; // number of attributes already defined in dsItem.
157 
158  //check for edge properties
159  if(m_graphMetadata->getEdgePropertySize() != 0)
160  {
161  for(size_t i = 0; i < it->second->getAttributes().size(); ++i)
162  {
163  dsItem->setValue(pos + i, it->second->getAttributes()[i]->clone());
164  }
165  }
166 
167  outDataSetUpdate->add(dsItem);
168 
169  hasUpdatedObjects = true;
170  }
171 
172  ++it;
173  }
174 
175  std::map<std::string, std::string> options;
176 
177  if(hasNewObjects)
178  {
179  outDataSet->moveFirst();
180  m_graphMetadata->getDataSource()->add(tableName, outDataSet.get(), options);
181  }
182  outDataSet->clear();
183 
184  if(hasUpdatedObjects)
185  {
186  outDataSetUpdate->moveFirst();
187  std::map<std::string, std::string> options;
188  std::vector<std::size_t> properties;
189 
190  for(size_t t = 0; t < dsType->getProperties().size(); ++t)
191  {
192  if(t != 0)
193  properties.push_back(t);
194  }
195 
196  m_graphMetadata->getDataSource()->update(tableName, outDataSetUpdate.get(), properties, 0, options);
197  }
198  outDataSetUpdate->clear();
199 
200  //must save the vertex attributes
201  if(m_graphMetadata->getVertexPropertySize() != 0)
202  {
203  saveVertexAttributes(data);
204  }
205 }
206 
208 {
209  if(data->isDirty() == false)
210  {
211  return;
212  }
213 
214  std::string tableName = m_graphMetadata->getVertexTableName();
215 
216  //get dataset type
217  std::auto_ptr<te::da::DataSetType> dsType(m_graphMetadata->getDataSource()->getDataSetType(tableName));
218 
219  //create outputs data sets
220  std::auto_ptr<te::mem::DataSet> outDataSet(new te::mem::DataSet(dsType.get()));
221  std::auto_ptr<te::mem::DataSet> outDataSetUpdate(new te::mem::DataSet(dsType.get()));
222 
223  te::graph::GraphData::VertexMap::iterator it = data->getVertexMap().begin();
224 
225  bool hasNewObjects = false;
226  bool hasUpdatedObjects = false;
227 
228  while(it != data->getVertexMap().end())
229  {
230  if(it->second->isNew()) //new element
231  {
232  //create new item
233  te::mem::DataSetItem* dsItem = new te::mem::DataSetItem(outDataSet.get());
234 
235  dsItem->setInt32(0, it->second->getId());
236 
237  int pos = 1; // number of attributes already defined in dsItem.
238 
239  //check for vertex properties
240  if(m_graphMetadata->getVertexPropertySize() != 0)
241  {
242  for(size_t i = 0; i < it->second->getAttributes().size(); ++i)
243  {
244  dsItem->setValue(pos + i, it->second->getAttributes()[i]->clone());
245  }
246  }
247 
248  outDataSet->add(dsItem);
249 
250  hasNewObjects = true;
251  }
252  else if(it->second->isDirty()) //update element
253  {
254  //create new item
255  te::mem::DataSetItem* dsItem = new te::mem::DataSetItem(outDataSetUpdate.get());
256 
257  dsItem->setInt32(0, it->second->getId());
258 
259  int pos = 1; // number of attributes already defined in dsItem.
260 
261  //check for vertex properties
262  if(m_graphMetadata->getVertexPropertySize() != 0)
263  {
264  for(size_t i = 0; i < it->second->getAttributes().size(); ++i)
265  {
266  dsItem->setValue(pos + i, it->second->getAttributes()[i]->clone());
267  }
268  }
269 
270  outDataSetUpdate->add(dsItem);
271 
272  hasUpdatedObjects = true;
273  }
274 
275  ++it;
276  }
277 
278  std::map<std::string, std::string> options;
279 
280  if(hasNewObjects)
281  {
282  outDataSet->moveFirst();
283  m_graphMetadata->getDataSource()->add(tableName, outDataSet.get(), options);
284  }
285  outDataSet->clear();
286 
287  if(hasUpdatedObjects)
288  {
289  outDataSetUpdate->moveFirst();
290  std::map<std::string, std::string> options;
291  std::vector<std::size_t> properties;
292 
293  for(size_t t = 0; t < dsType->getProperties().size(); ++t)
294  {
295  if(t != 0)
296  properties.push_back(t);
297  }
298 
299  m_graphMetadata->getDataSource()->update(tableName, outDataSetUpdate.get(), properties, 0, options);
300  }
301  outDataSetUpdate->clear();
302 }
303 
305 {
306  throw Exception(TE_TR("TO DO"));
307 }
308 
310 {
311  throw Exception(TE_TR("TO DO"));
312 }
313 
315 {
316  throw Exception(TE_TR("TO DO"));
317 }
318 
320 {
321  //get dataset type
322  std::string tableName = m_graphMetadata->getVertexTableName();
323  std::auto_ptr<te::da::DataSetType> dsType(m_graphMetadata->getDataSource()->getDataSetType(tableName));
324 
325  //create query
327  te::da::Fields* fields = new te::da::Fields;
328  fields->push_back(f);
329 
330  te::da::FromItem* t = new te::da::DataSetName(tableName);
331  te::da::From* from = new te::da::From;
332  from->push_back(t);
333 
335 
337 
338  te::da::Where* filter = new te::da::Where(equal);
339 
340  te::da::Select select(fields, from, filter);
341 
342  std::auto_ptr<te::da::DataSet> dataset = m_graphMetadata->getDataSource()->query(select);
343 
344  //create vertex
345  te::graph::Vertex* v = 0;
346 
347  if(dataset->moveNext())
348  {
349  //get fixed attribute
350  int id = dataset->getInt32(Globals::sm_tableVertexModelAttrId);
351 
352  v = new te::graph::Vertex(id);
353 
354  v->setAttributeVecSize(dsType->getProperties().size() - 1);
355 
356  for(size_t i = 1; i < dsType->getProperties().size(); ++i)
357  {
358  v->addAttribute(i - 1, dataset->getValue(dsType->getProperty(i)->getName()).release());
359  }
360  }
361 
362  return v;
363 }
364 
366 {
367  //get dataset type
368  std::string tableName = m_graphMetadata->getEdgeTableName();
369  std::auto_ptr<te::da::DataSetType> dsType(m_graphMetadata->getDataSource()->getDataSetType(tableName));
370 
371  //create query
373  te::da::Fields* fields = new te::da::Fields;
374  fields->push_back(f);
375 
376  te::da::FromItem* t = new te::da::DataSetName(tableName);
377  te::da::From* from = new te::da::From;
378  from->push_back(t);
379 
381 
383 
384  te::da::Where* filter = new te::da::Where(equal);
385 
386  te::da::Select select(fields, from, filter);
387 
388  std::auto_ptr<te::da::DataSet> dataset = m_graphMetadata->getDataSource()->query(select);
389 
390  //create vertex
391  te::graph::Edge* e = 0;
392 
393  if(dataset->moveNext())
394  {
395  //get fixed attributes
396  int id = dataset->getInt32(Globals::sm_tableEdgeModelAttrId);
397  int vFrom = dataset->getInt32(Globals::sm_tableEdgeModelAttrVFrom);
398  int vTo = dataset->getInt32(Globals::sm_tableEdgeModelAttrVTo);
399 
400  e = new te::graph::Edge(id, vFrom, vTo);
401 
402  e->setAttributeVecSize(dsType->getProperties().size() - 3);
403 
404  for(size_t i = 3; i < dsType->getProperties().size(); ++i)
405  {
406  e->addAttribute(i - 3, dataset->getValue(dsType->getProperty(i)->getName()).release());
407  }
408  }
409 
410  return e;
411 }
412 
414 {
415  throw Exception(TE_TR("TO DO"));
416 }
void saveEdgeAttributes(GraphData *data)
Used to save the edges elements attributes from a graph data.
void setAttributeVecSize(int size)
This function is used to set the number of attributes associated with the vertex elements.
Definition: Vertex.cpp:79
te::graph::GraphMetadata * getMetadata()
It returns a pointer to a class that describes the graph metadata.
void setAttributeVecSize(int size)
This function is used to set the number of attributes associated with the edge elements.
Definition: Edge.cpp:86
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
The Field class can be used to model an expression that takes part of the output items of a SELECT...
Definition: Field.h:50
VertexMap & getVertexMap()
It returns the the vertex map.
Definition: GraphData.cpp:89
static const std::string sm_tableVertexModelAttrId
Attribute id.
Definition: Globals.h:97
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
A class that models the name of any property of an object.
Definition: PropertyName.h:50
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
AbstractGraphLoaderStrategy(te::graph::GraphMetadata *metadata)
Default constructor.
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
virtual void saveData(GraphData *data)
Save the graph data structure in Data Source.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
static const std::string sm_tableEdgeModelAttrId
Attribute Id.
Definition: Globals.h:91
void saveGraphVertexList(GraphData *data)
Used to save the vertex elements from a graph data.
virtual void removeVertex(int id)
Function used to remove a vertex saved in a data source.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
Vertex * loadVertexAttrs(int id)
Function used to load one vertex given a ID.
Edge * loadEdgeAttrs(int id)
Function used to load one edge given a ID.
Edge * loadEdge(int id)
Function used to load one edge given a ID.
EdgeMap & getEdgeMap()
It returns the the edge map.
Definition: GraphData.cpp:133
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
void saveVertexAttributes(GraphData *data)
Used to save the vertex elements attributes from a graph data.
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
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
bool isDirty()
Used to check the graph data state.
Definition: GraphData.cpp:148
It models the comparison operator.
Definition: EqualTo.h:46
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Vertex.cpp:84
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:51
static const std::string sm_tableEdgeModelAttrVFrom
Attribute Vertex From.
Definition: Globals.h:92
Vertex * loadVertex(int id)
Function used to load one vertex given a ID.
virtual void removeEdge(int id)
Function used to remove a edge saved in a data source.
void saveGraphEdgeList(GraphData *data)
Used to save the edge elements from a graph data.
virtual ~AbstractGraphLoaderStrategy()
Default destructor.
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Edge.cpp:91
static const std::string sm_tableEdgeModelAttrVTo
Attribute Vertex To.
Definition: Globals.h:93
This class define the main functions necessary to save and load the graph data and metadata informati...
Class used to define the graph metadata informations.
Definition: GraphMetadata.h:56
This class models a string Literal value.
Definition: LiteralString.h:46