graph_analysis
EdgeTypeManager.hpp
Go to the documentation of this file.
1 #ifndef GRAPH_ANALYSIS_EDGE_TYPE_MANAGER_HPP
2 #define GRAPH_ANALYSIS_EDGE_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 "Edge.hpp"
11 #include "AttributeManager.hpp"
12 
13 namespace graph_analysis {
14 
15 class Edge;
16 
17 namespace edge {
18  // datatype for edge type specification
19  typedef std::string Type;
20 } // end namespace edge
21 
30 class EdgeTypeManager : public base::Singleton<EdgeTypeManager>, public AttributeManager
31 {
32 public:
33  typedef std::map<edge::Type, Edge::Ptr> TypeMap;
34 
35 private:
37  TypeMap mTypeMap;
39  std::set<std::string> mRegisteredTypes;
41  std::map<std::string, std::set<std::string> > mTypeHierarchy;
43  std::string mDefaultType;
44 
45  typedef std::map<std::string, io::AttributeSerializationCallbacks> AttributeSerializationCallbackMap;
46  std::map<std::string, AttributeSerializationCallbackMap > mRegisteredCallbacks;
47 
54  Edge::Ptr edgeByType(const edge::Type& type, bool throwOnDefault = false);
55 
56 protected:
59  friend class base::Singleton<EdgeTypeManager>;
60 public:
61 
62  template<typename T, typename P = graph_analysis::Edge>
63  void registerType(bool throwOnAlreadyRegistered = false)
64  {
65  Edge::Ptr edge = make_shared<T>();
66  Edge::PtrList parents;
67  if( std::is_base_of<P,T>::value )
68  {
69  if(!std::is_same<graph_analysis::Edge, P>::value)
70  {
71  parents.push_back( make_shared<P>() );
72  }
73  } else {
74  throw
75  std::invalid_argument("graph_analysis::EdgeTypeManager::registerType:"
76  " invalid registration since '" + std::string(typeid(T).name()) + "' "
77  "is not derived from '" + std::string(typeid(P).name()) + "'");
78  }
79  registerType(edge, parents, throwOnAlreadyRegistered );
80  edge->registerAttributes(this);
81  }
82 
90  void registerType(const Edge::Ptr& edge,
91  const Edge::PtrList& parents = Edge::PtrList(),
92  bool throwOnAlreadyRegistered = false);
93 
94  /* Register edge class
95  * \param type Type name -- GraphElement::getClassName() of the edge which shall be registered
96  * \param edge Instance of the registered type
97  * \param parents Instances of the parent types
98  * \param throwOnAlreadyRegistered If true, then this function will throw
99  * \param throwOnAlreadyRegistered If true, then this function will throw
100  * std::runtime_error upon double registration
101  */
102  void registerType(const edge::Type& type,
103  const Edge::Ptr& edge,
104  const Edge::PtrList& parents = Edge::PtrList(),
105  bool throwOnAlreadyRegistered = false);
106 
110  void setDefaultType(const std::string& type);
111 
115  const std::string& getDefaultType() const { return mDefaultType; }
116 
124  Edge::Ptr createEdge(const edge::Type& type,
125  const std::string& label = "",
126  bool throwOnMissing = false);
127 
128  Edge::Ptr createEdge(const edge::Type& type,
129  const Vertex::Ptr& source,
130  const Vertex::Ptr& target,
131  const std::string& label = "",
132  bool throwOnMissing = false);
134  std::set<std::string> getSupportedTypes() const;
135 
142  std::vector<std::string> getTypeHierarchy(const edge::Type& type) const;
143 
144  std::vector<Attribute> getKnownAttributes(
145  const std::set<std::string>& classnames = std::set<std::string>()) const;
146 
152  std::vector<Attribute> getAttributes(const edge::Type& type,
153  bool includeLegacySupport = false) const;
154 
158  std::vector< std::pair<Attribute::Id, std::string> >
159  getAttributeValues(const Edge::Ptr& edge) const;
160 };
161 
162 } // end namespace graph_analysis
163 #endif /* GRAPH_ANALYSIS_EDGE_TYPE_MANAGER_HPP */
Factory for Edge subclasses.
Definition: EdgeTypeManager.hpp:30
const std::string & getDefaultType() const
Definition: EdgeTypeManager.hpp:115
shared_ptr< Edge > Ptr
Definition: Edge.hpp:21
Manage object (vertex/edge) attributes and allow them to be serialized, e.g., using GEXF or GraphML...
Definition: AttributeManager.hpp:13
Main namespace of the graph analysis library.
Definition: BFS.cpp:3
std::string Type
Definition: EdgeTypeManager.hpp:19
shared_ptr< Vertex > Ptr
Definition: Vertex.hpp:22
void registerType(bool throwOnAlreadyRegistered=false)
Definition: EdgeTypeManager.hpp:63
std::vector< Ptr > PtrList
Definition: Edge.hpp:23
std::map< edge::Type, Edge::Ptr > TypeMap
Definition: EdgeTypeManager.hpp:33