**This is an old revision of the document!**


A relational database management system (RDBMS) maintains data sets called relations. A relation has a name, a schema, and a set of tuples. A schema is a set of attributes. A tuple is a set of attribute/value pairs. An attribute is the name associated with each data value in a tuple entry. Relations are used as operands in relational operations such as rename, selection, projection, union, and join. The goal of this project is to build a relational database system from a Datalog file, and then answer queries using relational algebra.

Project Description

A Datalog Program can be represented by a Database. Each scheme in a Datalog Program defines a relation in the Database. The scheme defines the name of the relation, and the attribute list of the scheme defines the schema of the relation. Each fact in the Datalog Program defines a tuple in a relation. The fact name identifies a relation to which the tuple belongs. The basic data structure is a database consisting of relations, each with their own name, schema, and set of tuples. How you choose and use your data structures effects the difficulty of the project. Inheritance and polymorphism are always useful. Each data structure implementation has its benefits, drawbacks and obstacles to overcome.

Your program must convert every Datalog query into a sequence of select, project, and rename operations. As you prepare to write your program, take out a piece of paper, convert each of the queries in the given examples into a relational expression, and carry out these expressions. Your program should create these same expressions.

Requirements (Get the Design Right)

The project is divided into two parts representing different aspects of the final solution: part 1 implements a relation class and how it is used to answer queries and part 2 connects the relation class to the datalog program object created by the parser.

Part 1: relation class with tests

Create a relation object that includes methods implementing select, project, and rename operations. The relation and its methods must follow the mathematical definitions described in class and in the textbook. For example, the operators must not produce relations which have duplicate tuples or duplicate names in the schema.

Implement at least 10 tests to validate the select, project, and rename methods. At least 4 of the 10 tests must mimic a datalog query in that they use a sequence of select, project, and rename to answer the query (in that order). Further, all the tests must be automatic; automatic means the tests run without any user input and reports to the console the status of every test: pass or fail. The tests must be documented and justified: what does the test accomplish?

  • Command line: no command line arguments
  • Output: A pass/fail report for each test. (For this project, each test case you create should pass.)

The pass-off is based on the quality of tests and whether or not the solution passes.

The pass/fail report of a test is based on a comparison between the expected result and the actual result of each test.

Part 2: RDBMS-based Interpreter

Write an RDBMS-based interpreter for a Datalog Program. Given a Datalog Program object from the datalog parser, your interpreter should create an instance of a database object holding relations and then compute and print the result for each query using only the facts form the input (interpretation of the rules will be added in Project 4). Your design should include a class for the database and use your relation from part 1. Queries must be implemented by performing the appropriate relational operations as in the tests from part 1 (select, project, and rename). As a reminder, use the datalog object from the parser to initialize the database object, and then interpret the queries in that same object to generate the output. Queries should become a series of select, rename, and project calls on one of the relations in the database.

To format the output for pass-off, you must make it look like the examples below. Note the following: The queries are to be evaluated in the order listed in the Datalog Program. For each query, print the query and a space. Then, if the query's resulting relation is empty, output “No”; and if the resulting relation is not empty, output “Yes(n)” where n is the number of tuples in the resulting relation. If there are free variables in the query, print the tuples of the resulting relation, one per line and indented by two spaces, according to the following directions. Each tuple should be a comma-space separated list of pairs–(variable, string-value)–ordered according to the order of variable appearance in the query. The format of a pair is v= 's', where v is the variable and 's' is the string value. Sort the tuples alphabetically based on string values in the tuples' variables (sort with the first sort key as the first variable (in order of appearance in the query), the second sort key as the second variable, and so on).

The focus of this project is on interpreting 236-Datalog (without rules)–not on building an industrial-strength interpreter. Thus, to retain the central focus and keep the amount of coding within a reasonable bound, you may assume:

  • The arity (number of parameters) of all schemes, facts, rules and queries that have the same name are guaranteed to match. You do not have to check for matching arity.
  • No two attributes in the same scheme will have the same name. You do not have to check for this.
  • No two schemes in the scheme list will have the same name. You do not have to check for this.
  • Every scheme (or relation) referenced in a fact, rule head, predicate, or query will have been defined in the scheme section.

Interface requirements:

Part 2 is scored on a set of 10 private tests at submission.

Examples

These are not sufficient to completely test your program. Your program must have output formatted exactly like the example outputs below. Example Input

Input

Schemes:
  SK(A,B)
Facts:
  SK('a','c').
  SK('b','c').
  SK('b','b').
  SK('b','c').
Rules:
  DoNothing(Z) :- Stuff(Z).
Queries:
  SK(A,'c')?
  SK('b','c')?
  SK(X,X)?
  SK(A,B)?

