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

MAKERERE UNIVERSITY

COLLEGE OF COMPUTING AND INFORMATION SCIENCES


SCHOOL OF COMPUTING AND INFORMATICS TECHNOLOGY
BACHELOR OF SCIENCE IN COMPUTER SCIENCE
COURSE WORK
COURSE NAME: FORMAL METHODS
BY : Mr.BITWIRE ALBERT GEORGE
COURSE CODE :BSE 2105

No. Student Name REG NO STUDENT NUMBER


1 MUGUME IAN 18/U/25478/PS 1800725478
A Z schema for a WeatherMap
database and vending machine.
This document presents a simple Z specification of a Weather Map and a
vending machine. It is intended to illustrate the use of the Z schema calcu-
lus and data structures in presenting readable schemas. Also this document
demonstrates the use of Latex to typeset Z specifications.

Question 1.

Weather Map Specification: the state space.

In this first part of the document, i produced a specification of Weather


Map given the following constraints.

ˆ A Weather map displays various meteorological features across a par-


ticular area ata particular point in time.

ˆ Each region has temperature associated with it.

ˆ The Weather Map contains a number of regions and temperature.

ˆ Temperature for each region has got variations from other regions mak-
ing it unique.

Types required:
[REGION , TEMPERATURE ]

(a)State Space Schema for WeatherMap.

WeatherMap
location : P REGION
weather : REGION → 7 TEMPERATURE
dom weather = location

i
(b) A schema that updates the WeatherMap database by adding tem-
perature of a particular region.
This schema changes the temperature of a region that is existing in the
database

Update
∆WeatherMap
reg? : REGION
temp? : TEMPERATURE
reg? ∈ location
weather 0 = weather ⊕ {reg? 7→ temp?}

(c) A Schema which searches for the temperature of a particular region.


This schema returns the temperature of a region that is in the WeatherMap
database

LookUp
ΞWeatherMap
reg? : REGION
temp! : TEMPERATURE
reg? ∈ location
temp! = weather (reg?)

(d) A schema that shows the initial state of the WeatherMap database.
This schema shows the state of the WeatherMap Database before putting
any weather information in it.

InitialMap
∆WeatherMap
weather 0 = {}

ii
Question 2.

Vending Machine Specification: the state space.

In this second part of the document, i produced a specification of the


Vending machine given the following constraints.

ˆ A Vending machine supplies GOODS’s in exchange for COIN’s.

ˆ Each coin has a value.

ˆ The vending machine contains a number of each product and a num-


ber of each coin.

ˆ Coins are entered one by one.

ˆ A good can be bought if sufficient coins have been entered, the good
is available and the exact change can be given.

ˆ The machine can be maintained, so money and goods can be added


and removed.

Below are the basic types required for the vending machine
[GOOD, COIN , REPORT ]

COIN=={50, 100, 200, 500}


GOOD=={Cakes, Cans of coke, Pens, Note Books, pencils, CDs, flash disks
and Samosas}

N=={n : Z | n≥ 0}

sum : bag COIN →


7 N

REPORT ::= some goods are not priced | Item already priced | Item not
in stock | More money is required | You have some balance | Operation is
unsuccessful | Item out of stock

iii
The constant for the maximum number of goods that can be in the vending
machine is
maxGoods : GOOD → 7 N

VENDING MACHINE

(i) The state space schema

VendingMachine
stock : bag GOOD
cost : GOOD → 7 N
float : bag COIN
dom stock v dom cost
dom cost ≤ dom maxGoods

(ii) The initial state schema.


This shows the state of the vending machine before putting any goods and
coins in it.

InitVendingMachine
∆VendingMachine
stock 0 = [[]]
cost 0 = {}
float 0 = [[]]

(iii) A schema used to give price to a product.


This schema gives the price to a good that is already in the vending machine.

GivePrice
∆VendingMachine
good ? : GOOD
price? : N
good ? 7→ price? ∈/ cost
cost 0 = cost ∪ {good ? 7→ price?}
stock 0 = stock
float 0 = float

iv
A schema that can be used to reprice an item that already has price
associated with it.
Reprice
∆VendingMachine
good ? : GOOD
price? : N
good ? 7→ price? ∈ cost
cost 0 = cost ⊕ {good ? 7→ price?}
stock 0 = stock
float 0 = float

(iv) A schema to add acceptable coins.


This schema adds a new coin to the set of acceptable coins in the vending
machine.
AcceptCoin
∆VendingMachine
coin? : COIN
coin? ∈/ float
float! = float ] {coin? 7→ 1}

(v) Schema specifying what happens when some one buys an item from
the machine.
This schema allows a person to remove a good from the vending machine
by putting an acceptable coin (in ?). When the coin is in excess of a given
price of the good, then the machine gives the coin(out ?).
Buy
∆VendingMachine
in?, out! : bagCOIN
item? : GOOD
item? in stock
sum(in?) ≥ cost(item?)
out! v float
sum(in?) = sum(out!) + cost(item?)
stock 0 ] {item? 7→ 1} = stock
float 0 ] out? = float ] in?
cost 0 = cost

v
(vi) The Schema specifying what happens when the Item someone wants
to buy is not in the machine.
This schema returns an item not in stock if the item you are looking for is
not in the system.
itemNotInSystem
ΞVendingMachine
item? : GOOD
rep! : REPORT
¬(dom stock 0 v dom cost 0 )
rep! = ”Item is not in stock”

(vii)The schema to report Success. This schema returns true if the op-
eration is successful.
Success
report! : REPORT
report! = ”Ok”

The schema for Reporting Errors in operation.


This schema returns true if the operation is unsuccessful.
Error
rep! : REPORT
rep! = ”Operation is unsuccessful”

(viii) Other schemas defining the different what-ifs in the Vending Ma-
chine Operations.
These schemas show other operations that can occur in the vending machine.

ˆ Not in stock

NoStock
ΞVendingMachine
item? : GOOD
rep! : REPORT
¬(item? in stock)
rep! = “Item out of stock”

vi
ˆ Less money

LessMoney
ΞVendingMachine
In? : bag COIN
item? : GOOD
rep! : REPORT
sum(in?) < cost(item?)
rep! = “More Money Required”

ˆ Already priced

AlreadyPriced
ΞVendingMachine
item? : GOOD
price? : N
rep! : REPORT
item? 7→ price? ∈ cost
rep! = ”Item already priced”

ˆ What if i want to restock

Restock
∆VendingMachine
new ? : bag GOOD
stock 0 = stock ] new ?
cost 0 = cost
float 0 = float

ˆ What if the good is not priced

vii
GoodNotPriced
ΞVendingMachine
new ? : bag GOOD
rep! : REPORT
¬(dom new ? v dom cost)
rep! = “Some Goods Not Priced”

viii
ˆ Give Change

Change
∆VendingMachine
item? : GOOD
rep! : REPORT
in?, out 0 : bag COIN
item?instock
sum(in?) ≥ cost(item?)
¬(out! v float)
sum(in?) = sum(out!) + cost(item?)
stock 0 ] item? 7→ 1 = stock
float 0 ] out? = float ] in?
cost 0 = cost
rep! = ”You have some balance”

ix

You might also like