1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2007--2009 Joe Neeman <joeneeman@gmail.com>
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (scm graphviz)
22 (make-empty-graph add-node add-edge add-cluster
26 (define graph-type (make-record-type "graph" '(nodes edges clusters name)))
28 (define make-graph (record-constructor graph-type))
29 (define (make-empty-graph name) (make-graph '() '() '() name))
31 (define nodes (record-accessor graph-type 'nodes))
32 (define edges (record-accessor graph-type 'edges))
33 (define clusters (record-accessor graph-type 'clusters))
34 (define set-nodes! (record-modifier graph-type 'nodes))
35 (define set-edges! (record-modifier graph-type 'edges))
36 (define set-clusters! (record-modifier graph-type 'clusters))
38 (define (add-cluster graph node-id cluster-name)
39 (let* ((cs (clusters graph))
40 (cluster (assq cluster-name cs))
41 (already-in-cluster (if cluster
44 (set-clusters! graph (assq-set! cs
46 (cons node-id already-in-cluster)))))
48 (define (add-node graph label . cluster-name)
49 (let* ((ns (nodes graph))
51 (set-nodes! graph (assq-set! ns id label))
52 (if (and (not (null? cluster-name))
53 (string? (car cluster-name)))
54 (add-cluster graph id (car cluster-name)))
57 (define (add-edge graph node1 node2)
58 (set-edges! graph (cons `(,node1 . ,node2) (edges graph))))
60 (define (graph-write graph out)
61 (let ((ns (nodes graph))
63 (cs (clusters graph)))
64 (ly:message (format (_ "Writing graph `~a'...") (port-filename out)))
65 (display "digraph G {\nrankdir=\"LR\"\nnode [shape=rectangle]\n" out)
66 (map (lambda (n) (display (format "~a [label=\"~a\"]\n" (car n) (cdr n)) out))
68 (map (lambda (e) (display (format "~a -> ~a\n" (car e) (cdr e)) out))
71 (display (format "subgraph cluster_~a {\nlabel= \"~a\"\ncolor=blue\n"
72 (string-filter (car c) char-alphabetic?)
75 (map (lambda (n) (display (format "~a\n" n) out)) (cdr c))