All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DirectedGraphFactory.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 DirectedGraphFactory.h
22 
23  \brief This is the concrete factory for the directed Graph type.
24 */
25 
26 // TerraLib
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 "DirectedGraph.h"
35 #include "DirectedGraphFactory.h"
36 
37 
38 // STL
39 #include <memory>
40 
42 
43 const std::string& te::graph::DirectedGraphFactory::getType() const
44 {
46 }
47 
48 void te::graph::DirectedGraphFactory::getCreationalParameters(std::vector< std::pair<std::string, std::string> >& params) const
49 {
50  params.push_back(std::pair<std::string, std::string>("GRAPH_DATA_SOURCE_TYPE", ""));
51  params.push_back(std::pair<std::string, std::string>("GRAPH_ID", ""));
52  params.push_back(std::pair<std::string, std::string>("GRAPH_NAME", ""));
53  params.push_back(std::pair<std::string, std::string>("GRAPH_DESCRIPTION", ""));
54  params.push_back(std::pair<std::string, std::string>("GRAPH_STORAGE_MODE", ""));
55  params.push_back(std::pair<std::string, std::string>("GRAPH_STRATEGY_LOADER", ""));
56  params.push_back(std::pair<std::string, std::string>("GRAPH_CACHE_POLICY", ""));
57 }
58 
60 {
61  finalize();
62  sm_factory = new DirectedGraphFactory;
63 }
64 
66 {
67  delete sm_factory;
68  sm_factory = 0;
69 }
70 
72  : te::graph::AbstractGraphFactory(Globals::sm_factoryGraphTypeDirectedGraph)
73 {
74 }
75 
76 te::graph::AbstractGraph* te::graph::DirectedGraphFactory::iOpen(const std::map<std::string, std::string>& dsInfo, const std::map<std::string, std::string>& gInfo)
77 {
78  std::map<std::string, std::string>::const_iterator it;
79  std::map<std::string, std::string>::const_iterator itend = gInfo.end();
80 
81  //create data source
82  it = gInfo.find("GRAPH_DATA_SOURCE_TYPE");
83 
84  std::auto_ptr<te::da::DataSource> dsPtr;
85 
86  if(it != itend)
87  {
88  dsPtr = te::da::DataSourceFactory::make(it->second); //example: dsType = POSTGIS
89  dsPtr->setConnectionInfo(dsInfo);
90  dsPtr->open();
91  }
92 
93  te::da::DataSource* ds = dsPtr.release();
94 
95  //create graph metadata
97 
98  it = gInfo.find("GRAPH_ID");
99  if(it != itend)
100  {
101  try
102  {
103  int id = atoi(it->second.c_str());
104 
105  gMetadata->load(id);
106  }
107  catch(const std::exception& e)
108  {
109  std::string errorMessage = TR_GRAPH("Error saving graph metadata: ");
110  errorMessage += e.what();
111 
112  throw Exception(errorMessage);
113  }
114  }
115  else
116  {
117  return 0;
118  }
119 
120  //create cache policy strategy
122  it = gInfo.find("GRAPH_CACHE_POLICY");
123  if(it != itend)
124  {
126  }
127 
128  //create graph strategy
130  it = gInfo.find("GRAPH_STRATEGY_LOADER");
131  if(it != itend)
132  {
133  ls = te::graph::AbstractGraphLoaderStrategyFactory::make(it->second, gMetadata);
134  }
135 
136  //create graph
138 
139  return g;
140 }
141 
142 te::graph::AbstractGraph* te::graph::DirectedGraphFactory::create(const std::map<std::string, std::string>& dsInfo, const std::map<std::string, std::string>& gInfo)
143 {
144  std::map<std::string, std::string>::const_iterator it;
145  std::map<std::string, std::string>::const_iterator itend = gInfo.end();
146 
147  //create data source
148  it = gInfo.find("GRAPH_DATA_SOURCE_TYPE");
149 
150  std::auto_ptr<te::da::DataSource> dsPtr;
151 
152  if(it != itend)
153  {
154  dsPtr = te::da::DataSourceFactory::make(it->second); //example: dsType = POSTGIS
155  dsPtr->setConnectionInfo(dsInfo);
156  dsPtr->open();
157  }
158 
159  te::da::DataSource* ds = dsPtr.release();
160 
161  //create graph metadata
163 
164  it = gInfo.find("GRAPH_NAME");
165  if(it != itend)
166  {
167  gMetadata->setName(it->second);
168  }
169 
170  it = gInfo.find("GRAPH_DESCRIPTION");
171  if(it != itend)
172  {
173  gMetadata->setDescription(it->second);
174  }
175 
176  it = gInfo.find("GRAPH_STORAGE_MODE");
177  if(it != itend)
178  {
179  if(it->second == TE_GRAPH_STORAGE_MODE_BY_VERTEX)
180  {
182  }
183  else if(it->second == TE_GRAPH_STORAGE_MODE_BY_EDGE)
184  {
186  }
187  }
188 
189  try
190  {
191  gMetadata->save();
192  }
193  catch(const std::exception& e)
194  {
195  std::string errorMessage = TR_GRAPH("Error saving graph metadata: ");
196  errorMessage += e.what();
197 
198  throw Exception(errorMessage);
199  }
200 
201  //create cache policy strategy
203  it = gInfo.find("GRAPH_CACHE_POLICY");
204  if(it != itend)
205  {
207  }
208 
209  //create graph strategy
211  it = gInfo.find("GRAPH_STRATEGY_LOADER");
212  if(it != itend)
213  {
214  ls = te::graph::AbstractGraphLoaderStrategyFactory::make(it->second, gMetadata);
215  }
216 
217  //create graph
219 
220  return g;
221 }
222 
224 {
225  return new Graph;
226 }
This is the abstract factory for Graphs.
This is the concrete factory for the directed Graph type.
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 is a implementation of a Directed Graph. By convention a directed graph provides access to out-e...
static void finalize()
It finalizes the factory: the singleton instance will be destroyed and will be unregistered from the ...
This class define the main functions necessary to save and load the graph data and metadata informati...
te::graph::AbstractGraph * build()
Builder Function used to create the class object.
static DirectedGraphFactory * 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 initialize()
It initializes the factory: the singleton instance will be registered in the abstract factory ...
This is the concrete factory for the directed Graph type.
#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
const std::string & getType() const
Returns the type (name) of this factory.
void load(int id)
Function used to load the graph information given a graph id.
void getCreationalParameters(std::vector< std::pair< std::string, std::string > > &params) const
It returns the list of parameters accepted as graph info.
static AbstractCachePolicy * make()
It creates and returns default cache policy.
void setName(std::string name)
Set the graph name.
static const std::string sm_factoryGraphTypeDirectedGraph
Directed Graph Factory Name.
Definition: Globals.h:54
#define TE_GRAPH_STORAGE_MODE_BY_EDGE
This definition is used to set the edge storage mode.
Definition: Config.h:135
This is a implementation of a Directed Graph. By convention a directed graph provides access to out-e...
Definition: DirectedGraph.h:54
This is the main graph implementation, that uses a cache policy anda graph loader to get all elements...
Definition: Graph.h:72
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:116
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).
void setDescription(std::string desc)
Set the graph description.
This class is used to set the main functions of a cache policy.
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...
Class used to define the graph metadata informations over a SGBD source.
void save()
Function used to save the graph information.