From fd4b07f29e6a313511755030c82bde64a60c7b4e Mon Sep 17 00:00:00 2001 From: Alexandr Smirnov Date: Sat, 5 Aug 2023 18:10:25 +0300 Subject: [PATCH] Implement infrastructure for IND algorithms Co-authored-by: Chizhov Anton --- src/core/algorithms/ind/ind.cpp | 25 ++++++++++++ src/core/algorithms/ind/ind.h | 46 +++++++++++++++++++++ src/core/algorithms/ind/ind_algorithm.cpp | 14 +++++++ src/core/algorithms/ind/ind_algorithm.h | 50 +++++++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 src/core/algorithms/ind/ind.cpp create mode 100644 src/core/algorithms/ind/ind.h create mode 100644 src/core/algorithms/ind/ind_algorithm.cpp create mode 100644 src/core/algorithms/ind/ind_algorithm.h diff --git a/src/core/algorithms/ind/ind.cpp b/src/core/algorithms/ind/ind.cpp new file mode 100644 index 0000000000..d4ec00e794 --- /dev/null +++ b/src/core/algorithms/ind/ind.cpp @@ -0,0 +1,25 @@ +#include "ind.h" + +#include + +namespace model { + +std::string CCToString(IND::ColumnCombination const& cc) { + std::vector const& col_ids = cc.GetColumnIndices(); + std::stringstream ss; + for (size_t i = 0; i != col_ids.size(); ++i) { + if (i != 0) { + ss << ", "; + } + ss << cc.GetTableIndex() << '.' << col_ids[i]; + } + return ss.str(); +} + +std::string IND::ToString() const { + std::stringstream ss; + ss << CCToString(GetLhs()) << " -> " << CCToString(GetRhs()); + return ss.str(); +} + +} // namespace model diff --git a/src/core/algorithms/ind/ind.h b/src/core/algorithms/ind/ind.h new file mode 100644 index 0000000000..de41bfaee8 --- /dev/null +++ b/src/core/algorithms/ind/ind.h @@ -0,0 +1,46 @@ +#pragma once + +#include +#include +#include + +namespace model { + +using AttributeIndex = unsigned int; + +class IND { +public: + class ColumnCombination { + protected: + size_t table_index_; + std::vector column_indices_; + + public: + ColumnCombination(size_t table_index, std::vector col_indices) + : table_index_(table_index), column_indices_(std::move(col_indices)) {} + + size_t GetTableIndex() const { + return table_index_; + } + std::vector const& GetColumnIndices() const { + return column_indices_; + } + }; + +private: + std::shared_ptr lhs, rhs; + +public: + IND(std::shared_ptr lhs, std::shared_ptr rhs) + : lhs(std::move(lhs)), rhs(std::move(rhs)) {} + + ColumnCombination const& GetLhs() const { + return *lhs; + } + ColumnCombination const& GetRhs() const { + return *rhs; + } + std::string ToString() const; +}; + +} // namespace model diff --git a/src/core/algorithms/ind/ind_algorithm.cpp b/src/core/algorithms/ind/ind_algorithm.cpp new file mode 100644 index 0000000000..4dc4245986 --- /dev/null +++ b/src/core/algorithms/ind/ind_algorithm.cpp @@ -0,0 +1,14 @@ +#include "ind_algorithm.h" + +#include "config/names_and_descriptions.h" +#include "config/tabular_data/input_tables/option.h" + +namespace algos { + +INDAlgorithm::INDAlgorithm(std::vector phase_names) + : Algorithm(std::move(phase_names)) { + RegisterOption(config::TablesOpt(&input_tables_)); + MakeOptionsAvailable({config::TablesOpt.GetName()}); +} + +} // namespace algos diff --git a/src/core/algorithms/ind/ind_algorithm.h b/src/core/algorithms/ind/ind_algorithm.h new file mode 100644 index 0000000000..419d6a65fb --- /dev/null +++ b/src/core/algorithms/ind/ind_algorithm.h @@ -0,0 +1,50 @@ +#pragma once + +#include +#include +#include +#include + +#include "algorithms/algorithm.h" +#include "ind.h" +#include "tabular_data/input_tables_type.h" +#include "util/primitive_collection.h" + +namespace algos { + +class INDAlgorithm : public Algorithm { +public: + using IND = model::IND; + +private: + util::PrimitiveCollection ind_collection_; + + void ResetState() final { + ind_collection_.Clear(); + ResetINDAlgorithmState(); + } + + virtual void ResetINDAlgorithmState() = 0; + +protected: + constexpr static std::string_view kDefaultPhaseName = "IND mining"; + + config::InputTables input_tables_; + + explicit INDAlgorithm(std::vector phase_names); + + virtual void RegisterIND(std::shared_ptr lhs, + std::shared_ptr rhs) { + ind_collection_.Register(std::move(lhs), std::move(rhs)); + } + virtual void RegisterIND(IND ind) { + ind_collection_.Register(std::move(ind)); + } + +public: + std::list const& IndList() const noexcept { + return ind_collection_.AsList(); + } +}; + +} // namespace algos