summaryrefslogtreecommitdiff
path: root/ccwl/graphviz.scm
blob: 7093bdb2aaf8affa41fc275fb511e13616b271fe (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
;;; ccwl --- Concise Common Workflow Language
;;; Copyright © 2021 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 (ccwl ccwl)
  #:use-module (ccwl utils)
  #:export (workflow->dot))

(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 (workflow->graph workflow)
  "Convert WORKFLOW, a <workflow> object, to a <graph> object."
  (graph 'workflow
         #:properties '((bgcolor . "#eeeeee"))
         #:nodes (map (lambda (step)
                        (graph-node (step-id step)
                                    '((fillcolor . "lightgoldenrodyellow")
                                      (shape . "record")
                                      (style . "filled"))))
                      (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 (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"))))
                                               (workflow-inputs workflow)))
                           (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"))))
                                               (workflow-outputs workflow))))))

(define (escape-id id)
  "Escape string ID if necessary according to graphviz dot syntax."
  (let ((id (if (symbol? id)
                (symbol->string id)
                id)))
    (if (string-every (char-set-union (char-set-intersection char-set:letter+digit
                                                             char-set:ascii)
                                      (char-set #\_))
                      id)
        id
        (call-with-output-string
          (cut write id <>)))))

(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 (escape-id value)) port)))
            (graph-properties graph))
  (for-each (lambda (node)
              (indent-level port (1+ level))
              (display (escape-id (graph-node-name node)) port)
              (unless (null? (graph-node-properties node))
                (display (format " [~a]"
                                 (string-join (map (match-lambda
                                                     ((key . value)
                                                      (format "~a=~a" key (escape-id value))))
                                                   (graph-node-properties node))
                                              ", "))
                         port))
              (display (format ";~%") port))
            (graph-nodes graph))
  (for-each (match-lambda
              ((from . to)
               (indent-level port (1+ level))
               (display (format "~a -> ~a;~%"
                                (escape-id from)
                                (escape-id 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))