0

機能依存関係をデータ構造として表現する方法を考えています。

(データベースの関係スキーマの) 機能依存関係は、属性のセットを属性のセットにマップします。たとえば、{A, B} -> {C, D} では、属性 C と D は機能的に A と B に依存します。

ここで、考えられる 4 つのケースを考えることができます。

  1. {A} -> {B} (2 つの単一属性)
  2. {A, B} -> {C} (属性のセットは単一の属性を意味します)
  3. {A} -> {B, C} (単一の属性は一連の属性を意味します)
  4. {A, B} -> {C, D} (属性のセットは属性のセットを意味する)

私の最初のアプローチは、単純な 2 メンバー クラスでした。

class attribute
{
    string name;
    set<attribute*> dependent_on;
}

これは、(1) のような機能依存関係では機能しますが、(2) - (4) では機能しないと思います。確かに (3) を分解することはできますが、(2) と (4) をそのようなクラスで表現する方法がわかりません。

CとD が AB に機能的に依存しているという情報を保持する必要があるためattributegroup、属性のセットが属性のセットにマップされるようなクラスを作成する必要があります。例えば:

class attributegroup
{
    set<attribute*> members;
    set<attribute*> dependent_on;
}

attributegroupしたがって、実際には、1 つの属性を 1 つのメンバーのみを持つとして単純に表すことができます。しかし、これが最善の方法だとは思いません。

どんな助け/考えも感謝します:)

4

1 に答える 1

1

依存関係を属性に保存しません。

include <iostream>
#include <map>
#include <vector>

struct Attribute;
typedef std::vector<Attribute> Attributes;
struct Attribute {
    char name;
    operator Attributes () const { return Attributes{ 1, *this }; }
};

inline bool operator < (const Attribute& a, const Attribute& b) {
    return a.name < b.name;
}

inline bool operator < (const Attributes& a, const Attributes& b) {
    return std::lexicographical_compare(
        a.begin(), a.end(),
        b.begin(), b.end());
}

inline std::ostream& operator << (std::ostream& stream, const Attributes& attributes) {
    for(const auto& a: attributes) {
        std::cout << a.name;
    }
    return stream;
}

typedef std::multimap<Attributes, Attributes> AttributeDependencies;
typedef AttributeDependencies::value_type AttributeDependency;

int main(int argc, const char* argv[]) {
    Attribute a {'A'};
    Attribute b {'B'};
    Attribute c {'C'};
    Attribute d {'D'};

    AttributeDependencies dpendencies;
    dpendencies.insert(AttributeDependency(a, b));
    dpendencies.insert(AttributeDependency(Attributes{a, b}, c));
    dpendencies.insert(AttributeDependency(a, Attributes{b, c}));
    dpendencies.insert(AttributeDependency(Attributes{a, b}, Attributes{c, d}));
    for(const auto& d: dpendencies) {
        std::cout << '{' << d.first << "} -> {" << d.second << "}\n";
    }
    return 0;
}

注: std::map は適切なコンテナーかもしれませんが、std::multimap はサンプル データに適合します。

于 2013-10-17T22:48:19.963 に答える