Exemple #1
0
 def is_nf2(self):
     nf_good = True
     fds0 = self.table.get_fds
     fds = []
     for fd in fds0:
         for right_e in fd.rhs:
             fds.append(FD(fd.get_lhs, [right_e]))
     ck_names = []
     for n in self.candidate_keys:
         temp = [a.name for a in n]
         ck_names.append(temp)
     try:
         for fd in fds:
             lhs = fd.get_lhs
             rhs = fd.get_rhs
             for r in rhs:
                 if not self.is_prime_attribute(r):
                     if lhs not in self.candidate_keys:
                         lhsn = [l.name for l in lhs]
                         for c in ck_names:
                             if set(lhsn) < set(c):
                                 if not fd in self.violating_fds:
                                     self.violating_fds.append(fd)
                                     nf_good = False
         return nf_good
     except Exception as ex:
         print('violates_2NF exception')
         print(ex)
Exemple #2
0
 def init_fds(self, table, table_name):
     fds = []
     try:
         for fd in table.findall('.//fd'):
             fds.append(
                 FD(self.check_fds_attributes(fd.find('lhs'), table_name),
                    self.check_fds_attributes(fd.find('rhs'), table_name)))
         if len(fds) == 0:
             print(
                 'No functional dependency found! please add the functional dependencies!'
             )
         return fds
     except Exception as e:
         print('get function dependencies exception: ' + str(e))
Exemple #3
0
 def find_fds(self):
     fd_list = []
     for part in self.partitions:
         #set_part = self.construct_set_from_part(part.column)
         for attr in self.table.get_attribute_string_list_no_pk():
             set_attr = set(attr)
             if attr not in part.column_set:
                 union = part.column_set | set_attr
                 union_part = self.find_partition_by_set(union)
                 if union_part:
                     if part.no_equivalence_classes == union_part.no_equivalence_classes:
                         lhs = []
                         rhs = []
                         for col in part.column_set:
                             lhs.append(Attribute(col))
                         rhs.append(Attribute(attr))
                         fd_list.append(FD(lhs, rhs, imported=True))
     return fd_list
Exemple #4
0
    def construct_fd_from_pk(self, primary, attributes):
        fds = []
        prim = []
        for col in primary:
            att = Attribute(col)
            prim.append(att)
        for att in attributes:
            same_att = False
            if isinstance(prim, list):
                for part in prim:
                    if part.get_name == att.get_name:
                        same_att = True
            elif lhs.get_name == att.get_name:
                same_att = True

            if not same_att:
                lhs = prim
                rhs = [att]
                fd = FD(lhs=lhs, rhs=rhs)
                fds.append(fd)
        return fds
    def set_table(table):
        name = table['name']
        t = Table(name)
        fds = table['fds']
        attributes = table['attributes']
        attributes_list = []
        fds_list = []
        for attr in attributes:
            attrs = attr.replace(')', '')
            attrs = attrs.replace('(', '')
            attrs = attrs.split(',')
            for at in attrs:
                attribute = Attribute(at)
                attributes_list.append(attribute)
        for fd in fds:
            lhs_list = []
            rhs_list = []
            for a in attributes_list:
                attr_name = a.get_name
                for lhs in fd['lhs']:
                    if lhs == attr_name:
                        lhs_list.append(a)
                for rhs in fd['rhs']:
                    if rhs == attr_name:
                        rhs_list.append(a)

            fd_elem = FD(lhs_list, rhs_list)
            fds_list.append(fd_elem)

        t.set_attributes(attributes_list)
        t.set_fds(fds_list)
        return t


#test_data = {'Schema': 'Test', 'Tables': [{'fds': [{'lhs': ['attr1'], 'rhs': ['attr2']}, {'lhs': ['attr1'], 'rhs': ['attr3']}, {'lhs': ['attr3'], 'rhs': ['attr2']}], 'name': 'Test1', 'attributes': ['attr1', '(attr2, attr4)', 'attr3']}]}
#mi = ManualImport(test_data)
#schema = mi.get_schema()
#tables = schema.get_tables()
#for t in tables:
    #print([a.get_name for a in t.get_attributes])
 def get_fds_union(self):
     fds = self.nf.calculate_mincover()
     united_fds = []
     lhs_fds = []
     i = 0
     for fd_1 in fds:
         lhs_1_name = [l.get_name for l in fd_1.get_lhs]
         j = 0
         for fd_2 in fds:
             lhs_2_name = [l.get_name for l in fd_2.get_lhs]
             if j >= i:
                 if lhs_1_name == lhs_2_name and j > i:
                     if lhs_1_name in lhs_fds:
                         lhs_fds.remove(lhs_1_name)
                     if fd_1 in united_fds:
                         united_fds.remove(fd_1)
                         rhs = fd_1.get_rhs + fd_2.get_rhs
                     else:
                         for f in united_fds:
                             if set([a.get_name for a in f.get_lhs
                                     ]) == set(lhs_1_name):
                                 united_fds.remove(f)
                                 rhs = f.get_rhs
                                 for r in fd_2.get_rhs:
                                     if r.get_name not in [
                                             a.get_name for a in rhs
                                     ]:
                                         rhs.append(r)
                     lhs_fds.append(lhs_1_name)
                     new_fd = FD(fd_1.get_lhs, rhs)
                     united_fds.append(new_fd)
                 else:
                     if lhs_1_name not in lhs_fds:
                         united_fds.append(fd_1)
                         lhs_fds.append(lhs_1_name)
             j += 1
         i += 1
     return united_fds
