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 "../../core/translator/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 
46 {
47  if(m_graph == nullptr || m_graph->getMetadata() == nullptr)
48  {
49  throw Exception(TE_TR("Invalid graph pointer."));
50  }
51 
52  if(m_vertexQuery.get())
53  {
54  if(m_vertexQuery->moveFirst())
55  {
57 
58  return m_graph->getVertex(id);
59  }
60  }
61 
63  {
64  //create a query to get eache vertex id
65  std::string tableName = m_graph->getMetadata()->getVertexTableName();
66 
68  te::da::Fields* fields = new te::da::Fields;
69  fields->push_back(f);
70 
71  te::da::FromItem* t = new te::da::DataSetName(tableName);
72  te::da::From* from = new te::da::From;
73  from->push_back(t);
74 
76  te::da::OrderBy* ob = new te::da::OrderBy();
77  ob->push_back(obItem);
78 
79  te::da::Select select(fields, from, ob);
80 
81  m_vertexQuery.reset(nullptr);
83  }
84  else
85  {
86  //create a query to get eache vertex id
87  std::string tableName = m_graph->getMetadata()->getVertexTableName();
88  std::string edgeAttrTable = m_graph->getMetadata()->getEdgeTableName();
89 
91  te::da::Fields* fields = new te::da::Fields;
92  fields->push_back(f);
93 
94  te::da::From* from = new te::da::From;
95 
96  te::da::FromItem* t = new te::da::DataSetName(tableName, "vertex");
97  from->push_back(t);
98 
99  te::da::FromItem* fiEdge = new te::da::DataSetName(edgeAttrTable, "edge");
100  from->push_back(fiEdge);
101 
102  std::string vertexFrom = "edge.";
104 
105  std::string vertexTo = "edge.";
107 
108  std::string vId = "vertex.";
110 
111  te::da::Field* fvf = new te::da::Field(vertexFrom);
112  te::da::Field* fv1id = new te::da::Field(vId);
113  te::da::Expression* exp1 = new te::da::EqualTo(fv1id->getExpression(), fvf->getExpression());
114 
115  te::da::Field* fvt = new te::da::Field(vertexTo);
116  te::da::Field* fv2id = new te::da::Field(vId);
117  te::da::Expression* exp2 = new te::da::EqualTo(fv2id->getExpression(), fvt->getExpression());
118 
119  te::da::Or* ora = new te::da::Or(exp1, exp2);
120 
121  te::da::Where* wh = new te::da::Where(ora);
122 
124  te::da::GroupBy* gb = new te::da::GroupBy();
125  gb->push_back(gbItem);
126 
128  te::da::OrderBy* ob = new te::da::OrderBy();
129  ob->push_back(obItem);
130 
131  te::da::Select select(fields, from, wh, gb, ob);
132 
133  m_vertexQuery.reset(nullptr);
135  }
136 
137  if(m_vertexQuery.get() == nullptr)
138  {
139  throw Exception(TE_TR("Iterator not initialized."));
140  }
141 
142  int id;
143 
144  if(m_vertexQuery->moveNext())
145  {
147  }
148  else
149  {
150  return nullptr;
151  }
152 
153  return m_graph->getVertex(id);
154 }
155 
157 {
158  if(m_graph == nullptr || m_graph->getMetadata() == nullptr)
159  {
160  throw Exception(TE_TR("Invalid graph pointer."));
161  }
162 
163  if(m_edgeQuery.get())
164  {
165  if(m_edgeQuery->moveFirst())
166  {
167  int id = m_edgeQuery->getInt32(Globals::sm_tableEdgeModelAttrId);
168 
169  return m_graph->getEdge(id);
170  }
171  }
172 
173  //create a query to get eache vertex id
174  std::string tableName = m_graph->getMetadata()->getEdgeTableName();
175 
177  te::da::Fields* fields = new te::da::Fields;
178  fields->push_back(f);
179 
180  te::da::FromItem* t = new te::da::DataSetName(tableName);
181  te::da::From* from = new te::da::From;
182  from->push_back(t);
183 
185  te::da::OrderBy* ob = new te::da::OrderBy();
186  ob->push_back(obItem);
187 
188  te::da::Select select(fields, from, ob);
189 
190  m_edgeQuery.reset(nullptr);
192 
193  if(m_edgeQuery.get() == nullptr)
194  {
195  throw Exception(TE_TR("Iterator not initialized."));
196  }
197 
198  int id;
199 
200  if(m_edgeQuery->moveNext())
201  {
203  }
204  else
205  {
206  return nullptr;
207  }
208 
209  return m_graph->getEdge(id);
210 }
virtual te::graph::Vertex * getFirstVertex()
It returns a pointer to the first vertex element of a graph.
te::da::DataSource * getDataSource()
It returns the data source associated with this graph.
boost::ptr_vector< GroupByItem > GroupBy
A class that can be used to model a GROUP BY clause.
Definition: GroupBy.h:37
te::graph::AbstractGraph * m_graph
Pointer to a abstract graph used to access the elements.
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...
static const std::string sm_tableVertexModelAttrId
Attribute id.
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.
Base exception class for plugin module.
Expression * getExpression() const
It returns the expression set for an output select query.
virtual te::graph::Edge * getEdge(int id)=0
It returns the edge element if it&#39;s exist.
std::unique_ptr< te::da::DataSet > m_vertexQuery
Attribute used to keep the vertex iterator.
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:242
This is an abstract class that models a query expression.
bool m_listIsolatedVertex
Flag used to indicated that the isolated vertex will be listed.
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...
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
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.
virtual ~SequenceIterator()
Virtual destructor.
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
URI C++ Library.
Definition: Attributes.h:37
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
std::unique_ptr< te::da::DataSet > m_edgeQuery
Attribute used to keep the edge iterator.
This class defines a commun interface to represents a graph iterator class. The main diferency to ano...
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
This class defines a commun interface to represents a graph iterator class. The main diferency to ano...
std::string getVertexTableName()
It returns the vertex table name that contains the vertex elements in data source.
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
virtual te::graph::Vertex * getVertex(int id)=0
It returns the vertex element if it&#39;s exist.
static const std::string sm_tableEdgeModelAttrVTo
Attribute Vertex To.