15
02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes My proposed solution uses two Active Classes: AC#1 = Hominid HM#1 with state = Direction d in {E, N, W, S} AC#2 = HominidLocation HL#1 with state = index i in [0..4] The first AC has one Active Instance: AI#1 = HD; AI.state = direction in enum{E, N, W, S} [Enum order selected so TL is {d = (d++)%4} and TR is {d = ( d--)%4}] The second AC has two active instances: AI#2 = HX; AI.state = column index of its Grid Square. AI#3 = HY; AI.state = row index of its Grid Square [Ref: Page-Jones FOOD Ch. 1, pp 1-8, 1.11(13), 1.12 p.56]

02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

Embed Size (px)

Citation preview

Page 1: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 1

Hominid Problem - Active Classes

My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction d in {E, N, W, S}– AC#2 = HominidLocation HL#1 with state = index i in [0..4]

The first AC has one Active Instance:– AI#1 = HD; AI.state = direction in enum{E, N, W, S}

–[Enum order selected so TL is {d = (d++)%4} and TR is {d = ( d--)%4}]

The second AC has two active instances:– AI#2 = HX; AI.state = column index of its Grid Square.– AI#3 = HY; AI.state = row index of its Grid Square

[Ref: Page-Jones FOOD Ch. 1, pp 1-8, 1.11(13), 1.12 p.56]

Page 2: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 2

Hominid: Supporting Classes

There is one main program or singleton class PathAssigner (PA) with one method: navigation.

There are two passive classes: SQuare and its GriD container class., with access methods.

GD and SQ are constant, so they don’t have behavioral states.

HD has the fkey SQid (and SQid_pp) SQ has the fkey GDid (and GDid_pp) HD has the fkey PAid (so PA will have HDid_fcp)

Page 3: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 3

Object Communication Diagram (OCD)

PA

HD

HL

TL TR Adv}

Incr Decr

Illustrates Object Classes

Page 4: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 4

UML Collaboration Diagram (UCD)

PA

HD1

HL1:HX HL2: HY

TL TR Adv

Incr Decr

Illustrates Object Instances

Incr Decr

Page 5: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 5

UML Class or E-R Diagram

PathAssigner PA

Hominid HM

HominidLocation HL

Grid GD

Square SQ

1..1

2..2HominidDirection HD

1..1

1. .1

1..1

0..*

1..11..1

1..1

0..*

Page 6: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 6

UML Class or E-R Diagram

PathAssigner PA navigate()

Hominid HD*

HominidLocation HL*

Grid GD

Square SQ onPath()? x y

0..11..1

2..21..1

1..11..1

0..*

0..*

AC

AIstate

3..31..1

1..1

0..*

*(Hominid HM merged into HominidDirection HD; HD.d = AIid_pp->state; HL.i = AIid_pp->state) *(Instance names: HX, HY)

(Including Active Classes and Instances for take-home exam)

Page 7: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 7

Pre-condition on Advance Cmd: The particular algorithm which PA uses for advancing the

hominid must insure that it will not attempt to advance into a wall.

While debugging, we can assert or check this pre-condition for each Advance command that reaches the HD object from the (trusted) PA navigator :

assert( (HD.state = E && HX.i < 4) ||

(HD.state = W && HX.i >0) || (HD.state = N && HY.i > 0) ||

(HD.state = S && HY.i < 4) ) One sub-expression of this assert can be placed inside each

case of switch(state).

Page 8: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 8

Invariant on HD State Transition:

The particular algorithm which PA uses for advancing the hominid must also insure that it will not reverse direction.

This condition is not a pre-condition but an invariant relationship between initial and final direction states for any TL|TR sequence: – assert (((dOLD - dNEW)%4 ) != 2)

Note the need to save the prior state dOLD while the next one dNEW is computed.

Page 9: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 9

Sample Path for Test Case This task requires hand-tracing the algorithm in FOOD

Ch. 1. As a test case, assume the following path which is square, with final cell equal to the starting one.

0,0 4,0

0,1

0,2

0,3

0,4

4,1

4,2

4,3

1,4 2,4 3,4 4,4

1,0

y

2,0 3,0

Exam2Start

Page 10: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 10

Behavioral Model

The hominid occupies one cell and supports three action methods: turnLeft, turnRight, advanceOneCell, and a Boolean query: isFacingWall.

It also has an initialization method insert (x,y,d). The PA.navigator method calls the HD state action

method corresponding to the command or event type. The HD.Advance action calls the HL.Up or HL.Down

method of the HX or Hy instance, depending on its direction state HN.d

(2) Sequence Diagram (TBD)(3) EventType List (TBD)

Page 11: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 11

Hominid Path Definition

0,0 4,0

0,1

0,2

0,3

0,4

4,1

4,2

4,3

1,4 2,4 3,4 4,4

1,0

y

x

2,0 3,0

START:W

For Exam 2 I specified the starting state <x,y,d> = <0,2,W>.

Page 12: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 12

State Model for Hominid Location HL

The x and y components of state have their own state diagrams, but they correspond to modulo-5 up/down counter shown below. Only one counter can change at a time; neither x nor y changes at the same time d does.

The single HL class has a common name for the current state it inherits from its AC. I use the name i here:

HX.i = AIcurr->state, HY.i = AIcurr->state;

0 1 2 3 4

Incr Incr Incr Incr

Decr Decr Decr Decr

Page 13: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 13

State Model for Hominid Direction HD

D = N = 1

D = E = 0D = W = 3

D = S = 2

Commands received from PA: TR = turnRightTL = turnLeftAD = advance

AD/HY:Up

AD/HX:Up

AD/HY:Down

AD/HX:Down

TR

TRTR

TR

TL

TLTL

TL

Commands sent to HL:Up = incrementDown = decrement

(Only ADV has actions other than the state transition.)

Page 14: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 14

(4A) State Action Code for HD #include HDdefines.h// contains enums or #defines for d = {e, N, W, S},//command = { TL, TR, ADV}, and count = {Up, Down}int HD::action(command) { switch(state) { // direction state of HD case E: {

switch(command) {case TL: {d = (d++)%4; } // next statecase TR: {d = (d-- )%4; } // next statecase ADV: {HX::count(Up);} // command to HX

} case N: {

switch(command) { // commands TL and TR as abovecase ADV:{(HY.count(Down);}

} // states W and S as above }; return; }

Page 15: 02s522Exam2Solution.ppt 1 Hominid Problem - Active Classes n My proposed solution uses two Active Classes: – AC#1 = Hominid HM#1 with state = Direction

02s522Exam2Solution.ppt 15

(4B) State Action Code for HL#include HDdefines.h /* enum or #defines for command */int HL::count(cmd) { // no need to do switch(state) - action is independent of state HL.i // as long as pre-condition is met switch(cmd) {

case Up: { #ifndef NDEBUG

assert (HL.i < 4); #endif

HL.i ++; } // “}” here to save spacecase Down: {

#ifndef NDEBUG assert (HL.i > 0);

#endifHL.i -- ; } // case Down

return;} // end count()