QueryGraphBuilder.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 QueryGraphBuilder.cpp
22 
23  \brief This class defines the Query strategy to build a graph having
24  a exist graph as input parameters.
25 
26  The query must be defined using only the terralib Query module
27  elements.
28 */
29 
30 // TerraLib
31 #include "../../core/translator/Translator.h"
32 #include "../../common/progress/TaskProgress.h"
33 #include "../../dataaccess/query/Expression.h"
34 #include "../core/AbstractGraphFactory.h"
35 #include "../core/Edge.h"
36 #include "../core/GraphMetadata.h"
37 #include "../core/Vertex.h"
38 #include "../core/VertexProperty.h"
39 #include "../graphs/Graph.h"
40 #include "../iterator/QueryIterator.h"
41 #include "../Config.h"
42 #include "../Exception.h"
43 #include "QueryGraphBuilder.h"
44 
45 // STL
46 #include <cassert>
47 #include <cstdlib>
48 #include <iostream>
49 
50 
52 {
53 
54 }
55 
57 
58 bool te::graph::QueryGraphBuilder::build(te::graph::AbstractGraph* g, te::da::Expression* eEdge, te::da::Expression* eVertex, const std::string& dsInfo, const std::string& graphType, const std::map<std::string, std::string>& gInfo)
59 {
60  //create output graph
61  m_graph.reset(te::graph::AbstractGraphFactory::make(graphType, dsInfo, gInfo));
62 
63  assert(m_graph);
64 
65  //copy attributes
66  for(int t = 0; t < g->getEdgePropertySize(); ++ t)
67  {
69  p->setParent(nullptr);
70 
71  m_graph->addEdgeProperty(p);
72  }
73 
74  for(int t = 0; t < g->getVertexPropertySize(); ++ t)
75  {
77  p->setParent(nullptr);
78 
79  m_graph->addVertexProperty(p);
80  }
81 
82  //create iterator
83  {
85 
86  te::graph::Vertex* vertex = it->getFirstVertex();
87 
89 
90  t.setTotalSteps(static_cast<int>(it->getVertexInteratorCount()));
91  t.setMessage(TE_TR("Query Graph Builder - Vertex..."));
92  t.useTimer(true);
93 
94 
95  //copy elements
96  while(it->isVertexIteratorAfterEnd() == false)
97  {
98  if(vertex)
99  {
100  te::graph::Vertex* vNew = new te::graph::Vertex(vertex);
101 
102  m_graph->add(vNew);
103  }
104 
105  vertex = it->getNextVertex();
106 
107  t.pulse();
108  }
109 
110  delete it;
111  }
112 
113  {
115 
116  te::graph::Edge* edge = it->getFirstEdge();
117 
119 
120  t.setTotalSteps(static_cast<int>(it->getEdgeInteratorCount()));
121  t.setMessage(TE_TR("Query Graph Builder - Edges..."));
122  t.useTimer(true);
123 
124 
125  //copy elements
126  while(it->isEdgeIteratorAfterEnd() == false)
127  {
128  if(edge)
129  {
130  te::graph::Edge* eNew = new te::graph::Edge(edge);
131 
132  m_graph->add(eNew);
133  }
134 
135  edge = it->getNextEdge();
136 
137  t.pulse();
138  }
139 
140  delete it;
141 
142  }
143 
144  return true;
145 }
146 
void setMessage(const std::string &message)
Set the task message.
virtual te::dt::Property * getVertexProperty(int idx)
Get a vertex property given a index.
virtual te::dt::Property * getEdgeProperty(int idx)
Get a edge property given a index.
void useTimer(bool flag)
Used to define if task use progress timer information.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
virtual te::graph::Edge * getNextEdge()
It returns a pointer to the next edge element of a graph.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
virtual te::graph::Edge * getFirstEdge()
It returns a pointer to the first edge element of a graph.
virtual Property * clone() const =0
It returns a clone of the object.
It models a property definition.
Definition: Property.h:59
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
void setTotalSteps(int value)
Set the task total stepes.
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 ~QueryGraphBuilder()
Virtual destructor.
This class defines the Query strategy to build a graph having a exist graph as input parameters...
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
virtual size_t getEdgeInteratorCount()
It returns the number of elements of this iterator.
te::gm::Polygon * p
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
virtual te::graph::Vertex * getNextVertex()
It returns a pointer to the next vertex element of a graph.
virtual int getEdgePropertySize()=0
Used to verify the number of properties associated to edge elements.
virtual int getVertexPropertySize()=0
Used to verify the number of properties associated to vertex elements.
virtual te::graph::Vertex * getFirstVertex()
It returns a pointer to the first vertex element of a graph.
virtual size_t getVertexInteratorCount()
It returns the number of elements of this iterator.
QueryGraphBuilder()
Default constructor.
bool build(te::graph::AbstractGraph *g, te::da::Expression *eEdge, te::da::Expression *eVertex, const std::string &dsInfo, const std::string &graphType, const std::map< std::string, std::string > &gInfo)
Function used to generated the vertex id based on raster coordenate.
This class defines a commun interface to represents a graph iterator class. The main diferency to ano...
Definition: QueryIterator.h:66
boost::shared_ptr< AbstractGraph > m_graph
Graph object.
static AbstractGraph * make()
It creates and returns an empty graph with default graph type.
virtual bool isVertexIteratorAfterEnd()
Used to check the iterator position.
virtual bool isEdgeIteratorAfterEnd()
Used to check the iterator position.
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177