@@ -40,8 +40,11 @@ public class CWLCollection {
40
40
private GitHubService githubService ;
41
41
private GithubDetails githubInfo ;
42
42
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 ;
45
48
46
49
/**
47
50
* Creates a new collection of CWL files from a Github repository
@@ -99,7 +102,7 @@ private void addDocs(List<RepositoryContents> repoContents) throws IOException {
99
102
JsonNode cwlFile = mapper .valueToTree (reader .load (fileContent ));
100
103
101
104
// Add document to those being considered
102
- addDoc (cwlFile );
105
+ addDoc (cwlFile , repoContent . getName () );
103
106
}
104
107
}
105
108
@@ -110,17 +113,18 @@ private void addDocs(List<RepositoryContents> repoContents) throws IOException {
110
113
/**
111
114
* Adds a document to the group of those being parsed
112
115
* @param newDoc The document to be added
116
+ * @param fileName The name of the file this document has come from
113
117
*/
114
- private void addDoc (JsonNode newDoc ) {
118
+ private void addDoc (JsonNode newDoc , String fileName ) {
115
119
// Make sure that this document is only one object and not multiple under a $graph directive
116
120
if (newDoc .has ("$graph" )) {
117
121
// Add each of the sub documents
118
122
for (JsonNode jsonNode : newDoc .get ("$graph" )) {
119
- cwlDocs .add ( jsonNode );
123
+ cwlDocs .put ( extractID ( jsonNode ), jsonNode );
120
124
}
121
125
} 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 );
124
128
}
125
129
}
126
130
@@ -130,9 +134,9 @@ private void addDoc(JsonNode newDoc) {
130
134
private void findMainWorkflow () {
131
135
// Find the first workflow we come across
132
136
// 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 () ;
136
140
return ;
137
141
}
138
142
}
@@ -143,49 +147,83 @@ private void findMainWorkflow() {
143
147
* @return A Workflow object representing the main workflow amongst the files added
144
148
*/
145
149
public Workflow getWorkflow () {
146
- if (mainWorkflowIndex < 0 ) {
150
+ // Get the main workflow
151
+ if (mainWorkflowKey == null ) {
147
152
findMainWorkflow ();
148
153
149
154
// If it is still less than 0 there is no workflow to be found
150
- if (mainWorkflowIndex < 0 ) {
155
+ if (mainWorkflowKey == null ) {
151
156
return null ;
152
157
}
153
158
}
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 ));
157
169
}
158
170
159
171
/**
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
161
203
* @param cwlDoc The document to get inputs for
162
204
* @return A map of input IDs and details related to them
163
205
*/
164
206
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" ));
169
209
}
170
210
return null ;
171
211
}
172
212
173
213
/**
174
- * Get a list of the outputs for a particular document
214
+ * Get the outputs for a particular document
175
215
* @param cwlDoc The document to get outputs for
176
216
* @return A map of output IDs and details related to them
177
217
*/
178
218
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" ));
183
221
}
184
222
return null ;
185
223
}
186
224
187
225
/**
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
189
227
* @param inputsOutputs The inputs or outputs node
190
228
* @return A map of input IDs and details related to them
191
229
*/
@@ -203,8 +241,7 @@ private Map<String, CWLElement> getInputsOutputs(JsonNode inputsOutputs) {
203
241
Iterator <Map .Entry <String , JsonNode >> iterator = inputsOutputs .fields ();
204
242
while (iterator .hasNext ()) {
205
243
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 ()));
208
245
}
209
246
}
210
247
@@ -243,16 +280,26 @@ private CWLElement getDetails(JsonNode inputOutput) {
243
280
return null ;
244
281
}
245
282
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
+
246
295
/**
247
296
* Extract the label from a node
248
297
* @param node The node to have the label extracted from
249
298
* @return The string for the label of the node
250
299
*/
251
300
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 ();
256
303
}
257
304
return null ;
258
305
}
@@ -330,5 +377,4 @@ private String extractTypes(JsonNode typeNode) {
330
377
}
331
378
return null ;
332
379
}
333
-
334
380
}
0 commit comments