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