-
Notifications
You must be signed in to change notification settings - Fork 18
/
05-TurtleAgent.fsx
223 lines (177 loc) · 6.39 KB
/
05-TurtleAgent.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
(* ======================================
05-TurtleAgent.fsx
Part of "Thirteen ways of looking at a turtle"
Related blog post: http://fsharpforfunandprofit.com/posts/13-ways-of-looking-at-a-turtle/
======================================
Way #5: API (hybrid approach) -- OO API posting messages to an Agent
In this design, an API layer communicates with a TurtleAgent
and the client talks to the API layer.
Because the Agent has a message queue, all possible commands are managed with a
single discriminated union type (`TurtleCommand`).
There are no mutables anywhere. The Agent manages the turtle state by
storing the current state as a parameter in the recursive message processing loop.
====================================== *)
#load "Common.fsx"
#load "FPTurtleLib.fsx"
#load "TurtleApiHelpers.fsx"
open Common
open TurtleApiHelpers // helpers for API validation, etc
// ======================================
// Agent
// ======================================
module AgentImplementation =
open FPTurtleLib
type TurtleCommand =
| Move of Distance
| Turn of Angle
| PenUp
| PenDown
| SetColor of PenColor
// --------------------------------------
// The Agent
// --------------------------------------
type TurtleAgent() =
/// Function to log a message
let log message =
printfn "%s" message
// logged versions
let move = Turtle.move log
let turn = Turtle.turn log
let penDown = Turtle.penDown log
let penUp = Turtle.penUp log
let setColor = Turtle.setColor log
let mailboxProc = MailboxProcessor.Start(fun inbox ->
let rec loop turtleState = async {
// read a command message from teh queue
let! command = inbox.Receive()
// create a new state from handling the message
let newState =
match command with
| Move distance ->
move distance turtleState
| Turn angle ->
turn angle turtleState
| PenUp ->
penUp turtleState
| PenDown ->
penDown turtleState
| SetColor color ->
setColor color turtleState
return! loop newState
}
loop Turtle.initialTurtleState )
// expose the queue externally
member this.Post(command) =
mailboxProc.Post command
// ======================================
// Turtle Api Layer
// ======================================
module TurtleApiLayer =
open Result
open AgentImplementation
type TurtleApi() =
let turtleAgent = TurtleAgent()
/// Execute the command string, and return a Result
/// Exec : commandStr:string -> Result<unit,ErrorMessage>
member this.Exec (commandStr:string) =
let tokens = commandStr.Split(' ') |> List.ofArray |> List.map trimString
// calculate the new state
let result =
match tokens with
| [ "Move"; distanceStr ] -> result {
let! distance = validateDistance distanceStr
let command = Move distance
turtleAgent.Post command
}
| [ "Turn"; angleStr ] -> result {
let! angle = validateAngle angleStr
let command = Turn angle
turtleAgent.Post command
}
| [ "Pen"; "Up" ] -> result {
let command = PenUp
turtleAgent.Post command
}
| [ "Pen"; "Down" ] -> result {
let command = PenDown
turtleAgent.Post command
}
| [ "SetColor"; colorStr ] -> result {
let! color = validateColor colorStr
let command = SetColor color
turtleAgent.Post command
}
| _ ->
Error (InvalidCommand commandStr)
// return any errors
result
// ======================================
// Turtle Api Client
// ======================================
module TurtleApiClient =
open TurtleApiLayer
open Result
let drawTriangle() =
let api = TurtleApi()
result {
do! api.Exec "Move 100"
do! api.Exec "Turn 120"
do! api.Exec "Move 100"
do! api.Exec "Turn 120"
do! api.Exec "Move 100"
do! api.Exec "Turn 120"
}
// back home at (0,0) with angle 0
let drawThreeLines() =
let api = TurtleApi()
result {
// draw black line
do! api.Exec "Pen Down"
do! api.Exec "SetColor Black"
do! api.Exec "Move 100"
// move without drawing
do! api.Exec "Pen Up"
do! api.Exec "Turn 90"
do! api.Exec "Move 100"
do! api.Exec "Turn 90"
// draw red line
do! api.Exec "Pen Down"
do! api.Exec "SetColor Red"
do! api.Exec "Move 100"
// move without drawing
do! api.Exec "Pen Up"
do! api.Exec "Turn 90"
do! api.Exec "Move 100"
do! api.Exec "Turn 90"
// back home at (0,0) with angle 0
// draw diagonal blue line
do! api.Exec "Pen Down"
do! api.Exec "SetColor Blue"
do! api.Exec "Turn 45"
do! api.Exec "Move 100"
}
let drawPolygon n =
let angle = 180.0 - (360.0/float n)
let api = TurtleApi()
// define a function that draws one side
let drawOneSide() = result {
do! api.Exec "Move 100.0"
do! api.Exec (sprintf "Turn %f" angle)
}
// repeat for all sides
for i in [1..n] do
drawOneSide() |> ignore
let triggerError() =
let api = TurtleApi()
api.Exec "Move bad"
// ======================================
// Turtle Api Tests
// ======================================
(*
TurtleApiClient.drawTriangle()
TurtleApiClient.drawThreeLines() // Doesn't go back home
TurtleApiClient.drawPolygon 4
// test errors
TurtleApiClient.triggerError()
// Error (InvalidDistance "bad")
*)