From 45eee62fc2ce48028b61713bcf57da0c0ca7c631 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Fri, 20 Oct 2017 11:54:04 +0100 Subject: [PATCH] chat-bot.rkt --- racket/syndicate/examples/actor/chat-bot.rkt | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 racket/syndicate/examples/actor/chat-bot.rkt diff --git a/racket/syndicate/examples/actor/chat-bot.rkt b/racket/syndicate/examples/actor/chat-bot.rkt new file mode 100644 index 0000000..444c179 --- /dev/null +++ b/racket/syndicate/examples/actor/chat-bot.rkt @@ -0,0 +1,46 @@ +#lang syndicate + +(require/activate syndicate/drivers/tcp2) +(require/activate syndicate/drivers/timestate) + +(define (random-selection items) + (list-ref items (random (length items)))) + +(define id (make-parameter #f)) + +(define (random-sleep secs) + (define duration (/ (random (* secs 1000)) 1000.0)) + (printf "Bot ~a sleeping for ~a seconds\n" (id) duration) + (sleep duration)) + +(define *utterances* (list "Lovely weather we're having!\n" + "It's a bit cold today\n" + "I've got a lovely bunch of coconuts\n")) + +(assertion-struct bot-running (id)) + +(define (bot) + (parameterize ((id (gensym 'chatbot))) + (spawn (assert (tcp-connection (id) (tcp-address "localhost" 5999))) + (stop-when (retracted (tcp-accepted (id)))) + + (on-start (printf "Bot ~a starting\n" (id))) + (assert (bot-running (id))) + (on-stop (printf "Bot ~a stopping\n" (id))) + + (on-start (random-sleep 30) + (stop-current-facet)) + + (on-start (let loop () + (random-sleep 10) + (send! (tcp-out (id) (random-selection *utterances*))) + (loop)))))) + +(parameterize ((id 'monitor)) + (spawn (on (retracted (bot-running $id)) + (random-sleep 5) + (bot)))) + +(bot) +(bot) +(bot)