From faf8f70b2ef18b9afb297764f259a67ce94dcd6d Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sat, 15 Feb 2020 20:30:46 +0100 Subject: [PATCH] bn: update: Add and test. --- bn-tests.rkt | 28 +++++++++++++++++++++++++++- bn.rkt | 14 +++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/bn-tests.rkt b/bn-tests.rkt index f7867db..0b80fdd 100644 --- a/bn-tests.rkt +++ b/bn-tests.rkt @@ -4,4 +4,30 @@ (require typed/rackunit "bn.rkt") -(check-equal? (+ 1 2 3) 6) +;;; This test case sets up the following Boolean network: +;;; x1 = x1 AND NOT x2 +;;; x2 = NOT x2 +(test-begin + (test-case "Simple two-variable Boolean network" + (let* ([f1 (λ ([s : State]) + (let ([x1 (hash-ref s 'x1)] + [x2 (hash-ref s 'x2)]) + (and x1 (not x2))))] + [f2 (λ ([s : State]) + (let ([x2 (hash-ref s 'x2)]) + (not x2)))] + [bn (hash 'x1 f1 'x2 f2)]) + + (test-case "One-step syncronous update" + (let* ([s (hash 'x1 #t 'x2 #f)] + [new-s (update bn s '(x2 x1))]) + (check-equal? (hash-ref new-s 'x1) #t) + (check-equal? (hash-ref new-s 'x2) #t) + (check-equal? (length (hash-keys new-s)) 2))) + + (test-case "One-step asynchronous update" + (let* ([s (hash 'x1 #f 'x2 #f)] + [new-s (update bn s '(x2 x1))]) + (check-equal? (hash-ref new-s 'x1) #f) + (check-equal? (hash-ref new-s 'x2) #t) + (check-equal? (length (hash-keys new-s)) 2)))))) diff --git a/bn.rkt b/bn.rkt index d8b3c5a..bec7be5 100644 --- a/bn.rkt +++ b/bn.rkt @@ -7,7 +7,8 @@ ;;; are updated according to their corresponding update functions. ;;; The variables to be updated at each step are given by the mode. -(provide Variable State UpdateFunc Network) +(provide Variable State UpdateFunc Network + update) (define-type Variable Symbol) @@ -22,3 +23,14 @@ ;;; A Boolean network is a mapping from its variables to its update ;;; functions. (define-type Network (HashTable Variable UpdateFunc)) + +;;; Given a state s updates all the variables from xs. This +;;; corresponds to a parallel mode. +(define (update [bn : Network] ; the Boolean network + [s : State] ; the state to operate on + [xs : (Listof Variable)]) ; the variables to update + (let ([new-s : State (hash-copy s)]) + (for ([x xs]) + (let ([f (hash-ref bn x)]) + (hash-set! new-s x (f s)))) + new-s))