5 ;;; 8sync --- Asynchronous programming for Guile
6 ;;; Copyright (C) 2016 Christopher Allan Webber <cwebber@dustycloud.org>
8 ;;; This file is part of 8sync.
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.
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.
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/>.
23 ;; Puppet show simulator.
25 (use-modules (8sync agenda)
31 (set! *random-state* (random-state-from-platform))
32 (define (random-choice lst)
33 (list-ref lst (random (length lst))))
37 '("Henry" "Harmony" "Rolf"))
39 (define (student-name-generator)
40 ;; a hashmap of student names with their current count
41 (define student-count (make-hash-table))
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))))
49 (define-class <student> (<actor>)
50 (name #:init-keyword #:name)
52 #:accessor student-dead)
53 (actions #:allocation #:each-subclass
57 (lambda* (actor message #:key target)
58 "Go bother a professor"
59 (while (not (student-dead actor))
60 (format #t "~a: Bother bother bother!\n"
61 (actor-id-actor actor))
62 (<- target 'be-bothered
63 #:noise "Bother bother bother!\n"))))
65 (be-lambda-consvardraed
66 (lambda (actor message)
67 "This kills the student."
68 (format #t "~a says: AAAAAAAHHHH!!! I'm dead!\n"
69 (actor-id-actor actor))
70 (set! (student-dead actor) #t))))))
73 '("Hey!" "Stop that!" "Oof!"))
75 (define (professor-be-bothered actor message . rest)
76 (define whos-bothering (professor-bothered-by actor))
77 (hash-set! whos-bothering (message-from message) #t)
80 (display (string-append (random-choice complaints)))
83 ;; More than one student is bothering us, lose our temper
84 (if (> (hash-count (const #t) whos-bothering)
87 (format #t "~s: LAMBDA CONSVARDRA!\n"
91 (<- student 'be-lambda-consvardraed)
92 ;; Remove student from bothering list
93 (hash-remove! whos-bothering student))
95 ;; Otherwise, remove them from the list and carry on
96 (hash-remove! whos-bothering (message-from message))))
98 (define-class <professor> (<actor>)
99 ;; This value checks whether any other actor is currently
100 ;; bothering this same character.
101 ;; We'll use a hash table as a fake set.
102 (bothered-by #:init-thunk make-hash-table
103 #:getter professor-bothered-by)
104 (actions #:allocation #:each-subclass
107 (be-bothered professor-be-bothered))))
109 (define num-students 10)
111 (define (main . args)
112 (define agenda (make-agenda))
113 (define hive (make-hive))
114 (define professor (bootstrap-actor* hive <professor> "prof"))
115 (define namegen (student-name-generator))
119 (let ((name (namegen)))
120 (bootstrap-actor* hive <student> name
122 (iota num-students)))
124 ;; Bootstrap each student into bothering-professor mode.
125 (define start-bothering-tasks
128 (bootstrap-message hive student 'bother-professor
132 (run-hive hive start-bothering-tasks))