Minor updates to dot-attribute code
[cl-graph.git] / dev / graphviz / graphviz-support.lisp
index e07e661..fe614aa 100644 (file)
@@ -17,6 +17,13 @@ This file contains the stuff that does not depend on cl-graphviz.
 |#
 (in-package metabang.graph)
 
+(export '(
+         print-dot-key-value
+         dot-attribute-value
+         dot-attributes-mixin
+         *dot-graph-attributes*
+         ))
+
 ;;; ---------------------------------------------------------------------------
 ;
 ; This outputs the graph to string in accordance with the DOT file format.  
@@ -243,7 +250,7 @@ B--D []
 |#
 
 (defparameter *dot-graph-attributes*
-  '((:size coord)
+  '((:size coordinate)
     (:bb bounding-box)
     (:page text)
     (:ratio (:fill :compress :auto)) ;; Could actually be a float number too
@@ -273,10 +280,12 @@ B--D []
              :doublecircle))
     (:fontsize integer)
     (:fontname text)
+    (:fontcolor text)
     (:color text)
     (:fillcolor text)
     (:style (:filled :solid :dashed :dotted :bold :invis))
-    (:layer text)))
+    (:layer text)
+    (:url text)))
 
 (defparameter *dot-edge-attributes*
   '((:pos spline)
@@ -315,32 +324,32 @@ B--D []
   (:export-p t))
 
 (defclass* dot-graph-mixin (dot-attributes-mixin) ()
-  (:export-p t))
+  (:export-p t)
+  (:default-initargs
+    :vertex-class 'dot-vertex
+    :directed-edge-class 'dot-directed-edge
+    :undirected-edge-class 'dot-edge))
 (defclass* dot-vertex-mixin (dot-attributes-mixin) ()
   (:export-p t))
 (defclass* dot-edge-mixin (dot-attributes-mixin) ()
   (:export-p t))
 
-(defclass* dot-graph (graph-container dot-graph-mixin)
+(defclass* dot-graph (dot-graph-mixin graph-container)
   ()
-  (:default-initargs
-    :vertex-class 'dot-vertex
-    :directed-edge-class 'dot-directed-edge
-    :undirected-edge-class 'dot-edge)
   (:export-p t))
 
-(defclass* dot-vertex (graph-container-vertex dot-vertex-mixin) ()
+(defclass* dot-vertex (dot-vertex-mixin graph-container-vertex) ()
   (:export-p t))
-(defclass* dot-edge (graph-container-edge dot-edge-mixin) ()
+(defclass* dot-edge (dot-edge-mixin graph-container-edge) ()
   (:export-p t))
-(defclass* dot-directed-edge (directed-edge-mixin dot-edge) ()
+(defclass* dot-directed-edge (dot-edge directed-edge-mixin) ()
   (:export-p t))
 
 
-(defmethod (setf dot-attribute) :before (value (attr symbol) (thing dot-attributes-mixin))
+(defmethod (setf dot-attribute-value) :before (value (attr symbol) (thing dot-attributes-mixin))
   (ensure-valid-dot-attribute attr thing))
 
-(defmethod (setf dot-attribute) (value (attr symbol) (thing dot-attributes-mixin))
+(defmethod (setf dot-attribute-value) (value (attr symbol) (thing dot-attributes-mixin))
   (setf (getf (dot-attributes thing) attr) value))
 
 (defmethod dot-attribute-value ((attr symbol) (thing dot-attributes-mixin))
@@ -349,8 +358,7 @@ B--D []
 (defmethod graph->dot-properties ((graph dot-graph-mixin) (stream t))
   (loop for (name value) on (dot-attributes graph) by #'cddr
         do
-        (print-dot-key-value name value *dot-graph-attributes* stream)
-        (format stream "                 ;~%")))
+        (print-dot-key-value name value *dot-graph-attributes* stream)))
 
 (defmethod vertex->dot ((vertex dot-vertex-mixin) (stream t))
   (format-dot-attributes vertex *dot-vertex-attributes* stream))
@@ -380,7 +388,8 @@ B--D []
   (destructuring-bind (key value-type)
       (or (assoc key dot-attributes)
           (error "Invalid attribute ~S" key))
-    (format stream "~a=~a" (string-downcase key)
+    (write-name-for-dot key stream)
+    (format stream "=~a" 
             (etypecase value-type
               ((member coordinate)
                (with-output-to-string (str)
@@ -392,7 +401,7 @@ B--D []
                      (princ el str)
                      (setf first nil)))
                  (princ "\"" str)))
-              ((member spline bounding-box)
+              ((member spline)
                (with-output-to-string (str)
                  (princ "\"" str)
                  (let ((first t))
@@ -404,6 +413,18 @@ B--D []
                      (princ (second el) str)
                      (setf first nil)))
                  (princ "\"" str)))
+              ((member bounding-box)
+               (with-output-to-string (str)
+                 (princ "\"" str)
+                 (let ((first t))
+                   (dolist (el value)
+                     (unless first
+                       (princ ", " str))
+                     (princ (first el) str)
+                     (princ "," str)
+                     (princ (second el) str)
+                     (setf first nil)))
+                 (princ "\"" str)))
               ((member integer)
                (unless (typep value 'integer)
                  (error "Invalid value for ~S: ~S is not an integer"
@@ -425,6 +446,12 @@ B--D []
                    (string-downcase value)
                    value))))))
 
+(defmethod write-name-for-dot (attribute stream)
+  (format stream "~(~A~)" attribute))
+
+(defmethod write-name-for-dot ((attribute (eql :url)) stream)
+  (format stream "URL"))
+
 (defun textify (object)
   (let ((string (princ-to-string object)))
     (with-output-to-string (stream)