23 apploader = _apploader;
24 resloader = _resloader;
31 selconnections.clear();
33 selapplications.clear();
80 static std::map<std::string, int> mapId;
84 if (mapId.find(std::string(app->
getName())) == mapId.end()) {
94 app->
setName(newlable.str().c_str());
95 app->
setLabel(newlable.str().c_str());
101 delete [] *szAppName_;
102 *szAppName_ =
nullptr;
105 *szAppName_ =
new char[len + 1];
106 strncpy(*szAppName_, app->
getName(), len);
107 (*szAppName_)[len] =
'\0';
112 msg<<
"Application "<<app->
getName()<<
" cannot be added to the graph.";
129 msg<<
"Module "<<mod->
getName()<<
" already exists.";
133 if(!addModuleToGraph(kbGraph, mod))
136 msg<<
"Module "<<mod->
getName()<<
" cannot be added to the graph.";
157 msg<<
"Resource "<<res->
getName()<<
" already exists.";
164 msg<<
"Resource "<<res->
getName()<<
" cannot be added to the graph.";
179 return removeModuleFromGraph(kbGraph, mod);
190 dummyApplications.clear();
196 if (app && (app->owner() == parent)) {
197 dummyApplications.push_back(app);
207 dummyApplications.push_back(app);
212 sort(dummyApplications.begin(), dummyApplications.end(),
sortApplication());
213 return dummyApplications;
219 dummyModules.clear();
224 auto* mod =
dynamic_cast<Module*
>(*itr);
225 if (mod && (mod->owner() == parent)) {
226 dummyModules.push_back(mod);
234 auto* mod =
dynamic_cast<Module*
>(*itr);
236 dummyModules.push_back(mod);
240 sort(dummyModules.begin(), dummyModules.end(),
sortModules());
248 dummyConnections.clear();
255 dummyConnections = selconnections;
258 return dummyConnections;
263 dummyResources.clear();
266 auto* res =
dynamic_cast<Computer*
>(*itr);
270 for (
auto& dummyResource : dummyResources) {
271 if(std::string(dummyResource->getName()) == std::string(res->getName()))
278 dummyResources.push_back(res);
285 auto* res =
dynamic_cast<Computer*
>(*itr);
289 for (
auto& dummyResource : dummyResources) {
290 if(std::string(dummyResource->getName()) == std::string(res->getName()))
297 dummyResources.push_back(res);
301 sort(dummyResources.begin(), dummyResources.end(),
sortResources());
302 return dummyResources;
307 dummyArbitrators.clear();
314 return dummyArbitrators;
323 if ((*itr)->getType() ==
INPUTD && (*itr)->hasSuc(output)) {
335 for (
int i = 0; i < input->
sucCount(); i++) {
345bool KnowledgeBase::makeupApplication(
Application* application)
357 if(std::string(interfaceApp.
getName()) == std::string(application->
getName()))
360 msg<<
"Application "<<interfaceApp.
getName()<<
" cannot be called from itself.";
369 msg<<
"Application "<<interfaceApp.
getName()<<
" not found.";
375 if (appList.find(std::string(interfaceApp.
getName())) == appList.end()) {
376 appList[interfaceApp.
getName()] = 1;
379 newname<<application->
getName()<<
":";
380 newname<<interfaceApp.
getName()<<
":"<<appList[interfaceApp.
getName()];
382 repapp = replicateApplication(tmpGraph, repapp, newname.str().c_str());
385 repapp->setBasePrefix(interfaceApp.
getPrefix());
390 std::string strPrefix = std::string(application->
getPrefix()) +
392 interfaceApp.
setPrefix(strPrefix.c_str());
396 updateApplication(repapp, &interfaceApp);
397 appList[interfaceApp.
getName()] = appList[interfaceApp.
getName()] + 1;
400 tmpGraph.
addLink(application, repapp, 0,
false);
401 repapp->setOwner(application);
403 makeupApplication(repapp);
430 std::string strPort = std::string(application->
getPrefix()) + std::string(cnn->
from());
436 std::string strPort = std::string(application->
getPrefix()) + std::string(cnn->
to());
437 cnn->
setTo(strPort.c_str());
453 strLabel<<application->
getLabel()<<
":MultipleResource";
454 mres.
setLabel(strLabel.str().c_str());
455 mres.
setName(
"MultipleResource");
461 tmpGraph.
addLink(application, node, 0);
472 module->setPrefix(szPrefix);
473 if (updateBasePrefix) {
474 module->setBasePrefix(szPrefix);
513 if (updateBasePrefix) {
520 for(
int i=0; i<application->
sucCount(); i++)
525 std::string strPrefix = std::string(szPrefix) + std::string(nestedApp->getBasePrefix());
530 auto*
module = dynamic_cast<Module*>(application->getLinkAt(i).to());
533 std::string strPrefix = std::string(szPrefix) + std::string(module->getBasePrefix());
576 selconnections.push_back(cnn);
614 for(citr=selconnections.begin(); citr<selconnections.end(); citr++)
639 for(citr=selconnections.begin(); citr<selconnections.end(); citr++)
643 selconnections.erase(citr);
652 if (appList.find(std::string(szAppName)) == appList.end()) {
653 appList[szAppName] = 1;
656 newname<<parent->
getName()<<
":";
657 newname<<szAppName<<
":"<<appList[szAppName];
658 return newname.str();
669 if(std::string(interfaceApp.
getName()) == std::string(application->
getName()))
672 msg<<
"Application "<<interfaceApp.
getName()<<
" cannot be called from itself.";
681 msg<<
"Application "<<interfaceApp.
getName()<<
" not found.";
686 if (appList.find(std::string(interfaceApp.
getName())) == appList.end()) {
687 appList[interfaceApp.
getName()] = 1;
690 newname<<application->
getName()<<
":";
691 newname<<interfaceApp.
getName()<<
":"<<appList[interfaceApp.
getName()];
692 repapp = replicateApplication(tmpGraph, repapp, newname.str().c_str());
699 std::string strPrefix = std::string(application->
getPrefix()) +
701 interfaceApp.
setPrefix(strPrefix.c_str());
705 updateApplication(repapp, &interfaceApp);
706 appList[interfaceApp.
getName()] = appList[interfaceApp.
getName()] + 1;
709 tmpGraph.
addLink(application, repapp, 0,
false);
712 makeupApplication(repapp);
734 module = replicateModule(tmpGraph, repmod, newname.str().c_str());
737 newmod.
setLabel(newname.str().c_str());
738 module = addModuleToGraph(tmpGraph, &newmod);
741 mod.
setTag(newname.str().c_str());
744 module->setBasePrefix(mod.getPrefix());
749 std::string strPrefix = std::string(application->
getPrefix()) +
755 updateModule(module, &mod);
763 (*itr).setOwner(module);
768 tmpGraph.
addLink(application, module, 0,
false);
769 module->setOwner(application);
783 auto*
module = dynamic_cast<Module*>(tmpGraph.getNode(szModTag));
785 removeModuleFromGraph(tmpGraph, module);
811 auto* nestedApp =
dynamic_cast<Application*
>(*itr);
812 if (nestedApp && (nestedApp->owner() == app)) {
819 auto* mod =
dynamic_cast<Module*
>(*itr);
820 if (mod && (mod->owner() == app)) {
821 removeModuleFromGraph(tmpGraph, mod);
824 if(res && (res->owner() == app))
854 if (std::string(res->getName()) == std::string(szName)) {
863InputData* KnowledgeBase::findInputByPort(
Graph& graph,
const char* szPort)
867 if((*itr)->getType() ==
INPUTD )
879OutputData* KnowledgeBase::findOutputByPort(
Graph& graph,
const char* szPort)
883 if((*itr)->getType() ==
OUTPUTD)
895 bool bAutoDependancy,
bool bSilent)
902 msg<<
"Application "<<std::string(szAppName)<<
" not found.";
903 logger->
addError(msg.str().c_str());
904 mainApplication =
nullptr;
912 bool bAutoDependancy,
bool bSilent)
919 msg<<
"Application "<<app->
getName()<<
" not found.";
920 logger->
addError(msg.str().c_str());
921 mainApplication =
nullptr;
927 mainApplication = replicateApplication(tmpGraph,
934 if(!makeupApplication(mainApplication))
936 mainApplication =
nullptr;
947 replicateResource(tmpGraph,
955 makeResourceLinks(tmpGraph);
962 bool ret = reason(&tmpGraph, mainApplication,
963 applications, modules, resources, connections,
964 bAutoDependancy, bSilent);
967 selconnections.clear();
969 selapplications.clear();
970 selresources.clear();
972 for(
auto& resource : resources)
980 if (yres && (find(selresources.begin(), selresources.end(), yres) == selresources.end())) {
981 selresources.push_back(yres);
1002 if (find(selresources.begin(), selresources.end(), resource)
1003 == selresources.end()) {
1004 selresources.push_back(resource);
1009 for(
auto& module : modules)
1011 if (find(selmodules.begin(), selmodules.end(), module)
1012 == selmodules.end()) {
1013 selmodules.push_back(module);
1017 for(
auto& application : applications)
1019 if (find(selapplications.begin(), selapplications.end(), application)
1020 == selapplications.end()) {
1021 selapplications.push_back(application);
1026 selconnections = connections;
1116bool KnowledgeBase::updateApplication(
Application* app,
1136 module->setHost(imod->getHost());
1137 module->setForced(true);
1141 module->setParam(imod->getParam());
1144 module->setRank(imod->getRank());
1147 module->setBroker(imod->getBroker());
1150 module->setStdio(imod->getStdio());
1153 module->setWorkDir(imod->getWorkDir());
1156 module->setDisplay(imod->getDisplay());
1159 module->setEnvironment(imod->getEnvironment());
1161 module->setPostExecWait(imod->getPostExecWait());
1162 module->setPostStopWait(imod->getPostStopWait());
1163 module->setModelBase(imod->getModelBase());
1176Module* KnowledgeBase::replicateModule(
Graph& graph,
1177 Module* module,
const char* szLabel)
1182 newmod->setBasePrefix(module->
getPrefix());
1183 newmod->removeAllSuc();
1184 if(!addModuleToGraph(graph, newmod))
1204 newapp->setBasePrefix(app->
getPrefix());
1205 newapp->removeAllSuc();
1222 newres->removeAllSuc();
1234 if (!moduleCompleteness(module)) {
1244 for(
int i=0; i<
module->inputCount(); i++)
1246 InputData* input = &(
module->getInputAt(i));
1251 graph.
addLink(module, input, 0,
1254 input = &(
module->getInputAt(i));
1255 module->removeInput(*input);
1258 msg<<createDataLabel(module->
getLabel(),
1260 msg<<
" already exists.";
1266 for(
int i=0; i<
module->outputCount(); i++)
1268 OutputData* output = &(
module->getOutputAt(i));
1273 graph.
addLink(output, module, 0);
1275 output = &(
module->getOutputAt(i));
1276 module->removeOutput(*output);
1279 msg<<createDataLabel(module->
getLabel(),
1281 msg<<
" already exists.";
1291 strLabel<<
module->getLabel()<<":MultipleResource";
1292 mres.
setLabel(strLabel.str().c_str());
1293 mres.
setName(
"MultipleResource");
1295 for (
int i = 0; i <
module->resourceCount(); i++) {
1299 graph.
addLink(module, node, 0);
1311 if(embApp && (embApp != application) && (embApp->owner() == application))
1314 iapp.
setPrefix(embApp->getBasePrefix());
1315 if (embApp->getModel()) {
1328 auto*
module = dynamic_cast<Module*>(*itr);
1329 if(module && (module->
owner() == application))
1355 return appSaver->
save(application);
1358bool KnowledgeBase::removeModuleFromGraph(
Graph& graph,
Module* mod)
1364 while(itr!=graph.
end())
1366 auto* input =
dynamic_cast<InputData*
>(*itr);
1367 if(input && (input->
owner() == mod))
1374 auto* output =
dynamic_cast<OutputData*
>(*itr);
1375 if(output && (output->
owner() == mod))
1383 if(res && (res->
owner() == mod))
1400bool KnowledgeBase::moduleCompleteness(
Module* module)
1405 if(strlen(module->
getName()) == 0)
1412 for(
int i=0; i<
module->inputCount(); i++)
1414 const char* szType =
module->getInputAt(i).getName();
1415 const char* szPort =
module->getInputAt(i).getPort();
1416 if (!strlen(szType)) {
1417 logger->
addWarning(std::string(module->
getName()) + std::string(
" has an input with no type."));
1419 if (!strlen(szPort)) {
1420 logger->
addWarning(std::string(module->
getName()) + std::string(
" has an input with no port."));
1425 for(
int i=0; i<
module->outputCount(); i++)
1427 const char* szType =
module->getOutputAt(i).getName();
1428 const char* szPort =
module->getOutputAt(i).getPort();
1429 if (!strlen(szType)) {
1430 logger->
addWarning(std::string(module->
getName()) + std::string(
" has an output with no type."));
1432 if (!strlen(szPort)) {
1433 logger->
addWarning(std::string(module->
getName()) + std::string(
" has an output with no port."));
1444 if((*itr)->getType() ==
MODULE)
1446 auto*
module = (Module*)(*itr);
1447 for(
int i=0; i<
module->sucCount(); i++)
1449 Link l =
module->getLinkAt(i);
1487void KnowledgeBase::updateNodesLink(
Graph& graph,
int level)
1494 if((*itr)->getType() ==
INPUTD)
1506 graph.
addLink(input, res, 0.0,
false);
1515 linkToOutputs(graph, input);
1521void KnowledgeBase::updateResourceWeight(
Graph& graph,
1526 for (
int i = 0; i < (*itr)->sucCount(); i++) {
1527 if ((*itr)->getLinkAt(i).to() == resource) {
1528 (*itr)->getLinkAt(i).setWeight(weight);
1535void KnowledgeBase::makeResourceLinks(
Graph& graph)
1543 if(resource && resource->
owner())
1546 auto*
module = dynamic_cast<Module*>(resource->owner());
1560 if (
dynamic_cast<Computer*
>(provider)) {
1561 w = calculateLoad((
Computer*)provider);
1563 graph.
addLink(resource, provider, w,
false);
1565 else if((module && !module->
getForced()))
1571 if (provider && !provider->
owner()) {
1572 if(provider->
satisfy(resource))
1575 if (
dynamic_cast<Computer*
>(provider)) {
1576 w = calculateLoad((
Computer*)provider);
1578 graph.
addLink(resource, provider, w,
false);
1588float KnowledgeBase::calculateLoad(
Computer* comp)
1592 if (siblings == 0) {
1600 return (lavg/(
float)siblings);
1608 if((*itr)->getType() ==
OUTPUTD)
1613 && (producer != findOwner(graph, input))) {
1614 graph.
addLink(input, output, (
float)getProducerRank(graph, output), !input->
isRequired());
1628 auto*
module = (Module*)output->getLinkAt(0).to();
1629 return module->getRank();
1666 return kbGraph.
getNode(appName.c_str());
1669bool KnowledgeBase::constrainSatisfied(
Node* node,
1670 bool bAutoDependancy,
1677 if(resource && resource->
isLeaf() &&
1683 msg<<
"Some resource dependencies of ";
1684 msg<<dynamic_cast<Module*>(resource->
owner())->
getName();
1685 msg<<
" are not satisfied.";
1720bool KnowledgeBase::reason(
Graph* graph,
Node* initial,
1725 bool bAutoDependancy,
bool bSilent)
1727 if(!constrainSatisfied(initial, bAutoDependancy, bSilent))
1731 if (resource && resource->
owner()) {
1732 resources.push_back(resource);
1745 auto* application =
dynamic_cast<Application*
>(initial);
1748 applications.push_back(application);
1760 bool bPathFound =
false;
1762 Link* candidateLink =
nullptr;
1763 float weight = -1.0;
1765 for(
int i=0; i<initial->
sucCount(); i++)
1779 bool ret = reason(graph, current,
1780 subApplications, subModules, subResources, subConnections,
1781 bAutoDependancy, bSilent);
1784 }
else if (
dynamic_cast<Application*
>(initial) ||
dynamic_cast<Module*
>(initial)) {
1792 dynamic_cast<Module*
>(initial))
1794 applications.insert(applications.end(), subApplications.begin(), subApplications.end());
1795 modules.insert(modules.end(), subModules.begin(), subModules.end());
1796 resources.insert(resources.end(), subResources.begin(), subResources.end());
1797 connections.insert(connections.end(), subConnections.begin(), subConnections.end());
1801 else if((weight<0.0) || (l.
weight() < weight))
1805 modules = subModules;
1806 applications = subApplications;
1807 resources = subResources;
1808 connections = subConnections;
1816 if(resource && resource->
owner() && candidateLink)
1818 auto*
module = dynamic_cast<Module*>(resource->owner());
1820 if(module && provider)
1823 module->setHost(provider->getName());
1828 auto* comp =
dynamic_cast<Computer*
>(provider);
1829 float default_tunning = 0.1F;
1833 float tunner = (
module->getRank()<10)? default_tunning : (float)module->getRank()/100.0F;
1834 updateResourceWeight(*graph, provider, candidateLink->
weight()+tunner);
1836 resources.push_back(resource);
1840 if (
dynamic_cast<Module*
>(initial)) {
1841 modules.push_back(
dynamic_cast<Module*
>(initial));
1845 auto* application =
dynamic_cast<Application*
>(initial);
1848 applications.push_back(
dynamic_cast<Application*
>(initial));
1859const char* KnowledgeBase::createDataLabel(
const char* modlabel,
1861 const char* postfix)
1863 static std::string name;
1865 name = std::string(modlabel) + std::string(port);
1867 name += std::string(postfix);
1869 return name.c_str();
Abstract Class AppLoader.
virtual Application * getNextApplication()=0
virtual bool save(Application *application)=0
Class ApplicationInterface.
void setModelBase(GraphicModel &mdl)
void setPrefix(const char *szPrefix)
GraphicModel & getModelBase()
std::map< std::string, int > modList
void setName(const char *szName)
const char * getXmlFile()
Arbitrator & addArbitrator(Arbitrator &arb)
bool addIapplication(ApplicationInterface &iapp)
Arbitrator & getArbitratorAt(int index)
void setBasePrefix(const char *szPrefix)
void removeAllIapplications()
bool removeImodule(ModuleInterface &imod)
ModuleInterface & getImoduleAt(int index)
bool removeConnection(Connection &cnn)
void setModelBase(GraphicModel &mdl)
Connection & addConnection(Connection &cnn)
void setOwner(Node *owner)
bool removeArbitrator(Arbitrator &arb)
bool addResource(ResYarpPort &res)
ResYarpPort & getResourceAt(int index)
bool removeIapplication(ApplicationInterface &iapp)
bool addImodule(ModuleInterface &imod)
Connection & getConnectionAt(int index)
ApplicationInterface & getIapplicationAt(int index)
void setPrefix(const char *szPrefix)
void setOwner(Node *owner)
GraphicModel * getModel()
void setModelBase(GraphicModel &mdl)
Processor & getProcessor()
void setTo(const char *szTo)
void setOwner(Node *owner)
void setModelBase(GraphicModel &mdl)
void setFrom(const char *szFrom)
GraphicModel * getModel()
Singleton class ErrorLogger.
void addError(const char *szError)
void addWarning(const char *szWarning)
static ErrorLogger * Instance()
Singleton class ErrorLogger.
void setName(const char *szName)
virtual bool satisfy(GenericResource *resource)=0
void setOwner(Node *owner)
Node * addNode(Node *node)
bool removeNode(Node *node)
Node * getNode(const char *szLabel)
bool removeLink(Node *first, Node *second)
bool addLink(Node *first, Node *second, float weight, bool _virtual=false)
void setSatisfied(bool sat)
Application * addIApplicationToApplication(Application *application, ApplicationInterface &app, bool isNew=false)
const ArbContainer & getArbitrators(Application *parent=nullptr)
const OutputContainer & getOutputCandidates(InputData *input)
bool removeModule(Module *module)
const ModulePContainer & getModules(Application *parent=nullptr)
bool removeIModuleFromApplication(Application *application, const char *szTag)
const InputContainer & getInputCandidates(OutputData *output)
bool updateConnectionOfApplication(Application *application, Connection &prev, Connection &con)
bool setModulePrefix(Module *module, const char *szPrefix, bool updateBasePrefix=true)
bool removeResource(GenericResource *resource)
bool removeConnectionFromApplication(Application *application, Connection &cnn)
Arbitrator & addArbitratorToApplication(Application *application, Arbitrator &arb)
bool reasolveDependency(const char *szName, bool bAutoDependancy=false, bool bSilent=false)
bool addResource(GenericResource *resource)
Connection & addConnectionToApplication(Application *application, Connection &cnn)
Node * getNode(std::string appName)
Module * addIModuleToApplication(Application *application, ModuleInterface &mod, bool isNew=false)
bool removeIApplicationFromApplication(Application *application, const char *szTag)
bool addModule(Module *module)
const ApplicaitonPContainer & getApplications(Application *parent=nullptr)
bool removeApplication(Application *application)
bool createFrom(ModuleLoader *_mloader, AppLoader *_apploader, ResourceLoader *_resloader)
const CnnContainer & getConnections(Application *parent=nullptr)
const std::string getUniqueAppID(Application *parent, const char *szAppName)
const ResourcePContainer & getResources(Application *parent=nullptr)
bool removeArbitratorFromApplication(Application *application, Arbitrator &arb)
bool setApplicationPrefix(Application *app, const char *szPrefix, bool updateBasePref=true)
bool addApplication(Application *application, char **szAppName_=nullptr, bool modifyName=false)
bool saveApplication(AppSaver *appSaver, Application *application)
Link holding all the links of a node.
void setTag(const char *szTag)
void setPrefix(const char *szPrefix)
ResourceContainer & getResources()
const char * getWorkDir()
const char * getEnvironment()
const char * getDisplay()
Abstract Class ModuleLoader.
virtual Module * getNextModule()=0
const char * getBasePrefix()
GenericResource & getResourceAt(int index) const
bool addResource(GenericResource &res)
GenericResource & getResourceAt(int index) const
int resourceCount() const
Link & getLinkAt(int index)
void setSatisfied(bool sat)
void setLabel(const char *szLabel)
void setVisited(bool vis)
void setOwner(Node *owner)
const LoadAvg & getCPULoad()
Abstract Class ResourceLoader.
virtual GenericResource * getNextResource()=0
bool compareString(const char *szFirst, const char *szSecond)
std::vector< InputData > InputContainer
std::vector< Arbitrator > ArbContainer
std::vector< ResYarpPort >::iterator ResourceIterator
std::vector< OutputData > OutputContainer
std::vector< Connection >::iterator CnnIterator
std::vector< Module * > ModulePContainer
std::stringstream OSTRINGSTREAM
std::vector< GenericResource * > ResourcePContainer
std::vector< Application * > ApplicaitonPContainer
std::vector< Connection > CnnContainer
#define __CHECK_NULLPTR(_ptr)