Search in sources :

Example 1 with PeekingIterator

use of org.apache.commons.collections4.iterators.PeekingIterator in project cloud-slang by CloudSlang.

the class ExecutableBuilder method compileWorkFlow.

private WorkflowModellingResult compileWorkFlow(List<Map<String, Map<String, Object>>> workFlowRawData, Map<String, String> imports, Workflow onFailureWorkFlow, boolean onFailureSection, String namespace, SensitivityLevel sensitivityLevel) {
    List<RuntimeException> errors = new ArrayList<>();
    Deque<Step> steps = new LinkedList<>();
    Set<String> stepNames = new HashSet<>();
    Deque<Step> onFailureSteps = !(onFailureSection || onFailureWorkFlow == null) ? onFailureWorkFlow.getSteps() : new LinkedList<Step>();
    List<String> onFailureStepNames = getStepNames(onFailureSteps);
    boolean onFailureStepFound = onFailureStepNames.size() > 0;
    String defaultFailure = onFailureStepFound ? onFailureStepNames.get(0) : ScoreLangConstants.FAILURE_RESULT;
    PeekingIterator<Map<String, Map<String, Object>>> iterator = new PeekingIterator<>(workFlowRawData.iterator());
    while (iterator.hasNext()) {
        Map<String, Map<String, Object>> stepRawData = iterator.next();
        String stepName = getStepName(stepRawData);
        validateStepName(stepName, errors);
        if (stepNames.contains(stepName) || onFailureStepNames.contains(stepName)) {
            errors.add(new RuntimeException("Step name: \'" + stepName + "\' appears more than once in the workflow. " + UNIQUE_STEP_NAME_MESSAGE_SUFFIX));
        }
        stepNames.add(stepName);
        Map<String, Object> stepRawDataValue;
        String message = "Step: " + stepName + " syntax is illegal.\nBelow step name, there should " + "be a map of values in the format:\ndo:\n\top_name:";
        try {
            stepRawDataValue = stepRawData.values().iterator().next();
            if (isNotEmpty(stepRawDataValue)) {
                boolean loopKeyFound = stepRawDataValue.containsKey(LOOP_KEY);
                boolean parallelLoopKeyFound = stepRawDataValue.containsKey(PARALLEL_LOOP_KEY);
                if (loopKeyFound) {
                    if (parallelLoopKeyFound) {
                        errors.add(new RuntimeException("Step: " + stepName + " syntax is illegal.\nBelow step name, " + "there can be either \'loop\' or \'aync_loop\' key."));
                    }
                    message = "Step: " + stepName + " syntax is illegal.\nBelow the 'loop' keyword, there " + "should be a map of values in the format:\nfor:\ndo:\n\top_name:";
                    @SuppressWarnings("unchecked") Map<String, Object> loopRawData = (Map<String, Object>) stepRawDataValue.remove(LOOP_KEY);
                    stepRawDataValue.putAll(loopRawData);
                }
                if (parallelLoopKeyFound) {
                    message = "Step: " + stepName + " syntax is illegal.\nBelow the 'parallel_loop' keyword, there " + "should be a map of values in the format:\nfor:\ndo:\n\top_name:";
                    @SuppressWarnings("unchecked") Map<String, Object> parallelLoopRawData = (Map<String, Object>) stepRawDataValue.remove(PARALLEL_LOOP_KEY);
                    errors.addAll(preCompileValidator.checkKeyWords(stepName, SlangTextualKeys.PARALLEL_LOOP_KEY, parallelLoopRawData, Collections.emptyList(), parallelLoopValidKeywords, null));
                    parallelLoopRawData.put(PARALLEL_LOOP_KEY, parallelLoopRawData.remove(FOR_KEY));
                    stepRawDataValue.putAll(parallelLoopRawData);
                }
            }
        } catch (ClassCastException ex) {
            stepRawDataValue = new HashMap<>();
            errors.add(new RuntimeException(message));
        }
        String defaultSuccess;
        Map<String, Map<String, Object>> nextStepData = iterator.peek();
        if (nextStepData != null) {
            defaultSuccess = nextStepData.keySet().iterator().next();
        } else {
            defaultSuccess = onFailureSection ? ScoreLangConstants.FAILURE_RESULT : SUCCESS_RESULT;
        }
        String onFailureStepName = onFailureStepFound ? onFailureStepNames.get(0) : null;
        StepModellingResult stepModellingResult = compileStep(stepName, stepRawDataValue, defaultSuccess, imports, defaultFailure, namespace, onFailureStepName, onFailureSection, sensitivityLevel);
        errors.addAll(stepModellingResult.getErrors());
        steps.add(stepModellingResult.getStep());
    }
    if (onFailureStepFound) {
        steps.addAll(onFailureSteps);
    }
    return new WorkflowModellingResult(new Workflow(steps), errors);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Workflow(io.cloudslang.lang.compiler.modeller.model.Workflow) StepModellingResult(io.cloudslang.lang.compiler.modeller.result.StepModellingResult) SeqStep(io.cloudslang.lang.compiler.modeller.model.SeqStep) Step(io.cloudslang.lang.compiler.modeller.model.Step) ExternalStep(io.cloudslang.lang.compiler.modeller.model.ExternalStep) PeekingIterator(org.apache.commons.collections4.iterators.PeekingIterator) LinkedList(java.util.LinkedList) WorkflowModellingResult(io.cloudslang.lang.compiler.modeller.result.WorkflowModellingResult) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Aggregations

ExternalStep (io.cloudslang.lang.compiler.modeller.model.ExternalStep)1 SeqStep (io.cloudslang.lang.compiler.modeller.model.SeqStep)1 Step (io.cloudslang.lang.compiler.modeller.model.Step)1 Workflow (io.cloudslang.lang.compiler.modeller.model.Workflow)1 StepModellingResult (io.cloudslang.lang.compiler.modeller.result.StepModellingResult)1 WorkflowModellingResult (io.cloudslang.lang.compiler.modeller.result.WorkflowModellingResult)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 PeekingIterator (org.apache.commons.collections4.iterators.PeekingIterator)1