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)
24 *dot-graph-attributes*
27 ;;; ---------------------------------------------------------------------------
29 ; This outputs the graph to string in accordance with the DOT file format.
30 ; For more information about DOT file format, search the web for "DOTTY" and
33 (defmethod graph->dot ((g basic-graph) (stream stream)
35 (graph-formatter 'graph->dot-properties)
36 (vertex-key 'vertex-id)
38 (vertex-formatter 'vertex->dot)
41 (edge-formatter 'edge->dot))
42 (format stream "~A G {~%graph " (if (contains-undirected-edge-p g) "graph" "digraph"))
44 (funcall graph-formatter g stream)
53 (let ((key (if vertex-key (funcall vertex-key v) v)))
57 (princ "label=\"" stream)
58 (funcall vertex-labeler v stream)
59 (princ "\", " stream))
60 (funcall vertex-formatter v stream)
61 (princ "];" stream))))
63 (let ((directed-edge-connector (if (contains-undirected-edge-p g) "--" "->"))
64 (directed-edge-tag (when (and (contains-undirected-edge-p g)
65 (contains-directed-edge-p g))
67 (flet ((format-edge (e connector from to directed?)
69 (princ (funcall vertex-key from) stream)
70 (princ connector stream)
71 (princ (funcall vertex-key to) stream)
73 (when (and directed? directed-edge-tag)
74 (princ directed-edge-tag stream))
76 (princ "label=\"" stream)
77 (funcall edge-labeler e stream)
79 (funcall edge-formatter e stream)
88 (when (directed-edge-p e)
89 (format-edge e directed-edge-connector
90 (source-vertex e) (target-vertex e) t))))))
93 (let ((edges (make-container 'simple-associative-container)))
100 (when (and (undirected-edge-p e)
101 (not (item-at-1 edges e)))
102 (setf (item-at-1 edges e) t)
103 (format-edge e "--" (vertex-1 e) (vertex-2 e) nil)))))))))
112 (let ((g (make-container 'graph-container :default-edge-type :undirected)))
113 (loop for (a b) in '((a b) (b c) (b d) (d e) (e f) (d f)) do
114 (add-edge-between-vertexes g a b))
134 (let ((g (make-container 'graph-container :default-edge-type :directed)))
135 (loop for (a b) in '((a b) (b c) (b d) (d e) (e f) (d f)) do
136 (add-edge-between-vertexes g a b))
156 (let ((g (make-container 'graph-container)))
157 (loop for (a b) in '((d e) (e f) (d f)) do
158 (add-edge-between-vertexes g a b :edge-type :directed))
159 (loop for (a b) in '((a b) (b c) (b d)) do
160 (add-edge-between-vertexes g a b :edge-type :undirected))
179 ;;; ---------------------------------------------------------------------------
181 (defmethod graph->dot ((g basic-graph) (stream (eql nil))
182 &rest args &key &allow-other-keys)
183 (declare (dynamic-extent args))
184 (with-output-to-string (out)
185 (apply #'graph->dot g out args)))
187 ;;; ---------------------------------------------------------------------------
189 (defmethod graph->dot ((g basic-graph) (stream (eql t))
190 &rest args &key &allow-other-keys)
191 (declare (dynamic-extent args))
192 (apply #'graph->dot g *standard-output* args))
194 ;;; ---------------------------------------------------------------------------
196 (defmethod graph->dot ((g basic-graph) (stream string)
197 &rest args &key &allow-other-keys)
198 (declare (dynamic-extent args))
199 (with-open-file (out stream :direction :output :if-exists :supersede)
200 (apply #'graph->dot g out args)))
202 ;;; ---------------------------------------------------------------------------
204 (defmethod graph->dot ((g basic-graph) (stream pathname)
205 &rest args &key &allow-other-keys)
206 (declare (dynamic-extent args))
207 (apply #'graph->dot g (namestring stream) args))
209 ;;; ---------------------------------------------------------------------------
211 (defmethod graph->dot-properties ((g t) (stream t))
214 ;;; ---------------------------------------------------------------------------
216 (defmethod vertex->dot ((v basic-vertex) (stream stream))
219 ;;; ---------------------------------------------------------------------------
221 (defmethod edge->dot ((v basic-edge) (stream stream))
224 ;;; ---------------------------------------------------------------------------
226 ;;; ---------------------------------------------------------------------------
229 (defmethod dot->graph ((dot-stream stream)
233 ;;; ---------------------------------------------------------------------------
235 (defmethod dot->graph ((dot-stream string)
236 &rest args &key &allow-other-keys)
237 (declare (dynamic-extent args))
238 (with-open-file (out stream :direction :output :if-exists :supersede)
239 (apply #'dot->graph g out args)))
241 ;;; ---------------------------------------------------------------------------
243 (defmethod dot->graph ((dot-stream pathname)
244 &rest args &key &allow-other-keys)
245 (declare (dynamic-extent args))
246 (with-open-file (out stream :direction :output :if-exists :supersede)
247 (apply #'dot->graph g out args))
248 (apply #'dot->graph g (namestring stream) args))
252 (defparameter *dot-graph-attributes*
256 (:ratio (:fill :compress :auto)) ;; Could actually be a float number too
261 (:rankdir ("LR" "RL" "BT"))
263 (:rank (:same :min :max))
272 (defparameter *dot-vertex-attributes*
276 (:fixed-size boolean)
278 (:shape (:record :plaintext :ellipse :circle :egg :triangle :box
279 :diamond :trapezium :parallelogram :house :hexagon :octagon
286 (:style (:filled :solid :dashed :dotted :bold :invis))
290 (defparameter *dot-edge-attributes*
298 (:style (:solid :dashed :dotted :bold :invis))
300 (:dir (:forward :back :both :none))
303 (:arrowhead (:none :normal :inv :dot :odot :invdot :invodot :tee
304 :empty :invempty :open :halfopen :diamond :odiamond
306 (:arrowtail (:none :normal :inv :dot :odot :invdot :invodot :tee
307 :empty :invempty :open :halfopen :diamond :odiamond
311 (:labelfontsize integer)
312 (:labelfontname text)
313 (:labelfontcolor text)
314 (:labeldistance integer)
315 (:port-label-distance integer)
319 (:constraint boolean)
322 (defclass* dot-attributes-mixin ()
323 ((dot-attributes nil ia))
326 (defclass* dot-graph-mixin (dot-attributes-mixin) ()
329 :vertex-class 'dot-vertex
330 :directed-edge-class 'dot-directed-edge
331 :undirected-edge-class 'dot-edge))
332 (defclass* dot-vertex-mixin (dot-attributes-mixin) ()
334 (defclass* dot-edge-mixin (dot-attributes-mixin) ()
337 (defclass* dot-graph (dot-graph-mixin graph-container)
341 (defclass* dot-vertex (dot-vertex-mixin graph-container-vertex) ()
343 (defclass* dot-edge (dot-edge-mixin graph-container-edge) ()
345 (defclass* dot-directed-edge (dot-edge directed-edge-mixin) ()
349 (defmethod (setf dot-attribute-value) :before (value (attr symbol) (thing dot-attributes-mixin))
350 (ensure-valid-dot-attribute attr thing))
352 (defmethod (setf dot-attribute-value) (value (attr symbol) (thing dot-attributes-mixin))
353 (setf (getf (dot-attributes thing) attr) value))
355 (defmethod dot-attribute-value ((attr symbol) (thing dot-attributes-mixin))
356 (getf (dot-attributes thing) attr))
358 (defmethod graph->dot-properties ((graph dot-graph-mixin) (stream t))
359 (loop for (name value) on (dot-attributes graph) by #'cddr
361 (print-dot-key-value name value *dot-graph-attributes* stream)))
363 (defmethod vertex->dot ((vertex dot-vertex-mixin) (stream t))
364 (format-dot-attributes vertex *dot-vertex-attributes* stream))
366 (defmethod edge->dot ((edge dot-edge-mixin) (stream t))
367 (format-dot-attributes edge *dot-edge-attributes* stream))
369 (defun format-dot-attributes (object dot-attributes stream)
370 (loop for (name value) on (dot-attributes object) by #'cddr
371 for prefix = "" then ", " do
372 (write-string prefix stream)
373 (print-dot-key-value name value dot-attributes stream)))
375 (defmethod ensure-valid-dot-attribute (key (object dot-graph-mixin))
376 (or (assoc key *dot-graph-attributes*)
377 (error "Invalid dot graph attribute ~S" key)))
379 (defmethod ensure-valid-dot-attribute (key (object dot-vertex-mixin))
380 (or (assoc key *dot-vertex-attributes*)
381 (error "Invalid dot vertex attribute ~S" key)))
383 (defmethod ensure-valid-dot-attribute (key (object dot-edge-mixin))
384 (or (assoc key *dot-edge-attributes*)
385 (error "Invalid dot edge attribute ~S" key)))
387 (defun print-dot-key-value (key value dot-attributes stream)
388 (destructuring-bind (key value-type)
389 (or (assoc key dot-attributes)
390 (error "Invalid attribute ~S" key))
391 (write-name-for-dot key stream)
393 (etypecase value-type
395 (with-output-to-string (str)
405 (with-output-to-string (str)
411 (princ (first el) str)
413 (princ (second el) str)
416 ((member bounding-box)
417 (with-output-to-string (str)
423 (princ (first el) str)
425 (princ (second el) str)
429 (unless (typep value 'integer)
430 (error "Invalid value for ~S: ~S is not an integer"
440 (coerce value 'single-float))
442 (unless (member value value-type :test 'equal)
443 (error "Invalid value for ~S: ~S is not one of ~S"
444 key value value-type))
446 (string-downcase value)
449 (defmethod write-name-for-dot (attribute stream)
450 (format stream "~(~A~)" attribute))
452 (defmethod write-name-for-dot ((attribute (eql :url)) stream)
453 (format stream "URL"))
455 (defun textify (object)
456 (let ((string (princ-to-string object)))
457 (with-output-to-string (stream)
458 (write-char #\" stream)
459 (loop for c across string do
460 ;; Note: #\\ should not be escaped to allow \n, \l, \N, etc.
464 (write-char #\\ stream)
465 (write-char c stream))
467 (write-char #\\ stream)
468 (write-char #\n stream))
470 (write-char c stream))))
471 (write-char #\" stream))))
473 ;;; ---------------------------------------------------------------------------
475 ; Calls the dot executable to create external output for graphs
477 #+(or win32 mswindows)
478 (defvar *dot-path* "\"C:/Program Files/ATT/Graphviz/bin/dot.exe\"")
480 (defvar *dot-path* "/usr/bin/dot" "Path to `dot`")
482 (defmethod graph->dot-external ((g basic-graph) file-name &key (type :ps))
483 "Generate an external represenation of a graph to a file, by running
484 the program in *dot-path*."
485 (let ((dot-string (graph->dot g nil))
486 (dot-type (concatenate 'string "-T" (string-downcase (symbol-name type)))))
487 #+lispworks (with-open-stream
488 (s (sys:open-pipe (concatenate 'string *dot-path* " -Tpng -o" file-name)
490 (write-line dot-string s)
494 (sb-ext:run-program *dot-path*
495 (list dot-type "-o" file-name)
496 :input (make-string-input-stream dot-string)
497 :output *standard-output*)
498 #-(or sbcl lispworks)
499 (error "Don't know how to execute a program on this platform")))
501 ;;; ---------------------------------------------------------------------------
505 (defun test-dot-external ()
506 (let* ((g (make-graph 'dot-graph))
507 (v1 (add-vertex g 'a :dot-attributes '(:shape :box
509 (v2 (add-vertex g 'b :dot-attributes '(:shape :circle
512 (add-edge-between-vertexes g v1 v2
513 :dot-attributes '(:arrowhead :open
516 (print (graph->dot g nil))
517 (graph->dot-external g "/tmp/test.gif" :type :gif)))