27 apploader = _apploader;
28 resloader = _resloader;
35 selconnections.clear();
37 selapplications.clear();
47 while((resource=resloader->getNextResource()))
48 addResource(resource);
58 while((
module=modloader->getNextModule()))
69 while((application = apploader->getNextApplication()))
70 addApplication(application);
77 bool KnowledgeBase::addApplication(
Application* app,
char** szAppName_,
bool modifyName)
81 static map<string, int> mapId;
83 if(kbGraph.hasNode(app))
85 if(mapId.find(
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';
109 if(!kbGraph.addNode(app))
112 msg<<
"Application "<<app->
getName()<<
" cannot be added to the graph.";
120 bool KnowledgeBase::addModule(
Module* mod)
126 if(kbGraph.hasNode(mod))
129 msg<<
"Module "<<mod->
getName()<<
" already exists.";
133 if(!addModuleToGraph(kbGraph, mod))
136 msg<<
"Module "<<mod->
getName()<<
" cannot be added to the graph.";
154 if(kbGraph.hasNode(res))
157 msg<<
"Resource "<<res->
getName()<<
" already exists.";
161 if(!kbGraph.addNode(res))
164 msg<<
"Resource "<<res->
getName()<<
" cannot be added to the graph.";
174 return kbGraph.removeNode(app);
177 bool KnowledgeBase::removeModule(
Module* mod)
179 return removeModuleFromGraph(kbGraph, mod);
184 return kbGraph.removeNode(res);
190 dummyApplications.clear();
193 for(
GraphIterator itr=tmpGraph.begin(); itr!=tmpGraph.end(); itr++)
196 if(app && (app->owner() == parent))
197 dummyApplications.push_back(app);
202 for(
GraphIterator itr=kbGraph.begin(); itr!=kbGraph.end(); itr++)
206 dummyApplications.push_back(app);
210 sort(dummyApplications.begin(), dummyApplications.end(),
sortApplication());
211 return dummyApplications;
217 dummyModules.clear();
220 for(
GraphIterator itr=tmpGraph.begin(); itr!=tmpGraph.end(); itr++)
222 auto* mod =
dynamic_cast<Module*
>(*itr);
223 if(mod && (mod->owner() == parent))
224 dummyModules.push_back(mod);
229 for(
GraphIterator itr=kbGraph.begin(); itr!=kbGraph.end(); itr++)
231 auto* mod =
dynamic_cast<Module*
>(*itr);
233 dummyModules.push_back(mod);
236 sort(dummyModules.begin(), dummyModules.end(),
sortModules());
244 dummyConnections.clear();
251 dummyConnections = selconnections;
253 return dummyConnections;
258 dummyResources.clear();
259 for(
GraphIterator itr=kbGraph.begin(); itr!=kbGraph.end(); itr++)
261 auto* res =
dynamic_cast<Computer*
>(*itr);
265 for(
auto& dummyResource : dummyResources)
266 if(
string(dummyResource->getName()) ==
string(res->getName()))
272 dummyResources.push_back(res);
276 for(
GraphIterator itr=tmpGraph.begin(); itr!=tmpGraph.end(); itr++)
278 auto* res =
dynamic_cast<Computer*
>(*itr);
282 for(
auto& dummyResource : dummyResources)
283 if(
string(dummyResource->getName()) ==
string(res->getName()))
289 dummyResources.push_back(res);
292 sort(dummyResources.begin(), dummyResources.end(),
sortResources());
293 return dummyResources;
298 dummyArbitrators.clear();
304 return dummyArbitrators;
312 for(
GraphIterator itr=kbGraph.begin(); itr!=kbGraph.end(); itr++)
313 if((*itr)->getType() ==
INPUTD &&
314 (*itr)->hasSuc(output))
324 for(
int i=0; i<input->
sucCount(); i++)
333 bool KnowledgeBase::makeupApplication(
Application* application)
344 if(
string(interfaceApp.
getName()) ==
string(application->
getName()))
347 msg<<
"Application "<<interfaceApp.
getName()<<
" cannot be called from itself.";
356 msg<<
"Application "<<interfaceApp.
getName()<<
" not found.";
362 if(appList.find(
string(interfaceApp.
getName()))==appList.end())
363 appList[interfaceApp.
getName()] = 1;
365 newname<<application->
getName()<<
":";
366 newname<<interfaceApp.
getName()<<
":"<<appList[interfaceApp.
getName()];
368 repapp = replicateApplication(tmpGraph, repapp, newname.str().c_str());
371 repapp->setBasePrefix(interfaceApp.
getPrefix());
376 string strPrefix = string(application->
getPrefix()) +
378 interfaceApp.
setPrefix(strPrefix.c_str());
382 updateApplication(repapp, &interfaceApp);
383 appList[interfaceApp.
getName()] = appList[interfaceApp.
getName()] + 1;
386 tmpGraph.addLink(application, repapp, 0,
false);
387 repapp->setOwner(application);
389 makeupApplication(repapp);
402 addIModuleToApplication(application, mod);
416 string strPort = string(application->
getPrefix()) + string(cnn->
from());
422 string strPort = string(application->
getPrefix()) + string(cnn->
to());
423 cnn->
setTo(strPort.c_str());
439 strLabel<<application->
getLabel()<<
":MultipleResource";
440 mres.
setLabel(strLabel.str().c_str());
441 mres.
setName(
"MultipleResource");
445 Node* node = tmpGraph.addNode(&mres);
446 tmpGraph.addLink(application, node, 0);
452 bool KnowledgeBase::setModulePrefix(
Module*
module,
const char* szPrefix,
bool updateBasePrefix)
457 module->setPrefix(szPrefix);
459 module->setBasePrefix(szPrefix);
491 bool KnowledgeBase::setApplicationPrefix(
Application* application,
const char* szPrefix,
bool updateBasePrefix)
503 for(
int i=0; i<application->
sucCount(); i++)
508 string strPrefix = string(szPrefix) + string(nestedApp->getBasePrefix());
509 setApplicationPrefix(nestedApp, strPrefix.c_str(),
false);
516 string strPrefix = string(szPrefix) + string(
module->getBasePrefix());
517 setModulePrefix(
module, strPrefix.c_str(),
false);
559 selconnections.push_back(cnn);
581 bool KnowledgeBase::updateConnectionOfApplication(
Application* application,
597 for(citr=selconnections.begin(); citr<selconnections.end(); citr++)
622 for(citr=selconnections.begin(); citr<selconnections.end(); citr++)
626 selconnections.erase(citr);
633 const std::string KnowledgeBase::getUniqueAppID(
Application* parent,
const char* szAppName)
635 if(appList.find(
string(szAppName)) == appList.end())
636 appList[szAppName] = 1;
638 newname<<parent->
getName()<<
":";
639 newname<<szAppName<<
":"<<appList[szAppName];
640 return newname.str();
651 if(
string(interfaceApp.
getName()) ==
string(application->
getName()))
654 msg<<
"Application "<<interfaceApp.
getName()<<
" cannot be called from itself.";
663 msg<<
"Application "<<interfaceApp.
getName()<<
" not found.";
668 if(appList.find(
string(interfaceApp.
getName()))==appList.end())
669 appList[interfaceApp.
getName()] = 1;
671 newname<<application->
getName()<<
":";
672 newname<<interfaceApp.
getName()<<
":"<<appList[interfaceApp.
getName()];
673 repapp = replicateApplication(tmpGraph, repapp, newname.str().c_str());
680 string strPrefix = string(application->
getPrefix()) +
682 interfaceApp.
setPrefix(strPrefix.c_str());
686 updateApplication(repapp, &interfaceApp);
687 appList[interfaceApp.
getName()] = appList[interfaceApp.
getName()] + 1;
690 tmpGraph.addLink(application, repapp, 0,
false);
693 makeupApplication(repapp);
712 auto* repmod =
dynamic_cast<Module*
>(kbGraph.getNode(mod.
getName()));
714 module = replicateModule(tmpGraph, repmod, newname.str().c_str());
718 newmod.
setLabel(newname.str().c_str());
719 module = addModuleToGraph(tmpGraph, &newmod);
722 mod.
setTag(newname.str().c_str());
730 string strPrefix = string(application->
getPrefix()) +
736 updateModule(
module, &mod);
749 tmpGraph.addLink(application,
module, 0,
false);
750 module->setOwner(application);
759 bool KnowledgeBase::removeIModuleFromApplication(
Application* application,
const char* szModTag)
763 auto*
module =
dynamic_cast<Module*
>(tmpGraph.getNode(szModTag));
765 removeModuleFromGraph(tmpGraph,
module);
779 bool KnowledgeBase::removeIApplicationFromApplication(
Application* application,
const char* szAppTag)
782 auto* app =
dynamic_cast<Application*
>(tmpGraph.getNode(szAppTag));
787 for(
GraphIterator itr=tmpGraph.begin(); itr!=tmpGraph.end(); itr++)
789 auto* nestedApp =
dynamic_cast<Application*
>(*itr);
790 if(nestedApp && (nestedApp->owner() == app))
791 removeIApplicationFromApplication(app, nestedApp->getLabel());
794 for(
GraphIterator itr=tmpGraph.begin(); itr!=tmpGraph.end(); itr++)
796 auto* mod =
dynamic_cast<Module*
>(*itr);
797 if(mod && (mod->owner() == app))
798 removeModuleFromGraph(tmpGraph, mod);
802 if(res && (res->owner() == app))
804 tmpGraph.removeLink(app, res);
805 tmpGraph.removeNode(res);
819 tmpGraph.removeLink(application, app);
820 tmpGraph.removeNode(app);
832 if(
string(res->getName()) ==
string(szName))
839 InputData* KnowledgeBase::findInputByPort(
Graph& graph,
const char* szPort)
843 if((*itr)->getType() ==
INPUTD )
854 OutputData* KnowledgeBase::findOutputByPort(
Graph& graph,
const char* szPort)
858 if((*itr)->getType() ==
OUTPUTD)
868 bool KnowledgeBase::reasolveDependency(
const char* szAppName,
869 bool bAutoDependancy,
bool bSilent)
872 auto* app =
dynamic_cast<Application*
>(kbGraph.getNode(szAppName));
876 msg<<
"Application "<<string(szAppName)<<
" not found.";
877 logger->
addError(msg.str().c_str());
878 mainApplication =
nullptr;
882 return reasolveDependency(app, bAutoDependancy, bSilent);
886 bool bAutoDependancy,
bool bSilent)
890 if(!kbGraph.hasNode(app))
893 msg<<
"Application "<<app->
getName()<<
" not found.";
894 logger->
addError(msg.str().c_str());
895 mainApplication =
nullptr;
901 mainApplication = replicateApplication(tmpGraph,
908 if(!makeupApplication(mainApplication))
910 mainApplication =
nullptr;
916 for(
GraphIterator itr=kbGraph.begin(); itr!=kbGraph.end(); itr++)
921 replicateResource(tmpGraph,
927 makeResourceLinks(tmpGraph);
933 tmpGraph.setSatisfied(
false);
934 bool ret = reason(&tmpGraph, mainApplication,
935 applications, modules, resources, connections,
936 bAutoDependancy, bSilent);
939 selconnections.clear();
941 selapplications.clear();
942 selresources.clear();
944 for(
auto& resource : resources)
952 if(yres && (find(selresources.begin(), selresources.end(), yres)
953 == selresources.end()))
954 selresources.push_back(yres);
974 if(find(selresources.begin(), selresources.end(), resource)
975 == selresources.end())
976 selresources.push_back(resource);
980 for(
auto&
module : modules)
982 if(find(selmodules.begin(), selmodules.end(),
module)
984 selmodules.push_back(
module);
987 for(
auto& application : applications)
989 if(find(selapplications.begin(), selapplications.end(), application)
990 == selapplications.end())
991 selapplications.push_back(application);
995 selconnections = connections;
1085 bool KnowledgeBase::updateApplication(
Application* app,
1136 Module* KnowledgeBase::replicateModule(
Graph& graph,
1141 newmod->setLabel(szLabel);
1142 newmod->setBasePrefix(
module->getPrefix());
1143 newmod->removeAllSuc();
1144 if(!addModuleToGraph(graph, newmod))
1164 newapp->setBasePrefix(app->
getPrefix());
1165 newapp->removeAllSuc();
1182 newres->removeAllSuc();
1194 if(!moduleCompleteness(
module))
1202 for(
int i=0; i<
module->inputCount(); i++)
1213 input = &(
module->getInputAt(i));
1214 module->removeInput(*input);
1217 msg<<createDataLabel(
module->getLabel(),
1219 msg<<
" already exists.";
1225 for(
int i=0; i<
module->outputCount(); i++)
1235 output = &(
module->getOutputAt(i));
1236 module->removeOutput(*output);
1239 msg<<createDataLabel(
module->getLabel(),
1241 msg<<
" already exists.";
1251 strLabel<<
module->getLabel()<<
":MultipleResource";
1252 mres.
setLabel(strLabel.str().c_str());
1253 mres.
setName(
"MultipleResource");
1255 for(
int i=0; i<
module->resourceCount(); i++)
1267 for(
GraphIterator itr=tmpGraph.begin(); itr!=tmpGraph.end(); itr++)
1270 if(embApp && (embApp != application) && (embApp->owner() == application))
1273 iapp.
setPrefix(embApp->getBasePrefix());
1274 if(embApp->getModel())
1284 for(
GraphIterator itr=tmpGraph.begin(); itr!=tmpGraph.end(); itr++)
1311 return appSaver->
save(application);
1314 bool KnowledgeBase::removeModuleFromGraph(
Graph& graph,
Module* mod)
1320 while(itr!=graph.
end())
1322 auto* input =
dynamic_cast<InputData*
>(*itr);
1323 if(input && (input->
owner() == mod))
1330 auto* output =
dynamic_cast<OutputData*
>(*itr);
1331 if(output && (output->
owner() == mod))
1339 if(res && (res->
owner() == mod))
1361 if(strlen(
module->getName()) == 0)
1368 for(
int i=0; i<
module->inputCount(); i++)
1370 const char* szType =
module->getInputAt(i).getName();
1371 const char* szPort =
module->getInputAt(i).getPort();
1373 logger->
addWarning(
string(
module->getName()) +
string(
" has an input with no type."));
1375 logger->
addWarning(
string(
module->getName()) +
string(
" has an input with no port."));
1379 for(
int i=0; i<
module->outputCount(); i++)
1381 const char* szType =
module->getOutputAt(i).getName();
1382 const char* szPort =
module->getOutputAt(i).getPort();
1384 logger->
addWarning(
string(
module->getName()) +
string(
" has an output with no type."));
1386 logger->
addWarning(
string(
module->getName()) +
string(
" has an output with no port."));
1396 if((*itr)->getType() ==
MODULE)
1399 for(
int i=0; i<
module->sucCount(); i++)
1438 void KnowledgeBase::updateNodesLink(
Graph& graph,
int level)
1445 if((*itr)->getType() ==
INPUTD)
1457 graph.
addLink(input, res, 0.0,
false);
1465 linkToOutputs(graph, input);
1470 void KnowledgeBase::updateResourceWeight(
Graph& graph,
1475 for(
int i=0; i<(*itr)->sucCount(); i++)
1476 if((*itr)->getLinkAt(i).to() == resource)
1477 (*itr)->getLinkAt(i).setWeight(weight);
1482 void KnowledgeBase::makeResourceLinks(
Graph& graph)
1490 if(resource && resource->
owner())
1507 if(
dynamic_cast<Computer*
>(provider))
1508 w = calculateLoad((
Computer*)provider);
1509 graph.
addLink(resource, provider, w,
false);
1517 if(provider && !provider->
owner())
1518 if(provider->
satisfy(resource))
1521 if(
dynamic_cast<Computer*
>(provider))
1522 w = calculateLoad((
Computer*)provider);
1523 graph.
addLink(resource, provider, w,
false);
1532 float KnowledgeBase::calculateLoad(
Computer* comp)
1536 if(siblings == 0) siblings = 1;
1542 return (lavg/(
float)siblings);
1546 void KnowledgeBase::linkToOutputs(
Graph& graph,
InputData* input)
1550 if((*itr)->getType() ==
OUTPUTD)
1555 &&(producer != findOwner(graph, input)))
1557 (
float)getProducerRank(graph, output),
1572 return module->getRank();
1578 bool KnowledgeBase::checkConsistency()
1607 Node* KnowledgeBase::getNode(
string appName)
1609 return kbGraph.getNode(appName.c_str());
1612 bool KnowledgeBase::constrainSatisfied(
Node* node,
1613 bool bAutoDependancy,
1620 if(resource && resource->
isLeaf() &&
1626 msg<<
"Some resource dependencies of ";
1627 msg<<dynamic_cast<Module*>(resource->
owner())->
getName();
1628 msg<<
" are not satisfied.";
1663 bool KnowledgeBase::reason(
Graph* graph,
Node* initial,
1668 bool bAutoDependancy,
bool bSilent)
1670 if(!constrainSatisfied(initial, bAutoDependancy, bSilent))
1674 if(resource && resource->
owner())
1675 resources.push_back(resource);
1686 auto* application =
dynamic_cast<Application*
>(initial);
1689 applications.push_back(application);
1700 bool bPathFound =
false;
1702 Link* candidateLink =
nullptr;
1703 float weight = -1.0;
1705 for(
int i=0; i<initial->
sucCount(); i++)
1719 bool ret = reason(graph, current,
1720 subApplications, subModules, subResources, subConnections,
1721 bAutoDependancy, bSilent);
1725 dynamic_cast<Module*
>(initial))
1732 dynamic_cast<Module*
>(initial))
1734 applications.insert(applications.end(), subApplications.begin(), subApplications.end());
1735 modules.insert(modules.end(), subModules.begin(), subModules.end());
1736 resources.insert(resources.end(), subResources.begin(), subResources.end());
1737 connections.insert(connections.end(), subConnections.begin(), subConnections.end());
1741 else if((weight<0.0) || (l.
weight() < weight))
1745 modules = subModules;
1746 applications = subApplications;
1747 resources = subResources;
1748 connections = subConnections;
1756 if(resource && resource->
owner() && candidateLink)
1768 auto* comp =
dynamic_cast<Computer*
>(provider);
1769 float default_tunning = 0.1F;
1772 float tunner = (
module->getRank()<10)? default_tunning : (
float)
module->getRank()/100.0F;
1773 updateResourceWeight(*graph, provider, candidateLink->
weight()+tunner);
1775 resources.push_back(resource);
1779 if(
dynamic_cast<Module*
>(initial))
1780 modules.push_back(
dynamic_cast<Module*
>(initial));
1783 auto* application =
dynamic_cast<Application*
>(initial);
1786 applications.push_back(
dynamic_cast<Application*
>(initial));
1796 const char* KnowledgeBase::createDataLabel(
const char* modlabel,
1798 const char* postfix)
1802 name = string(modlabel) + string(port);
1804 name += string(postfix);
1805 return name.c_str();