Exemple #7
0
 def calculate_mincover(self):
     table = self.table
     # first step: split rhs
     fd_1 = []
     for fd in self.table.get_fds:
         for right_e in fd.rhs:
             fd_1.append(FD(fd.get_lhs, [right_e]))
     step1_table = Table(self.table.get_name)
     step1_table.set_attributes(self.table.get_attributes)
     step1_table.set_fds(fd_1)
     # second step: check for attribute redundancy in lhs
     fd_2 = []
     step2_table = Table(self.table.get_name)
     step2_table.set_attributes(self.table.get_attributes)
     for fd1 in step1_table.get_fds:
         if len(fd1.get_lhs) == 1:
             fd_2.append(FD(fd1.get_lhs, fd1.get_rhs))
         else:
             a = fd1.get_lhs
             for left in a:
                 left_closure = [left]
                 stopper = 0
                 while stopper == 0:
                     left_closure_len_init = len(left_closure)
                     for fd in step1_table.get_fds:
                         fdlhs = [l.name for l in fd.get_lhs]
                         left_names = [n.name for n in left_closure]
                         if set(fdlhs) <= set(left_names):
                             for right_e in fd.get_rhs:
                                 if not ({right_e.name} < set(left_names)):
                                     left_closure.append(right_e)
                     if left_closure_len_init == len(left_closure):
                         stopper = 1
                 left_closure_names = [n.name for n in left_closure]
                 for left_2 in a:
                     if left.name != left_2.name:
                         if {left_2.name} <= set(left_closure_names):
                             a.remove(left_2)
             fd_2.append(FD(a, fd1.get_rhs))
     step2_table.set_fds(fd_2)
     # third step: remove redundant FDs
     fd_3 = []
     deletions = []
     step3_table = Table(self.table.get_name)
     step3_table.set_attributes(self.table.get_attributes)
     for fd1 in step2_table.get_fds:
         temp = []
         for x in step2_table.get_fds:
             temp.append(x)
         temp.remove(fd1)
         a = fd1.get_lhs
         b = fd1.get_rhs
         left_closure = []
         for left in a:
             left_closure.append(left)
         stopper = 0
         while stopper == 0:
             left_closure_len_init = len(left_closure)
             for fd in temp:
                 if not fd in deletions:
                     fdlhs = [l.name for l in fd.get_lhs]
                     left_names = [n.name for n in left_closure]
                     if set(fdlhs) <= set(left_names):
                         for right_e in fd.get_rhs:
                             if not right_e.name in left_names:
                                 left_closure.append(right_e)
             if left_closure_len_init == len(left_closure):
                 stopper = 1
         left_closure_names = [n.name for n in left_closure]
         c = []
         for right_2 in b:
             if not right_2.name in left_closure_names:
                 c.append(right_2)
         if len(c) > 0:
             fd_3.append(FD(a, c))
         else:
             deletions.append(fd1)
     step3_table.set_fds(fd_3)
     return fd_3
