X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=demos%2Factors%2Fbotherbotherbother.scm;h=57b20ff22a78abd9d6b2c685b3073969856d5aab;hb=38afa0b278e17953b64764d800beaaa6368f70be;hp=2e72b2d21034ac5ad7eb41c35485b27f47ad319a;hpb=dc2155083a90de90e24f5341b837d4d96ce2898c;p=8sync.git diff --git a/demos/actors/botherbotherbother.scm b/demos/actors/botherbotherbother.scm index 2e72b2d..57b20ff 100755 --- a/demos/actors/botherbotherbother.scm +++ b/demos/actors/botherbotherbother.scm @@ -3,7 +3,7 @@ !# ;;; 8sync --- Asynchronous programming for Guile -;;; Copyright (C) 2016 Christopher Allan Webber +;;; Copyright © 2016, 2017 Christopher Allan Webber ;;; ;;; This file is part of 8sync. ;;; @@ -22,11 +22,11 @@ ;; Puppet show simulator. -(use-modules (8sync agenda) - (8sync actors) +(use-modules (8sync actors) (oop goops) (ice-9 hash-table) - (ice-9 format)) + (ice-9 format) + (fibers conditions)) (set! *random-state* (random-state-from-platform)) (define (random-choice lst) @@ -46,28 +46,25 @@ (format #f "~a-~a" student current-number)))) -(define-class () +(define-actor () + ((bother-professor + (lambda* (actor message #:key target) + "Go bother a professor" + (while (not (student-dead actor)) + (format #t "~a: Bother bother bother!\n" + (actor-id-actor actor)) + (<- target 'be-bothered + #:noise "Bother bother bother!\n")))) + + (be-lambda-consvardraed + (lambda (actor message) + "This kills the student." + (format #t "~a says: AAAAAAAHHHH!!! I'm dead!\n" + (actor-id-actor actor)) + (set! (student-dead actor) #t)))) (name #:init-keyword #:name) (dead #:init-value #f - #:accessor student-dead) - (actions #:allocation #:each-subclass - #:init-value - (build-actions - (bother-professor - (lambda* (actor message #:key target) - "Go bother a professor" - (while (not (student-dead actor)) - (format #t "~a: Bother bother bother!\n" - (actor-id-actor actor)) - (<- target 'be-bothered - #:noise "Bother bother bother!\n")))) - - (be-lambda-consvardraed - (lambda (actor message) - "This kills the student." - (format #t "~a says: AAAAAAAHHHH!!! I'm dead!\n" - (actor-id-actor actor)) - (set! (student-dead actor) #t)))))) + #:accessor student-dead)) (define complaints '("Hey!" "Stop that!" "Oof!")) @@ -95,38 +92,37 @@ ;; Otherwise, remove them from the list and carry on (hash-remove! whos-bothering (message-from message)))) -(define-class () +(define-actor () + ((be-bothered professor-be-bothered)) ;; This value checks whether any other actor is currently ;; bothering this same character. ;; We'll use a hash table as a fake set. (bothered-by #:init-thunk make-hash-table - #:getter professor-bothered-by) - (actions #:allocation #:each-subclass - #:init-value - (build-actions - (be-bothered professor-be-bothered)))) + #:getter professor-bothered-by)) (define num-students 10) (define (main . args) - (define agenda (make-agenda)) - (define hive (make-hive)) - (define professor (hive-create-actor* hive "prof")) - (define namegen (student-name-generator)) - (define students - (map - (lambda _ - (let ((name (namegen))) - (hive-create-actor* hive name - #:name name))) - (iota num-students))) - - ;; Bootstrap each student into bothering-professor mode. - (define start-bothering-tasks - (map - (lambda (student) - (bootstrap-message hive student 'bother-professor - #:target professor)) - students)) - - (run-hive hive start-bothering-tasks)) + (run-hive + (lambda (hive) + (define professor (create-actor* "prof")) + (define namegen (student-name-generator)) + (define students + (map + (lambda _ + (let ((name (namegen))) + (create-actor* name + #:name name))) + (iota num-students))) + + ;; Bootstrap each student into bothering-professor mode. + (define start-bothering-tasks + (map + (lambda (student) + (<- student 'bother-professor + #:target professor)) + students)) + + (run-hive hive start-bothering-tasks) + ;; in other words, this program doesn't really halt + (wait (make-condition)))))