From 2a71a1cd930ea9568519b7756bdfa069a9435d30 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sat, 10 Oct 2020 23:51:52 +0200 Subject: [PATCH] networks: Add read-org-tbfs/state. --- networks.rkt | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/networks.rkt b/networks.rkt index d8b1459..c465b72 100644 --- a/networks.rkt +++ b/networks.rkt @@ -86,7 +86,8 @@ [apply-tbf/state (-> tbf/state? (hash/c variable? (or/c 0 1)) (or/c 0 1))] [lists->tbfs/state (->* ((listof (listof (or/c number? symbol?)))) (#:headers boolean?) - (listof tbf/state?))]) + (listof tbf/state?))] + [read-org-tbfs/state (->* (string?) (#:headers boolean?) (listof tbf/state?))]) ;; Predicates (contract-out [variable? (-> any/c boolean?)] [state? (-> any/c boolean?)] @@ -1028,5 +1029,31 @@ (tbf/state '#hash((a . 1) (b . 2)) 3) (tbf/state '#hash((a . 1) (b . 1)) 2))))) +;;; Reads a list of tbf/state from an Org-mode string containing a +;;; sexp, containing a list of lists of numbers. As in +;;; lists->tbfs/state, the last element of each list is taken to be +;;; the threshold of the TBFs, and the rest of the elements are taken +;;; to be the weights. +;;; +;;; If headers is #t, the names of the variables to appear as the +;;; inputs of the TBF are taken from the first list. The last element +;;; of this list is discarded. +;;; +;;; If headers is #f, the names of the variables are generated as xi, +;;; where i is the index of the variable. +(define (read-org-tbfs/state str #:headers [headers #t]) + (lists->tbfs/state (read-org-sexp str) #:headers headers)) + +(module+ test + (test-case "read-org-tbfs/state" + (check-equal? (read-org-tbfs/state "((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))) + (check-equal? (read-org-tbfs/state "((1 2 3) (1 1 2))" #:headers #f) + (list + (tbf/state '#hash((x0 . 1) (x1 . 2)) 3) + (tbf/state '#hash((x0 . 1) (x1 . 1)) 2))))) + ;;; A TBN is a network form mapping TBFs to variables. (define tbn? (hash/c variable? tbf?))