1 / 28

Implementing an ADT

Implementing an ADT. Example: Sum-Constraint ADT Ohad Barzilay April 2004. Agenda. We will implement the sum constraints presented here on 17/03/2004 This problem set was originally given as problem set 3 (ps3) on 2002 http://www1.idc.ac.il/oosc/handouts02.htm. The Problem.

avidan
Download Presentation

Implementing an ADT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Implementing an ADT Example: Sum-Constraint ADT Ohad Barzilay April 2004

  2. Agenda • We will implement the sum constraints presented here on 17/03/2004 • This problem set was originally given as problem set 3 (ps3) on 2002 http://www1.idc.ac.il/oosc/handouts02.htm

  3. The Problem

  4. The Sum-Constraint ADTOperations (functions) • make: creates a new sum constraint, all ports are uninitialized. • get-port: return the value of a given port. • defined: return true iff the value of the port was externally set. • deduced: return true iff the value of the port was internally deduced.

  5. The Sum-Constraint ADTOperations (functions) • contradiction: return true iff the constraint is in a contradictory state. • update: set the value of a port. • forget: remove the value of a port, if it has an externally-set value; otherwise do nothing.

  6. The Sum-Constraint ADT • TYPES • SC[G] • FUNCTIONS • make: → SC[G] • get-port: SC[G] × SUM-PORT  G • defined: SC[G] × SUM-PORT → BOOLEAN • deduced: SC[G] × SUM-PORT → BOOLEAN • contradiction: SC[G] → BOOLEAN • update: SC[G] × G × SUM-PORT → SC[G] • forget: SC[G] × SUM-PORT → SC[G]

  7. The Cell • Since we want to support a network of constraints, we will use a new data type, called cell, to hold the values of the constraint ports. • In this representation, each constraint port is connected to exactly one cell, but each cell may be connected to any number of constraints. • Constraints do not keep any values; instead, they refer to the associated cells.

  8. set_value(1) new_port_value(ADDEND) new_port_value(ADDEND) + + set_value(3) set_value(7) new_port_value(AUGEND) new_port_value(AUGEND) set_value(2) set_value(4) Implementing Constraints 1 3 2 7 4

  9. + + set_value(5) new_port_value(SUM) forget(SUM, …) A Contradiction 1 3 2 5 7 4

  10. forget(ADDEND,…) + + forget(…) forget(AUGEND,…) forget() Forgetting 1 3 2 5 7 4

  11. + + new_port_value(SUM) set_value(1) deduce() set_value(0) deduce() Forgetting 1 1 0 5 7 4

  12. The Cell • When constraints are created, they are supplied with their associated cells as parameters to the constructor. • The constraint is responsible for notifying the associated cells of the connection by calling the cells’ add_connection method. • Once created, the connections cannot be removed. • Note that the connection is between a specific constraint port and the cell • it is possible, for example, for two different ports of the same constraint to be connected to the same cell.

  13. External interface • The external interface for modifying the values stored in the constraint-propagation network is only through the cells, using the methods set_value and forget. • These methods notify the connected constraints of the change by the constraint methods new_port_value and forget, respectively.

  14. On a change • When a constraint is notified of a change in the value of a port, it tries to perform any deductions it can, and if possible, notifies associated cells of their new values. • The constraint records the fact that it made this deduction (and the values it depends on, if necessary). • The cell calls new_port_value for all its connections except for the constraint/port combination that notified it of its new value (this would incorrectly change the status of the port from “deduced” to “defined”.)

  15. An agenda • Similarly, when a constraint is told to forget a value that was used to deduce the value of another port, it must tell the associated cell to forget that value. • The cell will propagate this retraction, but it would be incorrect to make any deductions during this process. Instead, the first cell that initiates the retraction is responsible for making possible deductions after all retractions have been made. In order to do this, both cells and constraints have a forget method that takes a Set-valued parameter, called agenda. • Each constraint that forgot a value during the retraction process puts itself on the agenda. When the retraction process has finished, the initiating cell then calls the deduce method of each constraint on the agenda. • This method will cause the constraint to try to make any new deductions it can. Of course, it is possible that the constraint is unable to make any deduction, and in this case it will return without making any change. As usual, a deduction can cause a change of further deductions throughout the network.

  16. set_value(0) * * * 0 0 0 The Need for an Agenda 0

  17. forget() * * * 0 0 0 The Need for an Agenda 0

  18. Constraint abstract class package cp; import IL.ac.idc.sets.*; publicabstractclass Constraint { public abstract boolean port_valid(int port); public abstract Object get_port(int port); public abstract boolean defined(int port); public abstract boolean deduced(int port); public abstract boolean known(int port); public abstract boolean contradiction(); protected abstract void new_port_value(int port); protected abstract void forget(int port, Set agenda); protected abstract void deduce(); }

  19. Constraint abstract class • The method port_valid checks whether the port number is legal for the given type of constraint. • The method known is provided for convenience; a port is known if it is defined or deduced. • The new_port_value and forget methods are for use only by Cell; the constraint should add itself to the agenda. • The SumConstraint class should define constants for ADDEND, AUGEND, and SUM, with the values 0, 1, and 2, respectively. • The constant NUM_OF_PORTS will be 3, to denote the total number of ports available for this type of constraints

  20. The Cell interface • public Object value() • returns the value stored in the cell • public void add_connection(Constraint c, int port) • adds a connection to a specific port • public void set_value(Object new_val) • used to set the value externally (that is, by any part of the application except for a constraint)

  21. The Cell interface • public void set_value(Object new_val, Constraint notifier, int port) • used to set the value by a connected constraint • public void forget() • used to make the cell forget its current value by any part of the application except for a constraint • protected void forget(Set agenda): • used to make a cell forget its current value; for use only by Constraints

  22. The Solution

  23. SumConstraint publicclassSumConstraintextendsConstraint { publicstaticfinalintADDEND=0; publicstaticfinalintAUGEND=1; publicstaticfinalintSUM=2; publicstaticfinalintNUM_OF_PORTS=3; protectedCell[]ports; protectedboolean[]_deduced;

  24. SumConstraint publicSumConstraint(Celladdend,Cellaugend,Cellsum) { ports=newCell[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=newboolean[NUM_OF_PORTS]; for(inti=0;i<NUM_OF_PORTS;i++) _deduced[i]=false; deduce(); }

  25. SumConstraint - contract /** *CreatesanewSumConstraintwhichisconnectedtothe * givenCells. *TheCellsarefixedduringthelifeoftheconstraint. * *@preaddend!=null,"Non-nulladdend" *@preaugend!=null,"Non-nullaugend" *@presum!=null,"Non-nullsum" *@post($prev(addend.value())==null)||defined(ADDEND), *"Addenddefinedifcellhadvalue" *@post($prev(augend.value())==null)||defined(AUGEND), *"Augenddefinedifcellhadvalue" *@post($prev(sum.value())==null)||defined(SUM), *"Sumdefinedifcellhadvalue" */ publicSumConstraint(Celladdend,Cellaugend,Cellsum)

  26. Cell implementation /** *Acellcontainsavalue,andmaybeconnectedto *severalconstraintports. * *@invariantconnections!=null,"Non-nullconnections" */ publicclassCell { protectedObject_value; protectedListSetconnections;

  27. Cell implementation /** *CreatesanewCellwithnovalue. * *@postvalue()==null,"Nullvalue" */ publicCell() { connections=newListSet(); _value=null; } /** *CreatesanewCellwithagivenvalue. * *@postvalue()==new_val,"Valuestored" */ publicCell(Objectnew_val) { this(); _value=new_val; }

  28. Questions ?

More Related