Implementing an ADT Example: Sum-Constraint ADT Part II Ohad Barzilay May 2004.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Stacks, Queues, and Linked Lists
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
The Singleton Pattern II Recursive Linked Structures.
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
CHAPTER 7 Queues.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
Stacks Cmput Lecture 18 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Cmput Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:
The List Interface Cmput Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
CHAPTER 4 RECURSION. BASICALLY, A METHOD IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
Information and Computer Sciences University of Hawaii, Manoa
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.1 CSC7322: Object Oriented Development J Paul Gibson, A207
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
CS 2430 Day 12. Agenda for today Container class example: DateList Growable container classes.
Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013.
CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString.
TOWERS OF HANOI. : If n = 1, move disk 1 from pole 'A' to pole 'B'. else: 1.First, move n-1 disks from pole 'A' to pole 'C', using pole 'B' as.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Important Annoucement 1  I messed up something in the last class  if a subclass overrides a method that throws an exception then it must either 1. throw.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Linked Data Structures
CHAPTER 4: Linked Structures
Section 2.6 Linked List StringLog ADT Implementation
Single-Linked Lists.
The Class ArrayLinearList
Lecture 17: Polymorphism (Part II)
null, true, and false are also reserved.
Data Structures ADT List
Testing change to a linked list
slides created by Ethan Apter
Building Java Programs
slides created by Ethan Apter
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
Lecture 18: Polymorphism (Part II)
ITI Introduction to Computing II Lab-12
Hashing based on slides by Marty Stepp
Outline Software Development Activities
slides created by Ethan Apter and Marty Stepp
CSC 205 – Java Programming II
Presentation transcript:

Implementing an ADT Example: Sum-Constraint ADT Part II Ohad Barzilay May 2004

