Evoplex  0.2.1
abstractgraph.h
1 /* Evoplex <https://evoplex.org>
2  * Copyright (C) 2016-present - Marcos Cardinot <marcos@cardinot.net>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ABSTRACT_GRAPH_H
18 #define ABSTRACT_GRAPH_H
19 
20 #include <QtDebug>
21 #include <QMutex>
22 
23 #include "abstractplugin.h"
24 #include "attrsgenerator.h"
25 #include "edges.h"
26 #include "enum.h"
27 #include "nodes.h"
28 
29 namespace evoplex {
30 
36 {
37 public:
39  virtual ~AbstractGraphInterface() = default;
40 
42 
48  virtual bool reset() = 0;
49 };
50 
55 {
56  friend class Trial;
57 
58 public:
61 
65  const QString& id() const;
66 
70  GraphType type() const;
71 
75  inline bool isDirected() const;
76 
80  inline bool isUndirected() const;
81 
85  inline const Edges& edges() const;
86 
92  inline const Edge& edge(int edgeId) const;
93 
100  inline const Edge& edge(int originId, int neighbourId) const;
101 
105  inline const Nodes& nodes() const;
106 
112  inline Node node(int nodeId) const;
113 
118  Node randNode() const;
119 
123  inline int numNodes() const;
124 
128  inline int numEdges() const;
129 
135  inline Node addNode(Attributes attr);
136 
141  Node addNode(Attributes attr, float x, float y);
142 
151  inline Edge addEdge(int originId, int neighbourId, Attributes* attrs=new Attributes());
152 
161  Edge addEdge(const Node& origin, const Node& neighbour, Attributes* attrs=new Attributes());
162 
166  void removeAllEdges();
167 
172  void removeAllEdges(const Node& node);
173 
178  void removeNode(const Node& node);
179 
187  Nodes::iterator removeNode(Nodes::iterator it);
188 
193  void removeEdge(const Edge& edge);
194 
202  Edges::iterator removeEdge(Edges::iterator it);
203 
206 protected:
207  AttrsGeneratorPtr m_edgeAttrsGen;
208  Edges m_edges;
209  Nodes m_nodes;
210 
212  AbstractGraph();
213 
214 private:
215  int m_lastNodeId;
216  int m_lastEdgeId;
217  QMutex m_mutex;
218 
219  std::uniform_int_distribution<int> m_numNodesDist;
220 
221  bool setup(Trial& trial, AttrsGeneratorPtr edgeGen,
222  const Attributes& attrs, Nodes& nodes);
223 };
224 
225 
226 /************************************************************************
227  AbstractGraph: Inline member functions
228  ************************************************************************/
229 
230 inline bool AbstractGraph::isDirected() const
231 { return type() == GraphType::Directed; }
232 
233 inline bool AbstractGraph::isUndirected() const
234 { return type() == GraphType::Undirected; }
235 
236 inline const Edges& AbstractGraph::edges() const
237 { return m_edges; }
238 
239 inline const Nodes& AbstractGraph::nodes() const
240 { return m_nodes; }
241 
242 inline const Edge& AbstractGraph::edge(int edgeId) const
243 { return m_edges.at(edgeId); }
244 
245 inline const Edge& AbstractGraph::edge(int originId, int neighbourId) const
246 { return m_nodes.at(originId).outEdges().at(neighbourId); }
247 
248 inline Node AbstractGraph::node(int nodeId) const
249 { return m_nodes.at(nodeId); }
250 
251 inline int AbstractGraph::numEdges() const
252 { return static_cast<int>(m_edges.size()); }
253 
254 inline int AbstractGraph::numNodes() const
255 { return static_cast<int>(m_nodes.size()); }
256 
258 { return addNode(attr, 0, m_lastNodeId+1); }
259 
260 inline Edge AbstractGraph::addEdge(int originId, int neighbourId, Attributes* attrs)
261 { return addEdge(m_nodes.at(originId), m_nodes.at(neighbourId), attrs); }
262 
263 } // evoplex
264 #endif // ABSTRACT_GRAPH_H
AbstractGraph()
constructor
Node addNode(Attributes attr)
Creates a Node with attrs and adds it into the graph.
Definition: abstractgraph.h:257
const Edges & edges() const
Gets the edges.
Definition: abstractgraph.h:236
It wraps a std::shared_ptr<BaseNode>.
Definition: node.h:35
int numEdges() const
Gets the number of edges in the graph.
Definition: abstractgraph.h:251
Node randNode() const
Gets a random Node in the graph.
virtual ~AbstractGraphInterface()=default
Provide a default destructor to keep compilers happy.
Edge addEdge(int originId, int neighbourId, Attributes *attrs=new Attributes())
Creates and adds an Edge into the graph.
Definition: abstractgraph.h:260
const Attributes * attrs() const
Gets the plugin&#39;s attributes.
Definition: abstractplugin.h:121
void removeAllEdges()
Removes all edges of the graph.
Node node(int nodeId) const
Gets the Node corresponding to nodeId.
Definition: abstractgraph.h:248
const QString & id() const
Gets the graph id.
A Node container.
Definition: nodes.h:32
virtual bool reset()=0
Resets the graph object to the original state.
An Edge connects a Node to itself or to another Node.
Definition: edge.h:37
GraphType type() const
Gets the graph type.
const Value & attr(int attrId) const
Gets the attribute&#39;s value for attrId.
Definition: abstractplugin.h:127
const Edge & edge(int edgeId) const
Returns the Edge corresponding to edgeId.
Definition: abstractgraph.h:242
bool isDirected() const
Returns true if the graph is directed.
Definition: abstractgraph.h:230
Provides a common interface for Graph plugins.
Definition: abstractgraph.h:35
int numNodes() const
Gets the number of nodes in the graph.
Definition: abstractgraph.h:254
Base class for plugins.
Definition: abstractplugin.h:34
void removeNode(const Node &node)
Removes the node from the graph.
const Nodes & nodes() const
Gets the nodes.
Definition: abstractgraph.h:239
void removeEdge(const Edge &edge)
Removes the edge from the graph.
Definition: abstractgraph.h:29
A container of labeled values.
Definition: attributes.h:39
Abstract base class for graph plugins.
Definition: abstractgraph.h:54
virtual bool init()
Initializes the plugin.
bool isUndirected() const
Returns true if the graph is undirected.
Definition: abstractgraph.h:233
An Edge container.
Definition: edges.h:32