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

  Write a haskell program to calculates a balanced partition

Write a program in Haskell which calculates a balanced partition of N items where each item has a value between 0 and K such that the difference b/w the sum of the values of first partition,

  Create an application to run in the amazon ec2 service

In this project you will create an application to run in the Amazon EC2 service and you will also create a client that can run on local machine and access your application.

  Explain the process to develop a web page locally

Explain the process to develop a Web page locally

  Write functions

These 14 questions covers java class, Array, link list , generic class.

  Programming assignment

If the user wants to read the input from a file, then the output will also go into a different file . If the user wants to read the input interactively, then the output will go to the screen .

  Write a prolog program using swi proglog

Write a Prolog program using swi proglog

  Create a custom application using eclipse

Create a custom Application Using Eclipse Android Development

  Create a application using the mvc architecture

create a application using the MVC architecture. No scripting elements are allowed in JSP pages.

  Develops bespoke solutions for the rubber industry

Develops bespoke solutions for the rubber industry

  Design a program that models the worms behavior

Design a program that models the worm's behavior.

  Writing a class

Build a class for a type called Fraction

  Design a program that assigns seats on an airplane

Write a program that allows an instructor to keep a grade book and also design and implement a program that assigns seats on an airplane.

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