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 "../../core/translator/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 == nullptr || m_graphMetadata->getDataSource() == nullptr)
70  {
71  throw Exception(TE_TR(""));
72  }
73 
74  //save graph topology
76  {
77  saveGraphEdgeList(data);
78  }
79 }
80 
82 {
83  if(m_graphMetadata == nullptr || m_graphMetadata->getDataSource() == nullptr)
84  {
85  throw Exception(TE_TR(""));
86  }
87 }
88 
90 {
91  if(m_graphMetadata == nullptr || m_graphMetadata->getDataSource() == nullptr)
92  {
93  throw Exception(TE_TR(""));
94  }
95 }
96 
97 
99 {
100  if(data->isDirty() == false)
101  {
102  return;
103  }
104 
105  std::string tableName = m_graphMetadata->getEdgeTableName();
106 
107  //get dataset type
108  std::unique_ptr<te::da::DataSetType> dsType(m_graphMetadata->getDataSource()->getDataSetType(tableName));
109 
110  //create outputs data sets
111  std::unique_ptr<te::mem::DataSet> outDataSet(new te::mem::DataSet(dsType.get()));
112  std::unique_ptr<te::mem::DataSet> outDataSetUpdate(new te::mem::DataSet(dsType.get()));
113 
114  te::graph::GraphData::EdgeMap::iterator it = data->getEdgeMap().begin();
115 
116  bool hasNewObjects = false;
117  bool hasUpdatedObjects = false;
118 
119  while(it != data->getEdgeMap().end())
120  {
121  if(it->second->isNew()) //new element
122  {
123  //create new item
124  te::mem::DataSetItem* dsItem = new te::mem::DataSetItem(outDataSet.get());
125 
126  dsItem->setInt32(0, it->second->getId());
127  dsItem->setInt32(1, it->second->getIdFrom());
128  dsItem->setInt32(2, it->second->getIdTo());
129 
130  int pos = 3; // number of attributes already defined in dsItem.
131 
132  //check for edge properties
134  {
135  for(size_t i = 0; i < it->second->getAttributes().size(); ++i)
136  {
137  dsItem->setValue(pos + i, it->second->getAttributes()[i]->clone());
138  }
139  }
140 
141  outDataSet->add(dsItem);
142 
143  hasNewObjects = true;
144  }
145  else if(it->second->isDirty()) //update element
146  {
147  //create new item
148  te::mem::DataSetItem* dsItem = new te::mem::DataSetItem(outDataSetUpdate.get());
149 
150  dsItem->setInt32(0, it->second->getId());
151  dsItem->setInt32(1, it->second->getIdFrom());
152  dsItem->setInt32(2, it->second->getIdTo());
153 
154  int pos = 3; // number of attributes already defined in dsItem.
155 
156  //check for edge properties
158  {
159  for(size_t i = 0; i < it->second->getAttributes().size(); ++i)
160  {
161  dsItem->setValue(pos + i, it->second->getAttributes()[i]->clone());
162  }
163  }
164 
165  outDataSetUpdate->add(dsItem);
166 
167  hasUpdatedObjects = true;
168  }
169 
170  ++it;
171  }
172 
173  std::map<std::string, std::string> options;
174 
175  if(hasNewObjects)
176  {
177  outDataSet->moveFirst();
178  m_graphMetadata->getDataSource()->add(tableName, outDataSet.get(), options);
179  }
180  outDataSet->clear();
181 
182  if(hasUpdatedObjects)
183  {
184  outDataSetUpdate->moveFirst();
185  std::map<std::string, std::string> options;
186  std::vector<std::size_t> properties;
187 
188  for(size_t t = 0; t < dsType->getProperties().size(); ++t)
189  {
190  if(t != 0)
191  properties.push_back(t);
192  }
193 
194  m_graphMetadata->getDataSource()->update(tableName, outDataSetUpdate.get(), properties, nullptr, options);
195  }
196  outDataSetUpdate->clear();
197 
198  //must save the vertex attributes
200  {
201  saveVertexAttributes(data);
202  }
203 }
204 
206 {
207  if(data->isDirty() == false)
208  {
209  return;
210  }
211 
212  std::string tableName = m_graphMetadata->getVertexTableName();
213 
214  //get dataset type
215  std::unique_ptr<te::da::DataSetType> dsType(m_graphMetadata->getDataSource()->getDataSetType(tableName));
216 
217  //create outputs data sets
218  std::unique_ptr<te::mem::DataSet> outDataSet(new te::mem::DataSet(dsType.get()));
219  std::unique_ptr<te::mem::DataSet> outDataSetUpdate(new te::mem::DataSet(dsType.get()));
220 
221  te::graph::GraphData::VertexMap::iterator it = data->getVertexMap().begin();
222 
223  bool hasNewObjects = false;
224  bool hasUpdatedObjects = false;
225 
226  while(it != data->getVertexMap().end())
227  {
228  if(it->second->isNew()) //new element
229  {
230  //create new item
231  te::mem::DataSetItem* dsItem = new te::mem::DataSetItem(outDataSet.get());
232 
233  dsItem->setInt32(0, it->second->getId());
234 
235  int pos = 1; // number of attributes already defined in dsItem.
236 
237  //check for vertex properties
239  {
240  for(size_t i = 0; i < it->second->getAttributes().size(); ++i)
241  {
242  dsItem->setValue(pos + i, it->second->getAttributes()[i]->clone());
243  }
244  }
245 
246  outDataSet->add(dsItem);
247 
248  hasNewObjects = true;
249  }
250  else if(it->second->isDirty()) //update element
251  {
252  //create new item
253  te::mem::DataSetItem* dsItem = new te::mem::DataSetItem(outDataSetUpdate.get());
254 
255  dsItem->setInt32(0, it->second->getId());
256 
257  int pos = 1; // number of attributes already defined in dsItem.
258 
259  //check for vertex properties
261  {
262  for(size_t i = 0; i < it->second->getAttributes().size(); ++i)
263  {
264  dsItem->setValue(pos + i, it->second->getAttributes()[i]->clone());
265  }
266  }
267 
268  outDataSetUpdate->add(dsItem);
269 
270  hasUpdatedObjects = true;
271  }
272 
273  ++it;
274  }
275 
276  std::map<std::string, std::string> options;
277 
278  if(hasNewObjects)
279  {
280  outDataSet->moveFirst();
281  m_graphMetadata->getDataSource()->add(tableName, outDataSet.get(), options);
282  }
283  outDataSet->clear();
284 
285  if(hasUpdatedObjects)
286  {
287  outDataSetUpdate->moveFirst();
288  std::map<std::string, std::string> options;
289  std::vector<std::size_t> properties;
290 
291  for(size_t t = 0; t < dsType->getProperties().size(); ++t)
292  {
293  if(t != 0)
294  properties.push_back(t);
295  }
296 
297  m_graphMetadata->getDataSource()->update(tableName, outDataSetUpdate.get(), properties, nullptr, options);
298  }
299  outDataSetUpdate->clear();
300 }
301 
303  GraphData* /*data*/)
304 {
305  throw Exception(TE_TR("TO DO"));
306 }
307 
309  GraphData* /*data*/)
310 {
311  throw Exception(TE_TR("TO DO"));
312 }
313 
315  int /*id*/)
316 {
317  throw Exception(TE_TR("TO DO"));
318 }
319 
321 {
322  //get dataset type
323  std::string tableName = m_graphMetadata->getVertexTableName();
324  std::unique_ptr<te::da::DataSetType> dsType(m_graphMetadata->getDataSource()->getDataSetType(tableName));
325 
326  //create query
328  te::da::Fields* fields = new te::da::Fields;
329  fields->push_back(f);
330 
331  te::da::FromItem* t = new te::da::DataSetName(tableName);
332  te::da::From* from = new te::da::From;
333  from->push_back(t);
334 
336 
338 
339  te::da::Where* filter = new te::da::Where(equal);
340 
341  te::da::Select select(fields, from, filter);
342 
343  std::unique_ptr<te::da::DataSet> dataset = m_graphMetadata->getDataSource()->query(select);
344 
345  //create vertex
346  te::graph::Vertex* v = nullptr;
347 
348  if(dataset->moveNext())
349  {
350  //get fixed attribute
351  int id = dataset->getInt32(Globals::sm_tableVertexModelAttrId);
352 
353  v = new te::graph::Vertex(id);
354 
355  v->setAttributeVecSize(static_cast<int>(dsType->getProperties().size() - 1));
356 
357  for(size_t i = 1; i < dsType->getProperties().size(); ++i)
358  {
359  v->addAttribute(static_cast<int>(i - 1), dataset->getValue(dsType->getProperty(i)->getName()).release());
360  }
361  }
362 
363  return v;
364 }
365 
367 {
368  //get dataset type
369  std::string tableName = m_graphMetadata->getEdgeTableName();
370  std::unique_ptr<te::da::DataSetType> dsType(m_graphMetadata->getDataSource()->getDataSetType(tableName));
371 
372  //create query
374  te::da::Fields* fields = new te::da::Fields;
375  fields->push_back(f);
376 
377  te::da::FromItem* t = new te::da::DataSetName(tableName);
378  te::da::From* from = new te::da::From;
379  from->push_back(t);
380 
382 
384 
385  te::da::Where* filter = new te::da::Where(equal);
386 
387  te::da::Select select(fields, from, filter);
388 
389  std::unique_ptr<te::da::DataSet> dataset = m_graphMetadata->getDataSource()->query(select);
390 
391  //create vertex
392  te::graph::Edge* e = nullptr;
393 
394  if(dataset->moveNext())
395  {
396  //get fixed attributes
397  int id = dataset->getInt32(Globals::sm_tableEdgeModelAttrId);
398  int vFrom = dataset->getInt32(Globals::sm_tableEdgeModelAttrVFrom);
399  int vTo = dataset->getInt32(Globals::sm_tableEdgeModelAttrVTo);
400 
401  e = new te::graph::Edge(id, vFrom, vTo);
402 
403  e->setAttributeVecSize(static_cast<int>(dsType->getProperties().size() - 3));
404 
405  for(size_t i = 3; i < dsType->getProperties().size(); ++i)
406  {
407  e->addAttribute(static_cast<int>(i - 3), dataset->getValue(dsType->getProperty(i)->getName()).release());
408  }
409  }
410 
411  return e;
412 }
413 
415  int /*id*/)
416 {
417  throw Exception(TE_TR("TO DO"));
418 }
void saveEdgeAttributes(GraphData *data)
Used to save the edges elements attributes from a graph data.
te::da::DataSource * getDataSource()
It returns the data source associated with this graph.
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...
VertexMap & getVertexMap()
It returns the the vertex map.
Definition: GraphData.cpp:89
static const std::string sm_tableVertexModelAttrId
Attribute id.
te::graph::GraphMetadata * m_graphMetadata
Graph metadata attribute.
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
virtual int getVertexPropertySize()
Used to verify the number of properties associated to vertex elements.
A class that models the name of any property of an object.
Base exception class for plugin module.
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
virtual void add(const std::string &datasetName, DataSet *d, const std::map< std::string, std::string > &options, std::size_t limit=0)
It adds data items to the dataset in the data source.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
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.
virtual std::unique_ptr< DataSet > query(const Select &q, te::common::TraverseType travType=te::common::FORWARDONLY, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It executes a query that may return some data using a generic query. This method always returns a dis...
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...
static const std::string sm_tableEdgeModelAttrId
Attribute Id.
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.
virtual std::unique_ptr< te::da::DataSetType > getDataSetType(const std::string &name)
It gets information about the given dataset.
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...
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
std::string getEdgeTableName()
It returns the edge table name that contains the vertex elements in data source.
bool isDirty()
Used to check the graph data state.
Definition: GraphData.cpp:148
It models the comparison operator.
Definition: EqualTo.h:46
virtual int getEdgePropertySize()
Used to verify the number of properties associated to edge elements.
std::string getVertexTableName()
It returns the vertex table name that contains the vertex elements in data source.
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Vertex.cpp:84
GraphStorageMode getStorageMode()
It returns the the graph storage mode (defined in Enums file)
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56
static const std::string sm_tableEdgeModelAttrVFrom
Attribute Vertex From.
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 void update(const std::string &datasetName, DataSet *dataset, const std::vector< std::size_t > &properties, const te::da::ObjectIdSet *oids, const std::map< std::string, std::string > &options, std::size_t limit=0)
It updates the contents of a dataset for the set of data items.
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.
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