-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepl.jl
455 lines (403 loc) · 14.8 KB
/
repl.jl
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
const ModelRepl = Vector{Expr}
@with_kw mutable struct CTRepl
model::ModelRepl = ModelRepl()
ocp_name::Symbol = gensym(:ocp)
sol_name::Symbol = gensym(:sol)
debug::Bool = false
end
@with_kw mutable struct HistoryRepl
index::Int = 0
ct_repl_datas_data::Vector{CTRepl} = Vector{CTRepl}()
end
#
ct_repl_is_set::Bool = false
ct_repl_data::CTRepl = CTRepl()
ct_repl_history::HistoryRepl = HistoryRepl(0, Vector{ModelRepl}())
"""
$(TYPEDSIGNATURES)
Update the model adding the expression e. It must be public since in the ct repl, this function
is quoted each time an expression is parsed and is valid.
"""
function ct_repl_update_model(e::Expr)
ct_repl_data.debug && (println("debug> expression to add: ", e))
# update model
__update!(ct_repl_data.model, e)
ct_repl_data.debug && (println("debug> expression valid, model updated."))
# add ct_repl_data to ct_repl_history
__add!(ct_repl_history, ct_repl_data)
#
return nothing
end
"""
$(TYPEDSIGNATURES)
Create a ct REPL.
"""
function ct_repl(; debug=false, verbose=false)
global ct_repl_is_set
global ct_repl_data
global ct_repl_history
if !ct_repl_is_set
#
ct_repl_is_set = true
#
ct_repl_data.debug = debug
# # advice to start by setting the name of the ocp and the solution
# println("\nType > to enter into ct repl.\n")
# println("For a start, you can set the names of the optimal control problem and its solution.")
# println("In ct repl, type:\n")
# println(" ct> NAME=(ocp, sol)")
# add initial ct_repl_data to ct_repl_history
__add!(ct_repl_history, ct_repl_data)
# text invalid
txt_invalid =
"\nInvalid expression.\n\nType HELP to see the list of commands or enter a " *
"valid expression to update the model."
function parse_to_expr(s::AbstractString)
# remove spaces from s at the beginning and at the end
s = strip(s)
# check if it is a comment
startswith(s, "#") && return nothing
# parse string
e = Meta.parse(s)
#
ct_repl_data.debug && println("\ndebug> parsing string: ", s)
ct_repl_data.debug && println("debug> expression parsed: ", e)
ct_repl_data.debug && println("debug> expression type: ", typeof(e))
ct_repl_data.debug && println("debug> dump of expression: ", dump(e))
# test if e is a command
@match e begin
:($c = $a) => begin
command = __transform_to_command(c)
ct_repl_data.debug &&
println("debug> command: ", command, " and argument: ", a)
command ∈ keys(COMMANDS_ACTIONS) && (
return COMMANDS_ACTIONS[command](ct_repl_data, a, ct_repl_history)
)
end
:($c) => begin
command = __transform_to_command(c)
ct_repl_data.debug && println("debug> command: ", command)
command ∈ keys(COMMANDS_ACTIONS) && (
return COMMANDS_ACTIONS[command](ct_repl_data, ct_repl_history)
)
end
_ => nothing
end
# check if s finishes with a ";". If yes then remove it and return nothing at the end
return_nothing = endswith(s, ";") ? true : false
return_nothing && (s = s[1:(end - 1)])
e = Meta.parse(s)
#
return_nothing &&
ct_repl_data.debug &&
println("\ndebug> new parsing string: ", s)
return_nothing &&
ct_repl_data.debug &&
println("debug> new expression parsed: ", e)
if e isa Expr
#
ocp_q_for_test = __quote_anonym_ocp(ct_repl_data, e) # test if code is valid: if not, an exception is thrown
ocp_q = __quote_ocp(ct_repl_data, e)
# to update the model if needed
ee = QuoteNode(e)
#
ct_repl_data.debug && (println("debug> try to add expression: ", e))
q = quote
try
$ocp_q_for_test # test if the expression is valid
catch ex
$(ct_repl_data.debug) && (println("debug> exception thrown: ", ex))
println($txt_invalid)
return nothing
end
# test is ok
$(ct_repl_data.debug) && (println("debug> eval ocp quote ok "))
$(ct_repl_data.debug) && (println("debug> expr to add ", $ee))
# ----------------------------------------------------------------
# keep coherence between model and ocp variable
$ocp_q # define the ocp
ct_repl_update_model($ee) # add the expression in the model
# ----------------------------------------------------------------
if $return_nothing
nothing
else
begin
println("\n", string($ct_repl_data.ocp_name))
$(ct_repl_data.ocp_name)
end
end
end
return q
else
println(txt_invalid)
return nothing
end
end # parse_to_expr
# makerepl command
initrepl(
parse_to_expr;
prompt_text="ct> ",
prompt_color=:magenta,
start_key='>',
mode_name="ct_mode",
valid_input_checker=complete_julia,
startup_text=false,
)
return nothing
else
if verbose
println("ct repl is already set.")
end
end
end
# ----------------------------------------------------------------
# utils functions
# ----------------------------------------------------------------
function NAME_ACTION_FUNCTION(ct_repl_data::CTRepl, ct_repl_history::HistoryRepl)
println("")
println("Optimal control problem name: ", ct_repl_data.ocp_name)
return println("Solution name: ", ct_repl_data.sol_name)
end
function NAME_ACTION_FUNCTION(
ct_repl_data::CTRepl, name::Union{Symbol,Expr}, ct_repl_history::HistoryRepl
)
ocp_name = ct_repl_data.ocp_name
sol_name = ct_repl_data.sol_name
if isa(name, Symbol)
name = (name, Symbol(string(name, "_sol")))
elseif isa(name, Expr)
name = (name.args[1], name.args[2])
else
println(
"\nname error\n\nType HELP to see the list of commands or enter a valid expression to update the model.",
)
return nothing
end
ct_repl_data.ocp_name = name[1]
ct_repl_data.sol_name = name[2]
ct_repl_data.debug && println("debug> ocp name: ", ct_repl_data.ocp_name)
ct_repl_data.debug && println("debug> sol name: ", ct_repl_data.sol_name)
__add!(ct_repl_history, ct_repl_data) # update ct_repl_history
qo1 = if ct_repl_data.ocp_name ≠ ocp_name
:($(ct_repl_data.ocp_name) = "no optimal control")
else
:()
end
qs1 =
ct_repl_data.sol_name ≠ sol_name ? :($(ct_repl_data.sol_name) = "no solution") : :()
qo2 = ct_repl_data.ocp_name ≠ ocp_name ? :($(ct_repl_data.ocp_name) = $(ocp_name)) : :()
qs2 = ct_repl_data.sol_name ≠ sol_name ? :($(ct_repl_data.sol_name) = $(sol_name)) : :()
name_q = (
quote
$(qo1)
$(qs1)
try
$(qo2)
$(qs2)
nothing
catch e
nothing
end
end
)
ct_repl_data.debug && println("debug> new name quote: ", name_q)
return name_q
end
# dict of actions associated to ct repl commands
COMMANDS_ACTIONS = Dict{Symbol,Function}(
:SHOW =>
(ct_repl_data::CTRepl, ct_repl_history::HistoryRepl) -> begin
q = quote
println("\n", string($ct_repl_data.ocp_name))
$(ct_repl_data.ocp_name)
end
return q
end,
:SOLVE =>
(ct_repl_data::CTRepl, ct_repl_history::HistoryRepl) -> begin
return __quote_solve(ct_repl_data)
end,
:PLOT =>
(ct_repl_data::CTRepl, ct_repl_history::HistoryRepl) -> begin
return __quote_plot(ct_repl_data)
end,
:DEBUG =>
(ct_repl_data::CTRepl, ct_repl_history::HistoryRepl) -> begin
ct_repl_data.debug = !ct_repl_data.debug
println("\n debug mode: " * (ct_repl_data.debug ? "on" : "off"))
__add!(ct_repl_history, ct_repl_data) # update ct_repl_history
return nothing
end,
:NAME => NAME_ACTION_FUNCTION,
:UNDO =>
(ct_repl_data::CTRepl, ct_repl_history::HistoryRepl) -> begin
ct_repl_data_ = __undo!(ct_repl_history)
__copy!(ct_repl_data, ct_repl_data_)
ocp_q = __quote_ocp(ct_repl_data)
q = quote
$ocp_q
nothing
end
return q
end,
:REDO =>
(ct_repl_data::CTRepl, ct_repl_history::HistoryRepl) -> begin
ct_repl_data_ = __redo!(ct_repl_history)
__copy!(ct_repl_data, ct_repl_data_)
ocp_q = __quote_ocp(ct_repl_data)
q = quote
$ocp_q
nothing
end
return q
end,
:HELP =>
(ct_repl_data::CTRepl, ct_repl_history::HistoryRepl) -> begin
l = 8 # 22
n = 6
println("\nSpecial commands to interact with the ct repl:\n")
dict = sort(collect(COMMANDS_HELPS); by=x -> x[1])
for (k, v) in dict
m = length(string(k))
s = " "
s *= string(k) * " "^(n - m)
r = length(s)
s *= " "^(l - r)
printstyled(s; color=:magenta)
printstyled(": ", v, "\n")
end
return nothing
end,
:REPL =>
(ct_repl_data::CTRepl, ct_repl_history::HistoryRepl) -> begin
return :($ct_repl_data)
end,
:CLEAR =>
(ct_repl_data::CTRepl, ct_repl_history::HistoryRepl) -> begin
ct_repl_data.model = ModelRepl()
__add!(ct_repl_history, ct_repl_data) # update ct_repl_history
ocp_q = __quote_ocp(ct_repl_data)
q = quote
$ocp_q
nothing
end
return q
end,
)
# dict of help messages associated to ct repl commands
COMMANDS_HELPS = Dict{Symbol,String}(
:SOLVE => "solve the optimal control problem",
:PLOT => "plot the solution",
#:DEBUG => "toggle debug mode",
:NAME =>
"print the name of the optimal control problem and the solution. To set them: \n\n" *
" ct> NAME=ocp\n" *
" ct> NAME=(ocp, sol)\n",
:UNDO => "undo last command",
:REDO => "redo last command",
:HELP => "help",
#:REPL => "return the current ct REPL",
:SHOW => "show the optimal control problem",
:CLEAR => "clear the optimal control problem",
)
# non existing command
__non_existing_command() = :non_existing_command
# transform to a command
__transform_to_command(c::Symbol)::Symbol = c
__transform_to_command(e::Expr)::Symbol = __non_existing_command()
# get code from model
function __code(model::ModelRepl)::Expr
return Expr(:block, model...)
end
# get code from model and an extra expression
function __code(model::ModelRepl, e::Expr)
model_ = deepcopy(model) # copy model
__update!(model_, e) # update model_
return __code(model_) # get code
end
function __update!(model::ModelRepl, e::Expr)
return push!(model, e)
end
# make @def ocp quote
function __quote_ocp(ct_repl_data::CTRepl)
code = __code(ct_repl_data.model)
ocp_q = quote
@def $(ct_repl_data.ocp_name) $(code)
end
ct_repl_data.debug && println("debug> code: ", code)
ct_repl_data.debug && println("debug> quote code: ", ocp_q)
return ocp_q
end
function __quote_ocp(ct_repl_data::CTRepl, e::Expr)
code = __code(ct_repl_data.model, e)
ocp_q = quote
@def $(ct_repl_data.ocp_name) $(code)
end
ct_repl_data.debug && println("debug> code: ", code)
ct_repl_data.debug && println("debug> quote code: ", ocp_q)
return ocp_q
end
# get ocp quote
function __quote_anonym_ocp(ct_repl_data::CTRepl, e::Expr)
code = __code(ct_repl_data.model, e)
ocp = gensym()
ocp_q = quote
@def $ocp $code
end
ct_repl_data.debug && println("debug> code: ", code)
ct_repl_data.debug && println("debug> quote code: ", ocp_q)
return ocp_q
end
# quote solve: todo: update when using real solver
function __quote_solve(ct_repl_data::CTRepl)
solve_q = (
quote
$(ct_repl_data.sol_name) = solve($(ct_repl_data.ocp_name))
end
)
ct_repl_data.debug && println("debug> quote solve: ", solve_q)
return solve_q
end
# quote plot: todo: update when handle correctly solution
function __quote_plot(ct_repl_data::CTRepl)
plot_q = quote
if @isdefined($(ct_repl_data.sol_name))
plot($(ct_repl_data.sol_name); size=(600, 500))
else
println("\nNo solution available.")
end
end
ct_repl_data.debug && println("debug> quote plot: ", plot_q)
return plot_q
end
# add model to ct_repl_history
function __add!(ct_repl_history::HistoryRepl, ct_repl_data::CTRepl)
push!(ct_repl_history.ct_repl_datas_data, deepcopy(ct_repl_data))
return ct_repl_history.index += 1
end
# go to previous model in ct_repl_history
function __undo!(ct_repl_history::HistoryRepl)
ct_repl_history.index > 1 && (ct_repl_history.index -= 1)
return ct_repl_history.ct_repl_datas_data[ct_repl_history.index]
end
# go to next model in ct_repl_history
function __redo!(ct_repl_history::HistoryRepl)
ct_repl_history.index < length(ct_repl_history.ct_repl_datas_data) &&
(ct_repl_history.index += 1)
return ct_repl_history.ct_repl_datas_data[ct_repl_history.index]
end
# copy a ct_repl_data
function __copy!(ct_repl_data::CTRepl, ct_repl_data_to_copy::CTRepl)
ct_repl_data.model = deepcopy(ct_repl_data_to_copy.model)
ct_repl_data.ocp_name = ct_repl_data_to_copy.ocp_name
ct_repl_data.sol_name = ct_repl_data_to_copy.sol_name
return ct_repl_data.debug = ct_repl_data_to_copy.debug
end
function Base.show(io::IO, ::MIME"text/plain", ct_repl_data::CTRepl)
print(io, "\n")
println(io, "ct> ")
println(io, "model: ", ct_repl_data.model)
println(io, "ocp_name: ", ct_repl_data.ocp_name)
println(io, "sol_name: ", ct_repl_data.sol_name)
return println(io, "debug: ", ct_repl_data.debug)
end