001package org.galaxyproject.gxformat2; 002 003import java.util.ArrayList; 004import java.util.Arrays; 005import java.util.HashMap; 006import java.util.List; 007import java.util.Map; 008 009public class NativeWorkflowAdapter implements WorkflowAdapter { 010 private Map<String, Object> workflow; 011 012 public NativeWorkflowAdapter(final Map<String, Object> workflow) { 013 this.workflow = workflow; 014 } 015 016 @Override 017 public List<NormalizedStep> normalizedSteps() { 018 Map<String, Map<String, Object>> steps = 019 (Map<String, Map<String, Object>>) this.workflow.get("steps"); 020 Map<Integer, String> orderIndexToLabel = new HashMap(); 021 for (Map.Entry<String, Map<String, Object>> stepEntry : steps.entrySet()) { 022 Map<String, Object> stepDef = stepEntry.getValue(); 023 String label = (String) stepDef.get("label"); 024 if (label == null) { 025 label = stepEntry.getKey(); 026 } 027 orderIndexToLabel.put(Integer.parseInt(stepEntry.getKey()), label); 028 } 029 final List<NormalizedStep> normalizedSteps = new ArrayList(); 030 for (Map.Entry<String, Map<String, Object>> stepEntry : steps.entrySet()) { 031 final NormalizedStep step = new NormalizedStep(); 032 final Map<String, Object> stepDef = stepEntry.getValue(); 033 ensurePosition(stepDef, Integer.parseInt(stepEntry.getKey())); 034 step.stepDefinition = stepDef; 035 step.inputs = new ArrayList<Input>(); 036 String doc = (String) stepDef.get("annotation"); 037 if (doc.equals("")) { 038 // just how it translates to Format 2. Doesn't preserve empty annotations. 039 doc = null; 040 } 041 step.doc = doc; 042 final Map<String, Object> inputConnections = 043 (Map<String, Object>) stepDef.get("input_connections"); 044 045 for (final Map.Entry<String, Object> inputConnectionListEntry : inputConnections.entrySet()) { 046 final List<Map<String, Object>> inputConnectionsList; 047 if (inputConnectionListEntry.getValue() instanceof Map) { 048 inputConnectionsList = 049 Arrays.asList((Map<String, Object>) inputConnectionListEntry.getValue()); 050 } else { 051 inputConnectionsList = (List<Map<String, Object>>) inputConnectionListEntry.getValue(); 052 } 053 final String inputName = inputConnectionListEntry.getKey(); 054 for (final Map<String, Object> inputConnection : inputConnectionsList) { 055 String outputName = (String) inputConnection.get("output_name"); 056 if (outputName.equals("output")) { 057 // HACKY - this would be implicit in Format 2 :( 058 outputName = null; 059 } 060 final Integer outputIndex = (Integer) inputConnection.get("id"); 061 final String outputLabel = orderIndexToLabel.get(outputIndex); 062 final Input input = new Input(); 063 input.inputName = inputName; 064 input.sourceOutputName = outputName; 065 input.sourceStepLabel = outputLabel; 066 step.inputs.add(input); 067 } 068 } 069 String label = (String) stepDef.get("label"); 070 if (label == null) { 071 label = (String) stepDef.get("label"); 072 } 073 if (label == null) { 074 label = Integer.toString((int) stepDef.get("id")); 075 } 076 step.label = label; 077 normalizedSteps.add(step); 078 } 079 return normalizedSteps; 080 } 081}