Output

SK(A,'c')? Yes(2)
  A='a'
  A='b'
SK('b','c')? Yes(1)
SK(X,X)? Yes(1)
  X='b'
SK(A,B)? Yes(3)
  A='a', B='c'
  A='b', B='b'
  A='b', B='c'

Input

Schemes: 
	ab(A,B)
Facts:
	ab('joe','bob').
	ab('jim','bob').
	ab('joe','jim').
	ab('bob','bob').
Rules:

Queries:
	ab('joe','jim')?
	ab( who, 'bob')?
	ab('joe', anyone)?
	ab(X,X)?
	ab(X,Y)?

Output

ab('joe','jim')? Yes(1)
ab(who,'bob')? Yes(3)
  who='bob'
  who='jim'
  who='joe'
ab('joe',anyone)? Yes(2)
  anyone='bob'
  anyone='jim'
ab(X,X)? Yes(1)
  X='bob'
ab(X,Y)? Yes(4)
  X='bob', Y='bob'
  X='jim', Y='bob'
  X='joe', Y='bob'
  X='joe', Y='jim'

Input

Schemes: 
	dc(D,C)
Facts:
	dc('ralph','howard').
Rules:

Queries:
	dc('ralph', X)?
	dc('bob','bob')?
	dc(X,Y)?

Output

dc('ralph',X)? Yes(1)
  X='howard'
dc('bob','bob')? No
dc(X,Y)? Yes(1)
  X='ralph', Y='howard'

In the next example, notice that the rules are not evaluated, but the queries are still answered using only the facts included in the file. Input

Schemes: 
	people(person1,person2)
	employer(boss,employee)
Facts:
	people('joe','bob').
	people('jim','bob').
	people('joe','jim').
	employer('ralph','howard').
	people('bob','bob').
Rules:
	employer(X,Y):- people(Y,X).
	employer(X,Y):- people(X,Z),employer(Z,Y).
	people(X,Y):- people(Y,X).
Queries:
	people('joe','jim')?
	people( who, 'bob')?
	people('joe', anyone)?
	people(X,X)?
	people(X,Y)?
	employer('ralph', X)?
	employer('bob','bob')?
	employer(X,Y)?

Output

people('joe','jim')? Yes(1)
people(who,'bob')? Yes(3)
  who='bob'
  who='jim'
  who='joe'
people('joe',anyone)? Yes(2)
  anyone='bob'
  anyone='jim'
people(X,X)? Yes(1)
  X='bob'
people(X,Y)? Yes(4)
  X='bob', Y='bob'
  X='jim', Y='bob'
  X='joe', Y='bob'
  X='joe', Y='jim'
employer('ralph',X)? Yes(1)
  X='howard'
employer('bob','bob')? No
employer(X,Y)? Yes(1)
  X='ralph', Y='howard'

FAQ

When should I select, project, or rename in a query?

Here is a simple strategy to consider when deciding how to evaluate queries.

  1. Only do select operations as you traverse the parameters. The select will either be on a constant or it will be on an ID if the ID has been seen previously (i.e., the special case to avoid having two identical IDs in the scheme.
  2. Once all the select operations have been completed, then project to the original attributes that remain and rename those attributes to their new names.

The strategy implies that each select knows the list of IDs in the final project since it needs to check for duplicates to determine when to select on a column rather than a constant. Also in this strategy, it may be the case that no select takes place on an ID parameter (which is just fine).

Also, as a general rule that is always true, at any parameter, it is either a select operation or a delayed project/rename operation. It is never both.

Should project also re-order the tuple?

The output for the lab requires that tuples follow the order declared in the query. Although not apparent right now, consider what happens in a natural join? In such an operation, the order becomes less clear. A project-method can ensure the tuples follow the correct order as part of its operation with some little thought and foresight.

Should rename only change one attribute at a time or the entire attribute list at once?

Renaming the entire attribute list at once is simpler and avoids any issues with replacing A with B and then replacing B with A on the relation with attributes (A,B). That said, the test inputs are careful to never create such a situation.

Submission

Review the project standards for creating a zip archive.

Navigate to Learning Suite, select 'CS 236', and click on 'Assignments' on the course home page. Click on the link to the relevant project and at the bottom of the description click on 'View/Submit'. Use the resulting upload dialog to upload your zip archive.

Pass-off

Pass-off your project directly to a TA during normal TA hours. TAs help students on a first-come/first-serve basis and are under no obligation to stay later than designated hours so plan accordingly. Please review the syllabus and the project standards for the pass-off requirements including the published time bound.

cs-236/relational-database.1508794989.txt.gz · Last modified: 2017/10/23 21:43 by jrtyler
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0