3232import org .yaml .snakeyaml .Yaml ;
3333
3434import java .io .IOException ;
35+ import java .nio .file .Path ;
36+ import java .nio .file .Paths ;
3537import java .util .*;
3638
3739/**
@@ -49,22 +51,20 @@ public class CWLCollection {
4951 private int singleFileSizeLimit ;
5052
5153 // Maps of ID to associated JSON
52- private Map <String , JsonNode > workflows = new HashMap <>();
54+ private Map <String , JsonNode > cwlDocs = new HashMap <>();
5355
5456 // The main workflow
5557 private String mainWorkflowKey ;
5658
5759 // Extension of CWL files
5860 private final String CWL_EXTENSION = "cwl" ;
5961
60- // Github API specific strings
61- private final String DIR = "dir" ;
62- private final String FILE = "file" ;
63-
6462 // CWL specific strings
6563 private final String DOC_GRAPH = "$graph" ;
6664 private final String CLASS = "class" ;
6765 private final String WORKFLOW = "Workflow" ;
66+ private final String COMMANDLINETOOL = "CommandLineTool" ;
67+ private final String EXPRESSIONTOOL = "ExpressionTool" ;
6868 private final String STEPS = "steps" ;
6969 private final String INPUTS = "inputs" ;
7070 private final String IN = "in" ;
@@ -117,7 +117,7 @@ private void addDocs(List<RepositoryContents> repoContents) throws IOException {
117117 for (RepositoryContents repoContent : repoContents ) {
118118
119119 // Parse subdirectories if they exist
120- if (repoContent .getType ().equals (DIR )) {
120+ if (repoContent .getType ().equals (GitHubService . TYPE_DIR )) {
121121
122122 // Get the contents of the subdirectory
123123 GithubDetails githubSubdir = new GithubDetails (githubInfo .getOwner (),
@@ -127,7 +127,7 @@ private void addDocs(List<RepositoryContents> repoContents) throws IOException {
127127 // Add the files in the subdirectory to this new folder
128128 addDocs (subdirectory );
129129
130- } else if (repoContent .getType ().equals (FILE )) {
130+ } else if (repoContent .getType ().equals (GitHubService . TYPE_FILE )) {
131131
132132 // Keep track of total file size for limit - only track files which
133133 // will be added to the RO bundle due to being small enough
@@ -155,7 +155,7 @@ private void addDocs(List<RepositoryContents> repoContents) throws IOException {
155155 JsonNode cwlFile = mapper .valueToTree (reader .load (fileContent ));
156156
157157 // Add document to those being considered
158- addDoc (cwlFile , repoContent .getName ());
158+ addDoc (cwlFile , repoContent .getPath ());
159159 } else {
160160 throw new IOException ("File '" + repoContent .getName () + "' is over singleFileSizeLimit - " +
161161 FileUtils .byteCountToDisplaySize (repoContent .getSize ()) + "/" +
@@ -181,15 +181,11 @@ private void addDoc(JsonNode newDoc, String fileName) {
181181 if (newDoc .has (DOC_GRAPH )) {
182182 // Add each of the sub documents
183183 for (JsonNode jsonNode : newDoc .get (DOC_GRAPH )) {
184- if (isWorkflow (jsonNode )) {
185- workflows .put (extractID (jsonNode ), jsonNode );
186- }
184+ cwlDocs .put (extractID (jsonNode ), jsonNode );
187185 }
188186 } else {
189187 // Otherwise just add the document itself with ID of document name
190- if (isWorkflow (newDoc )) {
191- workflows .put (fileName , newDoc );
192- }
188+ cwlDocs .put (fileName , newDoc );
193189 }
194190 }
195191
@@ -204,12 +200,12 @@ private String findMainWorkflow() {
204200
205201 // Store the in degree of each workflow
206202 Map <String , Integer > inDegrees = new HashMap <String , Integer >();
207- for (String key : workflows .keySet ()) {
203+ for (String key : cwlDocs .keySet ()) {
208204 inDegrees .put (key , 0 );
209205 }
210206
211207 // Loop through documents and calculate in degrees
212- for (Map .Entry <String , JsonNode > doc : workflows .entrySet ()) {
208+ for (Map .Entry <String , JsonNode > doc : cwlDocs .entrySet ()) {
213209 JsonNode content = doc .getValue ();
214210 if (content .get (CLASS ).asText ().equals (WORKFLOW )) {
215211 // Parse workflow steps and see whether other workflows are run
@@ -269,16 +265,31 @@ public Workflow getWorkflow() {
269265 return null ;
270266 }
271267 }
272- JsonNode mainWorkflow = workflows .get (mainWorkflowKey );
268+ JsonNode mainWorkflow = cwlDocs .get (mainWorkflowKey );
273269
274270 // Use ID/filename for label if there is no defined one
275271 String label = extractLabel (mainWorkflow );
276272 if (label == null ) {
277273 label = mainWorkflowKey ;
278274 }
279275
280- return new Workflow (label , extractDoc (mainWorkflow ), getInputs (mainWorkflow ),
281- getOutputs (mainWorkflow ), getSteps (mainWorkflow ), getDockerLink (mainWorkflow ));
276+ Workflow workflowModel = new Workflow (label , extractDoc (mainWorkflow ), getInputs (mainWorkflow ),
277+ getOutputs (mainWorkflow ), getSteps (mainWorkflow ), getDockerLink (mainWorkflow ));
278+ fillStepRunTypes (workflowModel );
279+ return workflowModel ;
280+ }
281+
282+ /**
283+ * Fill the step runtypes based on types of other documents
284+ * @param workflow The workflow model to set runtypes for
285+ */
286+ private void fillStepRunTypes (Workflow workflow ) {
287+ Map <String , CWLStep > steps = workflow .getSteps ();
288+ for (CWLStep step : steps .values ()) {
289+ Path filePath = Paths .get (githubInfo .getPath ()).resolve (step .getRun ());
290+ JsonNode runDoc = cwlDocs .get (filePath .toString ());
291+ step .setRunType (extractProcess (runDoc ));
292+ }
282293 }
283294
284295 /**
@@ -295,7 +306,7 @@ private Map<String, CWLStep> getSteps(JsonNode cwlDoc) {
295306 // Explicit ID and other fields within each input list
296307 for (JsonNode step : steps ) {
297308 CWLStep stepObject = new CWLStep (extractLabel (step ), extractDoc (step ),
298- extractTypes (step ), getInputs (step ), getOutputs (step ));
309+ extractTypes (step ), extractRun ( step ), getInputs (step ), getOutputs (step ));
299310 returnMap .put (extractID (step ), stepObject );
300311 }
301312 } else if (steps .getClass () == ObjectNode .class ) {
@@ -305,7 +316,8 @@ private Map<String, CWLStep> getSteps(JsonNode cwlDoc) {
305316 Map .Entry <String , JsonNode > stepNode = iterator .next ();
306317 JsonNode stepJson = stepNode .getValue ();
307318 CWLStep stepObject = new CWLStep (extractLabel (stepJson ), extractDoc (stepJson ),
308- extractTypes (stepJson ), getInputs (stepJson ), getOutputs (stepJson ));
319+ extractTypes (stepJson ), extractRun (stepJson ), getInputs (stepJson ),
320+ getOutputs (stepJson ));
309321 returnMap .put (stepNode .getKey (), stepObject );
310322 }
311323 }
@@ -710,12 +722,23 @@ private String extractRun(JsonNode stepNode) {
710722 }
711723
712724 /**
713- * Identify a JsonNode as a workflow
714- * @param rootNode The root node
715- * @return Whether or not the node is a workflow
725+ * Extract the class parameter from a node representing a document
726+ * @param rootNode The root node of a cwl document
727+ * @return Which process this document represents
716728 */
717- private boolean isWorkflow (JsonNode rootNode ) {
718- return (rootNode .has (CLASS )
719- && rootNode .get (CLASS ).asText ().equals (WORKFLOW ));
729+ private CWLProcess extractProcess (JsonNode rootNode ) {
730+ if (rootNode != null ) {
731+ if (rootNode .has (CLASS )) {
732+ switch (rootNode .get (CLASS ).asText ()) {
733+ case WORKFLOW :
734+ return CWLProcess .WORKFLOW ;
735+ case COMMANDLINETOOL :
736+ return CWLProcess .COMMANDLINETOOL ;
737+ case EXPRESSIONTOOL :
738+ return CWLProcess .EXPRESSIONTOOL ;
739+ }
740+ }
741+ }
742+ return null ;
720743 }
721744}
0 commit comments