SequenceLoaderStrategy.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 SequenceLoaderStrategy.cpp
22 
23  \brief This class implements the main functions necessary to
24  save and load the graph data and metadata information
25  using as strategy a "order by" to create a sequence
26  of objects.
27 */
28 
29 // Terralib Includes
30 #include "../../core/translator/Translator.h"
31 #include "../../common/StringUtils.h"
32 #include "../../dataaccess/dataset/DataSet.h"
33 #include "../../dataaccess/dataset/DataSetType.h"
34 #include "../../dataaccess/datasource/DataSource.h"
35 #include "../../dataaccess/query_h.h"
36 #include "../../datatype/AbstractData.h"
37 #include "../core/AbstractGraph.h"
38 #include "../core/Edge.h"
39 #include "../core/GraphCache.h"
40 #include "../core/EdgeProperty.h"
41 #include "../core/GraphData.h"
42 #include "../core/GraphMetadata.h"
43 #include "../core/Vertex.h"
44 #include "../core/VertexProperty.h"
45 #include "../Config.h"
46 #include "../Globals.h"
47 #include "../Exception.h"
48 #include "SequenceLoaderStrategy.h"
49 
50 
52 {
53 }
54 
56 
58 {
59  if(m_graphMetadata == nullptr || m_graphMetadata->getDataSource() == nullptr || g == nullptr)
60  {
61  throw Exception(TE_TR(""));
62  }
63 
64  //ONLY WORKS FOR te::graph::Edge_List
66  {
67  throw Exception(TE_TR("TO DO"));
68  }
69 
70  //get the table names
71  std::string vertexAttrTable = m_graphMetadata->getVertexTableName();
72  std::string edgeAttrTable = m_graphMetadata->getEdgeTableName();
73 
74  //get all id's from vertex and edges that is inside that box
75 
76  //filds
77  te::da::Fields* all = new te::da::Fields;
78  all->push_back(new te::da::Field("*"));
79 
80  //from
81  te::da::From* from = new te::da::From;
82 
83  te::da::FromItem* fi = new te::da::DataSetName(vertexAttrTable, "vertex");
84  from->push_back(fi);
85 
86  te::da::FromItem* fiEdge = new te::da::DataSetName(edgeAttrTable, "edge");
87  from->push_back(fiEdge);
88 
89  std::string vertexFrom = "edge.";
91 
92  std::string vertexTo = "edge.";
94 
95  std::string vId = "vertex.";
97 
98  //where
101 
103  te::da::LessThanOrEqualTo* ltetvt = new te::da::LessThanOrEqualTo(fvtEnd->getExpression(), new te::da::LiteralInt32(static_cast<int32_t>(vertexId + m_graphMetadata->m_maxCacheSize)));
104 
105  te::da::And* andop = new te::da::And(gtetvt, ltetvt);
106 
107  te::da::Field* fvf = new te::da::Field(vertexFrom);
108  te::da::Field* fv1id = new te::da::Field(vId);
109  te::da::Expression* exp1 = new te::da::EqualTo(fvf->getExpression(), fv1id->getExpression());
110 
111  te::da::Field* fvt = new te::da::Field(vertexTo);
112  te::da::Field* fv2id = new te::da::Field(vId);
113  te::da::Expression* exp2 = new te::da::EqualTo(fvt->getExpression(), fv2id->getExpression());
114 
115  te::da::Or* orop = new te::da::Or(exp1, exp2);
116 
117  te::da::And* andd = new te::da::And(andop, orop);
118 
119  te::da::Where* wh = new te::da::Where(andd);
120 
121  // order by
123  te::da::OrderBy* ob = new te::da::OrderBy();
124  ob->push_back(obItem);
125 
126  //select
127  te::da::Select select(all, from, wh, ob);
128 
129  //query
130  std::unique_ptr<te::da::DataSet> dataset = m_graphMetadata->getDataSource()->query(select);
131 
132  std::unique_ptr<te::da::DataSetType> vertexDsType = m_graphMetadata->getDataSource()->getDataSetType(vertexAttrTable);
133 
134  int vertexProperties = static_cast<int>(vertexDsType->getProperties().size());
135 
136  std::string graphType = g->getMetadata()->getType();
137 
138  te::graph::Vertex* v = nullptr;
139 
140  int currentId = -1;
141 
142  //list of all attributes: vertex table + edge table
143  while(dataset->moveNext())
144  {
145  int vId = dataset->getInt32(0); //first item is the vertex id
146  int eId = dataset->getInt32(vertexProperties); //first after vertexProperties item is the edge id
147  int vFrom = dataset->getInt32(vertexProperties + 1); //second after vertexPropertie item is the vertex from id
148  //int vTo = dataset->getInt32(vertexProperties + 2); //third after vertexPropertie item is the vertex to id
149 
150  if(currentId != vId)
151  {
152  //verify if its already in cache
153  if(gc->checkCacheByVertexId(vId) == nullptr)
154  {
155  v = new te::graph::Vertex(vId, false);
156 
157  v->setAttributeVecSize(vertexProperties - 1);
158 
159  for(int i = 1; i < vertexProperties; ++i)
160  {
161  v->addAttribute(i - 1, dataset->getValue(i).release());
162  }
163 
164  g->add(v);
165  }
166 
167  currentId = vId;
168  }
169 
170  if(v)
171  {
173  {
174  if(vId == vFrom)
175  v->getSuccessors().insert(eId);
176  else
177  v->getPredecessors().insert(eId);
178  }
179 
180  //TODO for other graph types
181  }
182  }
183 }
184 
185 
187 {
188  if(m_graphMetadata == nullptr || m_graphMetadata->getDataSource() == nullptr || g == nullptr)
189  {
190  throw Exception(TE_TR(""));
191  }
192 
193  //ONLY WORKS FOR te::graph::Edge_List
195  {
196  throw Exception(TE_TR("TO DO"));
197  }
198 
199  //get the tables names
200  std::string edgeTable = m_graphMetadata->getEdgeTableName();
201 
202  //get all id's from vertex and edges that is inside that box
203 
204  //filds
205  te::da::Fields* all = new te::da::Fields;
206  all->push_back(new te::da::Field("*"));
207 
208  //from
209  te::da::From* from = new te::da::From;
210 
211  te::da::FromItem* fi1 = new te::da::DataSetName(edgeTable);
212  from->push_back(fi1);
213 
214  //where
217 
219  te::da::LessThanOrEqualTo* ltet = new te::da::LessThanOrEqualTo(feiEnd->getExpression(), new te::da::LiteralInt32(static_cast<int32_t>(edgeId + m_graphMetadata->m_maxCacheSize)));
220 
221  te::da::And* andop = new te::da::And(gtet, ltet);
222 
223  te::da::Where* wh = new te::da::Where(andop);
224 
225  // order by
227  te::da::OrderBy* ob = new te::da::OrderBy();
228  ob->push_back(obItem);
229 
230  //select
231  te::da::Select select(all, from, wh, ob);
232 
233  //query
234  std::unique_ptr<te::da::DataSet> dataset = m_graphMetadata->getDataSource()->query(select);
235 
236  std::unique_ptr<te::da::DataSetType> edgeDsType = m_graphMetadata->getDataSource()->getDataSetType(edgeTable);
237 
238  int edgeProperties = static_cast<int>(edgeDsType->getProperties().size());
239 
240  //list of all attributes: edge table + vertex table + vertex table
241  while(dataset->moveNext())
242  {
243  int edgeId = dataset->getInt32(0); //first item is the edge id
244  int vFromId = dataset->getInt32(1); //second item is the vertefFrom id
245  int vToId = dataset->getInt32(2); //third item is the verteTo id
246 
247  //verify if its already in cache
248  if(gc->checkCacheByEdgeId(edgeId) == nullptr)
249  {
250  te::graph::Edge* e = new te::graph::Edge(edgeId, vFromId, vToId, false);
251 
252  e->setAttributeVecSize(edgeProperties - 3);
253 
254  for(int i = 3; i < edgeProperties; ++i)
255  {
256  e->addAttribute(i - 3, dataset->getValue(i).release());
257  };
258 
259  g->add(e);
260  }
261  }
262 }
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
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...
static const std::string sm_tableVertexModelAttrId
Attribute id.
It models the inequality operator less than or equal to (<=).
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 ~SequenceLoaderStrategy()
Default destructor.
A class that models the name of any property of an object.
Base exception class for plugin module.
Expression * getExpression() const
It returns the expression set for an output select query.
std::set< int > & getPredecessors()
Returns the Predecessors vector.
Definition: Vertex.cpp:101
boost::ptr_vector< OrderByItem > OrderBy
A class that can be used to model an ORDER BY clause.
Definition: OrderBy.h:37
It models the inequality operator greater than or equal to (>=).
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
GraphData * checkCacheByVertexId(int id)
This functions check in cache if the vertex element with a given id was alredy in memory...
Definition: GraphCache.cpp:246
virtual void add(Vertex *v)=0
Add a new vertex element to a graph.
Boolean logic operator: AND.
This is an abstract class that models a query expression.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
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...
te::gm::GeometryCollection * gc
virtual te::graph::GraphMetadata * getMetadata()=0
Function used to access the graph metadata.
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
static const std::string sm_tableEdgeModelAttrId
Attribute Id.
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
SequenceLoaderStrategy(te::graph::GraphMetadata *metadata)
Default constructor.
virtual void loadDataByVertexId(int vertexId, te::graph::AbstractGraph *g, te::graph::GraphCache *gc=0)
Functio used to load a group of vertex elements given a base element.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
virtual void loadDataByEdgeId(int edgeId, te::graph::AbstractGraph *g, te::graph::GraphCache *gc=0)
Functio used to load a group of edges elements given a base element.
size_t m_maxCacheSize
Attribute used to set the max cache size.
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
This class define the main functions necessary to save and load the graph data and metadata informati...
std::set< int > & getSuccessors()
Returns the Successors vector.
Definition: Vertex.cpp:106
virtual std::unique_ptr< te::da::DataSetType > getDataSetType(const std::string &name)
It gets information about the given dataset.
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
Class used to manager the graph data elements. This class uses a cache policy to control the elements...
Definition: GraphCache.h:60
GraphData * checkCacheByEdgeId(int id)
This functions check in cache if the edge element with a given id was alredy in memory.
Definition: GraphCache.cpp:270
#define TE_GRAPH_FACTORY_GRAPH_TYPE_BIDIRECTIONALGRAPH
std::string getEdgeTableName()
It returns the edge table name that contains the vertex elements in data source.
It models the comparison operator.
Definition: EqualTo.h:46
std::string getVertexTableName()
It returns the vertex table name that contains the vertex elements in data source.
std::string getType()
It returns the graph type (defined in Enums file)
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)
static const std::string sm_tableEdgeModelAttrVFrom
Attribute Vertex From.
A class that can be used in an ORDER BY clause to sort the items of a resulting query.
Definition: OrderByItem.h:53
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 implements the main functions necessary to save and load the graph data and metadata infor...
Class used to define the graph metadata informations.
Definition: GraphMetadata.h:56