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);
}
Aggregations