• No results found

Class, in object-oriented approach, specifies: instance variables state (i.e. type ) of objects; methods operations on objects of a given type.

N/A
N/A
Protected

Academic year: 2022

Share "Class, in object-oriented approach, specifies: instance variables state (i.e. type ) of objects; methods operations on objects of a given type."

Copied!
8
0
0

Loading.... (view fulltext now)

Full text

(1)

Lecture 5: Classes and Objects Slide Number 1

Lecture 5: Classes and Objects

ØDefine:

• Class schemas to specify object classes(i.e.“types” of objects).

»models are possible object instances of a class.

• Bag-schemas to specify collections of object instances of a given class.

»models are specific collections of object instances− i.e. bags of models of a class schema.

ØIllustratethese two types of schema using a Library example.

Aims and Objectives

So far we have seen how state schemas can be used to represent possible states of a given system.

States are snapshots, over time, of a system behavior. System operations, formalised via operation schemas, are instead possible ways a system can change its state.

In this lecture we will see other two types of schema, called “class” schema and “bag-schema”

respectively. Class schemas are used to specify “classes” (or “types”) of objects, in terms of characteristic properties and main operations (i.e. methods) allowed on them. Each object of a given class is a model of its class schema. Bag-schemas are instead used to specify particular collections of object instances of a given class. Their models are therefore “bags” of models of their corresponding class schemas. Why are we interested in these schemas? Because they enable us to formally specify systems that include collections of objects (with relationships between them) and operations over these collections.

We will illustrate the definition and use of these two types of schema via the example of a library system. This is, in fact, a system that refers to different object classes, such as “book_copy”,

“borrower”, etc., as well as to collections of objects, such as collections of specific book copies or specific groups of borrowers.

Lecture 5: Classes and Objects Slide Number 2

Classes

• Class, in object-oriented approach, specifies:

– instance variables – “state” (i.e. “type”) of objects;

– methods – “operations” on objects of a given type.

• Class schemas for objects classes.

• Overall system is more complicated:

– a set of objects

– each object has a type−is a model of its class schema – system operations can operate on individual objects – … or can change set of objects (e.g. create, destroy)

• System contains bags or multisets of models of class schemas.

Within an object-oriented approach, classes specify “types” of objects, and methods (i.e.

“operations”) on the objects of that type. An object is an instance of a class, and the methods of that class are operations that can be applied to each of its object instances.

Analogously, we will define the notion of a class schema to specify the “type” of an object. For each class schema, operation schemas can be defined to specify the operations that are allowed on each object of that class.

Systems, specified by means of these schemas, are in general complex systems, which include objects of different types, and for each type, collections of object instances. From the specification point of view, given an object type and its associated class schema, an object instance is a model of the class schema, and operations on object instances are models of the operation schemas

associated with that class schema. For instance in a library system, “moving a copy of a book from one location to another” would be an example of an operation on objects instances “books”.

Complex systems also operate on collections of objects. For example, “moving a copy of a book in a specific collection of books”. This can be described as an operation over collections of objects, rather than individual books. To describe collections of objects of a given type we will use the notion of “bag-schemas”. Bag-schemas are always associated with class schemas. Given an object type, and therefore a class schema, models of a bag-schema defined over this type are “multisets”

of models of its class schema, i.e. multisets of objects instances that form a collection. Let’s see now what a “bag” is.

(2)

3

Lecture 5: Classes and Objects Slide Number 3

Bags

… half way between sets and lists.

• In a list: order and multiplicity all matter.

– [3,5], [5,3], [3,3,5] are all different lists

• In a set: order and multiplicity make no difference

– {3,5}, {5,3}, {3,3,5}are all the same set

• In a bag: multiplicity matters, order doesn’t.

– {|3,5|}, {|5,3|} are the same bag, – {|3,3,5|} is different

The system has a bag of models of class schema, because different objects may have the same class.

What are bags? A bag is half way between a set and a list. In a set the order of the elements and their multiplicities do not make any difference. So, for instance, the sets {3,2,5,5} and {3,2,5} are the same sets. In a list both multiplicity and order matters. For instance [2,2,5] and [2,5] are different lists, and the lists [2,5] and [5,2] are also different.

A bag is instead a collection of elements where multiplicity matters but not ordering. We denote this type of collection with the symbol {| |}. For instance, {|3,3,5|} and {|3,5|} are two different bags, since the first bag has two occurrences of the element 3 and the second bag has only one.

