Compare commits
2 Commits
632456dca7
...
9c7e8603c7
| Author | SHA1 | Date |
|---|---|---|
|
|
9c7e8603c7 | |
|
|
ec61e6e2a3 |
61
dot.rkt
61
dot.rkt
|
|
@ -1,6 +1,7 @@
|
||||||
#lang racket/base
|
#lang racket/base
|
||||||
|
|
||||||
(provide
|
(provide
|
||||||
|
dot-graph
|
||||||
dot-digraph
|
dot-digraph
|
||||||
dot-vertex
|
dot-vertex
|
||||||
dot-edge)
|
dot-edge)
|
||||||
|
|
@ -11,26 +12,35 @@
|
||||||
|
|
||||||
;; -----------------------------------------------------------------------------
|
;; -----------------------------------------------------------------------------
|
||||||
|
|
||||||
(define (dot-digraph f)
|
(define current-dot-edge-syntax (make-parameter "--"))
|
||||||
(write-string "digraph {\n")
|
|
||||||
(f)
|
(define ((dot-graph* header edge-stx) func)
|
||||||
|
(write header)
|
||||||
|
(write-string " {\n")
|
||||||
|
(parameterize ([current-dot-edge-syntax edge-stx])
|
||||||
|
(func))
|
||||||
(write-string "}\n"))
|
(write-string "}\n"))
|
||||||
|
|
||||||
|
(define dot-graph (dot-graph* 'graph "--"))
|
||||||
|
(define dot-digraph (dot-graph* 'digraph "->"))
|
||||||
|
|
||||||
|
(define (write-attrs attrs)
|
||||||
|
(for/fold ([beg "["] [end ""] #:result (write-string end))
|
||||||
|
([attr (in-list attrs)] #:when (cdr attr))
|
||||||
|
(write-string beg)
|
||||||
|
(printf "~a=~s" (car attr) (cdr attr))
|
||||||
|
(values "," "]")))
|
||||||
|
|
||||||
(define (dot-vertex id #:label [label #f] #:shape [shape #f])
|
(define (dot-vertex id #:label [label #f] #:shape [shape #f])
|
||||||
(let* ([params '()]
|
(write id)
|
||||||
[params (if shape (cons (cons 'shape shape) params) params)]
|
(write-attrs `([label . ,label]
|
||||||
[params (if label (cons (cons 'label label) params) params)])
|
[shape . ,shape]))
|
||||||
(write id)
|
(write-string ";\n"))
|
||||||
(for/fold ([beg "["] [end ""] #:result (write-string end))
|
|
||||||
([param (in-list params)])
|
|
||||||
(write-string beg)
|
|
||||||
(printf "~a=~s" (car param) (cdr param))
|
|
||||||
(values "," "]"))
|
|
||||||
(write-string ";\n")))
|
|
||||||
|
|
||||||
(define (dot-edge a b #:flip? [flip? #f])
|
(define (dot-edge a b #:flip? [flip? #f])
|
||||||
(printf "~a -> ~a;\n"
|
(printf "~a ~a ~a;\n"
|
||||||
(if flip? b a)
|
(if flip? b a)
|
||||||
|
(current-dot-edge-syntax)
|
||||||
(if flip? a b)))
|
(if flip? a b)))
|
||||||
|
|
||||||
(module+ test
|
(module+ test
|
||||||
|
|
@ -38,20 +48,39 @@
|
||||||
(check-equal?
|
(check-equal?
|
||||||
(with-output-to-string
|
(with-output-to-string
|
||||||
(λ ()
|
(λ ()
|
||||||
(dot-digraph
|
(dot-graph
|
||||||
(λ ()
|
(λ ()
|
||||||
(dot-vertex 'a)
|
(dot-vertex 'a)
|
||||||
(dot-vertex 'b #:label "B")
|
(dot-vertex 'b #:label "B")
|
||||||
(dot-vertex 'c #:shape 'box)
|
(dot-vertex 'c #:shape 'box)
|
||||||
(dot-vertex 'd #:label "D" #:shape 'box)))))
|
(dot-vertex 'd #:label "D" #:shape 'box)))))
|
||||||
(string-append
|
(string-append
|
||||||
"digraph {\n"
|
"graph {\n"
|
||||||
"a;\n"
|
"a;\n"
|
||||||
"b[label=\"B\"];\n"
|
"b[label=\"B\"];\n"
|
||||||
"c[shape=box];\n"
|
"c[shape=box];\n"
|
||||||
"d[label=\"D\",shape=box];\n"
|
"d[label=\"D\",shape=box];\n"
|
||||||
"}\n"))
|
"}\n"))
|
||||||
|
|
||||||
|
(check-equal?
|
||||||
|
(with-output-to-string
|
||||||
|
(λ ()
|
||||||
|
(dot-graph
|
||||||
|
(λ ()
|
||||||
|
(dot-vertex 'a)
|
||||||
|
(dot-vertex 'b)
|
||||||
|
(dot-vertex 'c)
|
||||||
|
(dot-edge 'a 'b)
|
||||||
|
(dot-edge 'b 'c)
|
||||||
|
(dot-edge 'a 'c #:flip? #t)))))
|
||||||
|
(string-append
|
||||||
|
"graph {\n"
|
||||||
|
"a;\nb;\nc;\n"
|
||||||
|
"a -- b;\n"
|
||||||
|
"b -- c;\n"
|
||||||
|
"c -- a;\n"
|
||||||
|
"}\n"))
|
||||||
|
|
||||||
(check-equal?
|
(check-equal?
|
||||||
(with-output-to-string
|
(with-output-to-string
|
||||||
(λ ()
|
(λ ()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue