All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AbstractGraphFactory.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 AbstractGraphFactory.cpp
22 
23  \brief This is the abstract factory for Graphs.
24 */
25 
26 // TerraLib
27 #include "../../dataaccess/datasource/DataSource.h"
28 #include "../../common/StringUtils.h"
29 #include "../../common/Translator.h"
30 #include "../../dataaccess/datasource/DataSourceFactory.h"
31 #include "../cache/AbstractCachePolicyFactory.h"
32 #include "../drivers/datasource/DataSourceGraphMetadata.h"
33 #include "../loader/AbstractGraphLoaderStrategyFactory.h"
34 #include "../Config.h"
35 #include "../Exception.h"
36 #include "AbstractGraphFactory.h"
37 #include "GraphMetadata.h"
38 
39 // STL
40 #include <memory>
41 
43 {
45 }
46 
48 {
49  std::string ucase = te::common::Convert2UCase(gType);
50 
52 }
53 
54 te::graph::AbstractGraph* te::graph::AbstractGraphFactory::make(const std::map<std::string, std::string>& dsInfo, const std::map<std::string, std::string>& gInfo)
55 {
56  return make(TE_DEFAULT_GRAPH_TYPE, dsInfo, gInfo);
57 }
58 
59 te::graph::AbstractGraph* te::graph::AbstractGraphFactory::make(const std::string& gType, const std::map<std::string, std::string>& dsInfo, const std::map<std::string, std::string>& gInfo)
60 {
61  std::string ucase = te::common::Convert2UCase(gType);
62 
64 
65  AbstractGraphFactory* f = static_cast<AbstractGraphFactory*>(d.find(ucase));
66 
67  if(f == 0)
68  throw Exception(TE_TR("Could not find concrete factory! Check if it was initialized!"));
69 
70  AbstractGraph* g = f->create(dsInfo, gInfo);
71 
72  g->getMetadata()->setType(ucase);
73 
74  return g;
75 }
76 
77 te::graph::AbstractGraph* te::graph::AbstractGraphFactory::open(const std::map<std::string, std::string>& dsInfo, const std::map<std::string, std::string>& gInfo)
78 {
79  return open(TE_DEFAULT_GRAPH_TYPE, dsInfo, gInfo);
80 }
81 
82 te::graph::AbstractGraph* te::graph::AbstractGraphFactory::open(const std::string& gType, const std::map<std::string, std::string>& dsInfo, const std::map<std::string, std::string>& gInfo)
83 {
84  std::string ucase = te::common::Convert2UCase(gType);
85 
87 
88  AbstractGraphFactory* f = static_cast<AbstractGraphFactory*>(d.find(ucase));
89 
90  if(f == 0)
91  throw Exception(TE_TR("Could not find concrete factory! Check if it was initialized!"));
92 
93  AbstractGraph* g = f->iOpen(dsInfo, gInfo);
94 
95  g->getMetadata()->setType(ucase);
96 
97  return g;
98 }
99 
101  : te::common::AbstractFactory<te::graph::AbstractGraph, std::string>(factoryKey)
102 {
103 }
104 
105 te::graph::GraphMetadata* te::graph::AbstractGraphFactory::getMetadata(const std::map<std::string, std::string>& dsInfo, const std::map<std::string, std::string>& gInfo)
106 {
107  te::graph::GraphMetadata* metadata = 0;
108 
109  std::map<std::string, std::string>::const_iterator it;
110  std::map<std::string, std::string>::const_iterator itend = gInfo.end();
111 
112  //create data source
113  it = gInfo.find("GRAPH_DATA_SOURCE_TYPE");
114 
115  std::auto_ptr<te::da::DataSource> dsPtr;
116 
117  if(it != itend)
118  {
119  if(it->second == "MEM")
120  {
121  metadata = new te::graph::GraphMetadata(0);
122 
123  metadata->m_memoryGraph = true;
124  }
125  else
126  {
127  dsPtr = te::da::DataSourceFactory::make(it->second); //example: dsType = POSTGIS
128  dsPtr->setConnectionInfo(dsInfo);
129  dsPtr->open();
130 
131  te::da::DataSource* ds = dsPtr.release();
132 
133  metadata = new te::graph::DataSourceGraphMetadata(ds);
134 
135  metadata->m_memoryGraph = false;
136  }
137  }
138 
139 
140  return metadata;
141 }
142 
143 int te::graph::AbstractGraphFactory::getId(const std::map<std::string, std::string>& dsInfo, const std::map<std::string, std::string>& gInfo)
144 {
145  std::map<std::string, std::string>::const_iterator it;
146  std::map<std::string, std::string>::const_iterator itend = gInfo.end();
147 
148  it = gInfo.find("GRAPH_ID");
149 
150  if(it != itend)
151  return atoi(it->second.c_str());
152 
153  return -1;
154 }
155 
157 {
158  std::map<std::string, std::string>::const_iterator it;
159  std::map<std::string, std::string>::const_iterator itend = gInfo.end();
160 
162 
163  it = gInfo.find("GRAPH_CACHE_POLICY");
164  if(it != itend)
165  {
167  }
168 
169  return cp;
170 }
171 
173 {
174  std::map<std::string, std::string>::const_iterator it;
175  std::map<std::string, std::string>::const_iterator itend = gInfo.end();
176 
178 
179  it = gInfo.find("GRAPH_STRATEGY_LOADER");
180  if(it != itend)
181  {
182  ls = te::graph::AbstractGraphLoaderStrategyFactory::make(it->second, metadata);
183  }
184 
185  return ls;
186 }
187 
188 void te::graph::AbstractGraphFactory::setMetadataInformation(const std::map<std::string, std::string>& gInfo, te::graph::GraphMetadata* metadata)
189 {
190  std::map<std::string, std::string>::const_iterator it;
191  std::map<std::string, std::string>::const_iterator itend = gInfo.end();
192 
193  it = gInfo.find("GRAPH_NAME");
194  if(it != itend)
195  {
196  metadata->setName(it->second);
197  }
198 
199  it = gInfo.find("GRAPH_DESCRIPTION");
200  if(it != itend)
201  {
202  metadata->setDescription(it->second);
203  }
204 
205  it = gInfo.find("GRAPH_STORAGE_MODE");
206  if(it != itend)
207  {
208  if(it->second == TE_GRAPH_STORAGE_MODE_BY_VERTEX)
209  {
211  }
212  else if(it->second == TE_GRAPH_STORAGE_MODE_BY_EDGE)
213  {
215  }
216  }
217 }
static te::graph::AbstractGraphLoaderStrategy * getLoaderStrategy(const std::map< std::string, std::string > &gInfo, te::graph::GraphMetadata *metadata)
This method is a auxiliar function used to get the loader strategy pointer.
static AbstractGraphLoaderStrategy * make()
It creates and returns default graph loader strategy.
static void setMetadataInformation(const std::map< std::string, std::string > &gInfo, te::graph::GraphMetadata *metadata)
void setStorageMode(GraphStorageMode value)
Set the graph storage mode (defined in Enums file)
static dictionary_type & getDictionary()
It returns a reference to the internal dictionary of concrete factories.
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:163
bool m_memoryGraph
Flag used to indicate if the graph is a memory graph.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:118
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
Class used to define the graph metadata informations.
virtual AbstractGraph * iOpen(const std::map< std::string, std::string > &dsInfo, const std::map< std::string, std::string > &gInfo)=0
This method must be re-implemented by subclasses in order to have a finner control for the graph obje...
void setName(std::string name)
Set the graph name.
TFACTORY * find(const TFACTORYKEY &factoryKey) const
It looks for a given factory identified by a key.
virtual te::graph::GraphMetadata * getMetadata()=0
Function used to access the graph metadata.
This class is used to set the main functions of a cache policy.
static std::auto_ptr< DataSource > make(const std::string &dsType)
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
void setType(std::string graphType)
Set the graph type (defined in Enums file)
#define TE_DEFAULT_GRAPH_TYPE
This definition is used to set the default graph type.
Definition: Config.h:51
This class define the main functions necessary to save and load the graph data and metadata informati...
virtual AbstractGraph * create(const std::map< std::string, std::string > &dsInfo, const std::map< std::string, std::string > &gInfo)=0
This method must be implemented by subclasses (graph types).
static TPRODUCT * make(const TFACTORYKEY &factoryKey)
It creates an object with the appropriated factory.
static AbstractGraph * open(const std::map< std::string, std::string > &dsInfo, const std::map< std::string, std::string > &gInfo)
It opens a graph with the given parameters and default graph type.
This is the abstract factory for Graphs.
Class used to define the graph metadata informations over a SGBD source.
#define TE_GRAPH_STORAGE_MODE_BY_EDGE
This definition is used to set the edge storage mode.
Definition: Config.h:100
void setDescription(std::string desc)
Set the graph description.
AbstractGraphFactory(const std::string &factoryKey)
Constructor.
static te::graph::GraphMetadata * getMetadata(const std::map< std::string, std::string > &dsInfo, const std::map< std::string, std::string > &gInfo)
This method is a auxiliar function used to get the metadata pointer.
This is the abstract factory for Graphs.
static AbstractGraph * make()
It creates and returns an empty graph with default graph type.
#define TE_GRAPH_STORAGE_MODE_BY_VERTEX
This definition is used to set the vertex storage mode.
Definition: Config.h:93
static int getId(const std::map< std::string, std::string > &dsInfo, const std::map< std::string, std::string > &gInfo)
This method is a auxiliar function used to get the graph id.
Class used to define the graph metadata informations.
Definition: GraphMetadata.h:56
static AbstractCachePolicy * make()
It creates and returns default cache policy.
This class represents a dictionary of factories.
static te::graph::AbstractCachePolicy * getCachePolicy(const std::map< std::string, std::string > &gInfo)
This method is a auxiliar function used to get the cache policy pointer.