Skip to content

Commit

Permalink
Simplify districts : remove all trace of template in class Sets, not …
Browse files Browse the repository at this point in the history
…needed.
  • Loading branch information
guilpier-code committed Jul 23, 2024
1 parent 3b2e9c6 commit faf1d72
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 230 deletions.
3 changes: 1 addition & 2 deletions src/libs/antares/study/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ set(SRC_STUDY

# Sets
include/antares/study/sets.h
include/antares/study/sets.hxx
area/sets.cpp
area/sets.cpp

# variable selection
include/antares/study/variable-print-info.h
Expand Down
178 changes: 172 additions & 6 deletions src/libs/antares/study/area/sets.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@

/*
** Copyright 2007-2024, RTE (https://www.rte-france.com)
** See AUTHORS.txt
** SPDX-License-Identifier: MPL-2.0
** This file is part of Antares-Simulator,
** Adequacy and Performance assessment for interconnected energy networks.
**
** Antares_Simulator is free software: you can redistribute it and/or modify
** it under the terms of the Mozilla Public Licence 2.0 as published by
** the Mozilla Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** Antares_Simulator is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** Mozilla Public Licence 2.0 for more details.
**
** You should have received a copy of the Mozilla Public Licence 2.0
** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/
#include "antares/study/sets.h"

namespace Antares::Data
Expand Down Expand Up @@ -53,6 +72,24 @@ const Sets::SetAreasType& Sets::operator[](uint i) const
return *(pByIndex[i]);
}

bool Sets::hasOutput(const Yuni::ShortString128& s) const
{
typename MapOptions::const_iterator i = pOptions.find(s);
return (i != pOptions.end()) ? i->second.output : false;
}

uint Sets::resultSize(const Yuni::ShortString128& s) const
{
typename MapOptions::const_iterator i = pOptions.find(s);
return (i != pOptions.end()) ? i->second.resultSize : 0;
}

Sets::IDType Sets::caption(const Yuni::ShortString128& s) const
{
typename MapOptions::const_iterator i = pOptions.find(s);
return (i != pOptions.end()) ? i->second.caption : IDType();
}

void Sets::defaultForAreas()
{
using namespace Yuni;
Expand All @@ -66,6 +103,128 @@ void Sets::defaultForAreas()
add("all areas", item, opts);
}

void Sets::rebuildAllFromRules(SetHandlerAreas& handler)
{
for (uint i = 0; i != pMap.size(); ++i)
{
rebuildFromRules(pNameByIndex[i], handler);
}
}

void Sets::rebuildFromRules(const IDType& id, SetHandlerAreas& handler)
{
using namespace Yuni;
using namespace Antares;

typename MapOptions::iterator i = pOptions.find(id);
if (i == pOptions.end())
{
return;
}
// Options
Options& opts = i->second;
auto& set = *(pMap[id]);

// Clear the result first
handler.clear(set);
// Apply all rules
for (uint i = 0; i != opts.rules.size(); ++i)
{
const Rule& rule = opts.rules[i];
const Yuni::String& arg = *(rule.second);
switch (rule.first) // type
{
case ruleAdd:
{
// Trying to add a single item
if (!handler.add(set, arg))
{
// Failed. Maybe the argument references another group
const IDType other = arg;
typename MapType::iterator i = pMap.find(other);
if (i != pMap.end())
{
if (handler.add(set, *(i->second)))
{
break;
}
}
}
break;
}
case ruleRemove:
{
// Trying to remove a single item
if (!handler.remove(set, arg))
{
// Failed. Maybe the argument references another group
const IDType other = arg;
typename MapType::iterator i = pMap.find(other);
if (i != pMap.end())
{
if (handler.remove(set, *(i->second)))
{
break;
}
}
}
break;
}
case ruleFilter:
{
handler.applyFilter(set, arg);
break;
}
case ruleNone:
case ruleMax:
{
// Huh ??
assert(false && "Should not be here !");
break;
}
}
}
// Retrieving the size of the result set
opts.resultSize = handler.size(set);
logs.debug() << " > set :: " << opts.caption << ": applying " << opts.rules.size()
<< " rules, got " << opts.resultSize << " items";
}

bool Sets::saveToFile(const Yuni::String &filename) const
{
Yuni::IO::File::Stream file;
if (!file.open(filename, Yuni::IO::OpenMode::write | Yuni::IO::OpenMode::truncate))
{
logs.error() << "I/O Error: " << filename << ": impossible to write the file";
return false;
}

static const char* cmds[ruleMax] = {"none", "+", "-", "apply-filter"};
const auto end = pOptions.cend();
for (auto i = pOptions.cbegin(); i != end; ++i)
{
const Options& opts = i->second;
file << '[' << i->first << "]\n";
file << "caption = " << opts.caption << '\n';
if (not opts.comments.empty())
{
file << "comments = " << opts.comments << '\n';
}
if (!opts.output)
{
file << "output = false\n";
}

for (uint r = 0; r != opts.rules.size(); ++r)
{
const Rule& rule = opts.rules[r];
file << cmds[rule.first] << " = " << rule.second << '\n';
}
file << '\n';
}
return true;
}

YString Sets::toString()
{
using namespace Yuni;
Expand Down Expand Up @@ -214,8 +373,6 @@ void Sets::rebuildIndexes()
}
}



bool Sets::hasOutput(const uint index) const
{
return hasOutput(IDType(pNameByIndex[index]));
Expand All @@ -231,12 +388,23 @@ uint Sets::resultSize(const uint index) const
return resultSize(IDType(pNameByIndex[index]));
}

void Sets::dumpToLogs() const
{
using namespace Yuni;
const typename MapType::const_iterator end = pMap.end();
for (typename MapType::const_iterator i = pMap.begin(); i != end; ++i)
{
logs.info() << " found `" << i->first << "` (" << (uint)i->second->size() << ' '
<< (i->second->size() < 2 ? "item" : "items")
<< ((!hasOutput(i->first)) ? ", no output" : "") << ')';
}
}

uint Sets::size() const
{
return (uint)pMap.size();
}


SetHandlerAreas::SetHandlerAreas(AreaList& areas):
areas_(areas)
{
Expand Down Expand Up @@ -320,6 +488,4 @@ bool SetHandlerAreas::applyFilter(Sets::SetAreasType& set, const Yuni::String& v
return false;
}



} // namespace Antares::Data
32 changes: 10 additions & 22 deletions src/libs/antares/study/include/antares/study/sets.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
** You should have received a copy of the Mozilla Public Licence 2.0
** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/
#ifndef __ANTARES_LIBS_STUDY_SETS_H__
#define __ANTARES_LIBS_STUDY_SETS_H__
#pragma once

#include <cassert>
#include <map>
Expand All @@ -35,12 +34,13 @@

namespace Antares::Data
{
class SetHandlerAreas;

class Sets final
{
public:
//
using IDType = Yuni::CString<128, false>;
using IDType = Yuni::ShortString128;

//! A single set of areas
// CompareAreaName : to control the order of areas in a set of areas. This order can have an
Expand Down Expand Up @@ -199,8 +199,7 @@ class Sets final
/*!
** \brief Get if the results for a given group should be written to the output
*/
template<class StringT>
bool hasOutput(const StringT& s) const;
bool hasOutput(const Yuni::ShortString128& s) const;

/*!
** \brief Get if the results for a given group should be written to the output
Expand All @@ -210,24 +209,21 @@ class Sets final
/*!
** \brief Get the size of a result set
*/
template<class StringT>
uint resultSize(const StringT& s) const;
uint resultSize(const Yuni::ShortString128& s) const;

/*!
** \brief Get the size of a result set
*/
uint resultSize(const uint index) const;

template<class L>
void dumpToLogs(L& log) const;
void dumpToLogs() const;

/*!
** \brief Load a rule set from an INI File
*/
bool loadFromFile(const std::filesystem::path& filename);

template<class StringT>
bool saveToFile(const StringT& filename) const;
bool saveToFile(const Yuni::String& filename) const;
/*!
** \brief format the string to match the options
*/
Expand All @@ -241,18 +237,15 @@ class Sets final
/*!
** \brief Rebuild the lists of all group from the rules
*/
template<class HandlerT>
void rebuildAllFromRules(HandlerT& handler);
void rebuildAllFromRules(SetHandlerAreas& handler);

const IDType& nameByIndex(const uint i) const
{
assert(i < pMap.size() && "Sets: operator[] index out of bounds");
return pNameByIndex[i];
}

template<class StringT>
IDType caption(const StringT& s) const;

IDType caption(const Yuni::ShortString128& s) const;
IDType caption(const uint i) const;

SetAreasType& operator[](uint i);
Expand All @@ -262,9 +255,7 @@ class Sets final
/*!
** \brief Rebuild the lists of a group from the rules
*/
template<class HandlerT>
void rebuildFromRules(const IDType& id, HandlerT& handler);

void rebuildFromRules(const IDType& id, SetHandlerAreas& handler);
void rebuildIndexes();

//! All groups
Expand Down Expand Up @@ -295,6 +286,3 @@ class SetHandlerAreas

} // namespace Antares::Data

#include "sets.hxx"

#endif // __ANTARES_LIBS_STUDY_SETS_H__
Loading

0 comments on commit faf1d72

Please sign in to comment.