2XB3 - Week - 8 - Searching - ArchitecturalPattern

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

COMP

SCI 2XB3/ SOFT ENG 2XB3


Prac5ce and Experience: Binding Theory to Prac5ce

Lecture 7
– Applica6on of Search Algorithms
– So=ware Design PaAerns

Dr. Reza Samavi

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 1


2XB3: learning through reflec5on on doing
•  Applica6on of computa6onal theories
•  Implementa6on and programming
•  Progressing from programming to implemen6ng informa6on
systems
•  Progressing from individual work to team work
Web Applica5on, Web Applica5on connected to DBs
So=ware Program Documenta6on
SoEware Versioning Design
Engineering
Final Project
Machine Learning Algorithms

So=ware Development Methodology

ADT Experiments Tes6ng

Sor6ng Searching Graphs

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 2


Outline
§  Applica6on of Search Algorithms

§  Why we need design paAerns?


§  MVC Design PaAern
§  MVC live example for developing a Web App

3
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 3
Symbol tables
Announcement
Key-value pair abstraction.
・ Insert a value with specified key.
•  Next week lecture on
・Given a key, search for the corresponding value.
Ex. DNS lookup.
・Insert domain name with specified IP address.
・Given domain name, find corresponding IP address.
domain name IP address

www.cs.princeton.edu 128.112.136.11

www.princeton.edu 128.112.128.15

www.yale.edu 130.132.143.21

www.harvard.edu 128.103.060.55

www.simpsons.com 209.052.165.60

key value
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 4 !X
Symbol table applications

application purpose of search key value

dictionary find definition word definition

book index find relevant pages term list of page numbers

file share find song to download name of song computer ID

financial account process transactions account number transaction details

web search find relevant web pages keyword list of page names

compiler find properties of variables variable name type and value

routing table route Internet packets destination best route

DNS find IP address domain name IP address

reverse DNS find domain name IP address domain name

genomics find markers DNA string known positions

file system find file on disk filename location on disk

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 5 !X


Symbol tables: context

Also known as: maps, dictionaries, associative arrays.



Generalizes arrays. Keys need not be between 0 and N – 1.

Language support.
・External libraries: C, VisualBasic, Standard ML, bash, ...
・Built-in libraries: Java, C#, C++, Scala, ...
・Built-in to language: Awk, Perl, PHP, Tcl, JavaScript, Python, Ruby, Lua.
every array is an every object is an table is the only
associative array associative array primitive data structure

hasNiceSyntaxForAssociativeArrays["Python"] = true
hasNiceSyntaxForAssociativeArrays["Java"] = false

legal Python code

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 6 !X


Basic symbol table API

Associative array abstraction. Associate one value with each key.

public class ST<Key, Value>

ST() create an empty symbol table

void put(Key key, Value val) put key-value pair into the table a[key] = val;

Value get(Key key) value paired with key a[key]

boolean contains(Key key) is there a value paired with key?

void delete(Key key) remove key (and its value) from table

boolean isEmpty() is the table empty?

int size() number of key-value pairs in the table

Iterable<Key> keys() all the keys in the table

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 7 !X


Conventions

・Values are not null. Java allows null value

・Method get() returns null if key not present.


・Method put() overwrites old value with new value.

Intended consequences.
・Easy to implement contains().
public boolean contains(Key key)
{ return get(key) != null; }

・Can implement lazy version of delete().


public void delete(Key key)
{ put(key, null); }

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 8 !X


Keys and values

Value type. Any generic type.

specify Comparable in API.

Key type: several natural assumptions.


・Assume keys are Comparable, use compareTo().
・Assume keys are any generic type, use equals() to test equality.
・Assume keys are any generic type, use equals() to test equality;

use hashCode() to scramble key.

built-in to Java
(stay tuned)

Best practices. Use immutable types for symbol table keys.


・Immutable in Java: Integer, Double, String, java.io.File, …
・Mutable in Java: StringBuilder, java.net.URL, arrays, ...
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 9 !X
Equality test

All Java classes inherit a method equals().

Java requirements. For any references x, y and z:


・Reflexive: x.equals(x) is true.
・Symmetric:
equivalence

x.equals(y) iff y.equals(x). relation

・Transitive: if x.equals(y) and y.equals(z), then x.equals(z).


・Non-null: x.equals(null) is false.

do x and y refer to
the same object?
Default implementation. (x == y)
Customized implementations. Integer, Double, String, java.io.File, …
User-defined implementations. Some care needed.

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 10 !X


ST test client for traces

