1 ;;;-*- Mode: Lisp; Package: metabang.graph -*-
5 $Id: graphviz-support.lisp,v 1.7 2005/06/21 20:51:51 moody Exp $
7 Copyright 1992 - 2005 Experimental Knowledge Systems Lab,
8 University of Massachusetts Amherst MA, 01003-4610
9 Professor Paul Cohen, Director
11 Author: Gary King, Levente Mészáros, Attila Lendvai
15 This file contains the stuff that does not depend on cl-graphviz.
18 (in-package metabang.graph)
22 *dot-graph-attributes*
25 ;;; ---------------------------------------------------------------------------
27 ; This outputs the graph to string in accordance with the DOT file format.
28 ; For more information about DOT file format, search the web for "DOTTY" and
31 (defmethod graph->dot ((g basic-graph) (stream stream)
33 (graph-formatter 'graph->dot-properties)
34 (vertex-key 'vertex-id)
36 (vertex-formatter 'vertex->dot)
39 (edge-formatter 'edge->dot))
40 (format stream "~A G {~%graph " (if (contains-undirected-edge-p g) "graph" "digraph"))
42 (funcall graph-formatter g stream)
51 (let ((key (if vertex-key (funcall vertex-key v) v)))
55 (princ "label=\"" stream)
56 (funcall vertex-labeler v stream)
57 (princ "\", " stream))
58 (funcall vertex-formatter v stream)
59 (princ "];" stream))))
61 (let ((directed-edge-connector (if (contains-undirected-edge-p g) "--" "->"))
62 (directed-edge-tag (when (and (contains-undirected-edge-p g)
63 (contains-directed-edge-p g))
65 (flet ((format-edge (e connector from to directed?)
67 (princ (funcall vertex-key from) stream)
68 (princ connector stream)
69 (princ (funcall vertex-key to) stream)
71 (when (and directed? directed-edge-tag)
72 (princ directed-edge-tag stream))
74 (princ "label=\"" stream)
75 (funcall edge-labeler e stream)
77 (funcall edge-formatter e stream)
86 (when (directed-edge-p e)
87 (format-edge e directed-edge-connector
88 (source-vertex e) (target-vertex e) t))))))
91 (let ((edges (make-container 'simple-associative-container)))
98 (when (and (undirected-edge-p e)
99 (not (item-at-1 edges e)))
100 (setf (item-at-1 edges e) t)
101 (format-edge e "--" (vertex-1 e) (vertex-2 e) nil)))))))))
110 (let ((g (make-container 'graph-container :default-edge-type :undirected)))
111 (loop for (a b) in '((a b) (b c) (b d) (d e) (e f) (d f)) do
112 (add-edge-between-vertexes g a b))
132 (let ((g (make-container 'graph-container :default-edge-type :directed)))
133 (loop for (a b) in '((a b) (b c) (b d) (d e) (e f) (d f)) do
134 (add-edge-between-vertexes g a b))
154 (let ((g (make-container 'graph-container)))
155 (loop for (a b) in '((d e) (e f) (d f)) do
156 (add-edge-between-vertexes g a b :edge-type :directed))
157 (loop for (a b) in '((a b) (b c) (b d)) do
158 (add-edge-between-vertexes g a b :edge-type :undirected))
177 ;;; ---------------------------------------------------------------------------
179 (defmethod graph->dot ((g basic-graph) (stream (eql nil))
180 &rest args &key &allow-other-keys)
181 (declare (dynamic-extent args))
182 (with-output-to-string (out)
183 (apply #'graph->dot g out args)))
185 ;;; ---------------------------------------------------------------------------
187 (defmethod graph->dot ((g basic-graph) (stream (eql t))
188 &rest args &key &allow-other-keys)
189 (declare (dynamic-extent args))
190 (apply #'graph->dot g *standard-output* args))
192 ;;; ---------------------------------------------------------------------------
194 (defmethod graph->dot ((g basic-graph) (stream string)
195 &rest args &key &allow-other-keys)
196 (declare (dynamic-extent args))
197 (with-open-file (out stream :direction :output :if-exists :supersede)
198 (apply #'graph->dot g out args)))
200 ;;; ---------------------------------------------------------------------------
202 (defmethod graph->dot ((g basic-graph) (stream pathname)
203 &rest args &key &allow-other-keys)
204 (declare (dynamic-extent args))
205 (apply #'graph->dot g (namestring stream) args))
207 ;;; ---------------------------------------------------------------------------
209 (defmethod graph->dot-properties ((g t) (stream t))
212 ;;; ---------------------------------------------------------------------------
214 (defmethod vertex->dot ((v basic-vertex) (stream stream))
217 ;;; ---------------------------------------------------------------------------
219 (defmethod edge->dot ((v basic-edge) (stream stream))
222 ;;; ---------------------------------------------------------------------------
224 ;;; ---------------------------------------------------------------------------
227 (defmethod dot->graph ((dot-stream stream)
231 ;;; ---------------------------------------------------------------------------
233 (defmethod dot->graph ((dot-stream string)
234 &rest args &key &allow-other-keys)
235 (declare (dynamic-extent args))
236 (with-open-file (out stream :direction :output :if-exists :supersede)
237 (apply #'dot->graph g out args)))
239 ;;; ---------------------------------------------------------------------------
241 (defmethod dot->graph ((dot-stream pathname)
242 &rest args &key &allow-other-keys)
243 (declare (dynamic-extent args))
244 (with-open-file (out stream :direction :output :if-exists :supersede)
245 (apply #'dot->graph g out args))
246 (apply #'dot->graph g (namestring stream) args))
250 (defparameter *dot-graph-attributes*
254 (:ratio (:fill :compress :auto)) ;; Could actually be a float number too
259 (:rankdir ("LR" "RL" "BT"))
261 (:rank (:same :min :max))
270 (defparameter *dot-vertex-attributes*
274 (:fixed-size boolean)
276 (:shape (:record :plaintext :ellipse :circle :egg :triangle :box
277 :diamond :trapezium :parallelogram :house :hexagon :octagon
283 (:style (:filled :solid :dashed :dotted :bold :invis))
286 (defparameter *dot-edge-attributes*
294 (:style (:solid :dashed :dotted :bold :invis))
296 (:dir (:forward :back :both :none))
299 (:arrowhead (:none :normal :inv :dot :odot :invdot :invodot :tee
300 :empty :invempty :open :halfopen :diamond :odiamond
302 (:arrowtail (:none :normal :inv :dot :odot :invdot :invodot :tee
303 :empty :invempty :open :halfopen :diamond :odiamond
307 (:labelfontsize integer)
308 (:labelfontname text)
309 (:labelfontcolor text)
310 (:labeldistance integer)
311 (:port-label-distance integer)
315 (:constraint boolean)
318 (defclass* dot-attributes-mixin ()
319 ((dot-attributes nil ia))
322 (defclass* dot-graph-mixin (dot-attributes-mixin) ()
324 (defclass* dot-vertex-mixin (dot-attributes-mixin) ()
326 (defclass* dot-edge-mixin (dot-attributes-mixin) ()
329 (defclass* dot-graph (dot-graph-mixin graph-container)
332 :vertex-class 'dot-vertex
333 :directed-edge-class 'dot-directed-edge
334 :undirected-edge-class 'dot-edge)
337 (defclass* dot-vertex (dot-vertex-mixin graph-container-vertex) ()
339 (defclass* dot-edge (dot-edge-mixin graph-container-edge) ()
341 (defclass* dot-directed-edge (dot-edge directed-edge-mixin) ()
345 (defmethod (setf dot-attribute-value) :before (value (attr symbol) (thing dot-attributes-mixin))
346 (ensure-valid-dot-attribute attr thing))
348 (defmethod (setf dot-attribute-value) (value (attr symbol) (thing dot-attributes-mixin))
349 (setf (getf (dot-attributes thing) attr) value))
351 (defmethod dot-attribute-value ((attr symbol) (thing dot-attributes-mixin))
352 (getf (dot-attributes thing) attr))
354 (defmethod graph->dot-properties ((graph dot-graph-mixin) (stream t))
355 (loop for (name value) on (dot-attributes graph) by #'cddr
357 (print-dot-key-value name value *dot-graph-attributes* stream)
358 (format stream " ;~%")))
360 (defmethod vertex->dot ((vertex dot-vertex-mixin) (stream t))
361 (format-dot-attributes vertex *dot-vertex-attributes* stream))
363 (defmethod edge->dot ((edge dot-edge-mixin) (stream t))
364 (format-dot-attributes edge *dot-edge-attributes* stream))
366 (defun format-dot-attributes (object dot-attributes stream)
367 (loop for (name value) on (dot-attributes object) by #'cddr
368 for prefix = "" then ", " do
369 (write-string prefix stream)
370 (print-dot-key-value name value dot-attributes stream)))
372 (defmethod ensure-valid-dot-attribute (key (object dot-graph-mixin))
373 (or (assoc key *dot-graph-attributes*)
374 (error "Invalid dot graph attribute ~S" key)))
376 (defmethod ensure-valid-dot-attribute (key (object dot-vertex-mixin))
377 (or (assoc key *dot-vertex-attributes*)
378 (error "Invalid dot vertex attribute ~S" key)))
380 (defmethod ensure-valid-dot-attribute (key (object dot-edge-mixin))
381 (or (assoc key *dot-edge-attributes*)
382 (error "Invalid dot edge attribute ~S" key)))
384 (defun print-dot-key-value (key value dot-attributes stream)
385 (destructuring-bind (key value-type)
386 (or (assoc key dot-attributes)
387 (error "Invalid attribute ~S" key))
388 (format stream "~a=~a" (string-downcase key)
389 (etypecase value-type
391 (with-output-to-string (str)
401 (with-output-to-string (str)
407 (princ (first el) str)
409 (princ (second el) str)
412 ((member bounding-box)
413 (with-output-to-string (str)
419 (princ (first el) str)
421 (princ (second el) str)
425 (unless (typep value 'integer)
426 (error "Invalid value for ~S: ~S is not an integer"
436 (coerce value 'single-float))
438 (unless (member value value-type :test 'equal)
439 (error "Invalid value for ~S: ~S is not one of ~S"
440 key value value-type))
442 (string-downcase value)
445 (defun textify (object)
446 (let ((string (princ-to-string object)))
447 (with-output-to-string (stream)
448 (write-char #\" stream)
449 (loop for c across string do
450 ;; Note: #\\ should not be escaped to allow \n, \l, \N, etc.
454 (write-char #\\ stream)
455 (write-char c stream))
457 (write-char #\\ stream)
458 (write-char #\n stream))
460 (write-char c stream))))
461 (write-char #\" stream))))
463 ;;; ---------------------------------------------------------------------------
465 ; Calls the dot executable to create external output for graphs
467 #+(or win32 mswindows)
468 (defvar *dot-path* "\"C:/Program Files/ATT/Graphviz/bin/dot.exe\"")
470 (defvar *dot-path* "/usr/bin/dot" "Path to `dot`")
472 (defmethod graph->dot-external ((g basic-graph) file-name &key (type :ps))
473 "Generate an external represenation of a graph to a file, by running
474 the program in *dot-path*."
475 (let ((dot-string (graph->dot g nil))
476 (dot-type (concatenate 'string "-T" (string-downcase (symbol-name type)))))
477 #+lispworks (with-open-stream
478 (s (sys:open-pipe (concatenate 'string *dot-path* " -Tpng -o" file-name)
480 (write-line dot-string s)
484 (sb-ext:run-program *dot-path*
485 (list dot-type "-o" file-name)
486 :input (make-string-input-stream dot-string)
487 :output *standard-output*)
488 #-(or sbcl lispworks)
489 (error "Don't know how to execute a program on this platform")))
491 ;;; ---------------------------------------------------------------------------
495 (defun test-dot-external ()
496 (let* ((g (make-graph 'dot-graph))
497 (v1 (add-vertex g 'a :dot-attributes '(:shape :box
499 (v2 (add-vertex g 'b :dot-attributes '(:shape :circle
502 (add-edge-between-vertexes g v1 v2
503 :dot-attributes '(:arrowhead :open
506 (print (graph->dot g nil))
507 (graph->dot-external g "/tmp/test.gif" :type :gif)))