On the other hand, {|3,5|} and {|5,3|} are the same bags because the elements 3 and 5 have the same number of occurrences.

In mathematical terms, bags are called “multisets”.

Systems may refer to a collection of objects instances that belong to the same class (i.e. are of the same type). Such a collection can be specified as a bag (multiset) to express multiplicities of objects of the same type.

4

Lecture 5: Classes and Objects Slide Number 4

How to define a bag

e.g. a bag of integers.

Define a set X (“indexing set”) and a family of integers indexed by X. e.g.

– {|3,5|}: X any 2-element set,e.g. {Harry, Tom}

aHarry= 3, aTom= 5, where “a” denotes the bag – {|3,3,5|}: X any 3-element set,e.g.{Tom, John, Harry}

bTom= bJohn= 3, bHarry= 5, where “b” denotes the bag

Why the funny X’s? Why not just {1,2} or {1,2,3}?

– Just as example to emphasize that X does not have to have any intrinsic ordering – if it did we’d just have lists.

This definition of bags somewhat similar to the notion of arrays in programming languages. The bag is supposed to have a name. In the example above we have used “a” and “b” as names for {|3,5|} and {|3,3,5|} respectively. Each element of the bag is denoted by indexing the name of the bag with each element of X. For instance, the second element of the second bag is denoted by bJohn.

X has, therefore, the same cardinality of the bag; different occurrences of the same element will be denoted by different indexes. For instance the first occurrence of 3 in the second bag is given by bTomand the second occurrence is given by bJohn.

(3)

Lecture 5: Classes and Objects Slide Number 5

About the indexing set

• Indexing set X provides “individual identities” for elements of bag.

» cf. addresses of objects: X = {addresses used}

• Two different objects may have same type, but they’ll still be distinguished by their different addresses.

• Finite bag – when X is finite.

One advantage of using indexing sets is that we can provide individual identities to the elements of a bag and still preserve its finiteness.

Lecture 5: Classes and Objects Slide Number 6

Schemas whose models are bags

Assume a set of elements Y and an indexing set X, then:

What’s X0for?

• It’s just a trick to make sure X is finite:

– X0is declared as a finite subset of X;

– the axioms says X is a subset of X0. – Hence X0= X and both are finite.

Bag(Y) [X]

f: X→Y

FinBag(Y) [X]

X0: FX f: X→Y

∀x:X. x∈X0 Model = bag of elements Y.

Model = finite bag of elements Y.

In this slide we have two schema definitions for bags. The schema in the left-hand side does not necessarily enforce finiteness. The indexing set X can well be infinite. The schema in the right-hand side guarantees, instead, the finiteness of the bag, as explained in the blue box above. Since X0a finite set, the double inclusion between X0and X implies that X is a finite indexing set. Since X has the same cardinality of Y the set Y of elements is also finite.

(4)

7

Lecture 5: Classes and Objects Slide Number 7

Bags of models of schemas

» What we saw before was bags of elements of sets.

» Given one schema S, we can define a new “bag- schema” whose models are bags of models of S.

» Simple, when S does not include any base sort.

– Add a new base sort X for indexing set.

– Constants become functions from X.

– Functions and predicates gain an extra argument of sort X.

In the previous slide we have defined a schema for a bag of elements of a given set Y. In the next few slides, we will define a schema for bags of elements that are models (i.e. instances) of a class schema. Such a new schema is a bag-schema. The idea is simple, in particular when the class schema does not include base sorts.

Consider a (class) schema S of the form given below. Its associated bag-schema, which defines a collection of elements of type S, is constructed by adding a base sort that is the indexing set.

Define each constant in S as a function in the bag-schema, and add an extra-argument to each function and predicate symbol defined in S. A general example is given below. Models of this bag-schema are bags of models of the class schema S. In fact, consider a particular index value x in the bag-schema. The function a applied to x (i.e. a(x)) gives an element of sort1, the function f gives a function from sort2 to sort3 and the predicate P with the first argument fixed to x gives a collection of pairs of elements from the Cartesian product sorti×sortj. These together constitute a model of the class schema S.

Bag_schema(S)[X]

a: X→sort1 f: X×sort2→sort3 P⊆X×sorti×sortj S

a: sort1 f: sort2→sort3 P⊆sorti×sortj

8

