X-Git-Url: https://jxself.org/git/?p=8sync.git;a=blobdiff_plain;f=demos%2Factors%2Fbotherbotherbother.scm;h=3bd30a4cfd9f2cc0d141de5ffc96a1fc441ac71a;hp=5f294fd8d46097c2c3d41bda4a943ef068cc60af;hb=4998e7a9fe3b303923d918cd6087633d5302274f;hpb=5985528115e276d273a6a4bb1df57c93cab61f84 diff --git a/demos/actors/botherbotherbother.scm b/demos/actors/botherbotherbother.scm index 5f294fd..3bd30a4 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. ;;; @@ -23,7 +23,7 @@ ;; Puppet show simulator. (use-modules (8sync agenda) - (8sync systems actors) + (8sync actors) (oop goops) (ice-9 hash-table) (ice-9 format)) @@ -46,41 +46,36 @@ (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) - (message-handler - #:init-value - (make-action-dispatch - (bother-professor - (lambda (actor message) - "Go bother a professor" - (while (not (student-dead actor)) - (format #t "~a: Bother bother bother!\n" - (actor-id-actor actor)) - (send-message - actor (message-ref message '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!")) -(define (professor-be-bothered actor message) +(define (professor-be-bothered actor message . rest) (define whos-bothering (professor-bothered-by actor)) - (hash-set! whos-bothering (message-from message) #t) ;; Oof! Those kids! (display (string-append (random-choice complaints))) + (newline) ;; More than one student is bothering us, lose our temper (if (> (hash-count (const #t) whos-bothering) @@ -90,47 +85,42 @@ (actor-id actor)) (hash-for-each (lambda (student _) - (send-message - actor student - 'be-lambda-consvardraed) + (<- student 'be-lambda-consvardraed) ;; Remove student from bothering list (hash-remove! whos-bothering student)) whos-bothering)) ;; 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) - (message-handler - #:init-value - (make-action-dispatch - (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 professor (bootstrap-actor* hive "prof")) (define namegen (student-name-generator)) (define students (map (lambda _ (let ((name (namegen))) - (hive-create-actor* hive name - #:name name))) + (bootstrap-actor* hive name + #:name name))) (iota num-students))) ;; Bootstrap each student into bothering-professor mode. (define start-bothering-tasks (map (lambda (student) - (hive-bootstrap-message hive student 'bother-professor + (bootstrap-message hive student 'bother-professor #:target professor)) students)) - (ez-run-hive hive start-bothering-tasks)) + (run-hive hive start-bothering-tasks))