Implement a query functionality to the catalogue

Assignment Help Programming Languages
Reference no: EM13923387

Problem: Queries on Music Catalogue

Background

In this machine problem, you will extend your work from the previous machine problem to support queries over the music catalogue.

The skeleton code for this machine problem is available on GitHub: https://github.com/EECE-210/mp3 (One should clone this repository to get started with this machine problem.)

Before starting with this part of the MP, you should understand the following:
• Introduction to regular expressions;
• Trees and binary tree traversals;
• Writing comparison methods for new classes/objects (e.g., overriding equals( ));
• Understanding the use of hashCodes and overriding hashCode for new classes;
• Tokenization and parsing;
• Java collections such as sets.

Queries over Music Catalogue 2.10

You will now implement a query functionality to the Catalogue. This will return a list of Albums that match a given criteria. We specify the criteria as a search rule (based on a very simple query language) that is executed against all the Genres and albums in the catalogue.

A search rule is expressed as a string (query). For example:
in ("Jazz") && by ("Louis Armstrong") || matches (".*Ripley.*")

The first step in processing the query string is to identify the basic logical units (tokens) in the query. The query string would be tokenized as:

<in> <(> <"Jazz"> <)> <&&> <by> <(> <"Louis Armstrong"> <)> <||> <matches>
<(> <".*Ripley.*"> <")">

Separate tokens are enclosed between < and >.

Notice that whitespace is ignored except when it is between the quotation marks. The tokenized query has only a linear structure. The tokenized query is then passed to the parser, which creates a tree representation of the query, called an abstract syntax tree (AST).

2416_Queries on Music Catalogue.png

The AST is produced according to a grammar, which is given in the following section. The grammar uniquely defines the AST generated from the query string.

Each node in the AST is a descendent of the ASTNode class. The interpret method of these classes is executed in a recursive manner by calling that method on the root node, which then calls that method on each of its children, and so on to the leaf nodes.

Let us walk through an example of executing our query string against a catalogue (represented in the figure below).

210_Queries on Music Catalogue1.png

We will call the interpret function on the root node, "||" in the AST above. This is actually the OrNode node. The OrNode object first calls interpret on its children nodes: "&&" which is the AndNode node and "matches" which is an InNode. (You should read about basic binary tree traversals to understand what traversals are.)

We execute interpret( ) on AndNode first. But AndNode has children too, so we execute interpret on its children and take the intersection of the result. The children of AndNode are InNode and ByNode.

InNode searches for all albums that contained in its argument - Jazz. In our diagram above, the albums are: Louis and the Angels, Crossings.

ByNode searches for albums written by the performer in the argument - Louis Armstrong. In out diagram above, the album is: Louis and the Angels.

AndNode intersects the results of its children and returns the result: Louis and the Angels. So that takes care of the left subtree of OrNode. Now, let's move on to the right subtree -

MatchesNode. MatchesAST searches for the title that matches its argument - *Ripley*. We treat its argument as a Java regular expression pattern and use the underlying regular expression implementation in Java to execute it. Running the regular expression *Ripley* on our current catalogue returns The Talented Mr. Ripley.

Now we are ready to take the union of the results from the left subtree and the right subtree of OrNode. So the final return value is the set {Louis and the Angels} U {The Talented Mr. Ripley} = {Louis and the Angels, The Talented Mr. Ripley}.

Grammar

This is the grammar of the query language:

<orExpr> ::= <andExpr>(<or><andExpr>)*
<andExpr> ::= <atom>(<and><atom>)*
<atom> ::= <in>|<matches>|<by>|<LParen><orExpr><RParen>
<or> ::= "||"
<and> ::= "&&"
<in> ::= "in" <LParen><string><RParen>
<matches> ::= "matches" <LParen><string><RParen>
<by> ::= "by" <LParen><string><RParen>
<LParen> ::= "("
<RParen> ::= ")"


<orExpr> and <andExpr> are non-leaf nodes; <in>, <matches> and <by> are leaf nodes.

The argument to each leaf node can be interpreted as follows:
in(Genre name)
matches(regular expression applied to title) by(performer)

Constructing our Parser

We will construct LL recursive descent parser. Recursive descent means that the expression is parsed top-down using a set of mutually-recursive procedures (e.g., orExpr, andExpr). LL means that the parser processes the tokens from left to right, producing a leftmost derivation of the query.

More importantly, what you should know is that each non-terminal above - <orExpr>, <andExpr> and <atom> should be a method in QueryParser that will be called recursively.

The andExpr method has been implemented for you. You must complete the orExpr method to build the AST and also change the getRoot method so it calls the proper method.

Implement functionality to support the "by" search query

First you will have to support the "by" query. To do this, create a class that extends ASTNode, add the implementation and then modify the parser and tokenizer to support the new element. This will mean, among others, that you have to create a new TokenType. Once you have done that, create tests that prove that your implementation works according to the specification.


Override equals and hashCode for your classes

Albums are equal if they have the same performer and title. Genres are equal if they have the same name. When you override equals it is good practice to override hashCode as well. See this for further information.

By the way, as a convenience, Eclipse can auto-generate equals and hashcode for simple cases. Go to Source > Generate equals and hashcode. You can use this feature but ensure that you also understand how to actually implement equals and hashcode on your own.

Implement the interpret methods of the ASTNode subclasses

The interpret() method of the ASTNode types is the method that actually performs some action to filter the albums and Genres according to the query string. This method will accept a Catalogue object as an argument and return a set of albums or Genres that are selected from the catalogue.

Implement the method query(String queryCat( )) in the Catalogue class

You must add this method:
public List<Album> queryCat(String query) {...}

The method uses the infrastructure you wrote and tested above, and serves as an access point to it.

Write JUnit tests to test the Catalogue.queryCat() method

This means that you will have to write tests for the interpret methods in ASTNode subclasses.

Test case #1: Create a test class for each leaf node (InNode, MatchesNode and ByNode) and test your implementation of the interpret() method.

Test case #2: Create a test class for each non-leaf node (AndNode and OrNode) that will test the interpret functionality.

Test case #3: Create a test class that will exercise some complex queries that will make use of all the AST subclasses.

Please note that it is not about the quantity of the individual tests - it is about their quality. Think carefully about what you are testing and what inputs you should use.

CHALLENGE TASK

Expand the Album class to include the year in which the album was released. Then add additional support to the query mechanism to retrieve albums that were released in, before or after a given year in conjunction with other query terms. (You will need to extend the query grammar to support this operation.)

Reference no: EM13923387

Questions Cloud

Compare and contrast each of the spi models? : Compare and contrast each of the SPI models. and give an example of specific organizational uses for SaaS, PaaS, and IaaS .
Assignment on apply repeated-measures : This assignment consists of two parts. In the first part, you will utilize an existing dataset to analyze the dataset from repeated-measures experimental design. All SPSS output should be pasted into your Word document. In the second part, you wil..
How arrangement can create an ethical dilemma for manager : Most money managers have a portion of their compensation tied to the performance of the portfolios they manage. Explain how this arrangement can create an ethical dilemma for the manager.
Festus temperature controls case study : Randy Cliff, manager of Festus (Missouri) Temperature Controls Company has a good problem. His company has been experiencing unexpected growth since 2009 and now has some extra plant capacity that he needs to use.
Implement a query functionality to the catalogue : Implement a query functionality to the Catalogue. This will return a list of Albums that match a given criteria. We specify the criteria as a search rule (based on a very simple query language) that is executed against all the Genres and albums in..
Samples-power analysis and design sensitivity : Download G*Power and play around with it. See how changes in assumptions and parameters affect sample size estimates.
What is abc mutual funds nav : Suppose ABC Mutual Fund had no liabilities and owned only four stocks as follows: The fund began by selling $50,000 of stock at $8.00 per share. What is its NAV?
Confidence interval and hypothesis test : As you did in StatCrunch Assignment 1B, look at the items in the StatCrunch U survey and develop a question regarding population proportions that can be answered using the survey data you collected.
What is the expiration date of the end user certificate? : Your organization has a Web based information system and it is discovered that your information system vulnerable to several high risk Open Web Application Security Project (OWASP) Top Ten vulnerabilities.

Reviews

Write a Review

Programming Languages Questions & Answers

  Trace the program

We are going to trace the following program, i.e. simulate in your head how it would execute on a computer. To help you, a trace table is provided for you to fill. Unlike exam E1, our focus here is not only on keeping track of the values of each v..

  Write a perl cgi program

Write a Perl CGI program (script) to insert new questions into the quiz table via a Web browser. You can assume that, when inserting questions into the table, there is only one transaction yielded by your program to access the table

  Use shared mutex for synchronization and locking mechanism

Execute two programs of matrix multiplication with thread programming. Use the shared mutex for synchronization and locking mechanism.

  Debug a simple visual basic program

The GUI program will have a button that creates a new window with the word Hello. Enhance the display by making the word change color, move, or change to another language (such as Hola).

  Write class to track name and population

Write down class called City to track name and population. Use name as key, and give a function which compares two cities, and points out greater.

  Write a function that accepts an array a as input and

write a function that accepts an array a as input and searches the contents of the array for elements in three

  An integer programming model

An integer programming model would be utilized in scheduling for high demand space in government buildings or classrooms or it can be used in high level budgeting for construction projects.

  Recursive method to replace all occurrences of character

Write down the program which uses the recursive method to replace all occurrences of character with another character in given sentence.

  Write liberty basic to make payment of current balance

Makes a payment equal to 5 percent of current balance suppose the customer makes no new purchases. Must be written in Liberty Basic.

  Formulate a linear programming model for given problem

A company produces two products that are processed on two assembly lines. Assembly line 1 has 100 available hours, and assembly 2 has 42 available hours. Formulate a linear programming model for this problem

  Write non-recursive function-compute n-th fibonacci number

Fibonnacci series 0,1,1,2,3,5,8,13,21,... starts with terms 0 and 1 and has property that each succeeding terms is sum of the two preceding terms. Write the non-recursive function which computes the n'th Fibonacci number.

  Program to print smallest number and largest number entered

Write C++ program; LargestSmallest.cpp; which inputs six real numbers from user and determines and prints smallest number and largest number entered.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd