graph_analysis
VertexTypeManager.hpp
Go to the documentation of this file.
1 #ifndef GRAPH_ANALYSIS_VERTEX_TYPE_MANAGER_HPP
2 #define GRAPH_ANALYSIS_VERTEX_TYPE_MANAGER_HPP
3 
4 #include <map>
5 #include <set>
6 #include <string>
7 #include <typeinfo>
8 #include <base-logging/Singleton.hpp>
9 
10 #include "Vertex.hpp"
11 #include "AttributeManager.hpp"
12 
13 namespace graph_analysis {
14 
15 namespace vertex {
16  // datatype for vertex type specification
17  typedef std::string Type;
18 } // end namespace vertex
19 
29 class VertexTypeManager : public base::Singleton<VertexTypeManager>, public AttributeManager
30 {
31 public:
32  typedef std::map<vertex::Type, Vertex::Ptr> TypeMap;
33 
34 private:
35 
37  TypeMap mTypeMap;
39  std::set<std::string> mRegisteredTypes;
41  std::map<std::string, std::set<std::string> > mTypeHierarchy;
43  std::string mDefaultVertexType;
44 
51  Vertex::Ptr vertexByType(const vertex::Type& type, bool throwOnMissing = false);
52 
53 protected:
56  friend class base::Singleton<VertexTypeManager>;
57 
58 public:
62  void setDefaultType(const std::string& type);
63 
67  const std::string& getDefaultType() const { return mDefaultVertexType; }
68 
69  template<typename T, typename P = graph_analysis::Vertex>
70  void registerType(bool throwOnAlreadyRegistered = false)
71  {
72  Vertex::Ptr vertex = make_shared<T>();
73  Vertex::PtrList parents;
74  if( std::is_base_of<P,T>::value )
75  {
76  if(!std::is_same<graph_analysis::Vertex, P>::value)
77  {
78  parents.push_back( make_shared<P>() );
79  }
80  } else {
81  throw
82  std::invalid_argument("graph_analysis::VertexTypeManager::registerType:"
83  " invalid registration since '" + std::string(typeid(T).name()) + "' "
84  "is not derived from '" + std::string(typeid(P).name()) + "'");
85  }
86  registerType(vertex, parents, throwOnAlreadyRegistered );
87  vertex->registerAttributes(this);
88  }
89 
97  void registerType(const Vertex::Ptr& vertex,
98  const Vertex::PtrList& parents = Vertex::PtrList(),
99  bool throwOnAlreadyRegistered = false);
100 
101  /* Register vertex class
102  * \param type Type name -- GraphElement::getClassName() of the vertex which shall be registered
103  * \param vertex Instance of the registered type
104  * \param parents Instances of the parent types
105  * \param throwOnAlreadyRegistered If true, then this function will throw
106  * \param throwOnAlreadyRegistered If true, then this function will throw
107  * std::runtime_error upon double registration
108  */
109  void registerType(const vertex::Type& type,
110  const Vertex::Ptr& vertex,
111  const Vertex::PtrList& parents = Vertex::PtrList(),
112  bool throwOnAlreadyRegistered = false);
113 
122  Vertex::Ptr createVertex(const vertex::Type& type, const std::string& label = std::string(),
123  bool throwOnMissing = false);
124 
129  std::set<std::string> getSupportedTypes() const;
130 
137  std::vector<std::string> getTypeHierarchy(const vertex::Type& type) const;
138 
146  std::vector<Attribute> getKnownAttributes(const std::set<std::string>& classnames = std::set<std::string>()) const;
147 
155  std::vector<Attribute> getAttributes(const vertex::Type& classname,
156  bool includeLegacySupport = false) const;
157 
162  std::vector< std::pair<Attribute::Id, std::string> >
163  getAttributeValues(const Vertex::Ptr& vertex) const;
164 };
165 
166 } // end namespace graph_analysis
167 #endif /* GRAPH_ANALYSIS_VERTEX_TYPE_MANAGER_HPP */
const std::string & getDefaultType() const
Definition: VertexTypeManager.hpp:67
std::map< vertex::Type, Vertex::Ptr > TypeMap
Definition: VertexTypeManager.hpp:32
Manage object (vertex/edge) attributes and allow them to be serialized, e.g., using GEXF or GraphML...
Definition: AttributeManager.hpp:13
std::vector< Ptr > PtrList
Definition: Vertex.hpp:24
Main namespace of the graph analysis library.
Definition: BFS.cpp:3
shared_ptr< Vertex > Ptr
Definition: Vertex.hpp:22
void registerType(bool throwOnAlreadyRegistered=false)
Definition: VertexTypeManager.hpp:70
Factory for Vertex subclasses.
Definition: VertexTypeManager.hpp:29
std::string Type
Definition: VertexTypeManager.hpp:17