Skip to content

Commit 8801806

Browse files
author
Mark Robinson
committed
Add simple step parsing and storage
Closes #31
1 parent 6d97305 commit 8801806

File tree

4 files changed

+106
-35
lines changed

4 files changed

+106
-35
lines changed

src/main/java/org/commonwl/viewer/domain/CWLCollection.java

Lines changed: 79 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ public class CWLCollection {
4040
private GitHubService githubService;
4141
private GithubDetails githubInfo;
4242

43-
private List<JsonNode> cwlDocs = new ArrayList<>();
44-
private int mainWorkflowIndex = -1;
43+
// Maps of ID to associated JSON
44+
private Map<String, JsonNode> cwlDocs = new HashMap<>();
45+
46+
// The main workflow
47+
private String mainWorkflowKey;
4548

4649
/**
4750
* Creates a new collection of CWL files from a Github repository
@@ -99,7 +102,7 @@ private void addDocs(List<RepositoryContents> repoContents) throws IOException {
99102
JsonNode cwlFile = mapper.valueToTree(reader.load(fileContent));
100103

101104
// Add document to those being considered
102-
addDoc(cwlFile);
105+
addDoc(cwlFile, repoContent.getName());
103106
}
104107
}
105108

@@ -110,17 +113,18 @@ private void addDocs(List<RepositoryContents> repoContents) throws IOException {
110113
/**
111114
* Adds a document to the group of those being parsed
112115
* @param newDoc The document to be added
116+
* @param fileName The name of the file this document has come from
113117
*/
114-
private void addDoc(JsonNode newDoc) {
118+
private void addDoc(JsonNode newDoc, String fileName) {
115119
// Make sure that this document is only one object and not multiple under a $graph directive
116120
if (newDoc.has("$graph")) {
117121
// Add each of the sub documents
118122
for (JsonNode jsonNode : newDoc.get("$graph")) {
119-
cwlDocs.add(jsonNode);
123+
cwlDocs.put(extractID(jsonNode), jsonNode);
120124
}
121125
} else {
122-
// Otherwise just add the document itself
123-
cwlDocs.add(newDoc);
126+
// Otherwise just add the document itself with ID of document name
127+
cwlDocs.put(fileName, newDoc);
124128
}
125129
}
126130

@@ -130,9 +134,9 @@ private void addDoc(JsonNode newDoc) {
130134
private void findMainWorkflow() {
131135
// Find the first workflow we come across
132136
// TODO: Consider relationship between run: parameters to better discover this
133-
for (int i=0; i < cwlDocs.size(); i++) {
134-
if (cwlDocs.get(i).get("class").asText().equals("Workflow")) {
135-
mainWorkflowIndex = 0;
137+
for (Map.Entry<String, JsonNode> doc : cwlDocs.entrySet()) {
138+
if (doc.getValue().get("class").asText().equals("Workflow")) {
139+
mainWorkflowKey = doc.getKey();
136140
return;
137141
}
138142
}
@@ -143,49 +147,83 @@ private void findMainWorkflow() {
143147
* @return A Workflow object representing the main workflow amongst the files added
144148
*/
145149
public Workflow getWorkflow() {
146-
if (mainWorkflowIndex < 0) {
150+
// Get the main workflow
151+
if (mainWorkflowKey == null) {
147152
findMainWorkflow();
148153

149154
// If it is still less than 0 there is no workflow to be found
150-
if (mainWorkflowIndex < 0) {
155+
if (mainWorkflowKey == null) {
151156
return null;
152157
}
153158
}
154-
JsonNode mainWorkflow = cwlDocs.get(mainWorkflowIndex);
155-
return new Workflow(extractLabel(mainWorkflow), extractDoc(mainWorkflow),
156-
getInputs(mainWorkflow), getOutputs(mainWorkflow));
159+
JsonNode mainWorkflow = cwlDocs.get(mainWorkflowKey);
160+
161+
// Use ID/filename for label if there is no defined one
162+
String label = extractLabel(mainWorkflow);
163+
if (label == null) {
164+
label = mainWorkflowKey;
165+
}
166+
167+
return new Workflow(label, extractDoc(mainWorkflow), getInputs(mainWorkflow),
168+
getOutputs(mainWorkflow), getSteps(mainWorkflow));
157169
}
158170

159171
/**
160-
* Get a list of the inputs for a particular document
172+
* Get the steps for a particular document
173+
* @param cwlDoc The document to get steps for
174+
* @return A map of step IDs and details related to them
175+
*/
176+
private Map<String, CWLElement> getSteps(JsonNode cwlDoc) {
177+
if (cwlDoc != null && cwlDoc.has("steps")) {
178+
Map<String, CWLElement> returnMap = new HashMap<>();
179+
180+
JsonNode steps = cwlDoc.get("steps");
181+
if (steps.getClass() == ArrayNode.class) {
182+
// Explicit ID and other fields within each input list
183+
for (JsonNode step : steps) {
184+
String id = step.get("id").asText();
185+
returnMap.put(id, getDetails(step));
186+
}
187+
} else if (steps.getClass() == ObjectNode.class) {
188+
// ID is the key of each object
189+
Iterator<Map.Entry<String, JsonNode>> iterator = steps.fields();
190+
while (iterator.hasNext()) {
191+
Map.Entry<String, JsonNode> stepNode = iterator.next();
192+
returnMap.put(stepNode.getKey(), getDetails(stepNode.getValue()));
193+
}
194+
}
195+
196+
return returnMap;
197+
}
198+
return null;
199+
}
200+
201+
/**
202+
* Get a the inputs for a particular document
161203
* @param cwlDoc The document to get inputs for
162204
* @return A map of input IDs and details related to them
163205
*/
164206
private Map<String, CWLElement> getInputs(JsonNode cwlDoc) {
165-
if (cwlDoc != null) {
166-
if (cwlDoc.has("inputs")) {
167-
return getInputsOutputs(cwlDoc.get("inputs"));
168-
}
207+
if (cwlDoc != null && cwlDoc.has("inputs")) {
208+
return getInputsOutputs(cwlDoc.get("inputs"));
169209
}
170210
return null;
171211
}
172212

173213
/**
174-
* Get a list of the outputs for a particular document
214+
* Get the outputs for a particular document
175215
* @param cwlDoc The document to get outputs for
176216
* @return A map of output IDs and details related to them
177217
*/
178218
private Map<String, CWLElement> getOutputs(JsonNode cwlDoc) {
179-
if (cwlDoc != null) {
180-
if (cwlDoc.has("outputs")) {
181-
return getInputsOutputs(cwlDoc.get("outputs"));
182-
}
219+
if (cwlDoc != null && cwlDoc.has("outputs")) {
220+
return getInputsOutputs(cwlDoc.get("outputs"));
183221
}
184222
return null;
185223
}
186224

187225
/**
188-
* Get a list of inputs or outputs from an inputs or outputs node
226+
* Get inputs or outputs from an inputs or outputs node
189227
* @param inputsOutputs The inputs or outputs node
190228
* @return A map of input IDs and details related to them
191229
*/
@@ -203,8 +241,7 @@ private Map<String, CWLElement> getInputsOutputs(JsonNode inputsOutputs) {
203241
Iterator<Map.Entry<String, JsonNode>> iterator = inputsOutputs.fields();
204242
while (iterator.hasNext()) {
205243
Map.Entry<String, JsonNode> inputOutputNode = iterator.next();
206-
String outputID = inputOutputNode.getKey();
207-
returnMap.put(outputID, getDetails(inputOutputNode.getValue()));
244+
returnMap.put(inputOutputNode.getKey(), getDetails(inputOutputNode.getValue()));
208245
}
209246
}
210247

@@ -243,16 +280,26 @@ private CWLElement getDetails(JsonNode inputOutput) {
243280
return null;
244281
}
245282

283+
/**
284+
* Extract the id from a node
285+
* @param node The node to have the id extracted from
286+
* @return The string for the id of the node
287+
*/
288+
private String extractID(JsonNode node) {
289+
if (node != null && node.has("id")) {
290+
return node.get("id").asText();
291+
}
292+
return null;
293+
}
294+
246295
/**
247296
* Extract the label from a node
248297
* @param node The node to have the label extracted from
249298
* @return The string for the label of the node
250299
*/
251300
private String extractLabel(JsonNode node) {
252-
if (node != null) {
253-
if (node.has("label")) {
254-
return node.get("label").asText();
255-
}
301+
if (node != null && node.has("label")) {
302+
return node.get("label").asText();
256303
}
257304
return null;
258305
}
@@ -330,5 +377,4 @@ private String extractTypes(JsonNode typeNode) {
330377
}
331378
return null;
332379
}
333-
334380
}

src/main/java/org/commonwl/viewer/domain/Workflow.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,18 @@ public class Workflow {
6060
private String doc;
6161
private Map<String, CWLElement> inputs;
6262
private Map<String, CWLElement> outputs;
63+
private Map<String, CWLElement> steps;
6364

6465
// DOT graph of the contents
6566
private String dotGraph;
6667

67-
public Workflow(String label, String doc, Map<String, CWLElement> inputs, Map<String, CWLElement> outputs) {
68+
public Workflow(String label, String doc, Map<String, CWLElement> inputs,
69+
Map<String, CWLElement> outputs, Map<String, CWLElement> steps) {
6870
this.label = label;
6971
this.doc = doc;
7072
this.inputs = inputs;
7173
this.outputs = outputs;
74+
this.steps = steps;
7275

7376
// Create a DOT graph for this workflow and store it
7477
StringWriter graphWriter = new StringWriter();
@@ -119,6 +122,14 @@ public void setOutputs(Map<String, CWLElement> outputs) {
119122
this.outputs = outputs;
120123
}
121124

125+
public Map<String, CWLElement> getSteps() {
126+
return steps;
127+
}
128+
129+
public void setSteps(Map<String, CWLElement> steps) {
130+
this.steps = steps;
131+
}
132+
122133
public String getRoBundle() {
123134
return roBundle;
124135
}

src/main/java/org/commonwl/viewer/services/DotWriter.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ public void writeGraph(Workflow workflow) throws IOException {
9393
// Write outputs as a subgraph
9494
writeOutputs(workflow);
9595

96+
// Write steps as nodes
97+
writeSteps(workflow);
98+
9699
// End graph
97100
writeLine("}");
98101
}
@@ -142,6 +145,18 @@ private void writeOutputs(Workflow workflow) throws IOException {
142145
writeLine(" }");
143146
}
144147

148+
/**
149+
* Writes a set of steps from a workflow to the Writer
150+
* @param workflow The workflow to get the steps from
151+
* @throws IOException Any errors in writing which may have occurred
152+
*/
153+
private void writeSteps(Workflow workflow) throws IOException {
154+
// Write each of the steps as a node
155+
for (Map.Entry<String, CWLElement> step : workflow.getSteps().entrySet()) {
156+
writeLine(step.getKey());
157+
}
158+
}
159+
145160
/**
146161
* Writes a single input or output to the Writer
147162
* @param inputOutput The input or output

src/main/java/org/commonwl/viewer/web/WorkflowController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import javax.servlet.http.HttpServletResponse;
3838
import javax.validation.Valid;
3939
import java.io.File;
40-
import java.io.IOException;
4140

4241
@Controller
4342
public class WorkflowController {

0 commit comments

Comments
 (0)