All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AddDeepAttribute.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 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
69 
70  te::graph::Vertex* vertex = it->getFirstVertex();
71 
73 
75  task.setMessage("Add Deep Attribute Operation");
76 
77  while(it->isVertexIteratorAfterEnd() == false)
78  {
79  if(vertex)
80  {
81  std::set<int> vertexIdSet;
82 
83  vertexIdSet.insert(vertex->getId());
84 
85  int deepValue = 0;
86 
87  //calculate deep attribute
88  calculateDeepValue(vertex, graph, deepValue, vertexIdSet);
89 
90  vertex->addAttribute(pIdx, new te::dt::SimpleData<int, te::dt::INT32_TYPE>(deepValue));
91 
92  //set the vertex as durty
93  graph->update(vertex);
94  }
95 
96  vertex = it->getNextVertex();
97 
98  task.pulse();
99  }
100 
101  delete it;
102 
103  //clear graph cache
104  graph->flush();
105 }
106 
108 {
109 }
110 
112 {
113  if(v->getPredecessors().empty() == false)
114  {
115  deepValue += v->getPredecessors().size();
116 
117  std::set<int> predecessors;
118 
119  predecessors.insert(v->getPredecessors().begin(), v->getPredecessors().end());
120 
121  std::set<int>::iterator it = predecessors.begin();
122 
123  while(it != predecessors.end())
124  {
125  te::graph::Edge* e = g->getEdge(*it);
126 
127  if(e)
128  {
129  te::graph::Vertex* vFrom = g->getVertex(e->getIdFrom());
130 
131  if(vFrom)
132  {
133  std::set<int>::iterator itSet = vertexIdSet.find(vFrom->getId());
134 
135  if(itSet == vertexIdSet.end())
136  {
137  vertexIdSet.insert(vFrom->getId());
138 
139  calculateDeepValue(vFrom, g, deepValue, vertexIdSet);
140  }
141  }
142  }
143 
144  ++it;
145  }
146  }
147 }
virtual te::graph::Vertex * getFirstVertex()
It returns a pointer to the first vertex element of a graph.
void setMessage(const std::string &message)
Set the task message.
An atomic property like an integer or double.
std::set< int > & getPredecessors()
Returns the Predecessors vector.
Definition: Vertex.cpp:101
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
virtual te::graph::Edge * getEdge(int id)
It returns the edge element if it's exist.
Definition: Graph.cpp:234
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.
void setId(unsigned int id)
It sets the property identifier.
Definition: Property.h:118
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::dt::Property * getVertexProperty(int idx)
Get a vertex property given a index.
Definition: Graph.cpp:174
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 int getVertexPropertySize()
Used to verify the number of properties associated to vertex elements.
Definition: Graph.cpp:184
virtual void flush()
Function used to clear the memory cache, all elements was released from memory, if any element was ch...
Definition: Graph.cpp:295
AddDeepAttribute(te::graph::BidirectionalGraph *graph, std::string attributeName)
Default constructor.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
int getIdFrom()
It returns the vertex origin identification.
Definition: Edge.cpp:71
virtual te::graph::Vertex * getNextVertex()
It returns a pointer to the next vertex element of a graph.
virtual void addVertexProperty(te::dt::Property *p)
Add a new property associated to the vertex element.
Definition: Graph.cpp:158
virtual size_t getVertexInteratorCount()
It returns the number of elements of this iterator.
virtual te::graph::Vertex * getVertex(int id)
It returns the vertex element if it's exist.
Definition: Graph.cpp:138
int getId()
It returns the vertex id.
Definition: Vertex.cpp:69
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Vertex.cpp:84
virtual ~AddDeepAttribute()
Virtual destructor.
This is a implementation of a Bidirectional Graph. By convention a bidirectional graph provides acces...
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
This class defines a function used to add to a graph the deep information attribute.
virtual void update(Vertex *v)
Update the vertex element.
Definition: Graph.cpp:113
virtual bool isVertexIteratorAfterEnd()
Used to check the iterator position.
const std::string & getName() const
It returns the property name.
Definition: Property.h:127
void setParent(Property *p)
It associate this property to the informed parent.
Definition: Property.h:177