YARP
Yet Another Robot Platform
kbase.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_MANAGER_KBASE
7 #define YARP_MANAGER_KBASE
8 
9 #include <cstring>
10 #include <algorithm>
11 
12 #include <yarp/manager/ymm-types.h>
13 #include <yarp/manager/graph.h>
15 #include <yarp/manager/resource.h>
18 
19 namespace yarp {
20 namespace manager {
21 
22 
23 #define NODELINK_SUPERFICIAL 1
24 #define NODELINK_DEEP 2
25 
26 typedef std::vector<Node*> NodePVector;
27 typedef std::vector<Node*>::iterator NodePVIterator;
28 
29 
34 
35 public:
37  modloader(nullptr),
38  apploader(nullptr),
39  resloader(nullptr),
40  mainApplication(nullptr)
41  {}
42 
43  virtual ~KnowledgeBase() {
44  kbGraph.clear();
45  tmpGraph.clear();
46  }
47 
48  bool createFrom(ModuleLoader* _mloader,
49  AppLoader* _apploader,
50  ResourceLoader* _resloader);
51  bool addApplication(Application* application,
52  char **szAppName_=nullptr,
53  bool modifyName=false);
54  bool addModule(Module* module);
55  bool addResource(GenericResource* resource);
56  bool removeApplication(Application* application);
57  bool removeModule(Module* module);
58  bool removeResource(GenericResource* resource);
59 
60  bool reasolveDependency(const char* szName,
61  bool bAutoDependancy=false, bool bSilent=false);
63  bool bAutoDependancy=false, bool bSilent=false);
64  bool checkConsistency();
65 
66  Node* getNode(std::string appName);
67 
68  const ModulePContainer& getSelModules() { return selmodules; }
69  const CnnContainer& getSelConnection() { return selconnections; }
70  const ResourcePContainer& getSelResources() { return selresources; }
71  const ApplicaitonPContainer& getSelApplications() { return selapplications; }
72 
73  const ApplicaitonPContainer& getApplications(Application* parent=nullptr);
74  const ModulePContainer& getModules(Application* parent=nullptr);
75  const ResourcePContainer& getResources(Application* parent=nullptr);
76  const CnnContainer& getConnections(Application* parent=nullptr);
77  const ArbContainer& getArbitrators(Application* parent=nullptr);
78 
81 
82  Module* getModule(const char* szName) {
83  return dynamic_cast<Module*>(kbGraph.getNode(szName));
84  }
85  Application* getApplication() { return mainApplication; }
86 
87  Application* getApplication(const char* szName) {
88  return dynamic_cast<Application*>(kbGraph.getNode(szName));
89  }
90  GenericResource* getResource(const char* szName) {
91  return dynamic_cast<GenericResource*>(kbGraph.getNode(szName));
92  }
93 
95  ModuleInterface &mod, bool isNew=false);
96  bool removeIModuleFromApplication(Application* application, const char* szTag);
100  Connection& prev, Connection& con );
102  ApplicationInterface &app, bool isNew=false);
103  bool removeIApplicationFromApplication(Application* application, const char* szTag);
104 
106  bool removeArbitratorFromApplication(Application* application, Arbitrator &arb);
107 
108  bool setModulePrefix(Module* module, const char* szPrefix, bool updateBasePrefix=true);
109  bool setApplicationPrefix(Application* app, const char* szPrefix, bool updateBasePref=true);
110  bool saveApplication(AppSaver* appSaver, Application* application);
111 
112  const std::string getUniqueAppID(Application* parent, const char* szAppName);
113 
114  bool exportAppGraph(const char* szFileName) {
115  return exportDotGraph(tmpGraph, szFileName); }
116 
117  bool exportKbGraph(const char* szFileName) {
118  return exportDotGraph(kbGraph, szFileName);
119  }
120 
121 
122 protected:
123 
124 private:
125  Graph kbGraph;
126  Graph tmpGraph;
127  ModuleLoader* modloader;
128  AppLoader* apploader;
129  ResourceLoader* resloader;
130  Application* mainApplication;
131 
132  ApplicaitonPContainer dummyApplications;
133  ModulePContainer dummyModules;
134  ResourcePContainer dummyResources;
135  CnnContainer dummyConnections;
136  ArbContainer dummyArbitrators;
137 
138  ApplicaitonPContainer selapplications;
139  ModulePContainer selmodules;
140  CnnContainer selconnections;
141  ResourcePContainer selresources;
142 
143  std::map<std::string, int> appList;
144 
145  bool moduleCompleteness(Module* module);
146  void updateNodesLink(Graph& graph, int level);
147  void makeResourceLinks(Graph& graph);
148  void updateResourceWeight(Graph& graph,
149  GenericResource* resource, float weight);
150 
151  //void updateExtraLink(Graph& graph, CnnContainer* connections);
152  void linkToOutputs(Graph& graph, InputData* input);
153  int getProducerRank(Graph& graph, OutputData* output);
154  const char* createDataLabel(const char* modlabel, const char* port,
155  const char* postfix=nullptr);
156  const char* createAppLabel(Application* app);
157  Module* replicateModule(Graph& graph,
158  Module* module, const char* szLabel);
159  Application* replicateApplication(Graph& graph,
160  Application* app, const char* szLabel);
161  GenericResource* replicateResource(Graph& graph,
162  GenericResource* res, const char* szLabel);
163 
164  Module* addModuleToGraph(Graph& graph, Module* module);
165  bool removeModuleFromGraph(Graph& graph, Module* mod);
166 
167  bool updateModule(Module* module, ModuleInterface* imod);
168  bool updateApplication(Application* app, ApplicationInterface* iapp);
169  bool reason(Graph* graph, Node* initial,
170  ApplicaitonPContainer &applications,
171  ModulePContainer &modules,
172  ResourcePContainer& resources,
173  CnnContainer &connections,
174  bool bAutoDependancy, bool bSilent);
175 
176  bool constrainSatisfied(Node* node,
177  bool bAutoDependancy, bool bSilent);
178  bool makeupApplication(Application* application);
179  bool isExternalResource(Graph& graph, const char* szName);
180  float calculateLoad(Computer* comp);
181 
182  OutputData* findOutputByPort(Graph& graph, const char* szPort);
183  InputData* findInputByPort(Graph& graph, const char* szPort);
184  GenericResource* findResByName(Graph& graph, const char* szName);
185  Module* findOwner(Graph& graph, InputData* input);
186 
187 };
188 
189 
190 
192 {
193 public:
195  {
196  std::string strFirst((*f).getName());
197  std::string strSecond((*s).getName());
198  transform(strFirst.begin(), strFirst.end(), strFirst.begin(),
199  (int(*)(int))toupper);
200  transform(strSecond.begin(), strSecond.end(), strSecond.begin(),
201  (int(*)(int))toupper);
202  return (strFirst.compare(strSecond) < 0);
203  }
204 };
205 
206 
208 {
209 public:
210  bool operator()(Module *f, Module *s)
211  {
212  std::string strFirst((*f).getName());
213  std::string strSecond((*s).getName());
214  transform(strFirst.begin(), strFirst.end(), strFirst.begin(),
215  (int(*)(int))toupper);
216  transform(strSecond.begin(), strSecond.end(), strSecond.begin(),
217  (int(*)(int))toupper);
218  return (strFirst.compare(strSecond) < 0);
219  }
220 };
221 
222 
224 {
225 public:
227  {
228  std::string strFirst(f->getName());
229  std::string strSecond(s->getName());
230  transform(strFirst.begin(), strFirst.end(), strFirst.begin(),
231  (int(*)(int))toupper);
232  transform(strSecond.begin(), strSecond.end(), strSecond.begin(),
233  (int(*)(int))toupper);
234  return (strFirst.compare(strSecond) < 0);
235  }
236 };
237 
238 } // namespace yarp
239 } // namespace manager
240 
241 
242 #endif // __YARP_MANAGER_KBASE__
static RFModule * module
Definition: RFModule.cpp:231
Abstract Class AppLoader.
Class ApplicationInterface.
Definition: application.h:253
Class Application.
Definition: application.h:289
Class port Arbitrator.
Definition: arbitrator.h:25
Class Connection.
Definition: application.h:57
Class Graph.
Definition: graph.h:28
Node * getNode(const char *szLabel)
Definition: graph.cpp:82
Class InputData.
Definition: data.h:22
Class KnowledgeBase.
Definition: kbase.h:33
Application * addIApplicationToApplication(Application *application, ApplicationInterface &app, bool isNew=false)
Definition: kbase.cpp:661
GenericResource * getResource(const char *szName)
Definition: kbase.h:90
const ArbContainer & getArbitrators(Application *parent=nullptr)
Definition: kbase.cpp:305
const OutputContainer & getOutputCandidates(InputData *input)
Definition: kbase.cpp:331
bool removeModule(Module *module)
Definition: kbase.cpp:177
const ModulePContainer & getModules(Application *parent=nullptr)
Definition: kbase.cpp:217
bool removeIModuleFromApplication(Application *application, const char *szTag)
Definition: kbase.cpp:779
Application * getApplication(const char *szName)
Definition: kbase.h:87
const InputContainer & getInputCandidates(OutputData *output)
Definition: kbase.cpp:318
bool updateConnectionOfApplication(Application *application, Connection &prev, Connection &con)
Definition: kbase.cpp:598
bool setModulePrefix(Module *module, const char *szPrefix, bool updateBasePrefix=true)
Definition: kbase.cpp:467
bool removeResource(GenericResource *resource)
Definition: kbase.cpp:182
bool removeConnectionFromApplication(Application *application, Connection &cnn)
Definition: kbase.cpp:634
Arbitrator & addArbitratorToApplication(Application *application, Arbitrator &arb)
Definition: kbase.cpp:580
Module * getModule(const char *szName)
Definition: kbase.h:82
bool reasolveDependency(const char *szName, bool bAutoDependancy=false, bool bSilent=false)
Definition: kbase.cpp:894
bool exportAppGraph(const char *szFileName)
Definition: kbase.h:114
bool addResource(GenericResource *resource)
Definition: kbase.cpp:144
Connection & addConnectionToApplication(Application *application, Connection &cnn)
Definition: kbase.cpp:562
Node * getNode(std::string appName)
Definition: kbase.cpp:1664
Module * addIModuleToApplication(Application *application, ModuleInterface &mod, bool isNew=false)
Definition: kbase.cpp:719
bool removeIApplicationFromApplication(Application *application, const char *szTag)
Definition: kbase.cpp:800
const ResourcePContainer & getSelResources()
Definition: kbase.h:70
bool addModule(Module *module)
Definition: kbase.cpp:120
const ApplicaitonPContainer & getApplications(Application *parent=nullptr)
Definition: kbase.cpp:188
bool removeApplication(Application *application)
Definition: kbase.cpp:172
Application * getApplication()
Definition: kbase.h:85
bool createFrom(ModuleLoader *_mloader, AppLoader *_apploader, ResourceLoader *_resloader)
Definition: kbase.cpp:19
const CnnContainer & getConnections(Application *parent=nullptr)
Definition: kbase.cpp:246
const std::string getUniqueAppID(Application *parent, const char *szAppName)
Definition: kbase.cpp:650
const ApplicaitonPContainer & getSelApplications()
Definition: kbase.h:71
const ResourcePContainer & getResources(Application *parent=nullptr)
Definition: kbase.cpp:261
bool removeArbitratorFromApplication(Application *application, Arbitrator &arb)
Definition: kbase.cpp:626
const CnnContainer & getSelConnection()
Definition: kbase.h:69
bool setApplicationPrefix(Application *app, const char *szPrefix, bool updateBasePref=true)
Definition: kbase.cpp:507
virtual ~KnowledgeBase()
Definition: kbase.h:43
bool exportKbGraph(const char *szFileName)
Definition: kbase.h:117
bool addApplication(Application *application, char **szAppName_=nullptr, bool modifyName=false)
Definition: kbase.cpp:76
const ModulePContainer & getSelModules()
Definition: kbase.h:68
bool saveApplication(AppSaver *appSaver, Application *application)
Definition: kbase.cpp:1304
Class ModuleInterface.
Definition: application.h:161
Abstract Class ModuleLoader.
Class Module.
Definition: module.h:100
a Node of a Graph
Definition: node.h:65
Abstract Class ResourceLoader.
bool operator()(Application *f, Application *s)
Definition: kbase.h:194
bool operator()(Module *f, Module *s)
Definition: kbase.h:210
bool operator()(GenericResource *f, GenericResource *s)
Definition: kbase.h:226
std::vector< InputData > InputContainer
Definition: module.h:86
std::vector< Node * > NodePVector
Definition: kbase.h:26
std::vector< Arbitrator > ArbContainer
Definition: arbitrator.h:91
std::vector< Node * >::iterator NodePVIterator
Definition: kbase.h:27
std::vector< OutputData > OutputContainer
Definition: module.h:87
std::vector< Module * > ModulePContainer
Definition: module.h:231
std::vector< GenericResource * > ResourcePContainer
Definition: resource.h:59
std::vector< Application * > ApplicaitonPContainer
Definition: application.h:406
std::vector< Connection > CnnContainer
Definition: application.h:150
bool exportDotGraph(Graph &graph, const char *szFileName)
Definition: utility.cpp:347
The main, catch-all namespace for YARP.
Definition: dirs.h:16