All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SequenceLoaderStrategy.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 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 "../../common/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 {
57 }
58 
59 
61 {
62  if(m_graphMetadata == 0 || m_graphMetadata->getDataSource() == 0 || g == 0)
63  {
64  throw Exception(TR_GRAPH(""));
65  }
66 
67  //ONLY WORKS FOR te::graph::Edge_List
68  if(m_graphMetadata->getStorageMode() == te::graph::Vertex_List)
69  {
70  throw Exception(TR_GRAPH("TO DO"));
71  }
72 
73  //get the table names
74  std::string vertexAttrTable = m_graphMetadata->getVertexTableName();
75  std::string edgeAttrTable = m_graphMetadata->getEdgeTableName();
76 
77  //get all id's from vertex and edges that is inside that box
78 
79  //filds
80  te::da::Fields* all = new te::da::Fields;
81  all->push_back(new te::da::Field("*"));
82 
83  //from
84  te::da::From* from = new te::da::From;
85 
86  te::da::FromItem* fi = new te::da::DataSetName(vertexAttrTable, "vertex");
87  from->push_back(fi);
88 
89  te::da::FromItem* fiEdge = new te::da::DataSetName(edgeAttrTable, "edge");
90  from->push_back(fiEdge);
91 
92  std::string vertexFrom = "edge.";
94 
95  std::string vertexTo = "edge.";
97 
98  std::string vId = "vertex.";
100 
101  //where
104 
106  te::da::LessThanOrEqualTo* ltetvt = new te::da::LessThanOrEqualTo(fvtEnd->getExpression(), new te::da::LiteralInt32(vertexId + m_graphMetadata->m_maxCacheSize));
107 
108  te::da::And* andop = new te::da::And(gtetvt, ltetvt);
109 
110  te::da::Field* fvf = new te::da::Field(vertexFrom);
111  te::da::Field* fv1id = new te::da::Field(vId);
112  te::da::Expression* exp1 = new te::da::EqualTo(fvf->getExpression(), fv1id->getExpression());
113 
114  te::da::Field* fvt = new te::da::Field(vertexTo);
115  te::da::Field* fv2id = new te::da::Field(vId);
116  te::da::Expression* exp2 = new te::da::EqualTo(fvt->getExpression(), fv2id->getExpression());
117 
118  te::da::Or* orop = new te::da::Or(exp1, exp2);
119 
120  te::da::And* andd = new te::da::And(andop, orop);
121 
122  te::da::Where* wh = new te::da::Where(andd);
123 
124  // order by
126  te::da::OrderBy* ob = new te::da::OrderBy();
127  ob->push_back(obItem);
128 
129  //select
130  te::da::Select select(all, from, wh, ob);
131 
132  //query
133  std::auto_ptr<te::da::DataSet> dataset = m_graphMetadata->getDataSource()->query(select);
134 
135  std::auto_ptr<te::da::DataSetType> vertexDsType = m_graphMetadata->getDataSource()->getDataSetType(vertexAttrTable);
136 
137  int vertexProperties = vertexDsType->getProperties().size();
138 
139  std::string graphType = g->getMetadata()->getType();
140 
141  te::graph::Vertex* v = 0;
142 
143  int currentId = -1;
144 
145  //list of all attributes: vertex table + edge table
146  while(dataset->moveNext())
147  {
148  int vId = dataset->getInt32(0); //first item is the vertex id
149  int eId = dataset->getInt32(vertexProperties); //first after vertexProperties item is the edge id
150  int vFrom = dataset->getInt32(vertexProperties + 1); //second after vertexPropertie item is the vertex from id
151  //int vTo = dataset->getInt32(vertexProperties + 2); //third after vertexPropertie item is the vertex to id
152 
153  if(currentId != vId)
154  {
155  //verify if its already in cache
156  if(gc->checkCacheByVertexId(vId) == false)
157  {
158  v = new te::graph::Vertex(vId, false);
159 
160  v->setAttributeVecSize(vertexProperties - 1);
161 
162  for(int i = 1; i < vertexProperties; ++i)
163  {
164  v->addAttribute(i - 1, dataset->getValue(i).release());
165  }
166 
167  g->add(v);
168  }
169 
170  currentId = vId;
171  }
172 
173  if(v)
174  {
176  {
177  if(vId == vFrom)
178  v->getSuccessors().insert(eId);
179  else
180  v->getPredecessors().insert(eId);
181  }
182 
183  //TODO for other graph types
184  }
185  }
186 }
187 
188 
190 {
191  if(m_graphMetadata == 0 || m_graphMetadata->getDataSource() == 0 || g == 0)
192  {
193  throw Exception(TR_GRAPH(""));
194  }
195 
196  //ONLY WORKS FOR te::graph::Edge_List
197  if(m_graphMetadata->getStorageMode() == te::graph::Vertex_List)
198  {
199  throw Exception(TR_GRAPH("TO DO"));
200  }
201 
202  //get the tables names
203  std::string edgeTable = m_graphMetadata->getEdgeTableName();
204 
205  //get all id's from vertex and edges that is inside that box
206 
207  //filds
208  te::da::Fields* all = new te::da::Fields;
209  all->push_back(new te::da::Field("*"));
210 
211  //from
212  te::da::From* from = new te::da::From;
213 
214  te::da::FromItem* fi1 = new te::da::DataSetName(edgeTable);
215  from->push_back(fi1);
216 
217  //where
220 
222  te::da::LessThanOrEqualTo* ltet = new te::da::LessThanOrEqualTo(feiEnd->getExpression(), new te::da::LiteralInt32(edgeId + m_graphMetadata->m_maxCacheSize));
223 
224  te::da::And* andop = new te::da::And(gtet, ltet);
225 
226  te::da::Where* wh = new te::da::Where(andop);
227 
228  // order by
230  te::da::OrderBy* ob = new te::da::OrderBy();
231  ob->push_back(obItem);
232 
233  //select
234  te::da::Select select(all, from, wh, ob);
235 
236  //query
237  std::auto_ptr<te::da::DataSet> dataset = m_graphMetadata->getDataSource()->query(select);
238 
239  std::auto_ptr<te::da::DataSetType> edgeDsType = m_graphMetadata->getDataSource()->getDataSetType(edgeTable);
240 
241  int edgeProperties = edgeDsType->getProperties().size();
242 
243  //list of all attributes: edge table + vertex table + vertex table
244  while(dataset->moveNext())
245  {
246  int edgeId = dataset->getInt32(0); //first item is the edge id
247  int vFromId = dataset->getInt32(1); //second item is the vertefFrom id
248  int vToId = dataset->getInt32(2); //third item is the verteTo id
249 
250  //verify if its already in cache
251  if(gc->checkCacheByEdgeId(edgeId) == 0)
252  {
253  te::graph::Edge* e = new te::graph::Edge(edgeId, vFromId, vToId, false);
254 
255  e->setAttributeVecSize(edgeProperties - 3);
256 
257  for(int i = 3; i < edgeProperties; ++i)
258  {
259  e->addAttribute(i - 3, dataset->getValue(i).release());
260  };
261 
262  g->add(e);
263  }
264  }
265 }
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:56
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...
void setAttributeVecSize(int size)
This function is used to set the number of attributes associated with the edge elements.
Definition: Edge.cpp:86
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
Expression * getExpression() const
It returns the expression set for an output select query.
Definition: Field.cpp:80
void setAttributeVecSize(int size)
This function is used to set the number of attributes associated with the vertex elements.
Definition: Vertex.cpp:79
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
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.
Class used to manager the graph data elements. This class uses a cache policy to control the elements...
Definition: GraphCache.h:60
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
The Field class can be used to model an expression that takes part of the output items of a SELECT...
Definition: Field.h:50
virtual te::graph::GraphMetadata * getMetadata()=0
Function used to access the graph metadata.
It models the inequality operator greater than or equal to (&gt;=).
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Edge.cpp:91
A class that models the name of any property of an object.
Definition: PropertyName.h:50
A class that can be used in an ORDER BY clause to sort the items of a resulting query.
Definition: OrderByItem.h:53
#define TR_GRAPH(message)
It marks a string in order to get translated. This is a special mark used in the Graph module of Terr...
Definition: Config.h:58
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
#define TE_GRAPH_FACTORY_GRAPH_TYPE_BIDIRECTIONALGRAPH
Definition: Config.h:144
Boolean logic operator: AND.
Definition: And.h:46
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
This class implements the main functions necessary to save and load the graph data and metadata infor...
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Vertex.cpp:84
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
static const std::string sm_tableEdgeModelAttrVFrom
Attribute Vertex From.
Definition: Globals.h:96
static const std::string sm_tableVertexModelAttrId
Attribute id.
Definition: Globals.h:101
It models the inequality operator less than or equal to (&lt;=).
boost::ptr_vector< OrderByItem > OrderBy
A class that can be used to model an ORDER BY clause.
Definition: OrderBy.h:37
std::set< int > & getPredecessors()
Returns the Predecessors vector.
Definition: Vertex.cpp:101
virtual void add(Vertex *v)=0
Add a new vertex element to a graph.
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.
Definition: Or.h:46
static const std::string sm_tableEdgeModelAttrId
Attribute Id.
Definition: Globals.h:95
This is an abstract class that models a query expression.
Definition: Expression.h:47
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
std::string getType()
It returns the graph type (defined in Enums file)
static const std::string sm_tableEdgeModelAttrVTo
Attribute Vertex To.
Definition: Globals.h:97
It models the comparison operator.
Definition: EqualTo.h:46
virtual ~SequenceLoaderStrategy()
Default destructor.
Class used to define the graph metadata informations.
Definition: GraphMetadata.h:56
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
std::set< int > & getSuccessors()
Returns the Successors vector.
Definition: Vertex.cpp:106
SequenceLoaderStrategy(te::graph::GraphMetadata *metadata)
Default constructor.