From 33734ad16febee8b3b4082d666e23aa49edc3f74 Mon Sep 17 00:00:00 2001 From: Linus Tan Date: Sun, 23 Apr 2023 14:46:00 +0800 Subject: [PATCH 1/7] allow user input --- main.py | 62 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/main.py b/main.py index d2d8dfe..5b8e0e3 100644 --- a/main.py +++ b/main.py @@ -17,15 +17,40 @@ ########################################### # Modify from this line onwards to suit your needs -R = ['A', 'B', 'C'] -FD = [[['A', 'B'], ['C']], [['A'], ['B']], [['B'], ['A']]] -normalform = normalforms.threenf +# R = ['A', 'B', 'C', 'D'] +# FD = [[['B', 'C'], ['D']], [['A'], ['B']]] +# normalform = normalforms.bcnf +R = input("Enter the list of unique variables (R) separated by commas: ").split(",") +FD = [] +while True: + dep = input("Enter a functional dependency e.g. a,b->c (or enter nothing to stop): ") + if not dep: + break + try: + lhs, rhs = dep.split("->") + FD.append([lhs.split(","), rhs.split(",")]) + except ValueError: + print("Invalid functional dependency!") +while True: + normalform_name = input("Enter the normal form you want to achieve (2nf, 3nf, or bcnf): ") + if normalform_name == "2nf": + normalform = normalforms.twonf + break + elif normalform_name == "3nf": + normalform = normalforms.threenf + break + elif normalform_name == "bcnf": + normalform = normalforms.bcnf + break + else: + print("Invalid normal form!") + # Do not modify anything from this line onwards ########################################### # find all candidate keys ########################################### -print('all candiate keys:') +print('all candidate keys:') print(api.candidate_keys(R, FD)) ########################################### @@ -34,20 +59,19 @@ print('') print('') print('a minimal cover:') -minCover = api.min_cover(R,FD) +minCover = api.min_cover(R, FD) for F in minCover: - print(F[0],'->',F[1]) + print(F[0], '->', F[1]) ########################################### # check all F in FD for violation of given normal form ########################################### print('') -print('') -print('check normal form:',normalform.__name__) -FDsimplified = api.min_cover_step1(R,FD) +print('') +print('check normal form:', normalform.__name__) +FDsimplified = api.min_cover_step1(R, FD) for F in FDsimplified: - print(F[0],'->',F[1],normalform(R,FDsimplified,F)) - + print(F[0], '->', F[1], normalform(R, FDsimplified, F)) ########################################### # decomposition to given normal form @@ -56,12 +80,12 @@ # also verify result against given normal form ########################################### useSigmaPlus = True -printStep = False # if true will print steps invovled in deriving the answer +printStep = False # if true will print steps invovled in deriving the answer print('') print('') -print('decomposition:',normalform.__name__, 'showStep' if printStep else '') -fragments = decomposition.decompose(R,FD,normalform,useSigmaPlus,printStep) -decomposition.print_fragments(fragments,normalform) +print('decomposition:', normalform.__name__, 'showStep' if printStep else '') +fragments = decomposition.decompose(R, FD, normalform, useSigmaPlus, printStep) +decomposition.print_fragments(fragments, normalform) ########################################### # synthesize to 3NF (not any form) @@ -69,11 +93,9 @@ # if not show which dependency is lost # also verify result against 3NF ########################################### -printStep = False # if true will print steps invovled in deriving the answer +printStep = False # if true will print steps invovled in deriving the answer print('') print('') print('synthesis: threenf', 'showStep' if printStep else '') -fragments = decomposition.simple_synthesis(R,FD,printStep) -decomposition.print_fragments(fragments,normalform) - - +fragments = decomposition.simple_synthesis(R, FD, printStep) +decomposition.print_fragments(fragments, normalform) From 8307fd679626938e5af941cb67d0ba5dfa10f5f1 Mon Sep 17 00:00:00 2001 From: Linus Tan Date: Sun, 23 Apr 2023 14:50:09 +0800 Subject: [PATCH 2/7] correct spelling --- main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 5b8e0e3..c5dfb45 100644 --- a/main.py +++ b/main.py @@ -75,12 +75,12 @@ ########################################### # decomposition to given normal form -# also check wether decomposition is dependency-preserving +# also check whether decomposition is dependency-preserving # if not show which dependency is lost # also verify result against given normal form ########################################### useSigmaPlus = True -printStep = False # if true will print steps invovled in deriving the answer +printStep = False # if true will print steps involved in deriving the answer print('') print('') print('decomposition:', normalform.__name__, 'showStep' if printStep else '') @@ -89,11 +89,11 @@ ########################################### # synthesize to 3NF (not any form) -# also check wether synthesize is dependency-preserving +# also check whether synthesize is dependency-preserving # if not show which dependency is lost # also verify result against 3NF ########################################### -printStep = False # if true will print steps invovled in deriving the answer +printStep = False # if true will print steps involved in deriving the answer print('') print('') print('synthesis: threenf', 'showStep' if printStep else '') From 54815949254fde1bf4f494aee38270fcb70beaf1 Mon Sep 17 00:00:00 2001 From: Linus Tan Date: Sun, 23 Apr 2023 14:59:22 +0800 Subject: [PATCH 3/7] adjust instructions --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index c5dfb45..0d8ca64 100644 --- a/main.py +++ b/main.py @@ -20,10 +20,10 @@ # R = ['A', 'B', 'C', 'D'] # FD = [[['B', 'C'], ['D']], [['A'], ['B']]] # normalform = normalforms.bcnf -R = input("Enter the list of unique variables (R) separated by commas: ").split(",") +R = input("Enter the list of unique variables of R separated by commas (e.g. a,b,c,d): ").split(",") FD = [] while True: - dep = input("Enter a functional dependency e.g. a,b->c (or enter nothing to stop): ") + dep = input("Enter a functional dependency (e.g. a,b->c) or enter nothing to stop: ") if not dep: break try: From c25018c871fccab39b53ee16f8c7b9eecda13bcf Mon Sep 17 00:00:00 2001 From: Linus Tan Date: Sun, 23 Apr 2023 15:22:57 +0800 Subject: [PATCH 4/7] adjust readme --- README.md | 53 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index c89e9fd..cfdf47d 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,43 @@ # DBNormalizer Minimal command line python program to help you normalise your relational database design. -## Before you Run: -Modify the portion indicated in the main.py file according to comment (YES pls modify inside the source code). -The modification basically gives the following inputs: -1. Set of variables in your database. -2. List of functional dependencies implied in your database (You don't have to list trivial ones). -3. Set the normal form you want to test/achieve. - -An example of the inputs will be: -``` -R = ['A','B', 'C'] -FD = [ - [['A', 'B'], ['C']], - [['A'], ['B']], - [['B'], ['A']] -] -normalform = normalforms.threenf -``` +[//]: # (## Before you Run:) + +[//]: # (Modify the portion indicated in the main.py file according to comment (YES pls modify inside the source code).) + +[//]: # (The modification basically gives the following inputs:) + +[//]: # (1. Set of variables in your database.) + +[//]: # (2. List of functional dependencies implied in your database (You don't have to list trivial ones).) + +[//]: # (3. Set the normal form you want to test/achieve.) + +[//]: # () +[//]: # (An example of the inputs will be:) + +[//]: # (```) + +[//]: # (R = ['A','B', 'C']) + +[//]: # (FD = [) + +[//]: # ( [['A', 'B'], ['C']], ) + +[//]: # ( [['A'], ['B']], ) + +[//]: # ( [['B'], ['A']]) + +[//]: # (]) + +[//]: # (normalform = normalforms.threenf) + +[//]: # (```) ## How to Run: -python main.py +`python main.py` + +The program will prompt you the enter the **attributes**, **FDs** and the **normal form type**. ## Output: (Note: Output is formated using python3. For best experience pls use python3.) From 715906c8b86f4f4a980009e2978ba200203f19b0 Mon Sep 17 00:00:00 2001 From: Linus Tan Date: Mon, 24 Apr 2023 17:00:38 +0800 Subject: [PATCH 5/7] update instructions --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 0d8ca64..35d4eba 100644 --- a/main.py +++ b/main.py @@ -30,7 +30,7 @@ lhs, rhs = dep.split("->") FD.append([lhs.split(","), rhs.split(",")]) except ValueError: - print("Invalid functional dependency!") + print("Invalid functional dependency! Reenter the functional dependency.") while True: normalform_name = input("Enter the normal form you want to achieve (2nf, 3nf, or bcnf): ") if normalform_name == "2nf": @@ -43,7 +43,7 @@ normalform = normalforms.bcnf break else: - print("Invalid normal form!") + print("Invalid normal form! Reenter the normal form.") # Do not modify anything from this line onwards From c5e3154de2db4ff0f12175e5c3f14001401ad968 Mon Sep 17 00:00:00 2001 From: Linus Tan Date: Mon, 24 Apr 2023 17:01:47 +0800 Subject: [PATCH 6/7] clean up --- README.md | 33 --------------------------------- main.py | 4 +--- 2 files changed, 1 insertion(+), 36 deletions(-) diff --git a/README.md b/README.md index cfdf47d..9e48af9 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,6 @@ # DBNormalizer Minimal command line python program to help you normalise your relational database design. -[//]: # (## Before you Run:) - -[//]: # (Modify the portion indicated in the main.py file according to comment (YES pls modify inside the source code).) - -[//]: # (The modification basically gives the following inputs:) - -[//]: # (1. Set of variables in your database.) - -[//]: # (2. List of functional dependencies implied in your database (You don't have to list trivial ones).) - -[//]: # (3. Set the normal form you want to test/achieve.) - -[//]: # () -[//]: # (An example of the inputs will be:) - -[//]: # (```) - -[//]: # (R = ['A','B', 'C']) - -[//]: # (FD = [) - -[//]: # ( [['A', 'B'], ['C']], ) - -[//]: # ( [['A'], ['B']], ) - -[//]: # ( [['B'], ['A']]) - -[//]: # (]) - -[//]: # (normalform = normalforms.threenf) - -[//]: # (```) - ## How to Run: `python main.py` diff --git a/main.py b/main.py index 35d4eba..c21f47a 100644 --- a/main.py +++ b/main.py @@ -17,9 +17,7 @@ ########################################### # Modify from this line onwards to suit your needs -# R = ['A', 'B', 'C', 'D'] -# FD = [[['B', 'C'], ['D']], [['A'], ['B']]] -# normalform = normalforms.bcnf + R = input("Enter the list of unique variables of R separated by commas (e.g. a,b,c,d): ").split(",") FD = [] while True: From 4878173020ed808c5f71ff7b9c7c0502df82978e Mon Sep 17 00:00:00 2001 From: Linus Tan Date: Mon, 24 Apr 2023 17:03:38 +0800 Subject: [PATCH 7/7] update readme --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9e48af9..a7257f5 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,10 @@ Minimal command line python program to help you normalise your relational databa ## How to Run: `python main.py` -The program will prompt you the enter the **attributes**, **FDs** and the **normal form type**. +The program will prompt you to enter the **attributes**, **FDs** and the **normal form type**. ## Output: -(Note: Output is formated using python3. For best experience pls use python3.) +(Note: Output is formatted using python3. For best experience pls use python3.) Following the previous example, the output will be: ``` @@ -46,9 +46,9 @@ already in 3NF * The 1st section gives you all possible candidate keys * The 2nd section computes the minimal cover * The 3rd section checks if the supplied functional dependencies are normalized. -* The 4th section trys to achieve required normalization by decomposition method (can loose some dependencies). -* The 5th section trys to achieve 3NF by synthesis method (only 3NF is guaranteed, and all dependencies preserved). +* The 4th section tries to achieve required normalization by the decomposition method (can lose some dependencies). +* The 5th section tries to achieve 3NF by synthesis method (only 3NF is guaranteed, and all dependencies preserved). -Unless you must achieve BCNF, synthesis method is more popular because it at least gives you 3NF and preserves +Unless you must achieve BCNF, the synthesis method is more popular because it at least gives you 3NF and preserves all original dependencies. And mostly likely, it gives you BCNF as well.