1 ;;;-*- Mode: Lisp; Package: metabang.graph -*-
5 $Id: graphviz-support.lisp,v 1.7 2005/06/21 20:51:51 moody Exp $
7 Author: Gary King, Levente Mészáros, Attila Lendvai
11 This file contains the stuff that does not depend on cl-graphviz.
14 (in-package #:metabang.graph)
16 ;;; ---------------------------------------------------------------------------
18 ; This outputs the graph to string in accordance with the DOT file format.
19 ; For more information about DOT file format, search the web for "DOTTY" and
22 (defmethod graph->dot ((g basic-graph) (stream stream)
24 (graph-formatter 'graph->dot-properties)
25 (vertex-key 'vertex-id)
27 (vertex-formatter 'vertex->dot)
29 (edge-formatter 'edge->dot)
31 (format stream "~A G {~%graph " (if (contains-undirected-edge-p g) "graph" "digraph"))
33 (funcall graph-formatter g stream)
42 (let ((key (if vertex-key (funcall vertex-key v) v)))
46 (princ "label=\"" stream)
47 (funcall vertex-labeler v stream)
48 (princ "\", " stream))
49 (funcall vertex-formatter v stream)
50 (princ "];" stream))))
52 (let ((directed-edge-connector (if (contains-undirected-edge-p g) "--" "->"))
53 (directed-edge-tag (when (and (contains-undirected-edge-p g)
54 (contains-directed-edge-p g))
56 (flet ((format-edge (e connector from to directed?)
58 (princ (funcall vertex-key from) stream)
59 (princ connector stream)
60 (princ (funcall vertex-key to) stream)
62 (when (and directed? directed-edge-tag)
63 (princ directed-edge-tag stream))
65 (princ "label=\"" stream)
66 (funcall edge-labeler e stream)
68 (funcall edge-formatter e stream)
77 (when (directed-edge-p e)
78 (format-edge e directed-edge-connector
79 (source-vertex e) (target-vertex e) t))))))
82 (let ((edges (make-container 'simple-associative-container)))
90 ;(spy e (undirected-edge-p e) (item-at-1 edges e))
91 (when (and (undirected-edge-p e)
92 (not (item-at-1 edges e)))
93 (setf (item-at-1 edges e) t)
94 (format-edge e "--" (vertex-1 e) (vertex-2 e) nil)))))))))
103 (let ((g (make-container 'graph-container :default-edge-type :undirected)))
104 (loop for (a b) in '((a b) (b c) (b d) (d e) (e f) (d f)) do
105 (add-edge-between-vertexes g a b))
125 (let ((g (make-container 'graph-container :default-edge-type :directed)))
126 (loop for (a b) in '((a b) (b c) (b d) (d e) (e f) (d f)) do
127 (add-edge-between-vertexes g a b))
147 (let ((g (make-container 'graph-container)))
148 (loop for (a b) in '((d e) (e f) (d f)) do
149 (add-edge-between-vertexes g a b :edge-type :directed))
150 (loop for (a b) in '((a b) (b c) (b d)) do
151 (add-edge-between-vertexes g a b :edge-type :undirected))
170 ;;; ---------------------------------------------------------------------------
172 (defmethod graph->dot ((g basic-graph) (stream (eql nil))
173 &rest args &key &allow-other-keys)
174 (declare (dynamic-extent args))
175 (with-output-to-string (out)
176 (apply #'graph->dot g out args)))
178 ;;; ---------------------------------------------------------------------------
180 (defmethod graph->dot ((g basic-graph) (stream (eql t))
181 &rest args &key &allow-other-keys)
182 (declare (dynamic-extent args))
183 (apply #'graph->dot g *standard-output* args))
185 ;;; ---------------------------------------------------------------------------
187 (defmethod graph->dot ((g basic-graph) (stream string)
188 &rest args &key &allow-other-keys)
189 (declare (dynamic-extent args))
190 (with-open-file (out stream :direction :output :if-exists :supersede)
191 (apply #'graph->dot g out args)))
193 ;;; ---------------------------------------------------------------------------
195 (defmethod graph->dot ((g basic-graph) (stream pathname)
196 &rest args &key &allow-other-keys)
197 (declare (dynamic-extent args))
198 (apply #'graph->dot g (namestring stream) args))
200 ;;; ---------------------------------------------------------------------------
202 (defmethod graph->dot-properties ((g t) (stream t))
205 ;;; ---------------------------------------------------------------------------
207 (defmethod vertex->dot ((v basic-vertex) (stream stream))
210 ;;; ---------------------------------------------------------------------------
212 (defmethod edge->dot ((v basic-edge) (stream stream))
215 ;;; ---------------------------------------------------------------------------
217 ;;; ---------------------------------------------------------------------------
220 (defmethod dot->graph ((dot-stream stream)
224 ;;; ---------------------------------------------------------------------------
226 (defmethod dot->graph ((dot-stream string)
227 &rest args &key &allow-other-keys)
228 (declare (dynamic-extent args))
229 (with-open-file (out stream :direction :output :if-exists :supersede)
230 (apply #'dot->graph g out args)))
232 ;;; ---------------------------------------------------------------------------
234 (defmethod dot->graph ((dot-stream pathname)
235 &rest args &key &allow-other-keys)
236 (declare (dynamic-extent args))
237 (with-open-file (out stream :direction :output :if-exists :supersede)
238 (apply #'dot->graph g out args))
239 (apply #'dot->graph g (namestring stream) args))
243 (defparameter *dot-graph-attributes*
248 (:ratio (:fill :compress :auto)) ;; Could actually be a float number too
254 (:rankdir ("LR" "RL" "BT"))
256 (:rank (:same :min :max))
266 (defparameter *dot-vertex-attributes*
271 (:fixed-size boolean)
273 (:shape (:record :plaintext :ellipse :circle :egg :triangle :box
274 :diamond :trapezium :parallelogram :house :hexagon :octagon
281 (:style (:filled :solid :dashed :dotted :bold :invis))
284 (:peripheries integer)))
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) ()
325 :vertex-class 'dot-vertex
326 :directed-edge-class 'dot-directed-edge
327 :undirected-edge-class 'dot-edge))
328 (defclass* dot-vertex-mixin (dot-attributes-mixin) ()
330 (defclass* dot-edge-mixin (dot-attributes-mixin) ()
333 (defclass* dot-graph (dot-graph-mixin graph-container)
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 (declare (ignore value))
347 (ensure-valid-dot-attribute attr thing))
349 (defmethod (setf dot-attribute-value) (value (attr symbol) (thing dot-attributes-mixin))
350 (setf (getf (dot-attributes thing) attr) value))
352 (defmethod dot-attribute-value ((attr symbol) (thing dot-attributes-mixin))
353 (getf (dot-attributes thing) attr))
355 (defmacro defpixel-inch-accessors (name attr type)
356 (bind ((actual-name (form-symbol name "-IN-PIXELS")))
358 (export ',actual-name)
359 (defmethod ,actual-name ((thing ,type))
360 "Return the attribute in pixels assuming 72 dpi"
361 (awhen (dot-attribute-value ,attr thing)
363 (defmethod (setf ,actual-name) (value (thing ,type))
364 "Set the attribute in pixels assuming 72 dpi"
365 (setf (dot-attribute-value ,attr thing) (coerce (/ value 72) 'double-float))))))
367 (defpixel-inch-accessors width :width dot-vertex-mixin)
368 (defpixel-inch-accessors height :height dot-vertex-mixin)
371 (defmethod graph->dot-properties ((graph dot-graph-mixin) (stream t))
372 (loop for (name value) on (dot-attributes graph) by #'cddr
374 (print-dot-key-value name value *dot-graph-attributes* stream)))
376 (defmethod vertex->dot ((vertex dot-vertex-mixin) (stream t))
377 (format-dot-attributes vertex *dot-vertex-attributes* stream))
379 (defmethod edge->dot ((edge dot-edge-mixin) (stream t))
380 (format-dot-attributes edge *dot-edge-attributes* stream))
382 (defun format-dot-attributes (object dot-attributes stream)
383 (loop for (name value) on (dot-attributes object) by #'cddr
384 for prefix = "" then ", " do
385 (write-string prefix stream)
386 (print-dot-key-value name value dot-attributes stream)))
388 (defmethod ensure-valid-dot-attribute (key (object dot-graph-mixin))
389 (or (assoc key *dot-graph-attributes*)
390 (error "Invalid dot graph attribute ~S" key)))
392 (defmethod ensure-valid-dot-attribute (key (object dot-vertex-mixin))
393 (or (assoc key *dot-vertex-attributes*)
394 (error "Invalid dot vertex attribute ~S" key)))
396 (defmethod ensure-valid-dot-attribute (key (object dot-edge-mixin))
397 (or (assoc key *dot-edge-attributes*)
398 (error "Invalid dot edge attribute ~S" key)))
400 (defun print-dot-key-value (key value dot-attributes stream)
401 (destructuring-bind (key value-type)
402 (or (assoc key dot-attributes)
403 (error "Invalid attribute ~S" key))
404 (write-name-for-dot key stream)
406 (etypecase value-type
408 (with-output-to-string (str)
418 (with-output-to-string (str)
424 (princ (first el) str)
426 (princ (second el) str)
429 ((member bounding-box)
430 (with-output-to-string (str)
436 (princ (first el) str)
438 (princ (second el) str)
442 (unless (typep value 'integer)
443 (error "Invalid value for ~S: ~S is not an integer"
453 ;; graphviz does not support the 1.2e-3 format
454 (with-output-to-string (str)
455 (format str "~,f" (coerce value 'single-float))))
457 (unless (member value value-type :test 'equal)
458 (error "Invalid value for ~S: ~S is not one of ~S"
459 key value value-type))
461 (string-downcase value)
464 (defmethod write-name-for-dot (attribute stream)
465 (format stream "~(~A~)" attribute))
467 (defmethod write-name-for-dot ((attribute (eql :url)) stream)
468 (format stream "URL"))
470 (defun textify (object)
471 (let ((string (princ-to-string object)))
472 (with-output-to-string (stream)
473 (write-char #\" stream)
474 (loop for c across string do
475 ;; Note: #\\ should not be escaped to allow \n, \l, \N, etc.
479 (write-char #\\ stream)
480 (write-char c stream))
482 (write-char #\\ stream)
483 (write-char #\n stream))
485 (write-char c stream))))
486 (write-char #\" stream))))
488 ;;; ---------------------------------------------------------------------------
490 ; Calls the dot executable to create external output for graphs
492 #+(or win32 mswindows)
493 (defvar *dot-path* "\"C:/Program Files/ATT/Graphviz/bin/dot.exe\"")
495 (defvar *dot-path* "/usr/bin/dot" "Path to `dot`")
497 (defmethod graph->dot-external ((g basic-graph) file-name &key (type :ps))
498 "Generate an external represenation of a graph to a file, by running
499 the program in *dot-path*."
500 (let ((dot-string (graph->dot g nil))
501 (dot-type (concatenate 'string "-T" (string-downcase (symbol-name type)))))
502 #+lispworks (with-open-stream
503 (s (sys:open-pipe (concatenate 'string *dot-path* " -Tpng -o" file-name)
505 (write-line dot-string s)
509 (sb-ext:run-program *dot-path*
510 (list dot-type "-o" file-name)
511 :input (make-string-input-stream dot-string)
512 :output *standard-output*)
513 #-(or sbcl lispworks)
514 (error "Don't know how to execute a program on this platform")))
516 ;;; ---------------------------------------------------------------------------
520 (defun test-dot-external ()
521 (let* ((g (make-graph 'dot-graph))
522 (v1 (add-vertex g 'a :dot-attributes '(:shape :box
524 (v2 (add-vertex g 'b :dot-attributes '(:shape :circle
527 (add-edge-between-vertexes g v1 v2
528 :dot-attributes '(:arrowhead :open
531 (print (graph->dot g nil))
532 (graph->dot-external g "/tmp/test.gif" :type :gif)))