Update reference to hive to be dynamic state, simplify create-actor.
[8sync.git] / demos / actors / botherbotherbother.scm
1 #!/usr/bin/guile \
2 -e main -s
3 !#
4
5 ;;; 8sync --- Asynchronous programming for Guile
6 ;;; Copyright © 2016, 2017 Christopher Allan Webber <cwebber@dustycloud.org>
7 ;;;
8 ;;; This file is part of 8sync.
9 ;;;
10 ;;; 8sync is free software: you can redistribute it and/or modify it
11 ;;; under the terms of the GNU Lesser General Public License as
12 ;;; published by the Free Software Foundation, either version 3 of the
13 ;;; License, or (at your option) any later version.
14 ;;;
15 ;;; 8sync is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;;; GNU Lesser General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU Lesser General Public
21 ;;; License along with 8sync.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;; Puppet show simulator.
24
25 (use-modules (8sync actors)
26              (oop goops)
27              (ice-9 hash-table)
28              (ice-9 format)
29              (fibers conditions))
30
31 (set! *random-state* (random-state-from-platform))
32 (define (random-choice lst)
33   (list-ref lst (random (length lst))))
34
35
36 (define student-names
37   '("Henry" "Harmony" "Rolf"))
38
39 (define (student-name-generator)
40   ;; a hashmap of student names with their current count
41   (define student-count (make-hash-table))
42   (lambda ()
43     (let* ((student (random-choice student-names))
44            (current-number (hash-ref student-count student 1)))
45       (hash-set! student-count student (1+ current-number))
46       (format #f "~a-~a" student current-number))))
47
48
49 (define-actor <student> (<actor>)
50   ((bother-professor
51     (lambda* (actor message #:key target)
52       "Go bother a professor"
53       (while (not (student-dead actor))
54         (format #t "~a: Bother bother bother!\n"
55                 (actor-id-actor actor))
56         (<- target 'be-bothered
57             #:noise "Bother bother bother!\n"))))
58
59    (be-lambda-consvardraed
60     (lambda (actor message)
61       "This kills the student."
62       (format #t "~a says: AAAAAAAHHHH!!! I'm dead!\n"
63               (actor-id-actor actor))
64       (set! (student-dead actor) #t))))
65   (name #:init-keyword #:name)
66   (dead #:init-value #f
67         #:accessor student-dead))
68
69 (define complaints
70   '("Hey!" "Stop that!" "Oof!"))
71
72 (define (professor-be-bothered actor message . rest)
73   (define whos-bothering (professor-bothered-by actor))
74   (hash-set! whos-bothering (message-from message) #t)
75
76   ;; Oof!  Those kids!
77   (display (string-append (random-choice complaints)))
78   (newline)
79
80   ;; More than one student is bothering us, lose our temper
81   (if (> (hash-count (const #t) whos-bothering)
82          1)
83       (begin
84         (format #t "~s: LAMBDA CONSVARDRA!\n"
85                 (actor-id actor))
86         (hash-for-each
87          (lambda (student _)
88            (<- student 'be-lambda-consvardraed)
89            ;; Remove student from bothering list
90            (hash-remove! whos-bothering student))
91          whos-bothering))
92       ;; Otherwise, remove them from the list and carry on
93       (hash-remove! whos-bothering (message-from message))))
94
95 (define-actor <professor> (<actor>)
96   ((be-bothered professor-be-bothered))
97   ;; This value checks whether any other actor is currently
98   ;; bothering this same character.
99   ;; We'll use a hash table as a fake set.
100   (bothered-by #:init-thunk make-hash-table
101                #:getter professor-bothered-by))
102
103 (define num-students 10)
104
105 (define (main . args)
106   (run-hive
107    (lambda (hive)
108      (define professor (create-actor* <professor> "prof"))
109      (define namegen (student-name-generator))
110      (define students
111        (map
112         (lambda _
113           (let ((name (namegen)))
114             (create-actor* <student> name
115                            #:name name)))
116         (iota num-students)))
117
118      ;; Bootstrap each student into bothering-professor mode.
119      (define start-bothering-tasks
120        (map
121         (lambda (student)
122           (<- student 'bother-professor
123               #:target professor))
124         students))
125
126      (run-hive hive start-bothering-tasks)
127      ;; in other words, this program doesn't really halt
128      (wait (make-condition)))))