From 9ce3040240a2acf2c9e5b8b135117eddcea59ccf Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Tue, 3 Mar 2020 00:51:53 +0100 Subject: [PATCH] rs: Add build-interactive-process. --- rs-tests.rkt | 10 +++++++++- rs.rkt | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/rs-tests.rkt b/rs-tests.rkt index b64560b..d25b355 100644 --- a/rs-tests.rkt +++ b/rs-tests.rkt @@ -73,4 +73,12 @@ (state (set 'y 'z) (list (set 'y) (set 'z) (set) (set 'z)))) (set (set 'a 'b))) - (check-equal? sgr ip))) + (check-equal? sgr ip) + + (check-equal? (build-interactive-process rs (list (set 'x) (set 'y) (set 'z) (set) (set 'z))) + (list + (list (set 'x) (set)) + (list (set 'y) (set 'y 'z)) + (list (set 'z) (set)) + (list (set) (set)) + (list (set 'z) (set)))))) diff --git a/rs.rkt b/rs.rkt index 142fe60..7b6a855 100644 --- a/rs.rkt +++ b/rs.rkt @@ -27,6 +27,7 @@ [dds-build-state-graph-annotated (-> dynamics? (set/c state? #:kind 'dont-care) graph?)] [dds-build-n-step-state-graph-annotated (-> dynamics? (set/c state? #:kind 'dont-care) number? graph?)] [build-interactive-process-graph (-> reaction-system/c (listof (set/c species?)) graph?)] + [build-interactive-process (-> reaction-system/c (listof (set/c species?)) (listof (list/c (set/c species?) (set/c species?))))] [pretty-print-state-graph (-> graph? graph?)]) ;; Predicates (contract-out [species? (-> any/c boolean?)]) @@ -173,6 +174,24 @@ (dds-build-state-graph-annotated (dynamics rs) (set (state (set) contexts)))) +;;; Builds the interactive process driven by the given context +;;; sequence. The output is a list of pairs of lists in which the +;;; first element is the current context and the second element is the +;;; result of the application of reactions to the previous state. The +;;; interactive process stops when there are no more contexts left. +(define (build-interactive-process rs contexts) + (let ([dyn (dynamics rs)]) + (for/fold ([proc '()] + [st (state (set) contexts)] + #:result (reverse proc)) + ([c contexts]) + (values + (cons (match st + [(state res ctx) + (list (if (empty? ctx) (set) (car ctx)) res)]) + proc) + (set-first (dds-step-one dyn st)))))) + ;;; Pretty-prints the context sequence and the current result of a ;;; state of the reaction system. Note that we need to keep the full ;;; context sequence in the name of each state to avoid confusion