Build ST by associating value i with ith string from standard input.

public static void main(String[] args)


{
ST<String, Integer> st = new ST<String, Integer>();
for (int i = 0; !StdIn.isEmpty(); i++) keys S E A R C H E X A M P L E
{ values 0 1 2 3 4 5 6 7 8 9 10 11 12
String key = StdIn.readString(); output for output
outputfor
st.put(key, i); basic symbol table ordered
(one possibility) symbol table
}
L 11 A 8
for (String s : st.keys())
P 10 C 4
StdOut.println(s + " " + st.get(s));
M 9 E 12
}
X 7 H 5
H 5 L 11
C 4 M 9
keys S E A R C H E X A M P L ER 3 P 10
values 0 1 2 3 4 5 6 7 8 9 10 11 12A 8 R 3
E 12 S 0
output for output for
basic symbol table ordered S 0 X 7
(one possibility) symbol table
Keys, values, and output for test client
Reza Samavi L 11 COMP SCI 2XB3/ SOFT ENG 2XB3
A 8 Lecture 8 14 !X

P 10 C 4
ST test client for analysis

Frequency counter. Read a sequence of strings from standard input



and print out one that occurs with highest frequency.

% more tinyTale.txt
it was the best of times
it was the worst of times
it was the age of wisdom
it was the age of foolishness
it was the epoch of belief
it was the epoch of incredulity
it was the season of light
it was the season of darkness
it was the spring of hope
it was the winter of despair tiny example

(60 words, 20 distinct)

% java FrequencyCounter 1 < tinyTale.txt real example



it 10 (135,635 words, 10,769 distinct)

% java FrequencyCounter 8 < tale.txt real example

business 122 (21,191,455 words, 534,580 distinct)

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 15 "X

% java FrequencyCounter 10 < leipzig1M.txt


Outline
§  Applica6on of Search Algorithms

§  Why we need design paAerns?


§  MVC Design PaAern
§  MVC live example for developing a Web App

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 17 17


Beyond Objects

•  Func6onality comes from the interac6on of objects


•  OO provides useful and desirable program
characteris6cs:
-  Abstrac6on, Inheritance, Encapsula6on, Polymorphism
•  OO is a way of organizing the code that helps with
management of big programs

There is a higher level of OO organiza6on:


paOerns of object interac6on
© J. Christopher Beck 20
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 20
The Idea of Design PaOerns

•  Systems design is difficult


•  Idea:
-  Don’t redesign every system from first principles
-  Reuse designs/solu6ons that you used in the past
•  There are paAerns in systems design

© J. Christopher Beck 21
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 21
Like Engineers

•  Bridges, buildings, planes, etc. are rarely designed


from first principles
-  Modify an exis6ng design
-  Use “sub-designs” from other structures
•  This is really the defini6on of “experience”
•  Systems designers can do the same thing

© J. Christopher Beck 22
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 22
Outline
§  Applica6on of Search Algorithms

§  Why we need design paAerns?


§  MVC Design PaAern
§  MVC live example for developing a Web App

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 25 25


Model-View-Controller

•  Components are models, views, and controllers


•  Models store informa6on about the state of the program
•  E.g., a list of customer records, sales, orders
•  View components present informa6on to the user
-  E.g, webpage input/output, mobile app interface
•  Controllers allow user to control model and tell models
to update (the way the UI reacts to user input)

Reza Samavi Slide from SDCL, department of Informa6cs, UC Irvine


COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 26
Model-View-Controller
•  Key benefits:
-  Decouples model and view – one model can support
mul6ple views
-  Decouples view and controller – can change the way a
view responds to user input
-  Modularity, an6cipa6on of change
•  Examples
-  Websites
-  Web applica6ons
-  Mobile applica6ons

Slide from SDCL, department of Informa6cs, UC Irvine


Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 27
MVC Example

Instructor

sees uses

Mean Spreadsheet Barchart Scaling

updates changes

List of Marks

© J. Christopher Beck 28
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 28
MVC Diagram

User

sees uses

View View View Controller

updates changes

Model

29
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 29
MVC in the JSP/Servlet world
•  Model (Java code) Holds the real business logic and the state of an
applica6on
•  View (JSP) Responsible for the presenta6on.
•  Controller (Servlet) Takes the user input from the request and figures
out what it means to the model

User

sees uses

View View View Controller

updates manipulates
Model

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 30


MVC in the JSP/Servlet world
•  Model (Java code) Holds the real business logic and the state of an
applica6on
•  View (JSP) Responsible for the presenta6on.
•  Controller (Servlet) Takes the user input from the request and figures
out what it means to the model

