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