Giter Site home page Giter Site logo

ldbn's People

Contributors

devng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ldbn's Issues

Can not find 2NF decomposition codes?

I am trying to show step by step 2NF, 3NF,BCNF decomposition in my webpage. But can not seem to find 2NF decomposition codes. However I found 3NF & BCNF decomposition codes in :
Algorithms.java

public static Collection synthese(Relation r, boolean areFDMinimalCoder) {
HashSet result = new HashSet();
List fds = r.getFds();
if (!areFDMinimalCoder) {
minimalCover(fds);
}
List allKeys = findAllKeyCandidates(fds, r.getAttrbutes());

    //2. create new relations
    for (Iterator<FD> iter = fds.iterator(); iter.hasNext();) {
        FD fd = iter.next();
        List<FD> fds3nf = new ArrayList<FD>();
        fds3nf.add(fd);
        AttributeSet att3nf = fd.getLHS().clone();
        att3nf.union(fd.getRHS());
        Relation r3nf = new Relation(att3nf);
        r3nf.setFDs(fds3nf);
        result.add(r3nf);
    }

    // Associate FDs for each relation , see Kemper's book p. 185
    for (Relation r1 : result) {

// AttributeSet atts = r1.getAttrbutes();
// List r1_fds = new ArrayList();
List r1_fds = reductionByResolution(r.getAttrbutes(), r.getFds(), r1.getAttrbutes());
minimalCover(r1_fds);
// for (FD fd : fds) {
// AttributeSet lhs = fd.getLHS();
// AttributeSet rhs = fd.getRHS();
// if(atts.containsAttSet(lhs) && atts.containsAttSet(rhs)) {
// if(!r1_fds.contains(fd)) {
// r1_fds.add(fd);
// }
// }
// }
r1.setFDs(r1_fds);
}

    if (allKeys.size() > 0) {
        boolean containsKey = false;
        for (Relation rr : result) {
            for (AttributeSet k : allKeys) {
                System.out.println(k);
                if (rr.getAttrbutes().containsAttSet(k)) {
                    containsKey = true;
                    break;
                }
            }
        }

        if (!containsKey) {
            Relation keyRelation = new Relation(allKeys.get(0));
            result.add(keyRelation);
            keyRelation.setFDs(new ArrayList<FD>()); // should not be
                                                        // null;
            ArrayList<AttributeSet> keys = new ArrayList<AttributeSet>();
            keys.add(allKeys.get(0));
            keyRelation.setKeyCandidates(keys);
            keyRelation.setPrimaryKey(allKeys.get(0));

        }
    } else {
        LOG.log(Level.WARNING,"Could not find key for the relation");
    }

    // find a key for each relation
    // TODO is this right, because it finds a key and not a key candidate??
    for (Relation r3nf : result) {
        List<FD> fds3nf = r3nf.getFds();
        for (FD fd3nf : fds3nf) {
            if (isSuperKey(fd3nf.getLHS(), r3nf.getAttrbutes(), fds3nf)) {
                r3nf.setPrimaryKey(fd3nf.getLHS());
            }
        }
        if (r3nf.getPrimaryKey() == null) {
            r3nf.setPrimaryKey(r3nf.getAttrbutes());
        }
    }

    // for (Relation r3nf : result) {
    // List<FD> fds3nf = r3nf.getFds();
    // AttributeSet att3nf = r3nf.getAttrbutes();
    // List<AttributeSet> keys = findAllKeyCandidates(fds3nf, att3nf);
    //          
    // if(keys.size() < 1) {
    // r3nf.setSuperKey(att3nf.clone());
    // keys.add(r3nf.getSuperKey());
    //              
    // } else {
    // r3nf.setSuperKey(keys.get(0));
    // }
    // r3nf.setKeyCandidates(keys);
    // }

    // eliminate relationships such as Ra is subset of Rb
    List<Relation> toRemove = new ArrayList<Relation>();
    for (Relation r1 : result) {
        for (Relation r2 : result) {
            if (r1 == r2)
                continue;
            if (r1.getAttrbutes().containsAttSet(r2.getAttrbutes())) {
                toRemove.add(r2);
            }
        }
    }
    result.removeAll(toRemove);
    return result;
}

public static Collection findBCNF(Collection synthese) {
List nonBCNF = null;
Relation curRel = null;
for (Relation r : synthese) {
List tmp = isBCNF(r);
if (tmp != null) {
nonBCNF = tmp;
curRel = r;
break;
}
}

    if (nonBCNF != null && curRel != null && nonBCNF.size() != 0) {
        FD fd = nonBCNF.get(0);
        AttributeSet lhs = fd.getLHS();
        AttributeSet rhs = fd.getRHS();
        AttributeSet attU = lhs.clone();
        attU.union(rhs);
        AttributeSet attN = curRel.getAttrbutes().clone();
        attN.removeAttSet(rhs);
        Relation r1 = new Relation(attU);
        Relation r2 = new Relation(attN);
        // associate FDs with r1 and r2;
        List<FD> fds = reductionByResolution(curRel.getAttrbutes(), curRel
                .getFds(), r1.getAttrbutes());
        r1.setFDs(fds);
        fds = reductionByResolution(curRel.getAttrbutes(), curRel.getFds(),
                r2.getAttrbutes());
        r2.setFDs(fds);
        // find keys for r1 and r2
        List<AttributeSet> keys = findAllKeyCandidates(r1.getFds(), r1
                .getAttrbutes());
        r1.setKeyCandidates(keys);
        r1.setPrimaryKey(keys.get(0));
        keys = findAllKeyCandidates(r2.getFds(), r2.getAttrbutes());
        r2.setKeyCandidates(keys);
        r2.setPrimaryKey(keys.get(0));

        synthese.remove(curRel);
        synthese.add(r1);
        synthese.add(r2);
        findBCNF(synthese);
    }

    return synthese;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.