Java Server Pages User

sees uses

HTML Pages +
View View View Controller
Java classes

updates manipulates
Model
Java classes
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 31
Outline
§  Applica6on of Search Algorithms

§  Why we need design paAerns?


§  MVC Design PaAern
§  MVC live example for developing a Web App

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 32 32


Example: Beer Advisor App

•  Development and deployment during the lecture…

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 33


Your first Web Applica5on in 30 minutes!
Beer Advisor Web Applica5on
1.  The web app is a beer advisor.

2.  The user uses an html page to get advice on beer


choices.

3.  User selects the color of the beer.

4.  The system responds with a beer recommenda6on.

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 34


MVC for Beer Advisor Applica5on

sees uses

Java Server Pages HTML Pages +


result: Servlet:
Brands Beer Select Java classes

changes
updates
Java App:
Beer Expert
Java classes

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 35


Environment
You need access to a JSP and Servlet web container for
Deployment Environment
•  Download and install Tomcat from hAp://tomcat.apache.org// (Version
6.0.xx or above)
•  If you don’t already have, download and install JSE Development Kit 7 from
hAp://www.oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-
download-432154.html
•  Set environment variables for JAVA_HOME and TOMCAT_HOME
•  Test Tomcat by launching tomcat/bin/startup script (use command line);
Point your browser to hAp://localhost:8080/ and you should be able to see
the Tomcat welcome page.

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 36


Your first Web Applica5on in 30 minutes!
Beer Advisor Web Applica5on
1.  The web app is a beer advisor. (the Model)

2.  The user uses an html page to get advice on beer


choices.

3.  User selects the color of the beer.

4.  The system responds with a beer recommenda6on.

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 37


MVC for Beer Expert Applica5on

sees uses

result: Servlet:
Brands Beer Select

changes
updates
Java App:
Beer Expert

1. The BeerExpert model


(Java)
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 38
Model: BeerExpert.java
package com.example.model;
import java.util.*;

public class BeerExpert {

public List getBrands(String color) {


List brands = new ArrayList();
if (color.equals("amber")) {
brands.add("Jack Amber");
brands.add("Red Moose");
} else {
brands.add("Jail Pale Ale");
brands.add("Gout Stout");
}
return brands;
}
}
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 39
Test the Model

•  Add a main method to test the Model off-line

public sta5c void main(String []args){


String color = "amber";
List brand = new BeerExpert().getBrands(color);
System.out.println(brand.get(0));
}

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 40


MVC for Beer Expert Applica5on

sees uses
2. html page view

result: Servlet:
Brands Beer Select

changes
updates
Java App:
Beer Expert

1. The BeerExpert model


(Java)
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 41
Input View - The HTML page as an input form

<html>
<head>
<title>Beer Selection</title>
</head>
<body>
<h1 align="center">Beer Selection Page</h1>

...insert the form here (next slide).

</body>
</html>

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 42


The HTML page, Cntd.

<form method="POST" action="SelectBeer.do">


Select beer characteristics:<p>
Color:
<select name="color" size="1">
<option>light</option>
<option>amber</option>
<option>brown</option>
<option>dark</option>
</select>
<br>
<br>
<center>
<input type="SUBMIT">
</center>
</form>

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 43


It starts with an HTML form... (User’s side)

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 44


MVC for Beer Expert Applica5on

sees uses
2. html page view

result: Servlet:
Brands Beer Select
3. The BeerSelect Controller
(Java)
changes
updates
Java App:
Beer Expert

1. The BeerExpert model


(Java)
Reza Samavi
COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 45
Controller: BeerSelect.java

package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

import com.example.model.BeerExpert; // notice this

