From 5a2307ed58a3ccee6bfafb1f2361b8bdc8b0919d Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 24 Aug 2023 13:53:42 +0200 Subject: [PATCH] Type pretty-print-state-graph. --- rs.rkt | 18 +++++++++++++++++- scribblings/rs.scrbl | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/rs.rkt b/rs.rkt index 1fde21d..543f2f9 100644 --- a/rs.rkt +++ b/rs.rkt @@ -13,7 +13,7 @@ (struct-out state) State dynamics% Dynamics% build-interactive-process-graph build-interactive-process-graph/simple-states pretty-print-state-graph/simple-states build-interactive-process - build-interactive-process/org pretty-print-state + build-interactive-process/org pretty-print-state pretty-print-state-graph ) (module+ test @@ -338,6 +338,22 @@ (check-equal? (pretty-print-state (state (set 'x 'y) (list (set 'z) (set) (set 'x)))) "C:{z}{}{x}\nD:{x y}"))) + + (: pretty-print-state-graph (-> Graph Graph)) + (define (pretty-print-state-graph sgr) + (update-graph + sgr + #:v-func (λ (st) (pretty-print-state (assert-type st State))) + #:e-func (λ (e) (pretty-print-set (assert-type e (Listof ReactionName)))))) + + (module+ test + (test-case "pretty-print-state-graph" + (define rs (hash 'a (make-reaction '(x) '(y) '(z)) + 'b (make-reaction '(x y) '() '(x)))) + (define ctx : (Listof (Setof Species)) (list (set) (set) (set 'x))) + (check-equal? (graphviz (build-interactive-process-graph rs ctx)) + "digraph G {\n\tnode0 [label=\"(state (set) '(#))\"];\n\tnode1 [label=\"(state (set 'z) '())\"];\n\tnode2 [label=\"(state (set) '(# #))\"];\n\tnode3 [label=\"(state (set) '(# # #))\"];\n\tsubgraph U {\n\t\tedge [dir=none];\n\t}\n\tsubgraph D {\n\t\tnode0 -> node1 [label=\"'(a)\"];\n\t\tnode2 -> node0 [label=\"'()\"];\n\t\tnode3 -> node2 [label=\"'()\"];\n\t}\n}\n") + )) ) (require graph "utils.rkt" "generic.rkt") diff --git a/scribblings/rs.scrbl b/scribblings/rs.scrbl index dccbfef..1721cbb 100644 --- a/scribblings/rs.scrbl +++ b/scribblings/rs.scrbl @@ -356,3 +356,18 @@ Pretty prints the context sequence and the current result of @ex[ (pretty-print-state (state (set 'x 'y) (list (set 'z) (set) (set 'x)))) ]} + +@defproc[(pretty-print-state-graph [sgr Graph]) Graph]{ + +Pretty prints the state graph of a 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. + +@ex[ +(let ([rs (hash 'a (make-reaction '(x) '(y) '(z)) + 'b (make-reaction '(x y) '() '(x)))] + [ctx : (Listof (Setof Species)) (list (set) (set) (set 'x))]) + (dotit (pretty-print-state-graph (build-interactive-process-graph rs ctx)))) +]}