aboutsummaryrefslogtreecommitdiff
path: root/ccwl/graphviz.scm
blob: 820aadc796276a3daeaff3f0e6e3583c7f09583d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
;;; ccwl --- Concise Common Workflow Language
;;; Copyright © 2021, 2023 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of ccwl.
;;;
;;; ccwl is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; ccwl is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with ccwl.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This file implements conversion from ccwl objects (<workflow>,
;; <command>, <input>, <output>, <step>) to the graphviz dot language.

;;; Code:

(define-module (ccwl graphviz)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9 gnu)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-28)
  #:use-module (ice-9 match)
  #:use-module (ice-9 string-fun)
  #:use-module (ccwl ccwl)
  #:use-module (ccwl utils)
  #:export (workflow->dot
            command->dot
            workflow-or-command->dot))

(define workflow-or-command->dot
  (match-lambda*
    (((? workflow? workflow) port)
     (workflow->dot workflow port))
    (((? command? command) port)
     (command->dot command port))))

(define (workflow->dot workflow port)
  "Render WORKFLOW, a <workflow> object, to PORT in the graphviz dot
language."
  (graph->dot (workflow->graph workflow)
              port))

(define-immutable-record-type <graph>
  (make-graph name properties nodes edges subgraphs)
  graph?
  (name graph-name)
  (properties graph-properties)
  (nodes graph-nodes)
  (edges graph-edges)
  (subgraphs graph-subgraphs))

(define* (graph name #:key (properties '()) (nodes '()) (edges '()) (subgraphs '()))
  "Construct <graph> object."
  (make-graph name properties nodes edges subgraphs))

(define-immutable-record-type <graph-node>
  (make-graph-node name properties)
  graph-node?
  (name graph-node-name)
  (properties graph-node-properties))

(define* (graph-node name #:optional (properties '()))
  "Construct <graph-node> object."
  (make-graph-node name properties))

(define-immutable-record-type <graph-edge>
  (make-graph-edge from to properties)
  graph-edge?
  (from graph-edge-from)
  (to graph-edge-to)
  (properties graph-edge-properties))

(define* (graph-edge from to #:optional (properties '()))
  "Construct <graph-edge> object."
  (make-graph-edge from to properties))

(define-immutable-record-type <graph-port>
  (graph-port node port)
  graph-port?
  (node graph-port-node)
  (port graph-port-name))

(define-immutable-record-type <html-string>
  (html-string str)
  html-string?
  (str html-string-underlying))

(define (workflow->graph workflow)
  "Convert WORKFLOW, a <workflow> object, to a <graph> object."
  (graph 'workflow
         #:properties '((bgcolor . "#eeeeee"))
         #:nodes (map (compose step-node step-id)
                      (workflow-steps workflow))
         #:edges (append
                  ;; Connect steps and inputs to steps.
                  (append-map (lambda (step)
                                (map (match-lambda
                                       ((_ . address)
                                        (cons (string->symbol
                                               (match (string-split address #\/)
                                                 ((step _) step)
                                                 ((workflow-input) workflow-input)))
                                              (step-id step))))
                                     (step-in step)))
                              (workflow-steps workflow))
                  ;; Connect output sources to outputs.
                  (map (lambda (output)
                         (cons (match (string-split (output-source output) #\/)
                                 ((source _) source))
                               (output-id output)))
                       (workflow-outputs workflow)))
         #:subgraphs (list (inputs-cluster (workflow-inputs workflow))
                           (outputs-cluster (workflow-outputs workflow)))))

(define (command->dot command port)
  "Render @var{command}, a @code{<command>} object, to @var{port} in the
graphviz dot language."
  (graph->dot (command->graph command)
              port))

(define (command->graph command)
  "Convert @var{command}, a @code{<command>} object, to a @code{<graph>}
object."
  (graph 'workflow
         #:properties '((bgcolor . "#eeeeee"))
         #:nodes (list (graph-node 'command
                                   '((fillcolor . "lightgoldenrodyellow")
                                     (shape . "record")
                                     (style . "filled"))))
         #:edges (append
                  ;; Connect inputs to command.
                  (map (lambda (input)
                         (cons (input-id input)
                               'command))
                       (command-inputs command))
                  ;; Connect command to outputs.
                  (map (lambda (output)
                         (cons 'command
                               (output-id output)))
                       (command-outputs command)))
         #:subgraphs (list (inputs-cluster (command-inputs command))
                           (outputs-cluster (command-outputs command)))))