Lecture 5: Classes and Objects Slide Number 8

Example: a library

Making many simplifying assumptions:

• Books are described by ISBNs (International Standard Book Numbers)

• Library may have more than one copy of the same book.

• Each copy has a location comprising a library (e.g. central or departmental) and a shelf-mark.

• Some copies may be identical twins of each other (same book, same location) but they are still distinct.

• Borrowers have id codes and upper limit on no. books they can borrow.

These are the main (simplified) features of a library system. Such features need to be taken into account when specifying the system. There are two types of objects: a (book) copy and a borrower. Therefore we would expect to define two different class schemas. The class schema for the book copy will include information such as the ISBN number, the location in the library and its status. These are the main features that each individual book copy will have.

The class schema for the borrower will specify the main features of a borrower, i.e. his/her id code and the upper limit of number of books that he/she is allowed to borrow.

(5)

Lecture 5: Classes and Objects Slide Number 9

Copies as objects in a class

• LOCATION = LIBRARY × SHELF-MARK – (e.g. LIBRARY = {Maths, Physics, …}

– SHELFMARK = … whatever.

– We don’t need to know the details.)

• COPYSTATUS = {in, out}

Copy isbn: seq CHAR location: LOCATION status: COPYSTATUS

Here is the definition of a class schema for a (book) copy. The ISBN is given by a sequence of characters, whereas the sorts for the location and the copy status need to be defined. These are, respectively, given by the sorts LOCATION and COPYSTATUS defined above.

Lecture 5: Classes and Objects Slide Number 10

Initialization, operations on copies

NewCopy Copy

isbn?: seq CHAR location?: LOCATION isbn = isbn?

location = location?

status = in

MoveCopy

∆Copy

to?: LOCATION isbn' = isbn status' = status location' = to?

The schema on the left-hand side defines an initialisation operation: the introduction of a new (book) copy in the library. This is an operation on an individual instance of the class schema Copy. Therefore it includes in its signature the class schema Copy in order to refer to the features of the specific new book. The axioms define these features for the specific new book copy, i.e. they identify a specific model of the class schema Copy. This schema is like instantiating a specific object in a class.

The schema on the right-hand side defines instead an operation on individual book copies: the relocation of a book. This operation changes some of the data of an individual book. Therefore, the schema includes in its signature a∆Copy. This specifies that some information about the specific book is changed by the operation. Specifically, the book’s location is changed, whereas its isbn and status are left unchanged, as stated by the axioms.This is an example of an operation schema associated with a given class schema.

(6)

11

Lecture 5: Classes and Objects Slide Number 11

Bag of copies

• Elements of stock identify individual copies held.

• For each x∈stock, values isbn(x), location(x) and status(x) are the values needed to define a model of Copy.

• Collection is FinBag(Copy): a model of Collection is a finite bag of models of Copy.

Collection(Copy)[stock]

stock0: F(stock) isbn: stock→seq CHAR location: stock→LOCATION status: stock→COPYSTATUS

∀x:stock. x∈stock0 (forces stock to be finite)

The above schema is instead a bag-schema constructed from the class schema Copy. “stock” is an indexing set. Each element of stock identifies an individual book copy held in the library. In fact, for a given stock value, the functions isbn, location and status give exactly the information needed to identify a book copy, i.e. to define a model of Copy. The axiom guarantees that the collection of book copies in the library is finite. This is an example of a finite bag of models of a class schema, or of a finite bag of objects of the same type.

12

Lecture 5: Classes and Objects Slide Number 12

Promotion

Operations on individual copies are “promoted” to give operations on the entire collection.

e.g. MoveCopy: moving one copy in collection –

MoveOneCopy

∆Collection moving?: stock to?: LOCATION stock' = stock

isbn' = isbn∧status' = status

