diff --git a/scribblings/tbn.scrbl b/scribblings/tbn.scrbl index 3738e5a..2cef959 100644 --- a/scribblings/tbn.scrbl +++ b/scribblings/tbn.scrbl @@ -115,7 +115,7 @@ input values. (hash 'a 1 'b 0 'c 1)) ]} -@section{Reading TBFs and SBFs} +@section{Reading and printing TBFs and SBFs} @defproc[(lists+vars->tbfs/state [vars (Listof Variable)] [lsts (Listof (Listof Real))]) @@ -200,3 +200,30 @@ is taken to contain the names of the variables, similarly to @ex[ (read-org-tbfs/state+headers "((a b f) (1 2 3) (1 1 2))") ]} + +@defproc[(print-org-tbfs/state [tbfs (Listof TBF/State)]) (Listof (Listof Real))]{ + +Given a list of @racket[TBF/State], produces a sexp that Org-mode can +interpret as a table. + +All @racket[TBF/State] in the list must have the same inputs. +The function does not check this property. + +@ex[ +(print-org-tbfs/state (list (tbf/state (hash 'a 1 'b 2) 3) + (tbf/state (hash 'a -2 'b 1) 1))) +]} + +@defproc[(print-org-tbfs/state+headers [tbfs (Listof TBF/State)]) + (Pairof (Listof Variable) (Listof (Listof Real)))]{ + +Like @racket[print-org-tbfs/state], but the first list of the result +is the list of input names of the first @racket[TBF/State] in +@racket[tbfs]. The last element of this first list is @racket['θ] and +corresponds to the column giving the thresholds of the TBFs. + +@ex[ +(print-org-tbfs/state+headers + (list (tbf/state (hash 'a 1 'b 2) 3) + (tbf/state (hash 'a -2 'b 1) 1))) +]} diff --git a/tbn.rkt b/tbn.rkt index 4e95d2b..06e9236 100644 --- a/tbn.rkt +++ b/tbn.rkt @@ -28,6 +28,7 @@ lists+vars->tbfs/state lists+headers->tbfs/state lists->tbfs/state lists+vars->sbfs/state lists+headers->sbfs/state lists->sbfs/state read-org-tbfs/state read-org-tbfs/state+headers + print-org-tbfs/state print-org-tbfs/state+headers ) (: apply-tbf-to-state (-> TBF (State (U Zero One)) (U Zero One))) @@ -181,6 +182,37 @@ (check-equal? (read-org-tbfs/state+headers "((a b f) (1 2 3) (1 1 2))") (list (tbf/state '#hash((a . 1) (b . 2)) 3) (tbf/state '#hash((a . 1) (b . 1)) 2))))) + + (: print-org-tbfs/state (-> (Listof TBF/State) (Listof (Listof Real)))) + (define (print-org-tbfs/state tbfs) + (for/list ([tbf (in-list tbfs)]) + (append (hash-map (tbf/state-w tbf) (λ (_ [w : Real]) w) #t) + (list (tbf/state-θ tbf))))) + + (module+ test + (test-case "print-org-tbfs/state" + (check-equal? + (print-org-tbfs/state (list (tbf/state (hash 'a 1 'b 2) 3) + (tbf/state (hash 'a -2 'b 1) 1))) + '((1 2 3) (-2 1 1))))) + + (: print-org-tbfs/state+headers (-> (Listof TBF/State) + (Pairof (Listof Variable) + (Listof (Listof Real))))) + (define (print-org-tbfs/state+headers tbfs) + (cons (append (hash-map (tbf/state-w (car tbfs)) + (λ ([x : Symbol] _) x) #t) + '(θ)) + (print-org-tbfs/state tbfs))) + + (module+ test + (test-case "print-org-tbfs/state+headers" + (check-equal? + (print-org-tbfs/state+headers (list (tbf/state (hash 'a 1 'b 2) 3) + (tbf/state (hash 'a -2 'b 1) 1))) + '((a b θ) + (1 2 3) + (-2 1 1))))) ) (module+ test