rs: Add build-interactive-process.

This commit is contained in:
Sergiu Ivanov 2020-03-03 00:51:53 +01:00
parent 9cfab2e45d
commit 9ce3040240
2 changed files with 28 additions and 1 deletions

View file

@ -73,4 +73,12 @@
(state (set 'y 'z) (list (set 'y) (set 'z) (set) (set 'z)))) (state (set 'y 'z) (list (set 'y) (set 'z) (set) (set 'z))))
(set (set 'a 'b))) (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))))))

19
rs.rkt
View file

@ -27,6 +27,7 @@
[dds-build-state-graph-annotated (-> dynamics? (set/c state? #:kind 'dont-care) graph?)] [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?)] [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-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?)]) [pretty-print-state-graph (-> graph? graph?)])
;; Predicates ;; Predicates
(contract-out [species? (-> any/c boolean?)]) (contract-out [species? (-> any/c boolean?)])
@ -173,6 +174,24 @@
(dds-build-state-graph-annotated (dynamics rs) (dds-build-state-graph-annotated (dynamics rs)
(set (state (set) contexts)))) (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 ;;; Pretty-prints the context sequence and the current result of a
;;; state of the reaction system. Note that we need to keep the full ;;; state of the reaction system. Note that we need to keep the full
;;; context sequence in the name of each state to avoid confusion ;;; context sequence in the name of each state to avoid confusion