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