All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AddDeepAttribute.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 AddDeepAttribute.cpp
22 
23  \brief This class defines a function used to add to a graph the deep information attribute
24 
25 */
26 
27 // Terralib
28 #include "../../common/Translator.h"
29 #include "../../common/progress/TaskProgress.h"
30 #include "../../datatype/SimpleProperty.h"
31 #include "../../datatype/SimpleData.h"
32 #include "../../datatype/Enums.h"
33 #include "../core/Edge.h"
34 #include "../core/Vertex.h"
35 #include "../core/VertexProperty.h"
36 #include "../graphs/Graph.h"
37 #include "../graphs/BidirectionalGraph.h"
38 #include "../iterator/SequenceIterator.h"
39 #include "../Exception.h"
40 #include "AddDeepAttribute.h"
41 
42 
44 {
45  //clear graph cache
46  graph->flush();
47 
48  //add new attribute
50  p->setParent(0);
51  p->setId(0);
52 
53  graph->addVertexProperty(p);
54 
55  // verify what the index of the new property
56  int pIdx = 0;
57 
58  for(int i = 0; i < graph->getVertexPropertySize(); ++ i)
59  {
60  if(graph->getVertexProperty(i)->getName() == attributeName)
61  {
62  pIdx = i;
63  break;
64  }
65  }
66 
67  //iterator for all vertex objects
68  te::graph::AbstractIterator* oldIt = graph->getIterator();
69 
71 
72  graph->setIterator(it);
73 
74  te::graph::Vertex* vertex = graph->getFirstVertex();
75 
77 
79  task.setMessage("Add Deep Attribute Operation");
80 
81 
82 
83  while(it->isVertexIteratorAfterEnd() == false)
84  {
85  if(vertex)
86  {
87  std::set<int> vertexIdSet;
88 
89  vertexIdSet.insert(vertex->getId());
90 
91  int deepValue = 0;
92 
93  //calculate deep attribute
94  calculateDeepValue(vertex, graph, deepValue, vertexIdSet);
95 
96  vertex->addAttribute(pIdx, new te::dt::SimpleData<int, te::dt::INT32_TYPE>(deepValue));
97 
98  //set the vertex as durty
99  graph->update(vertex);
100  }
101 
102  vertex = graph->getNextVertex();
103 
104  task.pulse();
105  }
106 
107  graph->setIterator(oldIt);
108 
109  delete it;
110 
111  //clear graph cache
112  graph->flush();
113 }
114 
116 {
117 }
118 
120 {
121  if(v->getPredecessors().empty() == false)
122  {
123  deepValue += v->getPredecessors().size();
124 
125  std::set<int> predecessors;
126 
127  predecessors.insert(v->getPredecessors().begin(), v->getPredecessors().end());
128 
129  std::set<int>::iterator it = predecessors.begin();
130 
131  while(it != predecessors.end())
132  {
133  te::graph::Edge* e = g->getEdge(*it);
134 
135  if(e)
136  {
137  te::graph::Vertex* vFrom = g->getVertex(e->getIdFrom());
138 
139  if(vFrom)
140  {
141  std::set<int>::iterator itSet = vertexIdSet.find(vFrom->getId());
142 
143  if(itSet == vertexIdSet.end())
144  {
145  vertexIdSet.insert(vFrom->getId());
146 
147  calculateDeepValue(vFrom, g, deepValue, vertexIdSet);
148  }
149  }
150  }
151 
152  ++it;
153  }
154  }
155 }
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:159
An atomic property like an integer or double.
void setIterator(te::graph::AbstractIterator *i)
Used to associate a iterator to graph.
void setTotalSteps(int value)
Set the task total stepes.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
int getIdFrom()
It returns the vertex origin identification.
Definition: Edge.cpp:71
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
const std::string & getName() const
It returns the property name.
Definition: Property.h:126
void setId(unsigned int id)
It sets the property identifier.
Definition: Property.h:117
virtual te::graph::Vertex * getNextVertex()
It returns a pointer to the next vertex element of a graph.
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Vertex.cpp:84
virtual int getVertexPropertySize()
Used to verify the number of properties associated to vertex elements.
Definition: Graph.cpp:195
This class defines a function used to add to a graph the deep information attribute.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
This class defines a commun interface to represents a graph iterator class. The main diferency to ano...
int getId()
It returns the vertex id.
Definition: Vertex.cpp:69
virtual te::dt::Property * getVertexProperty(int idx)
Get a vertex property given a index.
Definition: Graph.cpp:185
std::set< int > & getPredecessors()
Returns the Predecessors vector.
Definition: Vertex.cpp:101
virtual te::graph::Edge * getEdge(int id)
It returns the edge element if it&#39;s exist.
Definition: Graph.cpp:261
virtual bool isVertexIteratorAfterEnd()
Used to check the iterator position.
virtual size_t getVertexInteratorCount()
It returns the number of elements of this iterator.
void setMessage(const std::string &message)
Set the task message.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
AddDeepAttribute(te::graph::BidirectionalGraph *graph, std::string attributeName)
Default constructor.
This is a implementation of a Bidirectional Graph. By convention a bidirectional graph provides acces...
virtual void addVertexProperty(te::dt::Property *p)
Add a new property associated to the vertex element.
Definition: Graph.cpp:169
void calculateDeepValue(te::graph::Vertex *v, te::graph::BidirectionalGraph *g, int &deepValue, std::set< int > &vertexIdSet)
Recursive function used to calculate the deep attribute.
virtual void flush()
Function used to clear the memory cache, all elements was released from memory, if any element was ch...
Definition: Graph.cpp:329
virtual te::graph::Vertex * getVertex(int id)
It returns the vertex element if it&#39;s exist.
Definition: Graph.cpp:142
te::graph::AbstractIterator * getIterator()
Used to get a iterator associated to graph.
virtual ~AddDeepAttribute()
Virtual destructor.
virtual te::graph::Vertex * getFirstVertex()
It returns a pointer to the first vertex element of a graph.
virtual void update(Vertex *v)
Update the vertex element.
Definition: Graph.cpp:106