|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.commonwl.viewer.services; |
| 21 | + |
| 22 | +import org.commonwl.viewer.domain.CWLElement; |
| 23 | +import org.commonwl.viewer.domain.Workflow; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.Writer; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +/** |
| 32 | + * Writes GraphViz DOT files from Workflows |
| 33 | + */ |
| 34 | +public class DotWriter { |
| 35 | + |
| 36 | + private static final String EOL = System.getProperty("line.separator"); |
| 37 | + private Writer writer; |
| 38 | + |
| 39 | + public DotWriter(Writer writer) { |
| 40 | + this.writer = writer; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Write a graph representing a workflow to the Writer |
| 45 | + * @param workflow The workflow to be graphed |
| 46 | + * @throws IOException Any errors in writing which may have occurred |
| 47 | + */ |
| 48 | + public void writeGraph(Workflow workflow) throws IOException { |
| 49 | + |
| 50 | + /** |
| 51 | + * DOT graph styling is based on the Apache |
| 52 | + * Taverna workflow management system |
| 53 | + */ |
| 54 | + // Begin graph |
| 55 | + writeLine("digraph workflow {"); |
| 56 | + |
| 57 | + // Overall graph style |
| 58 | + writeLine(" graph ["); |
| 59 | + writeLine(" bgcolor = \"#EEEEEE\""); |
| 60 | + writeLine(" color = \"black\""); |
| 61 | + writeLine(" fontsize = \"10\""); |
| 62 | + writeLine(" labeljust = \"left\""); |
| 63 | + writeLine(" clusterrank = \"local\""); |
| 64 | + writeLine(" ranksep = \"0.22\""); |
| 65 | + writeLine(" nodesep = \"0.05\""); |
| 66 | + writeLine(" ]"); |
| 67 | + |
| 68 | + // Overall node style |
| 69 | + writeLine(" node ["); |
| 70 | + writeLine(" fontname = \"Helvetica\""); |
| 71 | + writeLine(" fontsize = \"10\""); |
| 72 | + writeLine(" fontcolor = \"black\""); |
| 73 | + writeLine(" shape = \"record\""); |
| 74 | + writeLine(" height = \"0\""); |
| 75 | + writeLine(" width = \"0\""); |
| 76 | + writeLine(" color = \"black\""); |
| 77 | + writeLine(" fillcolor = \"lightgoldenrodyellow\""); |
| 78 | + writeLine(" style = \"filled\""); |
| 79 | + writeLine(" ];"); |
| 80 | + |
| 81 | + // Overall edge style |
| 82 | + writeLine(" edge ["); |
| 83 | + writeLine(" fontname=\"Helvetica\""); |
| 84 | + writeLine(" fontsize=\"8\""); |
| 85 | + writeLine(" fontcolor=\"black\""); |
| 86 | + writeLine(" color=\"black\""); |
| 87 | + writeLine(" arrowsize=\"0.7\""); |
| 88 | + writeLine(" ];"); |
| 89 | + |
| 90 | + // Write inputs as a subgraph |
| 91 | + writeInputs(workflow); |
| 92 | + |
| 93 | + // Write outputs as a subgraph |
| 94 | + writeOutputs(workflow); |
| 95 | + |
| 96 | + // End graph |
| 97 | + writeLine("}"); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Writes a set of inputs from a workflow to the Writer |
| 102 | + * @param workflow The workflow to get the inputs from |
| 103 | + * @throws IOException Any errors in writing which may have occurred |
| 104 | + */ |
| 105 | + private void writeInputs(Workflow workflow) throws IOException { |
| 106 | + // Get inputs from workflow |
| 107 | + Map<String, CWLElement> inputs = workflow.getInputs(); |
| 108 | + |
| 109 | + // Start of subgraph with styling |
| 110 | + writeLine(" subgraph cluster_inputs {"); |
| 111 | + writeLine(" rank = \"same\";"); |
| 112 | + writeLine(" style = \"dashed\";"); |
| 113 | + writeLine(" label = \"Workflow Inputs\";"); |
| 114 | + |
| 115 | + // Write each of the inputs as a node |
| 116 | + for (Map.Entry<String, CWLElement> input : workflow.getInputs().entrySet()) { |
| 117 | + writeInputOutput(input); |
| 118 | + } |
| 119 | + |
| 120 | + // End subgraph |
| 121 | + writeLine(" }"); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Writes a set of outputs from a workflow to the Writer |
| 126 | + * @param workflow The workflow to get the outputs from |
| 127 | + * @throws IOException Any errors in writing which may have occurred |
| 128 | + */ |
| 129 | + private void writeOutputs(Workflow workflow) throws IOException { |
| 130 | + // Start of subgraph with styling |
| 131 | + writeLine(" subgraph cluster_outputs {"); |
| 132 | + writeLine(" rank = \"same\";"); |
| 133 | + writeLine(" style = \"dashed\";"); |
| 134 | + writeLine(" label = \"Workflow Outputs\";"); |
| 135 | + |
| 136 | + // Write each of the outputs as a node |
| 137 | + for (Map.Entry<String, CWLElement> output : workflow.getOutputs().entrySet()) { |
| 138 | + writeInputOutput(output); |
| 139 | + } |
| 140 | + |
| 141 | + // End subgraph |
| 142 | + writeLine(" }"); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Writes a single input or output to the Writer |
| 147 | + * @param inputOutput The input or output |
| 148 | + * @throws IOException Any errors in writing which may have occurred |
| 149 | + */ |
| 150 | + private void writeInputOutput(Map.Entry<String, CWLElement> inputOutput) throws IOException { |
| 151 | + // List of options for this node |
| 152 | + List<String> nodeOptions = new ArrayList<>(); |
| 153 | + nodeOptions.add("fillcolor=\"#94DDF4\""); |
| 154 | + |
| 155 | + // Use label if it is defined |
| 156 | + String label = inputOutput.getValue().getLabel(); |
| 157 | + if (label != null && !label.isEmpty()) { |
| 158 | + nodeOptions.add("label=\"" + label + "\";"); |
| 159 | + } |
| 160 | + |
| 161 | + // Write the line for the node |
| 162 | + writeLine(" \"" + inputOutput.getKey() + "\" [" + String.join(",", nodeOptions) + "];"); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Write a single line using the Writer |
| 167 | + * @param line The line to be written |
| 168 | + * @throws IOException Any errors in writing which may have occurred |
| 169 | + */ |
| 170 | + private void writeLine(String line) throws IOException { |
| 171 | + writer.write(line); |
| 172 | + writer.write(EOL); |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments