Title: Hominid Problem Active Classes
1Hominid Problem - Active Classes
Ref Page-Jones FOOD Ch. 1, pp 1-8, 1.11(13),
1.12 p.56
- My proposed solution uses two Active Classes
- AC1 Hominid HM1 with state Direction d in
E, N, W, S - AC2 HominidLocation HL1 with state index
i in 0..4 - The first AC has one Active Instance
- AI1 HD AI.state direction in enumE, 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
- AI2 HX AI.state column index of its Grid
Square. - AI3 HY AI.state row index of its Grid
Square
2Hominid 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 dont 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)
3Object Communication Diagram (OCD)
Illustrates Object Classes
PA HD HL
TL TR Adv
Incr Decr
4UML Collaboration Diagram (UCD)
Illustrates Object Instances
PA HD1 HL1HX HL2 HY
TL TR Adv
Incr Decr
Incr Decr
5UML Class or E-R Diagram
1..1
PathAssigner PA
1..1
Grid GD
1..1
1..1
0..
Square SQ
0..
Hominid HM
1..1
1..1
1. .1
2..2
HominidDirection HD
HominidLocation HL
6UML Class or E-R Diagram
(Including Active Classes and Instances for
take-home exam)
AC
1..1
PathAssigner PA navigate()
3..3
1..1
Grid GD
AI state
1..1
0..
1..1
0..
Square SQ onPath()? x y
1..1
0..
0..1
Hominid HD
1..1
(Hominid HM merged into HominidDirection HD
HD.d AIid_pp-gtstate HL.i AIid_pp-gtstate)
2..2
HominidLocation HL
(Instance names HX, HY)
7Pre-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 lt 4)
- (HD.state W HX.i gt0) (HD.state
N HY.i gt 0) - (HD.state S HY.i lt 4) )
- One sub-expression of this assert can be placed
inside each case of switch(state).
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 TLTR sequence - assert (((dOLD - dNEW)4 ) ! 2)
- Note the need to save the prior state dOLD while
the next one dNEW is computed.
9Sample 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
1,0
2,0 3,0
0,1
4,1
Exam2 Start
0,2
4,2
y
0,3
4,3
0,4
1,4
2,4
3,4
4,4
10Behavioral 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)
11Hominid Path Definition
x
0,0
4,0
1,0
2,0 3,0
0,1
4,1
0,2
4,2
STARTW
y
0,3
4,3
0,4
1,4
2,4
3,4
4,4
For Exam 2 I specified the starting state ltx,y,dgt
lt0,2,Wgt.
12State 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-gtstate, HY.i AIcurr-gtstate
Incr Incr Incr Incr
0
1
2
4
3
Decr Decr Decr Decr
13State Model for Hominid Direction HD
Commands received from PA TR turnRight TL
turnLeft AD advance
Commands sent to HL Up increment Down
decrement
AD/HYUp
D N 1
TR
TR
TL
TL
D W 3
D E 0
TL
TL
TR
TR
AD/HXDown
D S 2
AD/HXUp
(Only ADV has actions other than the state
transition.)
AD/HYDown
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 HDaction(command)
-
- switch(state) // direction state of HD
- case E
- switch(command)
- case TL d (d)4 // next state
- case TR d (d-- )4 // next state
- case ADV HXcount(Up) // command to HX
-
- case N
- switch(command) // commands TL and TR as
above - case ADV(HY.count(Down)
- // states W and S as above
- return
15(4B) State Action Code for HL
- include HDdefines.h / enum or defines for
command / - int HLcount(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 lt 4)
- endif
- HL.i // here to save space
- case Down
- ifndef NDEBUG
- assert (HL.i gt 0)
- endif
- HL.i -- // case Down
- return
- // end count()