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)
20 ;;; ---------------------------------------------------------------------------
22 ; This outputs the graph to string in accordance with the DOT file format.
23 ; For more information about DOT file format, search the web for "DOTTY" and
26 (defmethod graph->dot ((g basic-graph) (stream stream)
28 (graph-formatter 'graph->dot-properties)
29 (vertex-key 'vertex-id)
31 (vertex-formatter 'vertex->dot)
34 (edge-formatter 'edge->dot))
35 (format stream "~A G {~%graph " (if (contains-undirected-edge-p g) "graph" "digraph"))
37 (funcall graph-formatter g stream)
46 (let ((key (if vertex-key (funcall vertex-key v) v)))
50 (princ "label=\"" stream)
51 (funcall vertex-labeler v stream)
52 (princ "\", " stream))
53 (funcall vertex-formatter v stream)
54 (princ "];" stream))))
56 (let ((directed-edge-connector (if (contains-undirected-edge-p g) "--" "->"))
57 (directed-edge-tag (when (and (contains-undirected-edge-p g)
58 (contains-directed-edge-p g))
60 (flet ((format-edge (e connector from to directed?)
62 (princ (funcall vertex-key from) stream)
63 (princ connector stream)
64 (princ (funcall vertex-key to) stream)
66 (when (and directed? directed-edge-tag)
67 (princ directed-edge-tag stream))
69 (princ "label=\"" stream)
70 (funcall edge-labeler e stream)
72 (funcall edge-formatter e stream)
81 (when (directed-edge-p e)
82 (format-edge e directed-edge-connector
83 (source-vertex e) (target-vertex e) t))))))
86 (let ((edges (make-container 'simple-associative-container)))
94 ;(spy e (undirected-edge-p e) (item-at-1 edges e))
95 (when (and (undirected-edge-p e)
96 (not (item-at-1 edges e)))
97 (setf (item-at-1 edges e) t)
98 (format-edge e "--" (vertex-1 e) (vertex-2 e) nil)))))))))
107 (let ((g (make-container 'graph-container :default-edge-type :undirected)))
108 (loop for (a b) in '((a b) (b c) (b d) (d e) (e f) (d f)) do
109 (add-edge-between-vertexes g a b))
129 (let ((g (make-container 'graph-container :default-edge-type :directed)))
130 (loop for (a b) in '((a b) (b c) (b d) (d e) (e f) (d f)) do
131 (add-edge-between-vertexes g a b))
151 (let ((g (make-container 'graph-container)))
152 (loop for (a b) in '((d e) (e f) (d f)) do
153 (add-edge-between-vertexes g a b :edge-type :directed))
154 (loop for (a b) in '((a b) (b c) (b d)) do
155 (add-edge-between-vertexes g a b :edge-type :undirected))
174 ;;; ---------------------------------------------------------------------------
176 (defmethod graph->dot ((g basic-graph) (stream (eql nil))
177 &rest args &key &allow-other-keys)
178 (declare (dynamic-extent args))
179 (with-output-to-string (out)
180 (apply #'graph->dot g out args)))
182 ;;; ---------------------------------------------------------------------------
184 (defmethod graph->dot ((g basic-graph) (stream (eql t))
185 &rest args &key &allow-other-keys)
186 (declare (dynamic-extent args))
187 (apply #'graph->dot g *standard-output* args))
189 ;;; ---------------------------------------------------------------------------
191 (defmethod graph->dot ((g basic-graph) (stream string)
192 &rest args &key &allow-other-keys)
193 (declare (dynamic-extent args))
194 (with-open-file (out stream :direction :output :if-exists :supersede)
195 (apply #'graph->dot g out args)))
197 ;;; ---------------------------------------------------------------------------
199 (defmethod graph->dot ((g basic-graph) (stream pathname)
200 &rest args &key &allow-other-keys)
201 (declare (dynamic-extent args))
202 (apply #'graph->dot g (namestring stream) args))
204 ;;; ---------------------------------------------------------------------------
206 (defmethod graph->dot-properties ((g t) (stream t))
209 ;;; ---------------------------------------------------------------------------
211 (defmethod vertex->dot ((v basic-vertex) (stream stream))
214 ;;; ---------------------------------------------------------------------------
216 (defmethod edge->dot ((v basic-edge) (stream stream))
219 ;;; ---------------------------------------------------------------------------
221 ;;; ---------------------------------------------------------------------------
224 (defmethod dot->graph ((dot-stream stream)
228 ;;; ---------------------------------------------------------------------------
230 (defmethod dot->graph ((dot-stream string)
231 &rest args &key &allow-other-keys)
232 (declare (dynamic-extent args))
233 (with-open-file (out stream :direction :output :if-exists :supersede)
234 (apply #'dot->graph g out args)))
236 ;;; ---------------------------------------------------------------------------
238 (defmethod dot->graph ((dot-stream pathname)
239 &rest args &key &allow-other-keys)
240 (declare (dynamic-extent args))
241 (with-open-file (out stream :direction :output :if-exists :supersede)
242 (apply #'dot->graph g out args))
243 (apply #'dot->graph g (namestring stream) args))
247 (defparameter *dot-graph-attributes*
251 (:ratio (:fill :compress :auto)) ;; Could actually be a float number too
256 (:rankdir ("LR" "RL" "BT"))
258 (:rank (:same :min :max))
267 (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))
285 (defparameter *dot-edge-attributes*
293 (:style (:solid :dashed :dotted :bold :invis))
295 (:dir (:forward :back :both :none))
298 (:arrowhead (:none :normal :inv :dot :odot :invdot :invodot :tee
299 :empty :invempty :open :halfopen :diamond :odiamond
301 (:arrowtail (:none :normal :inv :dot :odot :invdot :invodot :tee
302 :empty :invempty :open :halfopen :diamond :odiamond
306 (:labelfontsize integer)
307 (:labelfontname text)
308 (:labelfontcolor text)
309 (:labeldistance integer)
310 (:port-label-distance integer)
314 (:constraint boolean)
317 (defclass* dot-attributes-mixin ()
318 ((dot-attributes nil ia))
321 (defclass* dot-graph-mixin (dot-attributes-mixin) ()
324 :vertex-class 'dot-vertex
325 :directed-edge-class 'dot-directed-edge
326 :undirected-edge-class 'dot-edge))
327 (defclass* dot-vertex-mixin (dot-attributes-mixin) ()
329 (defclass* dot-edge-mixin (dot-attributes-mixin) ()
332 (defclass* dot-graph (dot-graph-mixin graph-container)
336 (defclass* dot-vertex (dot-vertex-mixin graph-container-vertex) ()
338 (defclass* dot-edge (dot-edge-mixin graph-container-edge) ()
340 (defclass* dot-directed-edge (dot-edge directed-edge-mixin) ()
344 (defmethod (setf dot-attribute-value) :before (value (attr symbol) (thing dot-attributes-mixin))
345 (declare (ignore value))
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)))
359 (defmethod vertex->dot ((vertex dot-vertex-mixin) (stream t))
360 (format-dot-attributes vertex *dot-vertex-attributes* stream))
362 (defmethod edge->dot ((edge dot-edge-mixin) (stream t))
363 (format-dot-attributes edge *dot-edge-attributes* stream))
365 (defun format-dot-attributes (object dot-attributes stream)
366 (loop for (name value) on (dot-attributes object) by #'cddr
367 for prefix = "" then ", " do
368 (write-string prefix stream)
369 (print-dot-key-value name value dot-attributes stream)))
371 (defmethod ensure-valid-dot-attribute (key (object dot-graph-mixin))
372 (or (assoc key *dot-graph-attributes*)
373 (error "Invalid dot graph attribute ~S" key)))
375 (defmethod ensure-valid-dot-attribute (key (object dot-vertex-mixin))
376 (or (assoc key *dot-vertex-attributes*)
377 (error "Invalid dot vertex attribute ~S" key)))
379 (defmethod ensure-valid-dot-attribute (key (object dot-edge-mixin))
380 (or (assoc key *dot-edge-attributes*)
381 (error "Invalid dot edge attribute ~S" key)))
383 (defun print-dot-key-value (key value dot-attributes stream)
384 (destructuring-bind (key value-type)
385 (or (assoc key dot-attributes)
386 (error "Invalid attribute ~S" key))
387 (write-name-for-dot key stream)
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 (defmethod write-name-for-dot (attribute stream)
446 (format stream "~(~A~)" attribute))
448 (defmethod write-name-for-dot ((attribute (eql :url)) stream)
449 (format stream "URL"))
451 (defun textify (object)
452 (let ((string (princ-to-string object)))
453 (with-output-to-string (stream)
454 (write-char #\" stream)
455 (loop for c across string do
456 ;; Note: #\\ should not be escaped to allow \n, \l, \N, etc.
460 (write-char #\\ stream)
461 (write-char c stream))
463 (write-char #\\ stream)
464 (write-char #\n stream))
466 (write-char c stream))))
467 (write-char #\" stream))))
469 ;;; ---------------------------------------------------------------------------
471 ; Calls the dot executable to create external output for graphs
473 #+(or win32 mswindows)
474 (defvar *dot-path* "\"C:/Program Files/ATT/Graphviz/bin/dot.exe\"")
476 (defvar *dot-path* "/usr/bin/dot" "Path to `dot`")
478 (defmethod graph->dot-external ((g basic-graph) file-name &key (type :ps))
479 "Generate an external represenation of a graph to a file, by running
480 the program in *dot-path*."
481 (let ((dot-string (graph->dot g nil))
482 (dot-type (concatenate 'string "-T" (string-downcase (symbol-name type)))))
483 #+lispworks (with-open-stream
484 (s (sys:open-pipe (concatenate 'string *dot-path* " -Tpng -o" file-name)
486 (write-line dot-string s)
490 (sb-ext:run-program *dot-path*
491 (list dot-type "-o" file-name)
492 :input (make-string-input-stream dot-string)
493 :output *standard-output*)
494 #-(or sbcl lispworks)
495 (error "Don't know how to execute a program on this platform")))
497 ;;; ---------------------------------------------------------------------------
501 (defun test-dot-external ()
502 (let* ((g (make-graph 'dot-graph))
503 (v1 (add-vertex g 'a :dot-attributes '(:shape :box
505 (v2 (add-vertex g 'b :dot-attributes '(:shape :circle
508 (add-edge-between-vertexes g v1 v2
509 :dot-attributes '(:arrowhead :open
512 (print (graph->dot g nil))
513 (graph->dot-external g "/tmp/test.gif" :type :gif)))