All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SequenceIterator.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 SequenceIterator.cpp
22 
23  \brief
24 */
25 
26 // Terralib Includes
27 #include "../../common/Translator.h"
28 #include "../../common/StringUtils.h"
29 #include "../../dataaccess/dataset/DataSet.h"
30 #include "../../dataaccess/dataset/DataSetType.h"
31 #include "../../dataaccess/datasource/DataSource.h"
32 #include "../../dataaccess/query_h.h"
33 #include "../core/GraphMetadata.h"
34 #include "../Config.h"
35 #include "../Exception.h"
36 #include "../Globals.h"
37 #include "SequenceIterator.h"
38 
40 {
41 }
42 
44 {
45 }
46 
48 {
49  if(m_graph == 0 || m_graph->getMetadata() == 0)
50  {
51  throw Exception(TE_TR("Invalid graph pointer."));
52  }
53 
54  if(m_vertexQuery.get())
55  {
56  if(m_vertexQuery->moveFirst())
57  {
58  int id = m_vertexQuery->getInt32(Globals::sm_tableVertexModelAttrId);
59 
60  return m_graph->getVertex(id);
61  }
62  }
63 
64  if(m_listIsolatedVertex)
65  {
66  //create a query to get eache vertex id
67  std::string tableName = m_graph->getMetadata()->getVertexTableName();
68 
70  te::da::Fields* fields = new te::da::Fields;
71  fields->push_back(f);
72 
73  te::da::FromItem* t = new te::da::DataSetName(tableName);
74  te::da::From* from = new te::da::From;
75  from->push_back(t);
76 
78  te::da::OrderBy* ob = new te::da::OrderBy();
79  ob->push_back(obItem);
80 
81  te::da::Select select(fields, from, ob);
82 
83  m_vertexQuery.reset(0);
84  m_vertexQuery = m_graph->getMetadata()->getDataSource()->query(select);
85  }
86  else
87  {
88  //create a query to get eache vertex id
89  std::string tableName = m_graph->getMetadata()->getVertexTableName();
90  std::string edgeAttrTable = m_graph->getMetadata()->getEdgeTableName();
91 
93  te::da::Fields* fields = new te::da::Fields;
94  fields->push_back(f);
95 
96  te::da::From* from = new te::da::From;
97 
98  te::da::FromItem* t = new te::da::DataSetName(tableName, "vertex");
99  from->push_back(t);
100 
101  te::da::FromItem* fiEdge = new te::da::DataSetName(edgeAttrTable, "edge");
102  from->push_back(fiEdge);
103 
104  std::string vertexFrom = "edge.";
106 
107  std::string vertexTo = "edge.";
109 
110  std::string vId = "vertex.";
112 
113  te::da::Field* fvf = new te::da::Field(vertexFrom);
114  te::da::Field* fv1id = new te::da::Field(vId);
115  te::da::Expression* exp1 = new te::da::EqualTo(fv1id->getExpression(), fvf->getExpression());
116 
117  te::da::Field* fvt = new te::da::Field(vertexTo);
118  te::da::Field* fv2id = new te::da::Field(vId);
119  te::da::Expression* exp2 = new te::da::EqualTo(fv2id->getExpression(), fvt->getExpression());
120 
121  te::da::Or* ora = new te::da::Or(exp1, exp2);
122 
123  te::da::Where* wh = new te::da::Where(ora);
124 
126  te::da::GroupBy* gb = new te::da::GroupBy();
127  gb->push_back(gbItem);
128 
130  te::da::OrderBy* ob = new te::da::OrderBy();
131  ob->push_back(obItem);
132 
133  te::da::Select select(fields, from, wh, gb, ob);
134 
135  m_vertexQuery.reset(0);
136  m_vertexQuery = m_graph->getMetadata()->getDataSource()->query(select);
137  }
138 
139  if(m_vertexQuery.get() == 0)
140  {
141  throw Exception(TE_TR("Iterator not initialized."));
142  }
143 
144  int id;
145 
146  if(m_vertexQuery->moveNext())
147  {
148  id = m_vertexQuery->getInt32(Globals::sm_tableVertexModelAttrId);
149  }
150  else
151  {
152  return 0;
153  }
154 
155  return m_graph->getVertex(id);
156 }
157 
159 {
160  if(m_graph == 0 || m_graph->getMetadata() == 0)
161  {
162  throw Exception(TE_TR("Invalid graph pointer."));
163  }
164 
165  if(m_edgeQuery.get())
166  {
167  if(m_edgeQuery->moveFirst())
168  {
169  int id = m_edgeQuery->getInt32(Globals::sm_tableEdgeModelAttrId);
170 
171  return m_graph->getEdge(id);
172  }
173  }
174 
175  //create a query to get eache vertex id
176  std::string tableName = m_graph->getMetadata()->getEdgeTableName();
177 
179  te::da::Fields* fields = new te::da::Fields;
180  fields->push_back(f);
181 
182  te::da::FromItem* t = new te::da::DataSetName(tableName);
183  te::da::From* from = new te::da::From;
184  from->push_back(t);
185 
187  te::da::OrderBy* ob = new te::da::OrderBy();
188  ob->push_back(obItem);
189 
190  te::da::Select select(fields, from, ob);
191 
192  m_edgeQuery.reset(0);
193  m_edgeQuery = m_graph->getMetadata()->getDataSource()->query(select);
194 
195  if(m_edgeQuery.get() == 0)
196  {
197  throw Exception(TE_TR("Iterator not initialized."));
198  }
199 
200  int id;
201 
202  if(m_edgeQuery->moveNext())
203  {
204  id = m_edgeQuery->getInt32(Globals::sm_tableEdgeModelAttrId);
205  }
206  else
207  {
208  return 0;
209  }
210 
211  return m_graph->getEdge(id);
212 }
virtual te::graph::Vertex * getFirstVertex()
It returns a pointer to the first vertex element of a graph.
boost::ptr_vector< GroupByItem > GroupBy
A class that can be used to model a GROUP BY clause.
Definition: GroupBy.h:37
A class that can be used in a GROUP BY clause.
Definition: GroupByItem.h:50
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
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
Expression * getExpression() const
It returns the expression set for an output select query.
Definition: Field.cpp:80
boost::ptr_vector< OrderByItem > OrderBy
A class that can be used to model an ORDER BY clause.
Definition: OrderBy.h:37
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
This is an abstract class that models a query expression.
Definition: Expression.h:47
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
virtual te::graph::Edge * getFirstEdge()
It returns a pointer to the first edge element of a graph.
SequenceIterator(te::graph::AbstractGraph *g)
Default constructor.
static const std::string sm_tableEdgeModelAttrId
Attribute Id.
Definition: Globals.h:91
Definition: Or.h:46
virtual ~SequenceIterator()
Virtual destructor.
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
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
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
This class defines a commun interface to represents a graph iterator class. The main diferency to ano...
It models the comparison operator.
Definition: EqualTo.h:46
This class defines a commun interface to represents a graph iterator class. The main diferency to ano...
static const std::string sm_tableEdgeModelAttrVFrom
Attribute Vertex From.
Definition: Globals.h:92
A class that can be used in an ORDER BY clause to sort the items of a resulting query.
Definition: OrderByItem.h:53
static const std::string sm_tableEdgeModelAttrVTo
Attribute Vertex To.
Definition: Globals.h:93