Skip to content

Commit

Permalink
Remove TS links from solver : extract link names from study.
Browse files Browse the repository at this point in the history
  • Loading branch information
guilpier-code committed Jun 11, 2024
1 parent 8c5ba5c commit daaef31
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/libs/antares/study/area/links.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ bool AreaLinksInternalLoadFromProperty(AreaLink& link, const String& key, const
}
} // anonymous namespace

bool AreaLinksLoadFromFolder(Study& study, AreaList* l, Area* area, const fs::path& folder, bool loadTSGen)
bool AreaLinksLoadFromFolder(Study& study, AreaList* areaList, Area* area, const fs::path& folder, bool loadTSGen)
{
// Assert
assert(area);
Expand All @@ -594,16 +594,16 @@ bool AreaLinksLoadFromFolder(Study& study, AreaList* l, Area* area, const fs::pa
for (auto* s = ini.firstSection; s; s = s->next)
{
// Getting the name of the area
std::string buffer = transformNameIntoID(s->name);
std::string targetAreaName = transformNameIntoID(s->name);

// Trying to find it
Area* linkedWith = AreaListLFind(l, buffer.c_str());
if (!linkedWith)
Area* targetArea = AreaListLFind(areaList, targetAreaName.c_str());
if (!targetArea)
{
logs.error() << '`' << s->name << "`: Impossible to find the area";
continue;
}
AreaLink* lnk = AreaAddLinkBetweenAreas(area, linkedWith);
AreaLink* lnk = AreaAddLinkBetweenAreas(area, targetArea);
if (!lnk)
{
logs.error() << "Impossible to create a link between two areas";
Expand Down
90 changes: 89 additions & 1 deletion src/tools/ts-generator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ using namespace Antares;

namespace fs = std::filesystem;

using LinkPair = std::pair<std::string, std::string>;
using LinkPairs = std::vector<LinkPair>;

struct Settings
{
std::string studyFolder;
Expand Down Expand Up @@ -133,6 +136,84 @@ TSGenerator::listOfLinks getLinksToGen(Data::AreaList& areas, const std::string&
return links;
}

std::vector<std::string> extractTargetAreas(fs::path sourceLinkDir)
{
std::vector<std::string> to_return;
fs::path pathToIni = sourceLinkDir / "properties.ini";
IniFile ini;
ini.open(pathToIni);
for (auto* s = ini.firstSection; s; s = s->next)
{
std::string targetAreaName = transformNameIntoID(s->name);
to_return.push_back(targetAreaName);
}
return to_return;
}

LinkPairs extractLinksFromStudy(fs::path studyDir)
{
LinkPairs to_return;
fs::path linksDir = studyDir / "input" / "links";
for (auto const& item : fs::directory_iterator{linksDir})
{
if (item.is_directory())
{
std::string sourceAreaName = item.path().filename().generic_string();
auto targetAreas = extractTargetAreas(item);
for (auto& targetAreaName : targetAreas)
{
auto linkPair = std::make_pair(sourceAreaName, targetAreaName);
to_return.push_back(linkPair);
}
}
}
return to_return;
}

bool pairs_match(const LinkPair& p1, const LinkPair& p2)
{
return (p1.first == p2.first && p1.second == p2.second)
|| (p1.first == p2.second && p1.second == p2.first);
}

const LinkPair* getMatchingPairInCollection(const LinkPair& pair, const LinkPairs& collection)
{
for(const auto& p : collection)
{
if (pairs_match(pair, p))
return &p;
}
return nullptr;
}

LinkPairs extractLinksFromCmdLine(const LinkPairs& allLinks,
const std::string linksFromCmdLine)
{
LinkPairs to_return;
LinkPairs pairsFromCmdLine = splitStringIntoPairs(linksFromCmdLine, ';', '.');
for (auto& p : pairsFromCmdLine)
{
if (const auto* found_pair = getMatchingPairInCollection(p, allLinks); found_pair)
{
to_return.push_back(*found_pair);
}
else
{
logs.error() << "Link '" << p.first << "." << p.second << "' not found";
}
}
return to_return;
}

void logLinks(std::string title, LinkPairs& links)
{
std::cout << title << " : " << std::endl;
for (auto& link : links)
{
std::cout << "+ " << link.first << "." << link.second << std::endl;
}
}

int main(int argc, char* argv[])
{
logs.applicationName("ts-generator");
Expand Down Expand Up @@ -216,6 +297,13 @@ int main(int argc, char* argv[])
}

// LINKS

auto allLinksPairs = extractLinksFromStudy(settings.studyFolder);
logLinks("All links", allLinksPairs);

auto linksFromCmdLine = extractLinksFromCmdLine(allLinksPairs, settings.linksListToGen);
logLinks("Links from cmd line", linksFromCmdLine);

TSGenerator::listOfLinks links;
if (settings.allLinks)
{
Expand All @@ -239,4 +327,4 @@ int main(int argc, char* argv[])
&& ret;

return !ret; // return 0 for success
}
}

0 comments on commit daaef31

Please sign in to comment.