ctopology.h Source File¶

API: ctopology.h Source File
API
ctopology.h
1//==========================================================================
2// CTOPOLOGY.H - part of
3// OMNeT++/OMNEST
4// Discrete System Simulation in C++
5//
6//==========================================================================
7
8/*--------------------------------------------------------------*
9 Copyright (C) 1992-2017 Andras Varga
10 Copyright (C) 2006-2017 OpenSim Ltd.
11
12 This file is distributed WITHOUT ANY WARRANTY. See the file
13 `license' for details on this and other legal matters.
14*--------------------------------------------------------------*/
15
16#ifndef __OMNETPP_CTOPOLOGY_H
17#define __OMNETPP_CTOPOLOGY_H
18
19#include <string>
20#include <vector>
21#include <functional>
22#include "cownedobject.h"
23#include "csimulation.h"
24#include "cmodule.h"
25#include "cgate.h"
26
27namespace omnetpp {
28
29class cPar;
30
31// not all compilers define INFINITY (gcc does)
32#ifndef INFINITY
33#define INFINITY HUGE_VAL
34#endif
35
55class SIM_API cTopology : public cOwnedObject
56{
57 public:
58 class Link;
59 class LinkIn;
60 class LinkOut;
61
65 class SIM_API Node
66 {
67 friend class cTopology;
68
69 protected:
70 int moduleId;
71 double weight;
72 bool enabled;
73 std::vector<Link*> inLinks;
74 std::vector<Link*> outLinks;
75
76 // variables used by the shortest-path algorithms
77 double dist;
78 Link *outPath;
79
80 public:
84 Node(int moduleId=-1) {this->moduleId=moduleId; weight=0; enabled=true; dist=INFINITY; outPath=nullptr;}
85 virtual ~Node() {}
86
89
93 int getModuleId() const {return moduleId;}
94
98 cModule *getModule() const {return getSimulation()->getModule(moduleId);}
99
104 double getWeight() const {return weight;}
105
110 void setWeight(double d) {weight=d;}
111
116 bool isEnabled() const {return enabled;}
117
122 void enable() {enabled=true;}
123
128 void disable() {enabled=false;}
130
133
137 int getNumInLinks() const {return inLinks.size();}
138
143
147 int getNumOutLinks() const {return outLinks.size();}
148
154
157
161 double getDistanceToTarget() const {return dist;}
162
167 int getNumPaths() const {return outPath?1:0;}
168
174 LinkOut *getPath(int) const {return (LinkOut *)outPath;}
176 };
177
178
182 class SIM_API Link
183 {
184 friend class cTopology;
185
186 protected:
187 Node *srcNode;
188 int srcGateId;
189 Node *destNode;
190 int destGateId;
191 double weight;
192 bool enabled;
193
194 public:
198 Link(double weight=1) {srcNode=destNode=nullptr; srcGateId=destGateId=-1; this->weight=weight; enabled=true;}
199 virtual ~Link() {}
200
205 double getWeight() const {return weight;}
206
211 void setWeight(double d) {weight=d;}
212
217 bool isEnabled() const {return enabled;}
218
223 void enable() {enabled=true;}
224
229 void disable() {enabled=false;}
230 };
231
232
241 class SIM_API LinkIn : public Link
242 {
243 private:
244 LinkIn(double weight=1) : Link(weight) {}
245
246 public:
250 Node *getRemoteNode() const {return srcNode;}
251
255 Node *getLocalNode() const {return destNode;}
256
260 int getRemoteGateId() const {return srcGateId;}
261
265 int getLocalGateId() const {return destGateId;}
266
270 cGate *getRemoteGate() const {return srcNode->getModule()->gate(srcGateId);}
271
275 cGate *getLocalGate() const {return destNode->getModule()->gate(destGateId);}
276 };
277
278
287 class SIM_API LinkOut : public Link
288 {
289 private:
290 LinkOut(double weight=1) : Link(weight) {}
291
292 public:
296 Node *getRemoteNode() const {return destNode;}
297
301 Node *getLocalNode() const {return srcNode;}
302
306 int getRemoteGateId() const {return destGateId;}
307
311 int getLocalGateId() const {return srcGateId;}
312
316 cGate *getRemoteGate() const {return destNode->getModule()->gate(destGateId);}
317
321 cGate *getLocalGate() const {return srcNode->getModule()->gate(srcGateId);}
322 };
323
330 class SIM_API Predicate
331 {
332 public:
333 virtual ~Predicate() {}
334 virtual bool matches(cModule *module) = 0;
335 };
336
337 protected:
338 std::vector<Node*> nodes;
339 Node *target = nullptr;
340
341 // note: the purpose of the (unsigned int) cast is that nodes with moduleId==-1 are inserted at the end of the vector
342 static bool lessByModuleId(Node *a, Node *b) { return (unsigned int)a->moduleId < (unsigned int)b->moduleId; }
343 static bool isModuleIdLess(Node *a, int moduleId) { return (unsigned int)a->moduleId < (unsigned int)moduleId; }
344
345 void unlinkFromSourceNode(Link *link);
346 void unlinkFromDestNode(Link *link);
347
348 private:
349 virtual void parsimPack(cCommBuffer *) const override {throw cRuntimeError(this, E_CANTPACK);}
350 virtual void parsimUnpack(cCommBuffer *) override {throw cRuntimeError(this, E_CANTPACK);}
351
352 public:
355
359 explicit cTopology(const char *name=nullptr);
360
364 cTopology(const cTopology& topo);
365
369 virtual ~cTopology();
370
376
379
384 virtual cTopology *dup() const override {return new cTopology(*this);}
385
390 virtual std::string str() const override;
392
401
408 virtual void extractFromNetwork(bool (*selfunc)(cModule *,void *), void *userdata=nullptr);
409
413 virtual void extractFromNetwork(Predicate *predicate);
414
418 virtual void extractFromNetwork(std::function<bool(cModule *)> predicate);
419
429 virtual void extractByModulePath(const std::vector<std::string>& fullPathPatterns);
430
441 virtual void extractByNedTypeName(const std::vector<std::string>& nedTypeNames);
442
461 virtual void extractByProperty(const char *propertyName, const char *value=nullptr);
462
469 virtual void extractByParameter(const char *paramName, const char *paramValue=nullptr);
470
474 virtual void clear();
476
480
484 virtual int addNode(Node *node);
485
490 virtual void deleteNode(Node *node);
491
497 virtual void addLink(Link *link, Node *srcNode, Node *destNode);
498
506 virtual void addLink(Link *link, cGate *srcGate, cGate *destGate);
507
512 virtual void deleteLink(Link *link);
514
515
522
526 virtual int getNumNodes() const {return nodes.size();}
527
532 virtual Node *getNode(int i) const;
533
541 virtual Node *getNodeFor(cModule *mod) const;
543
545 /*
546 * To be implemented:
547 * - void unweightedMultiShortestPathsTo(Node *target);
548 * - void weightedMultiShortestPathsTo(Node *target);
549 */
551
557
564
569 virtual Node *getTargetNode() const {return target;}
571
572 protected:
576 virtual Node *createNode(cModule *module) { return new Node(module->getId()); }
577
581 virtual Link *createLink() { return new Link(); }
582};
583
584} // namespace omnetpp
585
586#endif
int getId() const
Definition ccomponent.h:435
Represents a module gate.
Definition cgate.h:63
This class represents modules in the simulation.
Definition cmodule.h:49
Represents a module or channel parameter.
Definition cpar.h:71
Supporting class for cTopology.
Definition ctopology.h:242
cGate * getLocalGate() const
Definition ctopology.h:275
int getRemoteGateId() const
Definition ctopology.h:260
int getLocalGateId() const
Definition ctopology.h:265
Node * getLocalNode() const
Definition ctopology.h:255
Node * getRemoteNode() const
Definition ctopology.h:250
cGate * getRemoteGate() const
Definition ctopology.h:270
Supporting class for cTopology.
Definition ctopology.h:288
cGate * getLocalGate() const
Definition ctopology.h:321
int getRemoteGateId() const
Definition ctopology.h:306
int getLocalGateId() const
Definition ctopology.h:311
Node * getLocalNode() const
Definition ctopology.h:301
Node * getRemoteNode() const
Definition ctopology.h:296
cGate * getRemoteGate() const
Definition ctopology.h:316
Supporting class for cTopology, represents a node in the graph.
Definition ctopology.h:66
double getWeight() const
Definition ctopology.h:104
void setWeight(double d)
Definition ctopology.h:110
Node(int moduleId=-1)
Definition ctopology.h:84
void enable()
Definition ctopology.h:122
cModule * getModule() const
Definition ctopology.h:98
double getDistanceToTarget() const
Definition ctopology.h:161
int getNumOutLinks() const
Definition ctopology.h:147
LinkOut * getLinkOut(int i)
void disable()
Definition ctopology.h:128
LinkIn * getLinkIn(int i)
bool isEnabled() const
Definition ctopology.h:116
int getNumPaths() const
Definition ctopology.h:167
LinkOut * getPath(int) const
Definition ctopology.h:174
int getModuleId() const
Definition ctopology.h:93
int getNumInLinks() const
Definition ctopology.h:137
Base class for selector objects used in extract...() methods of cTopology.
Definition ctopology.h:331
virtual void deleteNode(Node *node)
virtual Node * getTargetNode() const
Definition ctopology.h:569
virtual void addLink(Link *link, Node *srcNode, Node *destNode)
virtual void extractFromNetwork(bool(*selfunc)(cModule *, void *), void *userdata=nullptr)
virtual int getNumNodes() const
Definition ctopology.h:526
virtual cTopology * dup() const override
Definition ctopology.h:384
virtual void extractByParameter(const char *paramName, const char *paramValue=nullptr)
virtual void extractByProperty(const char *propertyName, const char *value=nullptr)
virtual ~cTopology()
virtual void extractFromNetwork(std::function< bool(cModule *)> predicate)
cTopology(const cTopology &topo)
virtual Node * getNode(int i) const
virtual Node * createNode(cModule *module)
Definition ctopology.h:576
virtual void calculateWeightedSingleShortestPathsTo(Node *target)
cTopology & operator=(const cTopology &topo)
virtual Link * createLink()
Definition ctopology.h:581
virtual void calculateUnweightedSingleShortestPathsTo(Node *target)
virtual void clear()
virtual void extractByNedTypeName(const std::vector< std::string > &nedTypeNames)
virtual void extractFromNetwork(Predicate *predicate)
virtual int addNode(Node *node)
virtual void extractByModulePath(const std::vector< std::string > &fullPathPatterns)
virtual std::string str() const override
cTopology(const char *name=nullptr)
virtual Node * getNodeFor(cModule *mod) const
virtual void deleteLink(Link *link)
virtual void addLink(Link *link, cGate *srcGate, cGate *destGate)
cSimulation * getSimulation()
Returns the currently active simulation, or nullptr if there is none.
Definition csimulation.h:623