All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UndirectedGraphFactory.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 UndirectedGraphFactory.h
22 
23  \brief This is the concrete factory for the undirected Graph type.
24 */
25 
26 // TerraLib Includes
27 #include "../../dataaccess/datasource/DataSource.h"
28 #include "../../dataaccess/datasource/DataSourceFactory.h"
29 #include "../cache/AbstractCachePolicyFactory.h"
30 #include "../drivers/database/DatabaseGraphMetadata.h"
31 #include "../loader/AbstractGraphLoaderStrategyFactory.h"
32 #include "../Exception.h"
33 #include "../Globals.h"
34 #include "UndirectedGraph.h"
35 #include "UndirectedGraphFactory.h"
36 
37 // STL Includes
38 #include <memory>
39 
41 
43 {
45 }
46 
47 void te::graph::UndirectedGraphFactory::getCreationalParameters(std::vector< std::pair<std::string, std::string> >& params) const
48 {
49  params.push_back(std::pair<std::string, std::string>("GRAPH_DATA_SOURCE_TYPE", ""));
50  params.push_back(std::pair<std::string, std::string>("GRAPH_ID", ""));
51  params.push_back(std::pair<std::string, std::string>("GRAPH_NAME", ""));
52  params.push_back(std::pair<std::string, std::string>("GRAPH_DESCRIPTION", ""));
53  params.push_back(std::pair<std::string, std::string>("GRAPH_STORAGE_MODE", ""));
54  params.push_back(std::pair<std::string, std::string>("GRAPH_STRATEGY_LOADER", ""));
55  params.push_back(std::pair<std::string, std::string>("GRAPH_CACHE_POLICY", ""));
56 }
57 
59 {
60  finalize();
61  sm_factory = new UndirectedGraphFactory;
62 }
63 
65 {
66  delete sm_factory;
67  sm_factory = 0;
68 }
69 
71  : te::graph::AbstractGraphFactory(Globals::sm_factoryGraphTypeUndirectedGraph)
72 {
73 }
74 
75 te::graph::AbstractGraph* te::graph::UndirectedGraphFactory::iOpen(const std::map<std::string, std::string>& dsInfo, const std::map<std::string, std::string>& gInfo)
76 {
77  std::map<std::string, std::string>::const_iterator it;
78  std::map<std::string, std::string>::const_iterator itend = gInfo.end();
79 
80  //create data source
81  it = gInfo.find("GRAPH_DATA_SOURCE_TYPE");
82 
83  std::auto_ptr<te::da::DataSource> dsPtr;
84 
85  if(it != itend)
86  {
87  dsPtr = te::da::DataSourceFactory::make(it->second); //example: dsType = POSTGIS
88  dsPtr->setConnectionInfo(dsInfo);
89  dsPtr->open();
90  }
91 
92  te::da::DataSource* ds = dsPtr.release();
93 
94  //create graph metadata
96 
97  it = gInfo.find("GRAPH_ID");
98  if(it != itend)
99  {
100  try
101  {
102  int id = atoi(it->second.c_str());
103 
104  gMetadata->load(id);
105  }
106  catch(const std::exception& e)
107  {
108  std::string errorMessage = TR_GRAPH("Error saving graph metadata: ");
109  errorMessage += e.what();
110 
111  throw Exception(errorMessage);
112  }
113  }
114  else
115  {
116  return 0;
117  }
118 
119  //create cache policy strategy
121  it = gInfo.find("GRAPH_CACHE_POLICY");
122  if(it != itend)
123  {
125  }
126 
127  //create graph strategy
129  it = gInfo.find("GRAPH_STRATEGY_LOADER");
130  if(it != itend)
131  {
132  ls = te::graph::AbstractGraphLoaderStrategyFactory::make(it->second, gMetadata);
133  }
134 
135  //create graph
137 
138  return g;
139 }
140 
141 te::graph::AbstractGraph* te::graph::UndirectedGraphFactory::create(const std::map<std::string, std::string>& dsInfo, const std::map<std::string, std::string>& gInfo)
142 {
143  std::map<std::string, std::string>::const_iterator it;
144  std::map<std::string, std::string>::const_iterator itend = gInfo.end();
145 
146  //create data source
147  it = gInfo.find("GRAPH_DATA_SOURCE_TYPE");
148 
149  std::auto_ptr<te::da::DataSource> dsPtr;
150 
151  if(it != itend)
152  {
153  dsPtr = te::da::DataSourceFactory::make(it->second); //example: dsType = POSTGIS
154  dsPtr->setConnectionInfo(dsInfo);
155  dsPtr->open();
156  }
157 
158  te::da::DataSource* ds = dsPtr.release();
159 
160  //create graph metadata
162 
163  it = gInfo.find("GRAPH_NAME");
164  if(it != itend)
165  {
166  gMetadata->setName(it->second);
167  }
168 
169  it = gInfo.find("GRAPH_DESCRIPTION");
170  if(it != itend)
171  {
172  gMetadata->setDescription(it->second);
173  }
174 
175  it = gInfo.find("GRAPH_STORAGE_MODE");
176  if(it != itend)
177  {
178  if(it->second == TE_GRAPH_STORAGE_MODE_BY_VERTEX)
179  {
181  }
182  else if(it->second == TE_GRAPH_STORAGE_MODE_BY_EDGE)
183  {
185  }
186  }
187 
188  try
189  {
190  gMetadata->save();
191  }
192  catch(const std::exception& e)
193  {
194  std::string errorMessage = TR_GRAPH("Error saving graph metadata: ");
195  errorMessage += e.what();
196 
197  throw Exception(errorMessage);
198  }
199 
200  //create cache policy strategy
202  it = gInfo.find("GRAPH_CACHE_POLICY");
203  if(it != itend)
204  {
206  }
207 
208  //create graph strategy
210  it = gInfo.find("GRAPH_STRATEGY_LOADER");
211  if(it != itend)
212  {
213  ls = te::graph::AbstractGraphLoaderStrategyFactory::make(it->second, gMetadata);
214  }
215 
216  //create graph
218 
219  return g;
220 }
221 
223 {
224  return new Graph;
225 }
This is the abstract factory for Graphs.
static AbstractGraphLoaderStrategy * make()
It creates and returns default graph loader strategy.
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:56
This class define the main functions necessary to save and load the graph data and metadata informati...
This is the concrete factory for the undirected Graph type.
te::graph::AbstractGraph * build()
Builder Function used to create the class object.
static UndirectedGraphFactory * sm_factory
Static instance used to register the factory.
void setStorageMode(GraphStorageMode value)
Set the graph storage mode (defined in Enums file)
An static class with global definitions for the TerraLib Graph Module.
Definition: Globals.h:45
#define TE_GRAPH_STORAGE_MODE_BY_VERTEX
This definition is used to set the vertex storage mode.
Definition: Config.h:128
static std::auto_ptr< DataSource > make(const std::string &dsType)
static void finalize()
It finalizes the factory: the singleton instance will be destroyed and will be unregistered from the ...
#define TR_GRAPH(message)
It marks a string in order to get translated. This is a special mark used in the Graph module of Terr...
Definition: Config.h:58
void load(int id)
Function used to load the graph information given a graph id.
te::graph::AbstractGraph * iOpen(const std::map< std::string, std::string > &dsInfo, const std::map< std::string, std::string > &gInfo)
This method must be re-implemented by subclasses in order to have a finner control for the graph obje...
This is the concrete factory for the undirected Graph type.
static AbstractCachePolicy * make()
It creates and returns default cache policy.
void setName(std::string name)
Set the graph name.
static const std::string sm_factoryGraphTypeUndirectedGraph
Undirected Graph Factory Name.
Definition: Globals.h:55
#define TE_GRAPH_STORAGE_MODE_BY_EDGE
This definition is used to set the edge storage mode.
Definition: Config.h:135
This is the main graph implementation, that uses a cache policy anda graph loader to get all elements...
Definition: Graph.h:72
te::graph::AbstractGraph * create(const std::map< std::string, std::string > &dsInfo, const std::map< std::string, std::string > &gInfo)
This method must be implemented by subclasses (graph types).
This is a implementation of a UndirectedGraph Graph. By definition a undirected graph has no directio...
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:116
static void initialize()
It initializes the factory: the singleton instance will be registered in the abstract factory ...
This is a implementation of a UndirectedGraph Graph. By definition a undirected graph has no directio...
void getCreationalParameters(std::vector< std::pair< std::string, std::string > > &params) const
It returns the list of parameters accepted as graph info.
void setDescription(std::string desc)
Set the graph description.
This class is used to set the main functions of a cache policy.
const std::string & getType() const
Returns the type (name) of this factory.
Class used to define the graph metadata informations over a SGBD source.
void save()
Function used to save the graph information.