YARP
Yet Another Robot Platform
Graph.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_PROFILER_GRAP_H
7 #define YARP_PROFILER_GRAP_H
8 
9 #include<yarp/os/Property.h>
10 
11 #include <iostream>
12 #include <vector>
13 #include <string>
14 
15 namespace yarp {
16  namespace profiler {
17  namespace graph {
18  class Vertex;
19  class Edge;
20  class Graph;
21  class Algorithm;
22  class GraphicVertex;
23  class OwnedVertex;
24  class ProcessVertex;
25  class PortVertex;
26  class MachineVertex;
27 
28  }
29  }
30 }
31 
32 
33 typedef std::vector<yarp::profiler::graph::Edge> edge_set;
34 typedef edge_set::iterator edge_iterator;
35 typedef edge_set::const_iterator edge_const_iterator;
36 
37 typedef std::vector<yarp::profiler::graph::Vertex*> pvertex_set;
38 typedef pvertex_set::iterator pvertex_iterator;
39 typedef pvertex_set::const_iterator pvertex_const_iterator;
40 
41 typedef std::vector<pvertex_set> graph_subset;
42 typedef graph_subset::iterator graph_subset_iterator;
43 typedef graph_subset::const_iterator graph_subset_const_iterator;
44 
45 
50 public:
51 
52  Edge(const yarp::profiler::graph::Vertex& firstV,
53  const yarp::profiler::graph::Vertex& secondV,
55 
56  Edge(const Edge& edge);
57 
58  virtual ~Edge();
59 
60  const yarp::profiler::graph::Vertex& first() const;
62  virtual bool operator == (const yarp::profiler::graph::Edge &edge) const;
63 
64 public:
66 
67 private:
68  const yarp::profiler::graph::Vertex* firstVertex;
69  const yarp::profiler::graph::Vertex* secondVertex;
70 };
71 
72 
77 
78 public:
79  Vertex(const yarp::os::Property &prop);
81  virtual ~Vertex();
82 
83  const edge_set& outEdges() const { return outs; }
84  const edge_set& inEdges() const { return ins; }
85  size_t degree() const { return ins.size() + outs.size(); }
86 
87  virtual bool operator == (const yarp::profiler::graph::Vertex &v1) const = 0;
88  virtual bool operator<(const Vertex &v1) const;
89 
90  friend class Graph;
91 
92 public:
94 
95 private:
96  void insertOuts(const yarp::profiler::graph::Edge& edge);
97  void insertIns(const yarp::profiler::graph::Edge& edge);
98 
99 private:
100  edge_set outs;
101  edge_set ins;
102 };
103 
104 
109 
110 public:
111  Graph();
112  //Graph(yarp::profiler::graph::Graph& graph);
113  virtual ~Graph();
114 
115  //void insert(Vertex *vertex);
116  pvertex_iterator insert(const Vertex &vertex);
117  void remove(const Vertex &vertex);
118  void remove(const pvertex_iterator vi);
119 
120  void insertEdge(const Vertex &v1, const Vertex &v2,
121  const yarp::os::Property &property="");
122 
123  void insertEdge(const pvertex_iterator vi1, const pvertex_iterator vi2,
124  const yarp::os::Property &property="");
125 
126  const pvertex_iterator find(const Vertex &v1);
127 
128  size_t size();
129  size_t nodesCount();
130  const pvertex_set& vertices() { return mVertices; }
131  size_t order() { return mVertices.size(); }
132 
133  void clear();
134 
135 private:
136  pvertex_set mVertices;
137 };
138 
139 
141 public:
147  static bool calcSCC(yarp::profiler::graph::Graph& graph, graph_subset &scc);
148 };
149 
151 {
152 public:
153  GraphicVertex(const yarp::os::Property &prop) : yarp::profiler::graph::Vertex(prop){
154  graphicItem = nullptr;
155  }
156  void setGraphicItem(void* item) { graphicItem= item; }
157  void* getGraphicItem() { return graphicItem; }
158 
159 private:
160  void* graphicItem;
161 };
162 
164 {
165 public:
166  OwnedVertex(const yarp::os::Property &prop) : yarp::profiler::graph::GraphicVertex(prop)
167  {
168  owner = nullptr;
169  }
171  if (_owner)
172  {
173  owner = _owner;
174  return true;
175  }
176  return false;
177  }
179 private:
181 
182 };
183 
185 {
186 public:
187  PortVertex(const std::string name) : yarp::profiler::graph::OwnedVertex("(type port)") {
188  property.put("name", name);
189  }
190  virtual ~PortVertex(){}
191 
192  bool operator == (const yarp::profiler::graph::Vertex &v1) const override {
193  return property.find("name").asString() == v1.property.find("name").asString();
194  }
195 
196 };
197 
199 {
200 public:
201  ProcessVertex(int pid, const std::string hostname) : yarp::profiler::graph::OwnedVertex("(type process)") {
202  property.put("hostname", hostname);
203  property.put("pid", pid);
204  }
205  virtual ~ProcessVertex(){}
206 
207  bool operator == (const yarp::profiler::graph::Vertex &v1) const override {
208  return property.find("hostname").asString() == v1.property.find("hostname").asString() &&
209  property.find("pid").asInt32() == v1.property.find("pid").asInt32();
210  }
211 
212 };
213 
215 {
216 public:
217  MachineVertex(std::string os, const std::string hostname) : yarp::profiler::graph::GraphicVertex("(type machine)") {
218  property.put("hostname", hostname);
219  property.put("os", os);
220  }
221  virtual ~MachineVertex() {}
222 
223  bool operator == (const yarp::profiler::graph::Vertex &v1) const override {
224  return property.find("hostname").asString() == v1.property.find("hostname").asString() &&
225  property.find("os").asString() == v1.property.find("os").asString() &&
226  property.find("type").asString() == v1.property.find("type").asString() ;
227  }
228 };
229 
230 
231 #endif // YARP_PROFILER_GRAP_H
graph_subset::const_iterator graph_subset_const_iterator
Definition: Graph.h:43
std::vector< pvertex_set > graph_subset
Definition: Graph.h:41
edge_set::iterator edge_iterator
Definition: Graph.h:34
edge_set::const_iterator edge_const_iterator
Definition: Graph.h:35
pvertex_set::const_iterator pvertex_const_iterator
Definition: Graph.h:39
graph_subset::iterator graph_subset_iterator
Definition: Graph.h:42
std::vector< yarp::profiler::graph::Vertex * > pvertex_set
Definition: Graph.h:37
pvertex_set::iterator pvertex_iterator
Definition: Graph.h:38
std::vector< yarp::profiler::graph::Edge > edge_set
Definition: Graph.h:33
A class for storing options and configuration information.
Definition: Property.h:34
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Property.cpp:1051
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:204
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
static bool calcSCC(yarp::profiler::graph::Graph &graph, graph_subset &scc)
calcSCC
Definition: Graph.cpp:249
The yarp::profiler::graph::Edge class.
Definition: Graph.h:49
virtual bool operator==(const yarp::profiler::graph::Edge &edge) const
Definition: Graph.cpp:49
const yarp::profiler::graph::Vertex & second() const
Definition: Graph.cpp:44
Edge(const yarp::profiler::graph::Vertex &firstV, const yarp::profiler::graph::Vertex &secondV, yarp::os::Property property="")
yarp::profiler::graph::Edge
Definition: Graph.cpp:22
yarp::os::Property property
Definition: Graph.h:65
const yarp::profiler::graph::Vertex & first() const
Definition: Graph.cpp:40
The yarp::profiler::graph::Graph class.
Definition: Graph.h:108
void insertEdge(const Vertex &v1, const Vertex &v2, const yarp::os::Property &property="")
Definition: Graph.cpp:144
const pvertex_set & vertices()
Definition: Graph.h:130
const pvertex_iterator find(const Vertex &v1)
Definition: Graph.cpp:160
void remove(const Vertex &vertex)
Definition: Graph.cpp:131
Graph()
yarp::profiler::graph::Graph
pvertex_iterator insert(const Vertex &vertex)
Definition: Graph.cpp:120
GraphicVertex(const yarp::os::Property &prop)
Definition: Graph.h:153
void setGraphicItem(void *item)
Definition: Graph.h:156
MachineVertex(std::string os, const std::string hostname)
Definition: Graph.h:217
bool operator==(const yarp::profiler::graph::Vertex &v1) const override
Definition: Graph.h:223
yarp::profiler::graph::Vertex * getOwner()
Definition: Graph.h:178
OwnedVertex(const yarp::os::Property &prop)
Definition: Graph.h:166
bool setOwner(yarp::profiler::graph::Vertex *_owner)
Definition: Graph.h:170
PortVertex(const std::string name)
Definition: Graph.h:187
bool operator==(const yarp::profiler::graph::Vertex &v1) const override
Definition: Graph.h:192
bool operator==(const yarp::profiler::graph::Vertex &v1) const override
Definition: Graph.h:207
ProcessVertex(int pid, const std::string hostname)
Definition: Graph.h:201
The yarp::profiler::graph::Vertex class.
Definition: Graph.h:76
yarp::os::Property property
Definition: Graph.h:93
virtual bool operator<(const Vertex &v1) const
Definition: Graph.cpp:86
virtual bool operator==(const yarp::profiler::graph::Vertex &v1) const =0
const edge_set & outEdges() const
Definition: Graph.h:83
Vertex(const yarp::os::Property &prop)
yarp::profiler::graph::Vertex
Definition: Graph.cpp:59
Vertex(const yarp::profiler::graph::Vertex &vertex)
const edge_set & inEdges() const
Definition: Graph.h:84
size_t degree() const
Definition: Graph.h:85
The main, catch-all namespace for YARP.
Definition: dirs.h:16