hw3
hw3
1 Assignment Policies
Under absolutely no circumstances code can be exchanged between students.
Excerpts of code presented in class can be used.
Assignments from previous offerings of the course must not be reused. Vio-
lations will be penalized appropriately.
Late Policy. Late submissions are allowed with a penalty of 2 points per hour past the
deadline.
2 Assignment
The aim of this assignment is to get familiar with the functional fragment of Erlang1 and
to learn how to work with some of the data structures provided by the language; notably
lists, maps, and records.
You are asked to implement various operations that provide support to a shipping company.
The shipping company administers (cargo) ships. Ships transport containers and navigate
from one port to another. Each port has a number of docks where the ships may load and
unload. Containers can either be in transit on a ship or in a port (in the latter case, no
distinction is made regarding the specific dock).
You are supplied with two files:
• shipping.hrl. The Erlang header file for the assignment. It contains all the
record declarations you should need to complete this assignment. These are described
in further detail below.
• shipping.erl. The module file that includes all the functions that need to be
completed. This is the file you will be editing.
1
Disregarding process spawning and message passing. The latter will be addressed in an upcoming
assignment.
1
2.0.1 Shipping Header File (shipping.hrl)
• Ship This record contains the fields:
· name a string value
· id a unique integer value
· container cap the maximum number of containers that a ship can hold
• Container This record contains the fields:
· id a unique integer value
· weight any positive integer value
• Port This record contains the fields:
· name a string value
· id a unique integer value
· docks a list of docks for the port. A dock can be represented by an unique integer
or a character and is only required to be unique at a given port (i.e. Port “New
York”, Dock ‘A’ and Port “Los Angeles”, Dock ‘A’ are both valid)
· container cap the maximum number of containers that a port can hold
• Shipping State This record contains the fields:
· ships a list of the ship records currently in the system
· containers a list of the container records currently in the system
· ports a list of the port records currently in the system
· ship locations a tuple containing a port id, dock id, and ship id (i.e.
(1,’A’,3)) if port 1, dock ‘A’ contains ship 3
· ship inventory a map that takes a ship id and maps it to the list of contain-
ers ids on that ship.
· port inventory a map that takes a port id and maps it to the list of con-
tainers ids at that port.
3 Shipping Module
We next describe each of the functions you are asked to implement. All of them have to be
defined in shipping.erl.
To illustrate some of the functions described below we will use a sample shipping company
ShipCo. The shipping state of ShipCo may be obtained by calling shipping:shipco().
It is provided for you in the stub. Before you try out the examples below, remember to
compile and then load the record definitions into the interpreter, as follows:
2
1> c(shipping).
shipping.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,shipping}
2> rr(shipping).
[container,port,ship,shipping_state]
3
5. get ship location(Shipping State, Ship ID)
This method returns the location, {Port ID, Dock ID}, of a given ship. It should
throw the exception error, if the ship id does not exist. For example:
11> shipping:get_ship_location(shipping:shipco(),3).
{1,’A’}
12> shipping:get_ship_location(shipping:shipco(),23).
** exception throw: error
in function shipping:get_ship_location/2 ...
4
#ship{id = 4,name = "SS Minnow", container_cap = 20},
#ship{id = 5,name = "Sir Leaks-A-Lot",container_cap = 20}],
containers = [...], %% not shown
ports = [#port{id = 1,name = "New York",
docks = [’A’,’B’,’C’,’D’],
container_cap = 200},
#port{id = 2,name = "San Francisco",
docks = [’A’,’B’,’C’,’D’],
container_cap = 200},
#port{id = 3,name = "Miami",
docks = [’A’,’B’,’C’,’D’],
container_cap = 200}],
ship_locations = [{1,’B’,1},
{1,’A’,3},
{3,’C’,2},
{2,’D’,4},
{2,’B’,5}],
ship_inventory = #{1 => [14,15,9,2,6,16,18,20], %% loaded here
2 => [1,3,4,13],
3 => [],
4 => [2,8,11,7],
5 => [5,10,12]},
port_inventory = #{1 => [17,19], %% removed from port 1
2 => [21,22,23,24,25],
3 => [26,27,28,29,30]}}}
> shipping:load_ship(shipping:shipco(), 45, [16,18,20]).
** exception throw: error
in function shipping:get_ship/2 ...
5
#port{id = 3,name = "Miami",
docks = [’A’,’B’,’C’,’D’],
container_cap = 200}],
ship_locations = [{1,’B’,1},
{1,’A’,3},
{3,’C’,2},
{2,’D’,4},
{2,’B’,5}],
ship_inventory = #{1 => [14,15,9,2,6],
2 => [], %% no more containers
3 => [],
4 => [2,8,11,7],
5 => [5,10,12]},
port_inventory = #{1 => [16,17,18,19,20],
2 => [21,22,23,24,25],
3 => [26,27,28,29,30,1,3,4,13]} %% loaded here
}}
6
{3,’C’,2},
{2,’D’,4},
{2,’B’,5}],
ship_inventory = #{1 => [15,9,6], %% removed from here
2 => [1,3,4,13],
3 => [],
4 => [2,8,11,7],
5 => [5,10,12]},
port_inventory = #{1 => [16,17,18,19,20,14,2], %% placed here
2 => [21,22,23,24,25],
3 => [26,27,28,29,30]}}}
7
5 => [5,10,12]},
port_inventory = #{1 => [16,17,18,19,20],
2 => [21,22,23,24,25],
3 => [26,27,28,29,30]}}}
4 Your Task
Your task is to complete all the functions in shipping.erl as mentioned above. You DO
NOT have to touch shipping.hrl. Some example modules to help you complete some
of these functions would be the lists and maps modules.
5 Submission Instructions
Submit a file hw3.zip through Canvas containing all the files included in the stub but where
all required operations have been implemented.