2020-03-01 20:25:01 +01:00
|
|
|
#lang racket
|
|
|
|
|
2023-08-08 17:22:03 +02:00
|
|
|
(module typed typed/racket
|
|
|
|
(require typed/graph "utils.rkt" "dynamics.rkt")
|
2020-03-01 19:05:28 +01:00
|
|
|
|
2023-08-08 17:22:03 +02:00
|
|
|
(provide
|
2023-08-09 11:29:09 +02:00
|
|
|
Species (struct-out reaction) Reaction ReactionName ReactionSystem
|
2023-08-10 01:07:41 +02:00
|
|
|
make-reaction enabled? list-enabled union-products)
|
2023-08-08 17:22:03 +02:00
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(require typed/rackunit))
|
|
|
|
|
|
|
|
(define-type Species Symbol)
|
2023-08-08 17:40:44 +02:00
|
|
|
|
|
|
|
(struct reaction ([reactants : (Setof Species)]
|
|
|
|
[inhibitors : (Setof Species)]
|
|
|
|
[products : (Setof Species)])
|
|
|
|
#:transparent
|
|
|
|
#:type-name Reaction)
|
2023-08-08 18:09:24 +02:00
|
|
|
|
2023-08-09 11:29:09 +02:00
|
|
|
(define-type ReactionName Symbol)
|
|
|
|
|
2023-08-08 18:09:24 +02:00
|
|
|
(: make-reaction (-> (Listof Species) (Listof Species) (Listof Species) Reaction))
|
|
|
|
(define (make-reaction r i p) (reaction (list->set r)
|
|
|
|
(list->set i)
|
|
|
|
(list->set p)))
|
|
|
|
(module+ test
|
|
|
|
(test-case "make-reaction"
|
|
|
|
(check-equal? (make-reaction '(a b) '(c d) '(e f))
|
|
|
|
(reaction (set 'b 'a) (set 'c 'd) (set 'f 'e)))))
|
2023-08-08 18:28:25 +02:00
|
|
|
|
|
|
|
(: enabled? (-> Reaction (Setof Species) Boolean))
|
|
|
|
(define/match (enabled? r s)
|
|
|
|
[((reaction r i _) s)
|
|
|
|
(and (subset? r s) (set-empty? (set-intersect i s)))])
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(test-case "enabled?"
|
|
|
|
(check-true (enabled? (make-reaction '(a b) '(c d) '())
|
|
|
|
(set 'a 'b 'e)))
|
|
|
|
(check-false (enabled? (make-reaction '(a b) '(c d) '())
|
|
|
|
(set 'a 'b 'c)))
|
|
|
|
(check-false (enabled? (make-reaction '(a b) '(c d) '())
|
|
|
|
(set 'b 'e)))))
|
2023-08-08 18:34:40 +02:00
|
|
|
|
2023-08-09 11:29:09 +02:00
|
|
|
(define-type ReactionSystem (HashTable ReactionName Reaction))
|
2023-08-09 11:15:43 +02:00
|
|
|
|
2023-08-09 11:29:09 +02:00
|
|
|
(: list-enabled (-> ReactionSystem (Setof Species) (Listof ReactionName)))
|
2023-08-09 11:15:43 +02:00
|
|
|
(define (list-enabled rs s)
|
|
|
|
(for/list ([(name reaction) (in-hash rs)]
|
|
|
|
#:when (enabled? reaction s))
|
|
|
|
name))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(test-case "list-enabled"
|
|
|
|
(define rs (hash 'a (make-reaction '(x) '(y) '(z))
|
|
|
|
'b (make-reaction '(x y) '() '(z))))
|
|
|
|
(check-equal? (list-enabled rs (set 'x 'y)) '(b))
|
|
|
|
(check-equal? (list-enabled rs (set 'x)) '(a))))
|
2023-08-10 01:07:41 +02:00
|
|
|
|
|
|
|
(: union-products (-> ReactionSystem (Listof ReactionName) (Setof Species)))
|
|
|
|
(define (union-products rs as)
|
|
|
|
(cond
|
|
|
|
[(empty? as) (set)]
|
|
|
|
[else (define products (for/list : (Listof (Setof Species))
|
|
|
|
([a as])
|
|
|
|
(reaction-products (hash-ref rs a))))
|
|
|
|
(apply set-union (assert-type products (NonemptyListof (Setof Species))))]))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(test-case "union-products"
|
|
|
|
(define rs (hash 'a (make-reaction '(x) '(y) '(z))
|
|
|
|
'b (make-reaction '(x y) '() '(t))))
|
|
|
|
(check-equal? (union-products rs '(a b))
|
|
|
|
(set 't 'z))
|
|
|
|
(check-equal? (union-products rs '(a))
|
|
|
|
(set 'z))
|
|
|
|
(check-equal? (union-products rs '())
|
|
|
|
(set))))
|
2023-08-08 17:22:03 +02:00
|
|
|
)
|
2020-03-01 19:05:28 +01:00
|
|
|
|
2020-03-02 18:32:11 +01:00
|
|
|
(require graph "utils.rkt" "generic.rkt")
|
2020-03-01 20:20:16 +01:00
|
|
|
|
2020-03-01 19:05:28 +01:00
|
|
|
(provide
|
|
|
|
;; Structures
|
2020-03-01 20:41:11 +01:00
|
|
|
(struct-out reaction)
|
2020-03-02 18:32:11 +01:00
|
|
|
(struct-out state)
|
|
|
|
(struct-out dynamics)
|
2020-03-01 19:05:28 +01:00
|
|
|
;; Functions
|
2020-03-01 20:41:11 +01:00
|
|
|
(contract-out [enabled? (-> reaction? (set/c symbol?) boolean?)]
|
|
|
|
[list-enabled (-> reaction-system/c (set/c species?) (listof symbol?))]
|
|
|
|
[union-products (-> reaction-system/c (listof symbol?) (set/c species?))]
|
|
|
|
[apply-rs (-> reaction-system/c (set/c species?) (set/c species?))]
|
2020-03-01 21:10:01 +01:00
|
|
|
[ht-str-triples->rs (-> (hash/c symbol? (list/c string? string? string?)) reaction-system/c)]
|
2020-03-24 21:31:01 +01:00
|
|
|
[read-org-rs (-> string? reaction-system/c)]
|
2020-03-02 23:03:10 +01:00
|
|
|
[read-context-sequence (-> string? (listof (set/c species?)))]
|
2020-03-02 18:32:11 +01:00
|
|
|
[rs->ht-str-triples (-> reaction-system/c (hash/c symbol? (list/c string? string? string?)))]
|
|
|
|
[dds-step-one (-> dynamics? state? (set/c state?))]
|
|
|
|
[dds-step-one-annotated (-> dynamics? state? (set/c (cons/c (set/c symbol?) state?)))]
|
|
|
|
[dds-step (-> dynamics? (set/c state? #:kind 'dont-care) (set/c state?))]
|
|
|
|
[dds-build-state-graph (-> dynamics? (set/c state? #:kind 'dont-care) graph?)]
|
|
|
|
[dds-build-n-step-state-graph (-> dynamics? (set/c state? #:kind 'dont-care) number? graph?)]
|
|
|
|
[dds-build-state-graph-annotated (-> dynamics? (set/c state? #:kind 'dont-care) graph?)]
|
2020-03-02 23:50:32 +01:00
|
|
|
[dds-build-n-step-state-graph-annotated (-> dynamics? (set/c state? #:kind 'dont-care) number? graph?)]
|
2020-03-03 00:27:52 +01:00
|
|
|
[build-interactive-process-graph (-> reaction-system/c (listof (set/c species?)) graph?)]
|
2020-11-10 09:42:50 +01:00
|
|
|
[build-reduced-state-graph (-> reaction-system/c (listof (set/c species?)) graph?)]
|
2020-11-11 00:36:49 +01:00
|
|
|
[pretty-print-reduced-state-graph (-> graph? graph?)]
|
2020-03-03 00:51:53 +01:00
|
|
|
[build-interactive-process (-> reaction-system/c (listof (set/c species?)) (listof (list/c (set/c species?) (set/c species?))))]
|
2020-03-03 00:12:45 +01:00
|
|
|
[pretty-print-state-graph (-> graph? graph?)])
|
2020-03-01 20:41:11 +01:00
|
|
|
;; Predicates
|
|
|
|
(contract-out [species? (-> any/c boolean?)])
|
|
|
|
;; Contracts
|
2020-03-24 23:22:20 +01:00
|
|
|
(contract-out [reaction-system/c contract?]))
|
2020-03-01 19:05:28 +01:00
|
|
|
|
2020-05-23 00:00:28 +02:00
|
|
|
(module+ test
|
|
|
|
(require rackunit))
|
|
|
|
|
|
|
|
|
2020-03-01 19:05:28 +01:00
|
|
|
;;; =================
|
|
|
|
;;; Basic definitions
|
|
|
|
;;; =================
|
|
|
|
|
|
|
|
;;; A species is a symbol.
|
2020-03-01 20:41:11 +01:00
|
|
|
(define species? symbol?)
|
2020-03-01 19:05:28 +01:00
|
|
|
|
|
|
|
;;; A reaction is a triple of sets, giving the reactants, the
|
|
|
|
;;; inhibitors, and the products, respectively.
|
2020-03-01 20:41:11 +01:00
|
|
|
(struct reaction (reactants inhibitors products) #:transparent)
|
2020-03-01 19:05:28 +01:00
|
|
|
|
|
|
|
;;; A reaction is enabled on a set if all of its reactants are in the
|
|
|
|
;;; set and none of its inhibitors are.
|
|
|
|
(define/match (enabled? r s)
|
|
|
|
[((reaction r i p) s)
|
|
|
|
(and (subset? r s) (set-empty? (set-intersect i s)))])
|
2020-03-01 19:19:58 +01:00
|
|
|
|
|
|
|
;;; A reaction system is a dictionary mapping reaction names to
|
|
|
|
;;; reactions.
|
2020-03-01 20:41:11 +01:00
|
|
|
(define reaction-system/c (hash/c symbol? reaction?))
|
2020-03-01 19:20:24 +01:00
|
|
|
|
|
|
|
;;; Returns the list of reaction names enabled on a given set.
|
|
|
|
(define (list-enabled rs s)
|
|
|
|
(for/list ([(name reaction) (in-hash rs)]
|
|
|
|
#:when (enabled? reaction s))
|
|
|
|
name))
|
2020-03-01 19:47:38 +01:00
|
|
|
|
|
|
|
;;; Returns the union of the product sets of the given reactions in a
|
2020-03-02 18:31:02 +01:00
|
|
|
;;; reaction system. If no reactions are supplied, returns the empty
|
|
|
|
;;; set.
|
2020-03-01 19:47:38 +01:00
|
|
|
;;;
|
|
|
|
;;; This function can be seen as producing the result of the
|
|
|
|
;;; application of the given reactions to a set. Clearly, it does not
|
|
|
|
;;; check whether the reactions are actually enabled.
|
|
|
|
(define (union-products rs as)
|
2020-03-02 18:31:02 +01:00
|
|
|
(if (empty? as)
|
|
|
|
(set)
|
|
|
|
(apply set-union
|
|
|
|
(for/list ([a as])
|
|
|
|
(reaction-products (hash-ref rs a))))))
|
2020-03-01 19:51:53 +01:00
|
|
|
|
|
|
|
;;; Applies a reaction system to a set.
|
|
|
|
(define (apply-rs rs s)
|
|
|
|
(let ([as (list-enabled rs s)])
|
|
|
|
(union-products rs as)))
|
2020-03-01 20:20:16 +01:00
|
|
|
|
2020-05-23 00:00:28 +02:00
|
|
|
(module+ test
|
|
|
|
(test-case "Basic definitions"
|
2020-05-27 23:48:22 +02:00
|
|
|
(define r1 (reaction (set 'x) (set 'y) (set 'z)))
|
|
|
|
(define r2 (reaction (set 'x) (set) (set 'y)))
|
|
|
|
(define rs (make-immutable-hash (list (cons 'a r1) (cons 'b r2))))
|
|
|
|
(define s1 (set 'x 'z))
|
|
|
|
(define s2 (set 'x 'y))
|
|
|
|
(check-true (enabled? r1 s1))
|
|
|
|
(check-false (enabled? r1 s2))
|
|
|
|
(check-equal? (list-enabled rs s1) '(a b))
|
|
|
|
(check-equal? (list-enabled rs s2) '(b))
|
|
|
|
(check-equal? (union-products rs '(a b)) (set 'y 'z))
|
|
|
|
(check-equal? (apply-rs rs s1) (set 'y 'z))
|
|
|
|
(check-equal? (apply-rs rs s2) (set 'y))))
|
2020-05-23 00:00:28 +02:00
|
|
|
|
2020-03-01 20:20:16 +01:00
|
|
|
|
|
|
|
;;; ====================
|
|
|
|
;;; Org-mode interaction
|
|
|
|
;;; ====================
|
|
|
|
|
2020-03-01 20:43:33 +01:00
|
|
|
;;; This section contains some useful primitives for Org-mode
|
|
|
|
;;; interoperability.
|
2020-03-01 20:20:16 +01:00
|
|
|
|
|
|
|
;;; Converts a triple of strings to a reaction.
|
|
|
|
(define/match (str-triple->reaction lst)
|
|
|
|
[((list str-reactants str-inhibitors str-products))
|
|
|
|
(reaction (list->set (read-symbol-list str-reactants))
|
|
|
|
(list->set (read-symbol-list str-inhibitors))
|
|
|
|
(list->set (read-symbol-list str-products)))])
|
|
|
|
|
|
|
|
;;; Converts a hash table mapping reaction names to triples of strings
|
|
|
|
;;; to a reaction system.
|
|
|
|
(define (ht-str-triples->rs ht)
|
2020-03-01 20:41:11 +01:00
|
|
|
(for/hash ([(a triple) (in-hash ht)])
|
2020-03-01 20:20:16 +01:00
|
|
|
(values a (str-triple->reaction triple))))
|
2020-03-01 20:26:16 +01:00
|
|
|
|
2020-05-23 00:00:28 +02:00
|
|
|
(module+ test
|
2020-05-27 23:48:22 +02:00
|
|
|
(test-case "ht-str-triples->rs"
|
|
|
|
(check-equal?
|
|
|
|
(ht-str-triples->rs #hash((a . ("x t" "y" "z"))))
|
|
|
|
(make-immutable-hash (list (cons 'a (reaction (set 'x 't) (set 'y) (set 'z))))))))
|
2020-05-23 00:00:28 +02:00
|
|
|
|
2020-03-24 21:31:01 +01:00
|
|
|
;;; Reads a reaction system from an Org-mode style string.
|
|
|
|
(define read-org-rs (compose ht-str-triples->rs read-org-variable-mapping))
|
|
|
|
|
2020-05-23 00:00:28 +02:00
|
|
|
(module+ test
|
2020-05-27 23:48:22 +02:00
|
|
|
(test-case "read-org-rs"
|
|
|
|
(check-equal? (read-org-rs "((\"a\" \"x t\" \"y\" \"z\") (\"b\" \"x\" \"q\" \"z\"))")
|
|
|
|
(hash
|
|
|
|
'a
|
|
|
|
(reaction (set 'x 't) (set 'y) (set 'z))
|
|
|
|
'b
|
|
|
|
(reaction (set 'x) (set 'q) (set 'z))))))
|
2020-05-23 00:00:28 +02:00
|
|
|
|
2020-03-02 23:03:10 +01:00
|
|
|
;;; Reads a context sequence from an Org sexp corresponding to a list.
|
|
|
|
(define (read-context-sequence str)
|
2020-05-16 00:16:44 +02:00
|
|
|
(map (compose list->set read-symbol-list) (flatten (string->any str))))
|
2020-03-02 23:03:10 +01:00
|
|
|
|
2020-05-23 00:00:28 +02:00
|
|
|
(module+ test
|
2020-05-27 23:48:22 +02:00
|
|
|
(test-case "read-context-sequence"
|
|
|
|
(check-equal? (read-context-sequence "((\"x y\") (\"z\") (\"\") (\"t\"))")
|
|
|
|
(list (set 'x 'y) (set 'z) (set) (set 't)))))
|
2020-05-23 00:00:28 +02:00
|
|
|
|
2020-03-01 21:10:01 +01:00
|
|
|
;;; Converts a reaction to a triple of strings.
|
|
|
|
(define/match (reaction->str-triple r)
|
|
|
|
[((reaction r i p))
|
|
|
|
(map (compose drop-first-last any->string set->list)
|
|
|
|
(list r i p))])
|
|
|
|
|
|
|
|
;;; Converts a reaction system to a hash table mapping reaction names
|
|
|
|
;;; to triples of strings.
|
|
|
|
(define (rs->ht-str-triples rs)
|
|
|
|
(for/hash ([(a r) (in-hash rs)])
|
|
|
|
(values a (reaction->str-triple r))))
|
|
|
|
|
2020-05-23 00:00:28 +02:00
|
|
|
(module+ test
|
2020-05-27 23:48:22 +02:00
|
|
|
(test-case "rs->ht-str-triples"
|
|
|
|
(check-equal?
|
|
|
|
(rs->ht-str-triples (make-immutable-hash (list (cons 'a (reaction (set 'x 't) (set 'y) (set 'z))))))
|
|
|
|
#hash((a . ("t x" "y" "z"))))))
|
2020-05-23 00:00:28 +02:00
|
|
|
|
2020-03-02 18:32:11 +01:00
|
|
|
|
|
|
|
;;; ============================
|
|
|
|
;;; Dynamics of reaction systems
|
|
|
|
;;; ============================
|
|
|
|
|
|
|
|
;;; An interactive process of a reaction system is a sequence of
|
2020-11-09 23:13:24 +01:00
|
|
|
;;; states driven by a sequence of contexts in the following way.
|
|
|
|
;;; The reaction system starts with the initial context. Then, at
|
|
|
|
;;; every step, the result of applying the reaction system is merged
|
|
|
|
;;; with the next element of the context sequence, and the reaction
|
|
|
|
;;; system is then applied to the result of the union. If the
|
|
|
|
;;; sequence of contexts is empty, the reaction system cannot evolve.
|
2020-03-02 18:32:11 +01:00
|
|
|
|
|
|
|
;;; A state of a reaction system is a set of species representing the
|
|
|
|
;;; result of the application of the reactions from the previous
|
2020-03-02 23:42:03 +01:00
|
|
|
;;; steps, plus the rest of the context sequence. When the context
|
|
|
|
;;; sequence is empty, nothing is added to the current state.
|
2020-03-02 18:32:11 +01:00
|
|
|
(struct state (result rest-contexts) #:transparent)
|
|
|
|
|
|
|
|
;;; The dynamics of the reaction system only stores the reaction
|
|
|
|
;;; system itself.
|
|
|
|
(struct dynamics (rs) #:transparent
|
|
|
|
#:methods gen:dds
|
|
|
|
[;; Since reaction systems are deterministic, a singleton set is
|
2020-11-09 23:13:24 +01:00
|
|
|
;; produced, unless the context sequence is empty, in which case an
|
|
|
|
;; empty set of states is generated. This transition is annotated
|
|
|
|
;; by the list of rules which were enabled in the current step.
|
2020-03-02 18:32:11 +01:00
|
|
|
(define (dds-step-one-annotated dyn st)
|
2020-11-09 23:13:24 +01:00
|
|
|
(define rs (dynamics-rs dyn))
|
|
|
|
(define (apply-rs-annotate s rest-ctx)
|
|
|
|
(define en (list-enabled rs s))
|
|
|
|
(set (cons (list->set en)
|
|
|
|
(state (union-products rs en) rest-ctx))))
|
|
|
|
(match st
|
|
|
|
[(state res (cons ctx rest-ctx))
|
|
|
|
(apply-rs-annotate (set-union res ctx) rest-ctx)]
|
|
|
|
[(state res '()) (set)]))])
|
2020-03-02 23:50:32 +01:00
|
|
|
|
|
|
|
;;; Builds the state graph of a reaction system driven by a given
|
2020-11-09 23:24:43 +01:00
|
|
|
;;; context sequence.
|
2020-03-03 00:27:52 +01:00
|
|
|
(define (build-interactive-process-graph rs contexts)
|
2020-03-02 23:50:32 +01:00
|
|
|
(dds-build-state-graph-annotated (dynamics rs)
|
|
|
|
(set (state (set) contexts))))
|
2020-03-03 00:12:45 +01:00
|
|
|
|
2020-11-10 09:42:50 +01:00
|
|
|
;;; Builds the reduced state graph of a reaction system driven by
|
|
|
|
;;; a given context sequence. Unlike build-interactive-process-graph,
|
|
|
|
;;; the nodes of this state graph do not contain the context sequence.
|
|
|
|
(define (build-reduced-state-graph rs contexts)
|
|
|
|
(define sgr (build-interactive-process-graph rs contexts))
|
|
|
|
(weighted-graph/directed
|
|
|
|
(for/list ([e (in-edges sgr)])
|
|
|
|
(define u (car e)) (define v (cadr e))
|
|
|
|
(list (edge-weight sgr u v) (state-result u) (state-result v)))))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(test-case "build-reduced-state-graph"
|
|
|
|
(define rs (hash 'a (reaction (set 'x) (set 'y) (set 'z))
|
|
|
|
'b (reaction (set 'x) (set) (set 'y))))
|
|
|
|
(define ctx (list (set 'x) (set 'y) (set 'z) (set) (set 'z)))
|
|
|
|
(check-equal? (graphviz (build-reduced-state-graph rs ctx))
|
|
|
|
"digraph G {\n\tnode0 [label=\"(set)\\n\"];\n\tnode1 [label=\"(set 'y 'z)\\n\"];\n\tsubgraph U {\n\t\tedge [dir=none];\n\t\tnode0 -> node0 [label=\"#<set: #<set:>>\"];\n\t}\n\tsubgraph D {\n\t\tnode0 -> node1 [label=\"#<set: #<set: a b>>\"];\n\t\tnode1 -> node0 [label=\"#<set: #<set:>>\"];\n\t}\n}\n")))
|
|
|
|
|
2020-11-11 00:36:49 +01:00
|
|
|
(define (pretty-print-reduced-state-graph sgr)
|
|
|
|
(update-graph sgr
|
|
|
|
#:v-func (λ (st) (~a "{" (pretty-print-set st) "}"))
|
|
|
|
#:e-func pretty-print-set-sets))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(test-case "pretty-print-reduced-graph"
|
|
|
|
(define rs (hash 'a (reaction (set 'x) (set 'y) (set 'z))
|
|
|
|
'b (reaction (set 'x) (set) (set 'y))))
|
|
|
|
(define ctx (list (set 'x) (set 'y) (set 'z) (set) (set 'z)))
|
|
|
|
(define sgr (build-reduced-state-graph rs ctx))
|
|
|
|
(graphviz (pretty-print-reduced-state-graph sgr))))
|
|
|
|
|
|
|
|
|
2020-03-03 00:51:53 +01:00
|
|
|
;;; 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
|
2020-03-03 01:00:21 +01:00
|
|
|
;;; interactive process stops one step after the end of the context
|
|
|
|
;;; sequence, to show the effect of the last context.
|
2020-03-03 00:51:53 +01:00
|
|
|
(define (build-interactive-process rs contexts)
|
2020-03-03 01:00:21 +01:00
|
|
|
(let ([dyn (dynamics rs)]
|
|
|
|
[padded-contexts (append contexts (list (set)))])
|
2020-03-03 00:51:53 +01:00
|
|
|
(for/fold ([proc '()]
|
2020-03-03 01:00:21 +01:00
|
|
|
[st (state (set) padded-contexts)]
|
2020-03-03 00:51:53 +01:00
|
|
|
#:result (reverse proc))
|
2020-03-03 01:00:21 +01:00
|
|
|
([c padded-contexts])
|
2020-03-03 00:51:53 +01:00
|
|
|
(values
|
|
|
|
(cons (match st
|
|
|
|
[(state res ctx)
|
|
|
|
(list (if (empty? ctx) (set) (car ctx)) res)])
|
|
|
|
proc)
|
|
|
|
(set-first (dds-step-one dyn st))))))
|
|
|
|
|
2020-03-03 00:26:45 +01:00
|
|
|
;;; 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
|
|
|
|
;;; between the states at different steps of the evolution.
|
2020-03-03 00:25:06 +01:00
|
|
|
(define/match (pretty-print-state st)
|
|
|
|
[((state res ctx))
|
|
|
|
(format "C:~a\nD:{~a}" (pretty-print-set-sets ctx) (pretty-print-set res))])
|
|
|
|
|
2020-03-03 00:12:45 +01:00
|
|
|
;;; Pretty prints the state graph of a reaction system.
|
|
|
|
(define (pretty-print-state-graph sgr)
|
2020-03-03 00:25:06 +01:00
|
|
|
(update-graph sgr #:v-func pretty-print-state #:e-func pretty-print-set-sets))
|
2020-05-23 00:00:28 +02:00
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(test-case "Dynamics of reaction systems"
|
2020-05-27 23:48:22 +02:00
|
|
|
(define r1 (reaction (set 'x) (set 'y) (set 'z)))
|
|
|
|
(define r2 (reaction (set 'x) (set) (set 'y)))
|
|
|
|
(define rs (make-immutable-hash (list (cons 'a r1) (cons 'b r2))))
|
|
|
|
(define dyn (dynamics rs))
|
|
|
|
(define state1 (state (set) (list (set 'x) (set 'y) (set 'z) (set) (set 'z))))
|
|
|
|
(define sgr (dds-build-state-graph-annotated dyn (set state1)))
|
|
|
|
(define ip (build-interactive-process-graph rs (list (set 'x) (set 'y) (set 'z) (set) (set 'z))))
|
|
|
|
|
|
|
|
(check-equal? (dds-step-one-annotated dyn state1)
|
|
|
|
(set (cons
|
|
|
|
(set 'a 'b)
|
|
|
|
(state (set 'y 'z) (list (set 'y) (set 'z) (set) (set 'z))))))
|
|
|
|
(check-equal? (dds-step-one dyn state1)
|
|
|
|
(set (state (set 'y 'z) (list (set 'y) (set 'z) (set) (set 'z)))))
|
|
|
|
|
|
|
|
(check-true (has-vertex? sgr (state (set 'y 'z) (list (set 'y) (set 'z) (set) (set 'z)))))
|
|
|
|
(check-true (has-vertex? sgr (state (set) (list (set 'z) (set) (set 'z)))))
|
|
|
|
(check-true (has-vertex? sgr (state (set) (list (set) (set 'z)))))
|
|
|
|
(check-true (has-vertex? sgr (state (set) (list (set 'z)))))
|
|
|
|
(check-true (has-vertex? sgr (state (set) (list (set 'x) (set 'y) (set 'z) (set) (set 'z)))))
|
|
|
|
(check-true (has-vertex? sgr (state (set) '())))
|
|
|
|
|
2020-11-09 23:13:24 +01:00
|
|
|
(check-false (has-edge? sgr
|
|
|
|
(state (set) '())
|
|
|
|
(state (set) '())))
|
2020-05-27 23:48:22 +02:00
|
|
|
(check-equal? (edge-weight sgr
|
|
|
|
(state (set 'y 'z) (list (set 'y) (set 'z) (set) (set 'z)))
|
|
|
|
(state (set) (list (set 'z) (set) (set 'z))))
|
|
|
|
(set (set)))
|
|
|
|
(check-equal? (edge-weight sgr
|
|
|
|
(state (set) (list (set 'z) (set) (set 'z)))
|
|
|
|
(state (set) (list (set) (set 'z))))
|
|
|
|
(set (set)))
|
|
|
|
(check-equal? (edge-weight sgr
|
|
|
|
(state (set) (list (set) (set 'z)))
|
|
|
|
(state (set) (list (set 'z))))
|
|
|
|
(set (set)))
|
|
|
|
(check-equal? (edge-weight sgr
|
|
|
|
(state (set) (list (set 'z)))
|
|
|
|
(state (set) '()))
|
|
|
|
(set (set)))
|
|
|
|
(check-equal? (edge-weight sgr
|
|
|
|
(state (set) (list (set 'x) (set 'y) (set 'z) (set) (set 'z)))
|
|
|
|
(state (set 'y 'z) (list (set 'y) (set 'z) (set) (set 'z))))
|
|
|
|
(set (set 'a 'b)))
|
|
|
|
|
|
|
|
(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))
|
|
|
|
(list (set) (set))))))
|