Ohad Barzilay, April Cell implementation /** * A cell contains a value, and may be connected to * several constraint ports. * connections != null, "Non-null connections" */ public class Cell { protected Object _value; protected ListSet connections;

Ohad Barzilay, April Cell implementation /** * Creates a new Cell with no value. * value() == null, "Null value" */ public Cell() { connections = new ListSet(); _value = null; } /** * Creates a new Cell with a given value. * value() == new_val, "Value stored" */ public Cell(Object new_val) { this(); _value = new_val; }

Ohad Barzilay, April value() /** * Returns the value stored in this Cell. */ public Object value() { return _value; } Don’t access fields directly No postcondition

Ohad Barzilay, April add_connection /** * Adds a connection (a 'constraint - port' * pair) to the Cell. * c != null, "Non-null constraint" c.port_valid(port), "Valid port" */ public void add_connection(Constraint c, int port) { connections.extend(new Pair(c, new Integer(port))); }

Ohad Barzilay, April set_value /** * Sets the value stored in the Cell to the given one. * Notifier is the user program. * new_val != null, "Non-null new value" value() == new_val, "Value stored" */ public void set_value(Object new_val) { set_value(new_val, null, 0); }

Ohad Barzilay, April set_value /** * Sets the value stored in the Cell to the given one. * If null, notifier is the user program. * new_val != null, "Non-null new value" notifier == null || notifier.port_valid(port), * "Valid port" notifier == null || value() == null, * "Constraint cannot override existing value" * value() == new_val, "Value stored" */ public void set_value(Object new_val, Constraint notifier, int port)

Ohad Barzilay, April set_value public void set_value(Object new_val, Constraint notifier, int port) { Set agenda = null; if (_value != null) { agenda = new ListSet(); forget(agenda); } _value = new_val;...

Ohad Barzilay, April set_value... Linear iter = connections.iterator(); for (iter.start(); !iter.after(); iter.forth()) { Pair connection = (Pair)iter.item(); Constraint c = (Constraint)connection.car(); int p = ((Integer)connection.cdr()).intValue(); if (!(c == notifier && p == port)) c.new_port_value(p); } if (agenda != null) { iter = agenda.iterator(); for (iter.start(); !iter.after(); iter.forth()) ((Constraint)iter.item()).deduce(); }

Ohad Barzilay, April forget /** * Begins a value retraction chain from this Cell. * When finished, the value of this Cell, and every value * in the network depending on it will be retracted. Then * it will be deduced if possible. */ public void forget() { Set agenda = new ListSet(); forget(agenda); Linear iter = agenda.iterator(); for (iter.start(); !iter.after(); iter.forth()) ((Constraint)iter.item()).deduce(); }

Ohad Barzilay, April forget /** * A link within a retracting chain. * When finished, the value of this Cell will be retracted. * agenda != null, "Non-null agenda" value() == null */ protected void forget(Set agenda) { if (_value != null) { _value = null; Linear iter = connections.iterator(); for (iter.start(); !iter.after(); iter.forth()) { Pair connection = (Pair)iter.item(); Constraint c = (Constraint)connection.car(); int p = ((Integer)connection.cdr()).intValue(); c.forget(p, agenda); }

Ohad Barzilay, April toString public String toString() { StringBuffer result = new StringBuffer("<Cell: "); result.append(value() == null ? "---" : value().toString()); result.append(">"); return result.toString(); }

The Solution Implementing SumConstraint

Ohad Barzilay, April SumConstraint public class SumConstraint extends Constraint { public static final int ADDEND = 0; public static final int AUGEND = 1; public static final int SUM = 2; public static final int NUM_OF_PORTS = 3; protected Cell[] ports; protected boolean[] _deduced;

Ohad Barzilay, April SumConstraint public SumConstraint(Cell addend, Cell augend, Cell sum) { ports = new Cell[NUM_OF_PORTS]; ports[ADDEND] = addend; ports[AUGEND] = augend; ports[SUM] = sum; addend.add_connection(this, ADDEND); augend.add_connection(this, AUGEND); sum.add_connection(this, SUM); _deduced = new boolean[NUM_OF_PORTS]; for (int i = 0; i < NUM_OF_PORTS; i++) _deduced[i] = false; deduce(); }

Ohad Barzilay, April SumConstraint - contract /** * Creates a new SumConstraint which is connected to the * given Cells. * The Cells are fixed during the life of the constraint. * addend != null, "Non-null addend" augend != null, "Non-null augend" sum != null, "Non-null sum" ($prev(addend.value()) == null) || defined(ADDEND), * "Addend defined if cell had value" ($prev(augend.value()) == null) || defined(AUGEND), * "Augend defined if cell had value" ($prev(sum.value()) == null) || defined(SUM), * "Sum defined if cell had value" */ public SumConstraint(Cell addend, Cell augend, Cell sum)

Ohad Barzilay, April port_valid /** * Returns true if the given port is one of * ADDEND, AUGEND or SUM. */ public boolean port_valid(int port) { return 0 <= port && port < NUM_OF_PORTS; }

Ohad Barzilay, April get_port /** * Returns the value stored in the given port. * port_valid(port), "Valid port" * known(port), "Port value is known" // * possible, but better: !known(port) == ($ret == null), * "Unknown iff null" */ public Object get_port(int port) { return ports[port].value(); }

Ohad Barzilay, April defined /** * returns true iff the given port is defined. * port_valid(port), "Valid port" * $ret == (!deduced(port) && get_port(port) != null), * "Defined iff has value but not deduced" */ public boolean defined(int port) { return !_deduced[port] && ports[port].value() != null; }

Ohad Barzilay, April deduced /** * Is the port deduced? * port_valid(port), "Valid port" * $ret == (!defined(port) && get_port(port) != null), * "Deduced iff has value but not defined" !$ret || (((Integer)get_port(SUM)).intValue() == * ((Integer)get_port(ADDEND)).intValue() + *((Integer)get_port(AUGEND)).intValue()), * "Deduction obeys constraint" */ public boolean deduced(int port) { return _deduced[port]; }

Ohad Barzilay, April known /** * Returns true if this port is either defined or deduced. * port_valid(port), "Valid port" * $ret == (get_port(port) != null), * "Known iff port is non-null" */ public boolean known(int port) { return ports[port].value() != null; }

Ohad Barzilay, April contradiction /** * Returns true if all the ports are defined, and the sum of the addend * and the augend port's values isn't equal to the value of the sum port. * $ret == (defined(ADDEND) && defined(AUGEND) && defined(SUM) * && (((Integer)get_port(ADDEND)).intValue() + * ((Integer)get_port(AUGEND)).intValue() != * ((Integer)get_port(SUM)).intValue())), * "Contradiction iff constraint not obeyed" */ public boolean contradiction() { return defined(ADDEND) && defined(AUGEND) && defined(SUM) && (((Integer)get_port(ADDEND)).intValue() + ((Integer)get_port(AUGEND)).intValue() != ((Integer)get_port(SUM)).intValue()); }

Ohad Barzilay, April new_port_value /** * Inform constraint of new value in port. To be called by * connected cell after changing the cell's own value. * port_valid(port), "Valid port" * defined(port), "Port defined" !deduced(port), "Port not deduced" */ protected void new_port_value(int port) { deduced[port] = false; deduce(); }

Ohad Barzilay, April forget /** * Retracts the value from the given port recursively. To be * called by connected cell after it forgot its own value. If the * given port is not defined and/or is deduced nothing happens. * The constraint adds itself to the agenda in order to check * deductions after the process is finished. * port_valid(port), "Valid port" agenda != null, "Non-null agenda" * !defined(port), "Port undefined" */ protected void forget(int port, Set agenda) { agenda.extend(this); retract(agenda); }

Ohad Barzilay, April retract /** Retract previous deduction if no longer valid. */ protected void retract(Set agenda) { if (_deduced[ADDEND] && !(defined(AUGEND) && defined(SUM))) { _deduced[ADDEND] = false; ports[ADDEND].forget(agenda); } else if (_deduced[AUGEND] && !(defined(ADDEND) && defined(SUM))) { _deduced[AUGEND] = false; ports[AUGEND].forget(agenda); } else if (_deduced[SUM] && !(defined(ADDEND) && defined(AUGEND))) { _deduced[SUM] = false; ports[SUM].forget(agenda); }

Ohad Barzilay, April deduce /** Attempt to deduce new information for ports in this constraint. */ protected void deduce() { if (defined(ADDEND) && defined(AUGEND) && !known(SUM)) { _deduced[SUM] = true; ports[SUM].set_value( new Integer(((Integer)get_port(ADDEND)).intValue() + ((Integer)get_port(AUGEND)).intValue()), this, SUM); }...

Ohad Barzilay, April deduce... if (defined(ADDEND) && !known(AUGEND) && defined(SUM)) { _deduced[AUGEND] = true; ports[AUGEND].set_value (new Integer(((Integer)get_port(SUM)).intValue() - ((Integer)get_port(ADDEND)).intValue()), this, AUGEND); } if (!known(ADDEND) && defined(AUGEND) && defined(SUM)) { _deduced[ADDEND] = true; ports[ADDEND].set_value (new Integer(((Integer)get_port(SUM)).intValue() - ((Integer)get_port(AUGEND)).intValue()), this, ADDEND); }

Ohad Barzilay, April port_string /** port_valid(port), "Valid port" */ protected String port_string(int port) { if (defined(port)) return "defined(" + get_port(port).toString() + ")"; else if (deduced(port)) return "deduced(" + get_port(port).toString() + ")"; else return "---"; }

Ohad Barzilay, April toString public String toString() { StringBuffer result = new StringBuffer("<Sum: "); result.append(port_string(ADDEND)); result.append(" + "); result.append(port_string(AUGEND)); result.append(" = "); result.append(port_string(SUM)); if (contradiction()) result.append(" ***"); result.append(">"); return result.toString(); }

Questions ?