(define (step-node id)
  "Return graph node describing step with @var{id}."
  (graph-node id
              '((fillcolor . "lightgoldenrodyellow")
                (shape . "record")
                (style . "filled"))))

(define (inputs-cluster inputs)
  "Return the subgraph clustering @var{inputs}."
  (graph 'cluster_inputs
         #:properties '((label . "Workflow Inputs")
                        (rank . "same")
                        (style . "dashed"))
         #:nodes (map (lambda (input)
                        (graph-node (input-id input)
                                    '((fillcolor . "#94ddf4")
                                      (shape . "record")
                                      (style . "filled"))))
                      inputs)))

(define (outputs-cluster outputs)
  "Return the subgraph clustering @var{outputs}."
  (graph 'cluster_outputs
         #:properties '((label . "Workflow Outputs")
                        (labelloc . "b")
                        (rank . "same")
                        (style . "dashed"))
         #:nodes (map (lambda (output)
                        (graph-node (output-id output)
                                    '((fillcolor . "#94ddf4")
                                      (shape . "record")
                                      (style . "filled"))))
                      outputs)))

(define (serialize object)
  "Serialize OBJECT according to graphviz dot syntax. OBJECT may a
symbol, a string, a <graph-port> object, or a <html-string> object."
  (cond
   ((symbol? object)
    (serialize (symbol->string object)))
   ((graph-port? object)
    (string-append (serialize (graph-port-node object))
                   ":"
                   (serialize (graph-port-name object))))
   ;; Surround HTML strings in <>, and don't escape.
   ((html-string? object)
    (format "<~a>" (html-string-underlying object)))
   ;; Don't quote safe strings.
   ((and (string? object)
         (string-every (char-set-union (char-set-intersection char-set:letter+digit
                                                              char-set:ascii)
                                       (char-set #\_))
                       object))
    object)
   ;; Quote strings with unsafe characters.
   ((string? object)
    (format "\"~a\"" (string-replace-substring object "\"" "\\\"")))
   (else (error "Unknown object type to serialize to graphviz dot:" object))))

(define (serialize-properties properties)
  (string-append
   "["
   (string-join (map (match-lambda
                       ((key . value)
                        (format "~a=~a" key (serialize value))))
                     properties)
                ", ")
   "]"))

(define* (graph->dot graph #:optional (port (current-output-port)) (level 0))
  "Render GRAPH, a <graph> object, in the graphviz dot syntax to
PORT."
  (indent-level port level)
  (display (format "~a ~a {~%"
                   (if (zero? level) "digraph" "subgraph")
                   (graph-name graph))
           port)
  (for-each (match-lambda
              ((key . value)
               (indent-level port (1+ level))
               (display (format "~a=~a;~%" key (serialize value)) port)))
            (graph-properties graph))
  (for-each (lambda (node)
              (indent-level port (1+ level))
              (display (serialize (graph-node-name node)) port)
              (unless (null? (graph-node-properties node))
                (display " " port)
                (display (serialize-properties (graph-node-properties node))
                         port))
              (display (format ";~%") port))
            (graph-nodes graph))
  (for-each (lambda (edge)
              (indent-level port (1+ level))
              (match edge
                ((? graph-edge? edge)
                 (display (format "~a -> ~a"
                                  (serialize (graph-edge-from edge))
                                  (serialize (graph-edge-to edge)))
                          port)
                 (unless (null? (graph-edge-properties edge))
                   (display " " port)
                   (display (serialize-properties (graph-edge-properties edge))
                            port))
                 (display (format ";~%") port))
                ((from . to)
                 (display (format "~a -> ~a;~%"
                                  (serialize from)
                                  (serialize to))
                          port))))
            (graph-edges graph))
  (for-each (lambda (subgraph)
              (graph->dot subgraph port (1+ level)))
            (graph-subgraphs graph))
  (indent-level port level)
  (display (format "}~%") port))