Skip to content

Commit

Permalink
Implement infrastructure for IND algorithms
Browse files Browse the repository at this point in the history
Co-authored-by: Chizhov Anton <[email protected]>
  • Loading branch information
alexandrsmirn and vs9h committed Nov 25, 2023
1 parent 5e37f81 commit de1e33b
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/core/algorithms/ind/ind.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "ind.h"

#include <sstream>

namespace model {

std::string IND::ToString() const {
std::stringstream ss;
ss << GetLhs().ToString() << " -> " << GetRhs().ToString();
return ss.str();
}

} // namespace model
30 changes: 30 additions & 0 deletions src/core/algorithms/ind/ind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <memory>
#include <string>

#include "model/table/column_combination.h"

namespace model {

// Inclusion dependency is a relation between attributes of tables
// that indicates possible Primary Key–Foreign Key references.
class IND {
private:
std::shared_ptr<ColumnCombination> lhs_;
std::shared_ptr<ColumnCombination> rhs_;

public:
IND(std::shared_ptr<ColumnCombination> lhs, std::shared_ptr<ColumnCombination> 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
14 changes: 14 additions & 0 deletions src/core/algorithms/ind/ind_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -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<std::string_view> phase_names)
: Algorithm(std::move(phase_names)) {
RegisterOption(config::TablesOpt(&input_tables_));
MakeOptionsAvailable({config::TablesOpt.GetName()});
}

} // namespace algos
50 changes: 50 additions & 0 deletions src/core/algorithms/ind/ind_algorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <memory>
#include <string>
#include <string_view>
#include <vector>

#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> 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<std::string_view> phase_names);

virtual void RegisterIND(std::shared_ptr<model::ColumnCombination> lhs,
std::shared_ptr<model::ColumnCombination> rhs) {
ind_collection_.Register(std::move(lhs), std::move(rhs));
}
virtual void RegisterIND(IND ind) {
ind_collection_.Register(std::move(ind));
}

public:
std::list<IND> const& INDList() const noexcept {
return ind_collection_.AsList();
}
};

} // namespace algos
19 changes: 19 additions & 0 deletions src/core/model/table/column_combination.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "column_combination.h"

#include <sstream>

namespace model {

std::string ColumnCombination::ToString() const {
std::vector<ColumnIndex> const& col_ids = GetColumnIndices();
std::stringstream ss;
for (auto it = col_ids.begin(); it != col_ids.end(); ++it) {
if (it != col_ids.begin()) {
ss << ", ";
}
ss << GetTableIndex() << '.' << *it;
}
return ss.str();
}

} // namespace model
32 changes: 32 additions & 0 deletions src/core/model/table/column_combination.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <string>
#include <vector>

#include "column_index.h"

namespace model {

// Represents an index for a table within a set of tables
// As an example, this type is used in inclusion dependencies
using TableIndex = unsigned int;

class ColumnCombination {
protected:
TableIndex table_index_;
std::vector<ColumnIndex> column_indices_;

public:
ColumnCombination(TableIndex table_index, std::vector<ColumnIndex> col_indices)
: table_index_(table_index), column_indices_(std::move(col_indices)) {}

TableIndex GetTableIndex() const {
return table_index_;
}
std::vector<ColumnIndex> const& GetColumnIndices() const {
return column_indices_;
}
std::string ToString() const;
};

} // namespace model

0 comments on commit de1e33b

Please sign in to comment.