Skip to content

Commit b9413ed

Browse files
committed
first commit
0 parents  commit b9413ed

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# dot

dot.lisp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
;; Library for graphiz
2+
3+
(defparameter *max-label-length* 30)
4+
5+
(defun dot-name (exp)
6+
(substitute-if #\_ (complement #'alphanumericp) (prin1-to-string exp)))
7+
8+
(defun dot-label (exp)
9+
(if exp
10+
(let ((s (write-to-string exp :pretty nil)))
11+
(if (> (length s) *max-label-length*)
12+
(concatenate 'string (subseq s 0 (- *max-label-length* 3)) "...")
13+
s))
14+
""))
15+
16+
(defun nodes->dot (nodes)
17+
(mapc (lambda (node)
18+
(fresh-line)
19+
(princ (dot-name (car node)))
20+
(princ "[label=\"")
21+
(princ (dot-label node))
22+
(princ "\"];"))
23+
nodes))
24+
25+
(defun edges->dot (edges)
26+
(mapc (lambda (node)
27+
(mapc (lambda (edge)
28+
(fresh-line)
29+
(princ (dot-name (car node)))
30+
(princ "->")
31+
(princ (dot-name (car edge)))
32+
(princ "[label=\"")
33+
(princ (dot-label (cdr edge)))
34+
(princ "\"];"))
35+
(cdr node)))
36+
edges))
37+
38+
(defun graph->dot (nodes edges)
39+
(princ "digraph{")
40+
(nodes->dot nodes)
41+
(edges->dot edges)
42+
(princ "}"))
43+
44+
(defun dot->png (fname thunk)
45+
(with-open-file (*standard-output*
46+
fname
47+
:direction :output
48+
:if-exists :supersede)
49+
(funcall thunk))
50+
51+
(sb-ext:run-program "/usr/bin/dot" (list "-Tpng" "-O" fname)))
52+
53+
(defun graph->png (fname nodes edges)
54+
(dot->png fname
55+
(lambda ()
56+
(graph->dot nodes edges))))
57+
58+
(defun uedges->dot (edges)
59+
(maplist (lambda (lst)
60+
(mapc (lambda (edge)
61+
(unless (assoc (car edge) (cdr lst))
62+
(fresh-line)
63+
(princ (dot-name (caar lst)))
64+
(princ "--")
65+
(princ (dot-name (car edge)))
66+
(princ "[label=\"")
67+
(princ (dot-label (cdr edge)))
68+
(princ "\"];")))
69+
(cdar lst)))
70+
edges))
71+
72+
(defun ugraph->dot (nodes edges)
73+
(princ "graph{")
74+
(nodes->dot nodes)
75+
(uedges->dot edges)
76+
(princ "}"))
77+
78+
(defun ugraph->png (fname nodes edges)
79+
(dot->png fname
80+
(lambda ()
81+
(ugraph->dot nodes edges))))

0 commit comments

Comments
 (0)