public class BeerSelect extends HttpServlet {

... doPost method goes here. ..

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 46


Controller: BeerSelect.java …cntd.

public void doPost(HttpServletRequest request,


HttpServletResponse response)
throws IOException, ServletException {

String c = request.getParameter("color");

BeerExpert be = new BeerExpert();


List result = be.getBrands(c);

request.setAttribute("styles", result);
RequestDispatcher view =
request.getRequestDispatcher("result.jsp");
view.forward(request, response);
}

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 47


MVC for Beer Expert Applica5on

sees uses
1. html page
4. The result view (JSP)

result: Servlet:
Brands Beer Select
3. The BeerSelect Controller
(Java)
changes
updates
Java App:
Beer Expert

2. The BeerExpert model


(Java)
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 48
Result View: The JSP file

•  The JSP file must have the extension .jsp


•  It is basically HTML, plus a few JSP direc6ves
•  It receives the HttpServletRequest and the
HttpServletResponse objects
•  The HttpServletResponse object may have been
par6ally wriAen by the servlet (but it’s a bad idea)
•  The resultant HTML page goes back to the user

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 49


result.jsp
<%@ page import="java.util.*" %>

<html>
<body>
<h1 align="center">Beer Recommendations JSP</h1>
<p>

<%
List styles = (List)request.getAttribute("styles");
Iterator it = styles.iterator();
while (it.hasNext()) {
out.print("<br>TRY: " + it.next());
}
%>

</body>
</html>
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 50
MVC

•  BeerSelect.java acts as the controller


•  It delegates the actual work to a model,
BeerExpert.java
•  It delegates (forwards) the informa6on to a JSP page
that will provide the view
-  RequestDispatcher view =
request.getRequestDispatcher("result.jsp");
view.forward(request, response);

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 51


The model class

•  BeerExpert is the model class; it computes results


and adds them to the HttpServletRequest object
-  Not the HttpServletResponse object; that’s the HTML
output
•  It returns, in the usual fashion, to the BeerSelect
class, which will then forward it to the JSP

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 52


MVC for Beer Expert Applica5on

sees uses
1. html page
4. The result view (JSP)
3. The BeerSelect Controller
result: (Java) Servlet:
Brands Beer Select

changes
updates
Java App:
Beer Expert

2. The BeerExpert model


(Java)
Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 53
The deployment descriptor

•  The request goes to the server, with the ac6on


<form method="POST" action="SelectBeer.do">
•  The name "SelectBeer.do" is not the name of an actual file
anywhere; it is a name given to the user
•  It is up to the deployment descriptor to find the correct
servlet to answer this request
•  The deployment descriptor must be named web.xml

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 54


web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

...important stuff goes here...


</web-app>

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 55


web.xml ….cntd.

<servlet>
<servlet-name>Beer</servlet-name>
<servlet-class>
com.example.web.BeerSelect
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 56


Development Directory Structure

Workspace
src
com
example
web
BeerSelect.java
model
BeerExpert.java
form.html
result.jsp
web.xml
bin
com
example
web
BeerSelect.class
model
BeerExpert.class

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 57


Deployment Directory structure
jakarta-tomcat-x.x.xx/
| webapps/ ß this is hAp://localhost:<port#>/
| | firstApp/
| | | form.html
| | | result.jsp
| | | WEB-INF/
| | | | web.xml
| | | | classes/
| | | | | com/
| | | | | | example/
| | | | | | | model/
| | | | | | | | BeerExpert.class
| | | | | | | web/
| | | | | | | | BeerSelect.class
| | | | lib/

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 58


The result page (jsp)

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 59


Security maOers: The deployment descriptor

•  The request goes to the server, with the ac6on


<form method="POST" action="SelectBeer.do">
•  The name "SelectBeer.do" is not the name of an actual file
anywhere; it is a name given to the user
-  Partly, this is for security; you don’t want the user to have
access to the actual file without going through your form
-  The extension .do is just a conven6on used by this
par6cular book; no extension is necessary
•  It is up to the deployment descriptor to find the correct
servlet to answer this request
•  The deployment descriptor must be named web.xml

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 62


The process flow
•  The web-client (browser) makes a request for the form.html
page.
•  The container (tomcat) retrieves the form.html page
•  The container returns the page to the browser, where the
user answers ques6on on the form, and..
•  The browser sends (POST) the request data to the container
•  The container finds the correct servlet based on the URL,
and passes the request to the servlet
•  The servlet calls the BeerExpert for help
•  The expert class returns an answer, which the servlet adds
to the request object
•  The servlet forwards the request to the JSP
•  The JSP gets the answer from the request object
•  The JSP generates a page for the container
•  The container returns the page to the user

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 63


More references on MVC
•  MVC for mobile applica6on development
-  hAps://developer.apple.com/library/ios/documenta6on/General/
Conceptual/DevPedia-CocoaCore/MVC.html
-  hAps://blog.inf.ed.ac.uk/sapm/2014/02/13/architectural-paAerns-
for-mobile-applica6on-development/
-  Plakalovic, D., and D. Simic. "Applying MVC and PAC paAerns in
mobile applica6ons." arXiv preprint arXiv:1001.3489 (2010).

Reza Samavi COMP SCI 2XB3/ SOFT ENG 2XB3 Lecture 8 64

You might also like