∀x:stock. (location'(x)=location(x)∨x = moving?) location'(moving?) = to?

Operations on individual objects can be defined also to be operations on a collection of objects of the same type. This is an example of how the operation of moving a book copy in the library can be redefined as “moving a book copy within a given collection”. This operation affects therefore the specific collection of book copies. This is specified by including in the signature the∆Collection.

The object to move is specified by its index (e.g. moving?:stock) and the new location is the input variable to?. The operation does not affect the indexing set. So stock’=stock. Similarly for the data “isbn” and “status” of all the book copies in the collection. What is changed is the location of the specific book copy that needs to be moved. The notation “location(x)”, where x is the index in the collection that identifies the book copy, gives the current location of the book copy.

(7)

Lecture 5: Classes and Objects Slide Number 13

NewCopy:

creating one new copy in collection

OneNewCopy

∆Collection isbn?: seq CHAR location?: LOCATION new!: stock'

new!∉stock

stock' = stock∪{new!}

∀x:stock. (isbn'(x) = isbn(x)

∧location'(x) = location(x)

∧status'(x) = status(x)) isbn'(new!) = isbn?

location'(new!) = location?

status'(new!) = in

Creation

Similarly for the operation of introducing a new book copy. In this case the operation is defined with respect to a collection where the new book copy is introduced. Therefore, the operation schema changes the collection, since a new book is added to it. Thus, its signature includes

∆Collection. The data isbn? and location? for the new book copy are given as input. The indexing set needs to be expanded with a new index for the new element, i.e. new!.

The first axiom states that the index new! is in fact a new index. The second axiom defines the new indexing set stock'. The third axiom states that the operation does not change any information on the books which are already in the collection. In the remaining axioms, the data of a new book is instantiated by using the input variables.

Lecture 5: Classes and Objects Slide Number 14

Other classes

System is more than just a collection of objects from a single class.

Suppose it also has

• a class Borrower:

•a corresponding bag-schema

– Borrowers = FinBag(Borrower), with indexing set “borrower”.

Borrower id: seq CHAR max:NN

(max = max. no. books to be borrowed) Library

Collection Borrowers

borrowed:FF(borrower×stock)

∀x: stock. (status(x) = out→ ∃y:borrower. (y,x)∈borrowed)

∀y:borrower. |{x:stock | (y,x)∈borrowed}|≤max(y)

In general, systems include more than just a collection of objects of the same type. In this example of a library system, we could for instance identify other classes and associated bag- schemas. An example is the class “Borrower”. This defines the type borrower. Each borrower has to have an id and a maximum number of book copies that he/she can borrow. The collection “Borrowers” is instead a finite bag of borrowers.

The library system can therefore be defined as composed by a collection of book copies, a collection of borrowers, and a set of borrowed book copies. The first axiom specifies that any book out of the library must be assigned to a borrower, whereas the second axiom states that the number of book copies borrowed by a borrower cannot be more than his/her maximum number allowed.

(8)

15

Lecture 5: Classes and Objects Slide Number 15

Example

Borrow

∆Library x?:stock y?:borrower status(x?) = in

|{x:stock. (y?,x)∈borrowed}| < max(y?) stock' = stock∧borrower' = borrower isbn' = isbn∧location' = location id' = id∧max' = max

∀x:stock. (status'(x) = status(x)∨x = x?) status'(x?) = out

borrowed' = borrowed∪{(y?,x?)}

This is the operation schema for the operation of borrowing a book copy from the library. The operation changes the status of the library (so it includes∆Library). x? and y? are the input to the operation.

The operation can be applied only if the book to borrow is available in the library, and the borrower hasn’t reached yet his/her maximum number of books that he/she can borrow. These two conditions for the operation to take place are specified by the first two axioms. The remaining axioms define the operation.

References

Related documents

The present work evaluates the nutritional and feed value of fermented sweet potato meal (ProEn-K TM ) to replace soybean meal in the diet of juvenileP.

In Figure 7 we present the mid-IR luminosity versus low- frequency radio luminosity (at 151-MHz) for the radio sources in our samples. In both plots we see a correlation between

Division of Air Quality State Local Programs Federal Public Associations Central Office Regional Offices -Complaints -Stakeholders -Public Hearings • Business / Industry •

Figures 8-2 through 8-9 depict the census block groups or census tracts (in the case of households below the poverty level and zero-vehicle households) within the airport study

In order to provide context for the body of public art examined in Chapters 4 and 5, this chapter examines post-contact art practices of Aboriginal people in southern Sydney since

Solving and Dissolving Musical Affection: A Critical Study of Spotify and Automated Music Recommendation in the 21st Century.. A dissertation submitted in partial satisfaction of

The proposed divestiture will ensure the continuation of the Delaware Divestiture Assets as an ongoing, viable Psychiatric Hospital Facility by giving control of the

A 70-year old retired employee with a 10cm mass in the right upper lobe diagnosed to be squamous cell carcinoma from bronchial biopsy suddenly developed marked facial