Exemple #8
0
def upload(button):
    """
    Upload handler
    :param button: the domId of the button that was clicked
    :return:
    """
    global schema
    global db_structure
    global normalized_schemas
    global db_c
    print(button)
    try:
        if button == "xmlButton":
            xml_data = request.form["data"]
            xml_structure = XMLImport(xml_data, False)
            xml_structure.init_objects()
            db_structure = None
            schema = xml_structure.get_schema()
            js_object = get_display_tables_js_object()
            return js_object
        elif button == "insertDBButton":
            url = request.form.get('url')
            username = request.form.get('user')
            pwd = request.form.get('pwd')
            db_name = request.form.get('dbName')
            schema_name = request.form.get('schema')
            db_c = {
                'url': url,
                'username': username,
                'pwd': pwd,
                'db':db_name
            }
            db_structure = DBImport(url, username, pwd, schema_name, db_name)
            if None == db_structure.get_error():
                schema = db_structure.map_tables()
                js_object = get_display_tables_js_object()
                return js_object
            else:
                error = db_structure.get_error()
                return error
        elif button == "insertmanual":
            data = request.form.get('data')
            schema_data = json.loads(data)
            mi = ManualImport(schema_data)
            schema = mi.get_schema()
            js_object = get_display_tables_js_object()
            return js_object
        elif button == "requestFD" or button == "attributeClosure":
            table_name = request.form['table']
            attrs = schema.get_table_attributes(table_name)
            js_object = get_attr_list_js(attrs)
            return js_object
        elif button == "insertFDButton":
            table_name = request.form['table']
            lhs = []
            rhs = []
            lhs_list = request.form['lhs'].split(',')
            rhs_list = request.form['rhs'].split(',')
            table = schema.get_table_by_name(table_name)
            for l in lhs_list:
                lhs.append(table.get_attribute_by_name(l))
            for r in rhs_list:
                rhs.append(table.get_attribute_by_name(r))
            fd = FD(lhs, rhs)
            table.add_fd(fd)
            return "success"
        elif button == "editFDButton":
            table_name = request.form['table']
            lhs_list = request.form['lhs'].split(',')
            rhs_list = request.form['rhs'].split(',')
            lhs = []
            rhs = []
            fd_id = request.form['id']
            table = schema.get_table_by_name(table_name)
            for l in lhs_list:
                lhs.append(table.get_attribute_by_name(l))
            for r in rhs_list:
                rhs.append(table.get_attribute_by_name(r))
            fd = table.get_fd_by_id(int(fd_id))
            fd.set_lhs(lhs)
            fd.set_rhs(rhs)
            return "success"
        elif button == "removeFDButton":
            table_name = request.form['table']
            fd_lhs = request.form['lhs']
            fd_rhs = request.form['rhs']
            fds_lhs_list = fd_lhs.split(',')
            fds_rhs_list = fd_rhs.split(',')
            table = schema.get_table_by_name(table_name)
            table.delete_fds(fds_lhs_list, fds_rhs_list)
            return "success"
        elif button == "minimalCover":
            table_name = request.form['table']
            table = schema.get_table_by_name(table_name)
            nf = NF(table)
            mc = nf.get_min_cover()
            js_object = get_display_fd_js_object(mc)
            return js_object
        elif button == "getAttributeClosure":
            table_name = request.form['table']
            table = schema.get_table_by_name(table_name)
            attributes = []
            attr_list = request.form['attributes'].split(',')
            for attr in attr_list:
                attributes.append(table.get_attribute_by_name(attr))
            nf = NF(table)
            ac = nf.get_attr_closure(attributes)
            return get_attr_list_js(ac)
        elif button == "candidateKeys":
            table_name = request.form['table']
            table = schema.get_table_by_name(table_name)
            nf = NF(table)
            ck = nf.get_candidate_keys()
            return get_candidate_keys_js(ck)
        elif button == "normalForm":
            table_name = request.form['table']
            table = schema.get_table_by_name(table_name)
            nf = NF(table)
            current_nf = nf.determine_nf()
            violated_fd = nf.get_violating_fds()
            js_object = get_nf_js_object(current_nf, violated_fd)

            return js_object
        elif button == "checkfds":
            table_name = request.form['table']
            table = schema.get_table_by_name(table_name)
            fds_hold_object = db_structure.check_fds_hold(table.get_fds, table.get_name)
            js_object = get_hold_fds_js_object(fds_hold_object)
            return js_object
        elif button == "normalizer":
            table_name = request.form['table']
            table = schema.get_table_by_name(table_name)
            nf = NF(table)
            current_nf = nf.determine_nf()
            if current_nf == 'BCNF':
                return 'false'
            normalization = Normalizer(nf)
            normalization.decomposition()
            if normalization.get_nf3_is_not_bcnf():
                table_nf3 = normalization.get_new_tables_nf3()
                table_bcnf = normalization.get_new_tables_bcnf()
                normalized_schemas[table_name] = normalization
                js_object = get_normalized_tables_js_object({'3nf': table_nf3, 'bcnf': table_bcnf})
            else:
                table = normalization.get_new_tables_nf3()
                normalized_schemas[table_name] = normalization
                js_object = get_normalized_tables_js_object({'3nf': table})
            return js_object
        return "Undefined button: " + button
    except Exception as e:
        print(e)